Class: Wikiloc::InfoGetter

Inherits:
Object
  • Object
show all
Defined in:
lib/wikiloc/info_getter.rb

Instance Method Summary collapse

Constructor Details

#initialize(location, data_type) ⇒ InfoGetter

Returns a new instance of InfoGetter.



3
4
5
# File 'lib/wikiloc/info_getter.rb', line 3

def initialize(location, data_type)
  @location, @data_type = location, data_type
end

Instance Method Details

#agentObject



26
27
28
# File 'lib/wikiloc/info_getter.rb', line 26

def agent
  @agent ||= Mechanize.new.tap { |agent| agent.user_agent_alias = 'Mac Safari' }
end

#cut_number_off(data) ⇒ Object



39
40
41
# File 'lib/wikiloc/info_getter.rb', line 39

def cut_number_off(data)
  data.gsub /[0-9]+.*/, ""
end

#find_data_in_table(datatype) ⇒ Object



30
31
32
33
# File 'lib/wikiloc/info_getter.rb', line 30

def find_data_in_table(datatype)
  key_row = page.parser.xpath("//tr[contains(.,'#{datatype}')]").first
  cut_number_off key_row.children[2].children.first.text
end

#get_infoObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wikiloc/info_getter.rb', line 11

def get_info
  case @data_type.downcase
  #when 'link'
  #  'http://en.wikipedia.org/wiki/' + @location
  when 'capital'
    find_data_in_table 'Capital'
  when 'currency'
    find_data_in_table 'Currency'
  when 'language'
    find_data_in_table 'Official language'
  else
    :invalid
  end
end

#pageObject



35
36
37
# File 'lib/wikiloc/info_getter.rb', line 35

def page
  @page ||= agent.get(page_url + @location)
end

#page_urlObject



7
8
9
# File 'lib/wikiloc/info_getter.rb', line 7

def page_url
  'http://en.wikipedia.org/wiki/'
end