Skip to content

Commit 73e5ff0

Browse files
authored
Merge pull request #3339 from Vizzuality/fix/country-fetch-for-seo
update rails country model to use gadm28 tables for country names
2 parents 03d342b + dadfba3 commit 73e5ff0

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

app/javascript/pages/country/meta/meta-component.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class Meta extends PureComponent {
77
// eslint-disable-line react/prefer-stateless-function
88
render() {
99
const { page } = this.props;
10-
return (
10+
return page ? (
1111
<Helmet>
12-
<title>{`${page ? `${page} | ` : ''}Global Forest Watch`}</title>
12+
<title>{`${page} | Global Forest Watch`}</title>
1313
<meta
1414
name="description"
1515
content={`Data about forest change, tenure, forest related employment and land use in ${page}`}
@@ -22,7 +22,7 @@ class Meta extends PureComponent {
2222
/>
2323
<meta property="og:url" content={window.location.href} />
2424
</Helmet>
25-
);
25+
) : null;
2626
}
2727
}
2828

app/javascript/services/country.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import axios from 'axios';
22

33
const REQUEST_URL = `${process.env.CARTO_API_URL}/sql?q=`;
4-
const CARTO_REQUEST_URL = `${process.env.CARTO_API_URL}/sql?q=`;
54

65
const SQL_QUERIES = {
76
getCountries:
@@ -24,7 +23,7 @@ export const getCountriesProvider = () => {
2423
};
2524

2625
export const getFAOCountriesProvider = () => {
27-
const url = `${CARTO_REQUEST_URL}${SQL_QUERIES.getFAOCountries}`;
26+
const url = `${REQUEST_URL}${SQL_QUERIES.getFAOCountries}`;
2827
return axios.get(url);
2928
};
3029

@@ -44,7 +43,7 @@ export const getSubRegionsProvider = (admin0, admin1) => {
4443
};
4544

4645
export const getCountryLinksProvider = () => {
47-
const url = `${CARTO_REQUEST_URL}${SQL_QUERIES.getCountryLinks}`;
46+
const url = `${REQUEST_URL}${SQL_QUERIES.getCountryLinks}`;
4847
return axios.get(url);
4948
};
5049

app/models/country.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Country
22
class << self
33
def base_path
4-
ENV['GFW_API_HOST']
4+
"#{ENV['CARTO_API_URL']}/sql?q="
55
end
66

77
def find_all
8-
url = "#{base_path}/countries"
8+
url = "#{base_path}SELECT iso, country as name FROM umd_nat_staging GROUP BY iso, name ORDER BY name"
99
response = Typhoeus.get(url, headers: {"Accept" => "application/json"})
1010
if response.success?
1111
Rails.cache.fetch 'countries', expires_in: 1.day do
@@ -22,12 +22,11 @@ def find_by_iso_or_name(value)
2222

2323
def find_by_iso(iso)
2424
return nil unless iso
25-
url = "#{base_path}/countries/#{iso.downcase}?thresh=30"
25+
url = "#{base_path}SELECT%20iso,%20name_0%20as%20name%20FROM%20gadm28_adm2%20WHERE%20iso%20=%20'#{iso}'%20LIMIT%201"
2626
# CACHE: &bust=true if you want to flush the cache
2727
response = Typhoeus.get(url, headers: {"Accept" => "application/json"})
28-
2928
if response.success? and (response.body.length > 0)
30-
JSON.parse(response.body)
29+
JSON.parse(response.body)["rows"][0]
3130
else
3231
nil
3332
end
@@ -36,7 +35,7 @@ def find_by_iso(iso)
3635
def find_by_name(country_name)
3736
country_name, *_ = country_name.split(/_/)
3837
country_name = country_name.capitalize
39-
url = "https://wri-01.cartodb.com/api/v2/sql?q=SELECT%20*%20FROM%20gfw2_countries%20where%20name%20like%20'#{country_name}%25'"
38+
url = "#{base_path}SELECT iso, name_0 as name FROM gadm28_adm2 WHERE name like '#{country_name}' LIMIT 1"
4039
response = Typhoeus.get(url, headers: {"Accept" => "application/json"})
4140

4241
if response.success?

0 commit comments

Comments
 (0)