Class: AmazonDeets::GeneralMerchandiseFragment::Context

Inherits:
MechanizedContext show all
Defined in:
lib/amazon_deets/general_merchandise.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



48
49
50
51
52
53
54
55
56
# File 'lib/amazon_deets/general_merchandise.rb', line 48

def current_price
  cp_element = agent.page.search("//span[@id='priceblock_saleprice']").first
  if cp_element
    return cp_element.text
  else
    LOG.debug "Looks like no sale is going on.  Returning list price"
    return list_price
  end
end

#list_priceObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/amazon_deets/general_merchandise.rb', line 37

def list_price
  lp_element = agent.page.search("//span[@id='priceblock_ourprice']").first
  if lp_element.nil?
    lp_element = agent.page.search("//td[text()='Price:']/following-sibling::td")
  end

  if lp_element
    return lp_element.text.gsub(/[^.\d]/, "")
  end
end

#ratingObject



58
59
60
61
62
63
64
65
66
# File 'lib/amazon_deets/general_merchandise.rb', line 58

def rating
  result = agent.page.search("//div[@id='averageCustomerReviews']//span[@title]").first
  if result
    m = RatingRegex.match result[:title]
    if m and m[1]
      return m[1]
    end
  end
end

#reviewsObject



68
69
70
71
72
73
74
75
76
# File 'lib/amazon_deets/general_merchandise.rb', line 68

def reviews
  reviews_element = agent.page.search("//div[@id='averageCustomerReviews']//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



79
80
81
82
83
84
85
86
87
88
# File 'lib/amazon_deets/general_merchandise.rb', line 79

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/general_merchandise.rb', line 26

def title
  result = agent.page.search("//h1[@id='title']").first
  if result
    return result.text.strip
  end
end

#urlObject



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

def url
  agent.page.uri.to_s
end