Class: AmazonReview::Review

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon-review/review.rb

Instance Method Summary collapse

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

#dateObject



30
31
32
# File 'lib/amazon-review/review.rb', line 30

def date
  @date ||= Date.parse(@div.css("nobr").first.text)
end

#helpful_countObject



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_ratioObject



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

#idObject



13
14
15
# File 'lib/amazon-review/review.rb', line 13

def id
  @id ||= @html['name']
end

#inspectObject



9
10
11
# File 'lib/amazon-review/review.rb', line 9

def inspect
  "<Review: id=#{id}>"
end

#ratingObject



39
40
41
42
# File 'lib/amazon-review/review.rb', line 39

def rating
  regex = /[0-9\.]+/
  @rating ||= Float( @div.css("span.swSprite").first['title'][regex] )
end

#textObject



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

#titleObject



26
27
28
# File 'lib/amazon-review/review.rb', line 26

def title
  @title ||= @div.css("b").first.text.strip
end

#to_hashObject



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

#urlObject



17
18
19
# File 'lib/amazon-review/review.rb', line 17

def url   
  @url ||= "http://www.amazon.com/review/#{id}"
end

#user_idObject



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