Class: Wikiloc::InfoGetter
- Inherits:
-
Object
- Object
- Wikiloc::InfoGetter
- Defined in:
- lib/wikiloc/info_getter.rb
Instance Method Summary collapse
- #agent ⇒ Object
- #cut_number_off(data) ⇒ Object
- #find_data_in_table(datatype) ⇒ Object
- #get_info ⇒ Object
-
#initialize(location, data_type) ⇒ InfoGetter
constructor
A new instance of InfoGetter.
- #page ⇒ Object
- #page_url ⇒ Object
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
#agent ⇒ Object
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_info ⇒ Object
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 |
#page ⇒ Object
35 36 37 |
# File 'lib/wikiloc/info_getter.rb', line 35 def page @page ||= agent.get(page_url + @location) end |
#page_url ⇒ Object
7 8 9 |
# File 'lib/wikiloc/info_getter.rb', line 7 def page_url 'http://en.wikipedia.org/wiki/' end |