Class: Reality::Country
- Inherits:
-
Object
- Object
- Reality::Country
- Defined in:
- lib/reality/country.rb
Defined Under Namespace
Classes: List
Constant Summary collapse
- PROPERTIES =
%i[ continent name long_name tld tlds calling_code utc_offset capital languages currency leaders area population gdp_ppp gdp_nominal ]
Class Method Summary collapse
Instance Method Summary collapse
- #area ⇒ Object
- #calling_code ⇒ Object
- #capital ⇒ Object
- #continent ⇒ Object
- #currencies ⇒ Object
- #currency ⇒ Object
- #gdp_nominal ⇒ Object (also: #gdp)
- #gdp_ppp ⇒ Object
-
#initialize(page) ⇒ Country
constructor
A new instance of Country.
- #inspect ⇒ Object
- #languages ⇒ Object
- #leaders ⇒ Object
- #long_name ⇒ Object
- #member_of?(org) ⇒ Boolean
- #name ⇒ Object
- #organizations ⇒ Object
- #population ⇒ Object
- #tld ⇒ Object
- #tlds ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #utc_offset ⇒ Object
Constructor Details
#initialize(page) ⇒ Country
Returns a new instance of Country.
57 58 59 |
# File 'lib/reality/country.rb', line 57 def initialize(page) @page = page end |
Class Method Details
.by_continents ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/reality/country.rb', line 170 def by_continents @by_continents ||= Reality.wp. get('List of countries by continent'). sections.first. sections.map{|s| continent = s.heading.text_ s.tables.first. lookup(:Wikilink, :bold?).map(&:link). map{|country| [country, continent]} }.flatten(1). to_h end |
.organizations ⇒ Object
183 184 185 |
# File 'lib/reality/country.rb', line 183 def organizations @organizations ||= YAML.load(File.read(File.('../../../data/country_orgs.yaml', __FILE__))) end |
Instance Method Details
#area ⇒ Object
104 105 106 |
# File 'lib/reality/country.rb', line 104 def area Reality::Measure(infobox.fetch('area_km2').text.gsub(',', '').to_i, 'km²') end |
#calling_code ⇒ Object
88 89 90 |
# File 'lib/reality/country.rb', line 88 def calling_code infobox.fetch('calling_code').text.strip end |
#capital ⇒ Object
69 70 71 |
# File 'lib/reality/country.rb', line 69 def capital infobox.fetch('capital').lookup(:Wikilink).first end |
#continent ⇒ Object
133 134 135 |
# File 'lib/reality/country.rb', line 133 def continent self.class.by_continents[page.title] end |
#currencies ⇒ Object
100 101 102 |
# File 'lib/reality/country.rb', line 100 def currencies infobox_links('currency').reject{|l| l.link == 'ISO 4217'} end |
#currency ⇒ Object
96 97 98 |
# File 'lib/reality/country.rb', line 96 def currency currencies.first end |
#gdp_nominal ⇒ Object Also known as: gdp
120 121 122 123 |
# File 'lib/reality/country.rb', line 120 def gdp_nominal val = infobox.fetch('GDP_nominal').text.strip.sub(/^((Int|US)?\$|USD)/, '') val.empty? ? nil : Reality::Measure(parse_scaled(val), '$') end |
#gdp_ppp ⇒ Object
115 116 117 118 |
# File 'lib/reality/country.rb', line 115 def gdp_ppp val = infobox.fetch('GDP_PPP').text.strip.sub(/^((Int|US)?\$|USD)/, '') val.empty? ? nil : Reality::Measure(parse_scaled(val), '$') end |
#inspect ⇒ Object
149 150 151 |
# File 'lib/reality/country.rb', line 149 def inspect "#<#{self.class}(#{name})>" end |
#languages ⇒ Object
73 74 75 76 77 78 |
# File 'lib/reality/country.rb', line 73 def languages [ ['Official', infobox_links('official_languages')], [infobox.fetch('languages_type').text.sub(/ languages?$/, ''), infobox_links('languages')] ].reject{|k, v| k.empty? || v.empty?}.to_h end |
#leaders ⇒ Object
127 128 129 130 131 |
# File 'lib/reality/country.rb', line 127 def leaders titles = infobox.fetch(/^leader_title\d/).map(&:text_) names = infobox.fetch(/^leader_name\d/).map{|v| v.lookup(:Wikilink).first} titles.zip(names).to_h end |
#long_name ⇒ Object
65 66 67 |
# File 'lib/reality/country.rb', line 65 def long_name infobox.fetch('conventional_long_name').text.strip end |
#member_of?(org) ⇒ Boolean
141 142 143 |
# File 'lib/reality/country.rb', line 141 def member_of?(org) organizations_list.any?{|o| o[:name] == org || o[:abbr] == org} end |
#name ⇒ Object
61 62 63 |
# File 'lib/reality/country.rb', line 61 def name page.title end |
#organizations ⇒ Object
137 138 139 |
# File 'lib/reality/country.rb', line 137 def organizations organizations_list.map{|o| o[:name]} end |
#population ⇒ Object
108 109 110 111 112 113 |
# File 'lib/reality/country.rb', line 108 def population val = %w[population_estimate population_census].map{|var| infobox.fetch(var).text.strip }.reject(&:empty?).first val && Reality::Measure(parse_maybe_scaled(val), 'person') end |
#tld ⇒ Object
80 81 82 |
# File 'lib/reality/country.rb', line 80 def tld tlds.first end |
#tlds ⇒ Object
84 85 86 |
# File 'lib/reality/country.rb', line 84 def tlds infobox_links('cctld').map(&:link) end |
#to_h ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/reality/country.rb', line 161 def to_h #p self PROPERTIES. map{|prop| [prop, to_simple_type(send(prop))] }. #reject{|prop, val| !val || val.respond_to?(:empty?) && val.empty?}. to_h end |
#to_s ⇒ Object
145 146 147 |
# File 'lib/reality/country.rb', line 145 def to_s name end |
#utc_offset ⇒ Object
92 93 94 |
# File 'lib/reality/country.rb', line 92 def utc_offset infobox.fetch('utc_offset').text.sub('−', '-').to_i end |