Class: Scraper

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

Class Method Summary collapse

Class Method Details

.scrape_adoptable_pets(animal) ⇒ Object



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

def self.scrape_adoptable_pets(animal)
  pets = []
  doc = Nokogiri::HTML(open("http://bestfriends.org/adopt/adopt-our-sanctuary/#{animal}"))
  doc.css("div.rg-animal").each do |pet|
    pet_details = {}
    pet_details[:species] = animal
    pet_details[:name] = pet.css("span.animalName").text
    pet_details[:breed] = pet.css("span.animalBreed").text
    pet_details[:age] = pet.css("span.animalAge").text
    pet_details[:url] = pet.css("a").attribute("href").value
    pets << pet_details
  end
  pets
end

.scrape_pet_profile(url) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scraper.rb', line 18

def self.scrape_pet_profile(url)
  pet_profile_details = {}

  pet_profile = "http://bestfriends.org" + url
  doc = Nokogiri::HTML(open(pet_profile))

  pet_items = doc.css("div.rescue-groups-pet-info-item")

  pet_items.each do |attribute|
    if attribute.css("span.gray").text == "Size:"
      pet_profile_details[:size] = attribute.css("span.bold.black").text
    elsif attribute.css("span.gray").text == "Color:"
      pet_profile_details[:color] = attribute.css("span.bold.black").text
    elsif attribute.css("span.gray").text == "Sex:"
      pet_profile_details[:sex] = attribute.css("span.bold.black").text
    end
  end
  pet_profile_details[:description] = doc.css("section.rescue-groups-pet-info-section p").text.gsub("\n                        ", "")
  pet_profile_details
end