Class: AmazonDeets::Grabber
- Inherits:
-
Object
- Object
- AmazonDeets::Grabber
- Defined in:
- lib/amazon_deets.rb
Constant Summary collapse
- LOG =
Logbert[self]
- RatingRegex =
/(.+)\s+out\sof/
- ReviewsRegex =
/(\d+)/
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
Instance Method Summary collapse
- #current_price ⇒ Object
- #details_hash ⇒ Object
- #grab(url) ⇒ Object
-
#initialize(agent: Mechanize.new) ⇒ Grabber
constructor
A new instance of Grabber.
- #list_price ⇒ Object
- #rating ⇒ Object
- #reviews ⇒ Object
- #title ⇒ Object
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
#agent ⇒ Object
Returns the value of attribute agent.
12 13 14 |
# File 'lib/amazon_deets.rb', line 12 def agent @agent end |
Instance Method Details
#current_price ⇒ Object
33 34 35 |
# File 'lib/amazon_deets.rb', line 33 def current_price agent.page.search("//span[@id='actualPriceValue']").text end |
#details_hash ⇒ Object
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: , 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_price ⇒ Object
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 |
#rating ⇒ Object
38 39 40 41 42 |
# File 'lib/amazon_deets.rb', line 38 def text = agent.page.search("//div[@id='acr']//span[@title]").text m = RatingRegex.match(text) m[1].to_f end |
#reviews ⇒ Object
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 |
#title ⇒ Object
18 19 20 |
# File 'lib/amazon_deets.rb', line 18 def title agent.page.search("//span[@id='btAsinTitle']").text end |