Class: AmazonReview::Review
- Inherits:
-
Object
- Object
- AmazonReview::Review
- Defined in:
- lib/amazon-review/review.rb
Instance Method Summary collapse
- #date ⇒ Object
- #helpful_count ⇒ Object
- #helpful_ratio ⇒ Object
- #id ⇒ Object
-
#initialize(html) ⇒ Review
constructor
A new instance of Review.
- #inspect ⇒ Object
- #rating ⇒ Object
- #text ⇒ Object
- #title ⇒ Object
- #to_hash ⇒ Object
- #url ⇒ Object
- #user_id ⇒ Object
Constructor Details
#initialize(html) ⇒ Review
Returns a new instance of Review.
4 5 6 7 |
# File 'lib/amazon-review/review.rb', line 4 def initialize(html) @html = html @div = html.next_element.next_element end |
Instance Method Details
#date ⇒ Object
30 31 32 |
# File 'lib/amazon-review/review.rb', line 30 def date @date ||= Date.parse(@div.css("nobr").first.text) end |
#helpful_count ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/amazon-review/review.rb', line 44 def helpful_count if helpful_match @helpful_count ||= Float(helpful_match.captures[0]) else @helpful_count = nil end @helpful_count end |
#helpful_ratio ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/amazon-review/review.rb', line 54 def helpful_ratio if helpful_match @helpful_ratio ||= Float(helpful_match.captures[0]) / Float(helpful_match.captures[1]) else @helpful_ratio = nil end @helpful_ratio end |
#id ⇒ Object
13 14 15 |
# File 'lib/amazon-review/review.rb', line 13 def id @id ||= @html['name'] end |
#inspect ⇒ Object
9 10 11 |
# File 'lib/amazon-review/review.rb', line 9 def inspect "<Review: id=#{id}>" end |
#rating ⇒ Object
39 40 41 42 |
# File 'lib/amazon-review/review.rb', line 39 def regex = /[0-9\.]+/ @rating ||= Float( @div.css("span.swSprite").first['title'][regex] ) end |
#text ⇒ Object
34 35 36 37 |
# File 'lib/amazon-review/review.rb', line 34 def text # remove leading and trailing line returns, tabs, and spaces @text ||= @div.css(".reviewText").first.content.strip #sub(/\A[\n\t\s]+/,"").sub(/[\n\t\s]+\Z/,"") end |
#title ⇒ Object
26 27 28 |
# File 'lib/amazon-review/review.rb', line 26 def title @title ||= @div.css("b").first.text.strip end |
#to_hash ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/amazon-review/review.rb', line 64 def to_hash attrs = [:id, :url, :user_id, :title, :date, :text, :rating, :helpful_count, :helpful_ratio] attrs.inject({}) do |r,attr| r[attr] = self.send(attr) r end end |
#url ⇒ Object
17 18 19 |
# File 'lib/amazon-review/review.rb', line 17 def url @url ||= "http://www.amazon.com/review/#{id}" end |
#user_id ⇒ Object
21 22 23 24 |
# File 'lib/amazon-review/review.rb', line 21 def user_id regex = /[A-Z0-9]+/ @user_id ||= @div.css('a[href^="/gp/pdp/profile"]').first["href"][regex] end |