Class: PetRescue::ListingPageParser

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

Overview

Parses an HTML listing page from petrescue.com.au

Instance Method Summary collapse

Instance Method Details

#parse(document) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pet_rescue/listing_page_parser.rb', line 7

def parse(document)
  @document = Nokogiri::HTML(document)

  Pet.new(name: pet_name,
          size: pet_size,
          gender: pet_gender,
          breed: pet_breed,
          age: pet_age,
          location: pet_location,
          vaccinated: pet_vaccinated?,
          desexed: pet_desexed?,
          biography: pet_biography,
          rescue_group: pet_rescue_group,
          small_photo_url: pet_small_photo_url,
          large_photo_url: pet_large_photo_url,
          adoption_fee: pet_adoption_fee,
          adoption_process: pet_adoption_process,
          contact_name: pet_contact_name,
          contact_number: pet_contact_number,
          rescued: pet_rescued?)
end

#pet_adoption_feeObject



65
66
67
# File 'lib/pet_rescue/listing_page_parser.rb', line 65

def pet_adoption_fee
  @document.at_css("dd.adoption_fee").text.strip.gsub("$", "").to_i
end

#pet_adoption_processObject



69
70
71
72
# File 'lib/pet_rescue/listing_page_parser.rb', line 69

def pet_adoption_process
  node = @document.at_css(".adoption_process")
  node && node.text.strip
end

#pet_ageObject



45
46
47
# File 'lib/pet_rescue/listing_page_parser.rb', line 45

def pet_age
  @document.at_css("dd.age").text.strip
end

#pet_biographyObject



61
62
63
# File 'lib/pet_rescue/listing_page_parser.rb', line 61

def pet_biography
  @document.at_css(".personality").text.strip
end

#pet_breedObject



41
42
43
# File 'lib/pet_rescue/listing_page_parser.rb', line 41

def pet_breed
  species.split(" ").drop(2).join(" ")
end

#pet_contact_nameObject



78
79
80
81
# File 'lib/pet_rescue/listing_page_parser.rb', line 78

def pet_contact_name
  node = @document.at_css("dd.contact_name")
  node && node.text.strip
end

#pet_contact_numberObject



83
84
85
86
87
# File 'lib/pet_rescue/listing_page_parser.rb', line 83

def pet_contact_number
  node = @document.at_css("dt.contact_number")
  node &&= node.next_element.children.first
  node && node.text.strip
end

#pet_desexed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/pet_rescue/listing_page_parser.rb', line 57

def pet_desexed?
  @document.at_css("dd.desexed").text.strip == "Yes"
end

#pet_genderObject



37
38
39
# File 'lib/pet_rescue/listing_page_parser.rb', line 37

def pet_gender
  species.split(" ")[1]
end

#pet_large_photo_urlObject



93
94
95
# File 'lib/pet_rescue/listing_page_parser.rb', line 93

def pet_large_photo_url
  photo_node && photo_node.at_css("a").attributes["href"].value
end

#pet_locationObject



49
50
51
# File 'lib/pet_rescue/listing_page_parser.rb', line 49

def pet_location
  @document.at_css(".located_in").text.strip.gsub("Located in ", "").strip
end

#pet_nameObject



29
30
31
# File 'lib/pet_rescue/listing_page_parser.rb', line 29

def pet_name
  @document.at_css("#main h1").text.strip
end

#pet_rescue_groupObject



74
75
76
# File 'lib/pet_rescue/listing_page_parser.rb', line 74

def pet_rescue_group
  @document.at_css("dd.fostered_by").text.strip
end

#pet_rescued?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/pet_rescue/listing_page_parser.rb', line 97

def pet_rescued?
  !!@document.at_css(".adopt_this_pet_container .rehomed")
end

#pet_sizeObject



33
34
35
# File 'lib/pet_rescue/listing_page_parser.rb', line 33

def pet_size
  species.split(" ")[0]
end

#pet_small_photo_urlObject



89
90
91
# File 'lib/pet_rescue/listing_page_parser.rb', line 89

def pet_small_photo_url
  photo_node && photo_node.at_css("img").attributes["src"].value
end

#pet_vaccinated?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/pet_rescue/listing_page_parser.rb', line 53

def pet_vaccinated?
  @document.at_css("dd.vaccinated").text.strip == "Yes"
end