Class: CatsToAdopt::Scraper

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

Class Method Summary collapse

Class Method Details

.scrape_main_page(main_page_url) ⇒ Object

for each cat on the main page, scrapes the page for the cat’s data and instantiates a Cat object with that data.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cats_to_adopt/scraper.rb', line 5

def self.scrape_main_page(main_page_url)
  main_page = Nokogiri::HTML(open(main_page_url))

  main_page.search(".pet-list-item").each do |cat|
    new_cat = CatsToAdopt::Cat.new
    new_cat.name = cat.search(".views-field-field-animal-name").text.strip
    new_cat.id = cat.search(".views-field-field-animal-name a").attr("href").text.gsub("/get-involved/adopt/pet/", "").strip
    new_cat.gender = cat.search(".views-field-field-animal-sex .field-content").text.strip
    new_cat.size = cat.search(".views-field-field-animal-size .field-content").text.strip
    new_cat.location = cat.search(".views-field-field-shelter-state .field-content").text.strip
  end
end

.scrape_profile_page(profile_url) ⇒ Object

given a cat’s profile page URL, scrapes the cat’s profile page and returns a hash of additional attributes for that cat



20
21
22
23
24
25
26
27
28
29
# File 'lib/cats_to_adopt/scraper.rb', line 20

def self.scrape_profile_page(profile_url)
  profile_page = Nokogiri::HTML(open(profile_url))
  attributes = {}

  attributes[:color] = profile_page.search(".petpoint-pet-color .info").text.strip
  attributes[:weight] = profile_page.search(".card__info .petpoint-pet-weight:nth-child(7) .info").text.strip
  attributes[:age] = profile_page.search(".card__info .petpoint-pet-age:nth-child(4) .info").text.strip

  attributes
end