Class: WhatAWorld::Scraper::ScraperCli

Inherits:
Object
  • Object
show all
Defined in:
lib/what_a_world/scraper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(letter) ⇒ ScraperCli

Returns a new instance of ScraperCli.



7
8
9
10
11
12
13
# File 'lib/what_a_world/scraper.rb', line 7

def initialize(letter)
    @letter = letter
    @all_countries = []
    @all_url_extensions = []
    @letter_countries = []
    @letter_url_extensions = []
end

Instance Attribute Details

#all_countriesObject

Returns the value of attribute all_countries.



4
5
6
# File 'lib/what_a_world/scraper.rb', line 4

def all_countries
  @all_countries
end

#all_url_extensionsObject

Returns the value of attribute all_url_extensions.



4
5
6
# File 'lib/what_a_world/scraper.rb', line 4

def all_url_extensions
  @all_url_extensions
end

#letterObject (readonly)

Returns the value of attribute letter.



5
6
7
# File 'lib/what_a_world/scraper.rb', line 5

def letter
  @letter
end

#letter_countriesObject (readonly)

Returns the value of attribute letter_countries.



5
6
7
# File 'lib/what_a_world/scraper.rb', line 5

def letter_countries
  @letter_countries
end

#letter_url_extensionsObject (readonly)

Returns the value of attribute letter_url_extensions.



5
6
7
# File 'lib/what_a_world/scraper.rb', line 5

def letter_url_extensions
  @letter_url_extensions
end

Instance Method Details

#find_all_countriesObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/what_a_world/scraper.rb', line 15

def find_all_countries
    html = open(URL)
    all_countries_page = Nokogiri::HTML(html)
    country_names = ""
    country_names = all_countries_page.css(".selecter_links option[value^='geos']").text.strip
    self.all_countries = country_names.split("  ")
    self.all_url_extensions = all_countries_page.css(".selecter_links option").collect{ |link|
        link.attr('value') unless link.attr('value') === ""
    }
    self.all_url_extensions.reject!{|url| url.nil?}
end

#find_countries_by_letterObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/what_a_world/scraper.rb', line 27

def find_countries_by_letter
    indices = []
    self.all_countries.each.with_index{ |country, index|
        if country[0] == self.letter && country.gsub(" ", "") != "EuropeanUnion"
            indices << index
            self.letter_countries << country
        end
    }
    iterator = indices.first
    while iterator <= indices.last
        self.letter_url_extensions << self.all_url_extensions[iterator]
        iterator += 1
    end
end