|
1 | 1 | class CountriesController < ApplicationController
|
2 |
| - before_action :check_country_iso, only: :show |
| 2 | + before_action :set_country, only: :show |
3 | 3 |
|
4 | 4 | include ActionView::Helpers::NumberHelper
|
5 | 5 |
|
6 | 6 | layout 'countries'
|
7 | 7 |
|
8 | 8 | def index
|
9 |
| - @countries = find_countries |
| 9 | + @countries = Country.find_all |
10 | 10 | @title = 'Country Profiles'
|
11 | 11 | @desc = 'Explore country-specific statistics and graphs to see the how forests change and contribute to various sectors.'
|
12 | 12 | @keywords = 'GFW, list, forest data, visualization, data, national, country, analysis, statistic, tree cover loss, tree cover gain, climate domain, boreal, tropical, subtropical, temperate, deforestation, overview'
|
@@ -44,53 +44,8 @@ def overview
|
44 | 44 | end
|
45 | 45 |
|
46 | 46 | private
|
47 |
| - def find_countries |
48 |
| - response = Typhoeus.get("#{ENV['GFW_API_HOST']}/countries", headers: {"Accept" => "application/json"}) |
49 |
| - if response.success? |
50 |
| - Rails.cache.fetch 'countries', expires_in: 1.day do |
51 |
| - JSON.parse(response.body)['countries'] |
52 |
| - end |
53 |
| - else |
54 |
| - nil |
55 |
| - end |
56 |
| - end |
57 |
| - |
58 |
| - def find_by_iso(iso) |
59 |
| - unless iso.blank? |
60 |
| - # CACHE: &bust=true if you want to flush the cache |
61 |
| - iso = iso.downcase |
62 |
| - response = Typhoeus.get( |
63 |
| - "#{ENV['GFW_API_HOST']}/countries/#{iso}?thresh=30", |
64 |
| - headers: {"Accept" => "application/json"} |
65 |
| - ) |
66 |
| - if response.success? and (response.body.length > 0) |
67 |
| - JSON.parse(response.body) |
68 |
| - else |
69 |
| - nil |
70 |
| - end |
71 |
| - end |
72 |
| - end |
73 |
| - |
74 |
| - def find_by_name(country_name) |
75 |
| - country_name, *_ = country_name.split(/_/) |
76 |
| - country_name = country_name.capitalize |
77 |
| - response = Typhoeus.get("https://wri-01.cartodb.com/api/v2/sql?q=SELECT%20*%20FROM%20gfw2_countries%20where%20name%20like%20'#{country_name}%25'", |
78 |
| - headers: {"Accept" => "application/json"} |
79 |
| - ) |
80 |
| - if response.success? |
81 |
| - JSON.parse(response.body)['rows'][0] |
82 |
| - else |
83 |
| - nil |
84 |
| - end |
85 |
| - end |
86 | 47 |
|
87 |
| - def check_country_iso |
88 |
| - @country = find_by_iso(params[:id]) |
89 |
| - unless @country.nil? |
90 |
| - @country |
91 |
| - else |
92 |
| - @country = find_by_name(params[:id]) |
93 |
| - end |
94 |
| - @country |
| 48 | + def set_country |
| 49 | + @country = Country.find_by_iso_or_name(params[:id]) |
95 | 50 | end
|
96 | 51 | end
|
0 commit comments