Class: AKCScraper
- Inherits:
-
Object
- Object
- AKCScraper
- Defined in:
- lib/pup_finder/scraper.rb
Constant Summary collapse
- @@load_more_link =
nil
Class Method Summary collapse
- .get_all_pages_of_breeds(size_url) ⇒ Object
- .get_first_page_breeds(size_url) ⇒ Object
- .initial_breed_instantiation(page_of_breeds, size_url) ⇒ Object
- .make_breed(number, list) ⇒ Object
Class Method Details
.get_all_pages_of_breeds(size_url) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pup_finder/scraper.rb', line 17 def self.get_all_pages_of_breeds(size_url) self.get_first_page_breeds(size_url) while @@load_more_link do # get all breeds on page # next_page_doc = Nokogiri::HTML(open(@@load_more_link)) next_page_breeds = next_page_doc.css('.breed-card-type-grid .grid-col') initial_breed_instantiation(next_page_breeds, size_url) # get load more link on page # @@load_more_link = next_page_doc.css(".mt2 a").attribute("href") end end |
.get_first_page_breeds(size_url) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/pup_finder/scraper.rb', line 5 def self.get_first_page_breeds(size_url) #open first url based on user input doc = Nokogiri::HTML(open(size_url)) # get all breeds on page 1 first_page_breeds = doc.css('.breed-card-type-grid .grid-col') initial_breed_instantiation(first_page_breeds, size_url) # get load more link on page 1 @@load_more_link = doc.css(".mt2 a").attribute("href").value end |
.initial_breed_instantiation(page_of_breeds, size_url) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/pup_finder/scraper.rb', line 31 def self.initial_breed_instantiation(page_of_breeds, size_url) page_of_breeds.each do |breed| breed_name = breed.css(".breed-type-card a h3").text breed_link = breed.css(".breed-type-card a").attribute('href').value breed_size = size_url.split("=")[1] Breed.new(breed_name, breed_link, breed_size) end end |
.make_breed(number, list) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pup_finder/scraper.rb', line 40 def self.make_breed(number, list) breed_to_scrape = list[number - 1] # Creates an array with just the object we want to add more data too breed_object_array = Breed.all.select {|breed| breed.breed_name == breed_to_scrape} doc = Nokogiri::HTML(open(breed_object_array[0].breed_link)) # Get each li and set each attribute all_lis = doc.css(".panel-flex__aside ul li.attribute-list__row") all_lis.each do |li| if li.css(".attribute-list__term").text == "Temperament:" breed_object_array[0].temperament = li.css(".attribute-list__description").text elsif li.css(".attribute-list__term").text == "Weight:" breed_object_array[0].weight = li.css(".attribute-list__description").text elsif li.css(".attribute-list__term").text == "Life Expectancy:" breed_object_array[0].life_expectancy = li.css(".attribute-list__description").text.strip end end # Sets the breed description if doc.css(".breed-hero__footer") breed_object_array[0].description = doc.css(".breed-hero__footer").text.strip else breed_object_array[0].description = "none available" end end |