Class: AmazonDeets::KindleFragment::Context

Inherits:
MechanizedContext show all
Defined in:
lib/amazon_deets/kindle.rb

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from MechanizedContext

#agent

Instance Method Summary collapse

Methods inherited from MechanizedContext

#initialize

Constructor Details

This class inherits a constructor from AmazonDeets::MechanizedContext

Instance Method Details

#current_priceObject



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

def current_price
  cp_element = agent.page.search("td b.priceLarge").first
  if cp_element
    return cp_element.text.gsub(/[^.\d]/, "")
  end
end

#list_priceObject



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

def list_price
  lp_element = agent.page.search("td.listPrice").first
  if lp_element
    return lp_element.text.gsub(/[^.\d]/, "")
  end
end

#ratingObject



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

def rating
  result = agent.page.search("span.crAvgStars span[title$='5 stars']").first
  if result
    m = RatingRegex.match result[:title]
    LOG.info result[:title]
    if m and m[1]
      return m[1]
    end
  else
    LOG.warning "Unable to locate rating element"
  end
end

#reviewsObject



64
65
66
67
68
69
70
71
72
# File 'lib/amazon_deets/kindle.rb', line 64

def reviews
  reviews_element = agent.page.search("//span[@class='crAvgStars']/a[contains(text(), 'reviews')]")
  if reviews_element
    text = reviews_element.text.gsub(/[^\d]/, "")
    return text.to_i unless text.empty?
  else
    LOG.warning "Reviews element could not be found"
  end
end

#scrapeObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/amazon_deets/kindle.rb', line 74

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

#titleObject



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

def title
  result = agent.page.search("span#btAsinTitle").first
  if result
    return result.text.strip
  end
end

#urlObject



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

def url
  agent.page.uri.to_s
end