Class: AmazonDeets::Grabber

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

Constant Summary collapse

LOG =
Logbert[self]
RatingRegex =
/(.+)\s+out\sof/
ReviewsRegex =
/(\d+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent: Mechanize.new) ⇒ Grabber

Returns a new instance of Grabber.



14
15
16
# File 'lib/amazon_deets.rb', line 14

def initialize(agent: Mechanize.new)
  @agent = agent
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



12
13
14
# File 'lib/amazon_deets.rb', line 12

def agent
  @agent
end

Instance Method Details

#current_priceObject



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

def current_price
  agent.page.search("//span[@id='actualPriceValue']").text
end

#details_hashObject



51
52
53
54
55
56
57
58
59
# File 'lib/amazon_deets.rb', line 51

def details_hash
  return {
    title:         title,
    list_price:    list_price,
    current_price: current_price,
    rating:        rating,
    reviews:       reviews
  }
end

#grab(url) ⇒ Object



62
63
64
65
# File 'lib/amazon_deets.rb', line 62

def grab(url)
  agent.get(url)
  details_hash
end

#list_priceObject



23
24
25
26
27
28
29
30
31
# File 'lib/amazon_deets.rb', line 23

def list_price
  nodes = agent.page.search("//span[@id='listPriceValue']")
  if nodes.any?
    return nodes.text
  else
    LOG.debug "List price was not found.  Returning current price instead."
    return current_price
  end
end

#ratingObject



38
39
40
41
42
# File 'lib/amazon_deets.rb', line 38

def rating
  text = agent.page.search("//div[@id='acr']//span[@title]").text
  m = RatingRegex.match(text)
  m[1].to_f
end

#reviewsObject



44
45
46
47
48
# File 'lib/amazon_deets.rb', line 44

def reviews
  text = agent.page.search("//div[@id='acr']//div[contains(@class, 'acrCount')]").text
  m = ReviewsRegex.match(text)
  m[1].to_i
end

#titleObject



18
19
20
# File 'lib/amazon_deets.rb', line 18

def title
  agent.page.search("//span[@id='btAsinTitle']").text
end