Class: Reality::Country

Inherits:
Object
  • Object
show all
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

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_continentsObject



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

.organizationsObject



183
184
185
# File 'lib/reality/country.rb', line 183

def organizations
  @organizations ||= YAML.load(File.read(File.expand_path('../../../data/country_orgs.yaml', __FILE__)))
end

Instance Method Details

#areaObject



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_codeObject



88
89
90
# File 'lib/reality/country.rb', line 88

def calling_code
  infobox.fetch('calling_code').text.strip
end

#capitalObject



69
70
71
# File 'lib/reality/country.rb', line 69

def capital
  infobox.fetch('capital').lookup(:Wikilink).first
end

#continentObject



133
134
135
# File 'lib/reality/country.rb', line 133

def continent
  self.class.by_continents[page.title]
end

#currenciesObject



100
101
102
# File 'lib/reality/country.rb', line 100

def currencies
  infobox_links('currency').reject{|l| l.link == 'ISO 4217'}
end

#currencyObject



96
97
98
# File 'lib/reality/country.rb', line 96

def currency
  currencies.first
end

#gdp_nominalObject 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_pppObject



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

#inspectObject



149
150
151
# File 'lib/reality/country.rb', line 149

def inspect
  "#<#{self.class}(#{name})>"
end

#languagesObject



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

#leadersObject



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_nameObject



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

Returns:

  • (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

#nameObject



61
62
63
# File 'lib/reality/country.rb', line 61

def name
  page.title
end

#organizationsObject



137
138
139
# File 'lib/reality/country.rb', line 137

def organizations
  organizations_list.map{|o| o[:name]}
end

#populationObject



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

#tldObject



80
81
82
# File 'lib/reality/country.rb', line 80

def tld
  tlds.first
end

#tldsObject



84
85
86
# File 'lib/reality/country.rb', line 84

def tlds
  infobox_links('cctld').map(&:link)
end

#to_hObject



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_sObject



145
146
147
# File 'lib/reality/country.rb', line 145

def to_s
  name
end

#utc_offsetObject



92
93
94
# File 'lib/reality/country.rb', line 92

def utc_offset
  infobox.fetch('utc_offset').text.sub('', '-').to_i
end