1
1
class Country
2
2
class << self
3
3
def base_path
4
- ENV [ 'GFW_API_HOST' ]
4
+ " #{ ENV [ 'CARTO_API_URL' ] } /sql?q="
5
5
end
6
6
7
7
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 "
9
9
response = Typhoeus . get ( url , headers : { "Accept" => "application/json" } )
10
10
if response . success?
11
11
Rails . cache . fetch 'countries' , expires_in : 1 . day do
@@ -22,12 +22,11 @@ def find_by_iso_or_name(value)
22
22
23
23
def find_by_iso ( iso )
24
24
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 "
26
26
# CACHE: &bust=true if you want to flush the cache
27
27
response = Typhoeus . get ( url , headers : { "Accept" => "application/json" } )
28
-
29
28
if response . success? and ( response . body . length > 0 )
30
- JSON . parse ( response . body )
29
+ JSON . parse ( response . body ) [ "rows" ] [ 0 ]
31
30
else
32
31
nil
33
32
end
@@ -36,7 +35,7 @@ def find_by_iso(iso)
36
35
def find_by_name ( country_name )
37
36
country_name , *_ = country_name . split ( /_/ )
38
37
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 "
40
39
response = Typhoeus . get ( url , headers : { "Accept" => "application/json" } )
41
40
42
41
if response . success?
0 commit comments