class AmazonReview::Review
Public Class Methods
new(html)
click to toggle source
# File lib/amazon-review/review.rb, line 4 def initialize(html) @html = html @div = html.next_element.next_element end
Public Instance Methods
date()
click to toggle source
# File lib/amazon-review/review.rb, line 30 def date @date ||= Date.parse(@div.css("nobr").first.text) end
helpful_count()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/amazon-review/review.rb, line 13 def id @id ||= @html['name'] end
inspect()
click to toggle source
# File lib/amazon-review/review.rb, line 9 def inspect "<Review: id=#{id}>" end
rating()
click to toggle source
# File lib/amazon-review/review.rb, line 39 def rating regex = /[0-9\.]+/ @rating ||= Float( @div.css("span.swSprite").first['title'][regex] ) end
text()
click to toggle source
# 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()
click to toggle source
# File lib/amazon-review/review.rb, line 26 def title @title ||= @div.css("b").first.text.strip end
to_hash()
click to toggle source
# 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()
click to toggle source
# File lib/amazon-review/review.rb, line 17 def url @url ||= "http://www.amazon.com/review/#{id}" end
user_id()
click to toggle source
# 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
Private Instance Methods
helpful_match()
click to toggle source
# File lib/amazon-review/review.rb, line 74 def helpful_match @helpful_match ||= @div.text.match(/(\d+) of (\d+) people/) end