-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.rb
42 lines (34 loc) · 1.16 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Parser
class Base
@@iso_countries_by_id = Hash.new
@@iso_countries_by_name = Hash.new
attr_accessor :data, :set, :results
def initialize(data, set:, previous_results: Hash.new)
@data, @set, @results = data, set.to_sym, previous_results
fetch_iso_countries
end
def iso_countries_by_name
@@iso_countries_by_name || fetch_iso_countries
end
def iso_countries_by_id
@@iso_countries_by_id || fetch_iso_countries
end
def execute
raise NotImplementedError
end
def fetch_iso_countries
# Load the countries
iso_countries = JSON.parse(HTTParty.get("https://raw.githubusercontent.com/olahol/iso-3166-2.json/master/iso-3166-2.json").body)
# Re-map the countries for better access
iso_countries.each do |key, country|
new_country = country
new_country['code'] = key
new_country['divisions'] = new_country['divisions'].invert
@@iso_countries_by_name[country['name']] = new_country
new_divisions = country
new_divisions['divisions'] = country['divisions']
@@iso_countries_by_id[key] = new_divisions
end
end
end
end