Class: Ruboty::Handlers::Amazon_wishlist

Inherits:
Base
  • Object
show all
Defined in:
lib/ruboty/handlers/amazon_wishlist.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Amazon_wishlist

Returns a new instance of Amazon_wishlist.



15
16
17
18
# File 'lib/ruboty/handlers/amazon_wishlist.rb', line 15

def initialize(*args)
  super
  @agent = Mechanize.new
end

Instance Method Details

#cacheObject



97
98
99
# File 'lib/ruboty/handlers/amazon_wishlist.rb', line 97

def cache
  robot.brain.data[Ruboty::Amazon_wishlist::NAMESPACE] ||= {}
end

#endpoint(page = 1) ⇒ Object



93
94
95
# File 'lib/ruboty/handlers/amazon_wishlist.rb', line 93

def endpoint(page = 1)
  "#{Ruboty::Amazon_wishlist::WISHLIST_URL}/#{Ruboty::Amazon_wishlist::WISHLIST_ID}/ref=?page=#{page}"
end

#list(message) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruboty/handlers/amazon_wishlist.rb', line 20

def list(message)
  items = []
  list_items.each do |id, item|
    items << "#{item[:title]} (#{item[:price]})"
    cache[id] = item[:price]
  end

  text = StringIO.new
  if items.empty?
    text.puts "Amazonほしい物リストに登録されているタイトルがないですね"
    message.reply(text.string) if message[:verbose]
  else
    text.puts "Amazonほしい物リストに次のタイトルが登録されていますね"
    text.puts endpoint
    text.puts
    items.each { |item| text.puts item }
    message.reply(text.string)
  end
rescue
  log(:error, 'Error fetching wishlist items')
end

#list_itemsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruboty/handlers/amazon_wishlist.rb', line 69

def list_items
  items = {}

  page = @agent.get(endpoint)
  body = page.at("//body")

  page_size = body.css("div#wishlistPagination ul.a-pagination li[data-action]").size
  page_size.times do |index|
    page = @agent.get(endpoint(index + 1))
    body = page.at("//body")

    body.css("div[id^='item_']").each do |div|
      item = Hash.new
      item[:title] = div.css("a[id^='itemName_']").text.strip
      item[:price] = div.css("span[id^='itemPrice_']").text.strip.gsub('', '')

      id = div.get_attribute('id')
      items[id] = item
    end
  end

  items
end

#list_updated(message) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruboty/handlers/amazon_wishlist.rb', line 42

def list_updated(message)
  items = []
  list_items.each do |id, item|
    item[:price] = 'null' unless item[:price] =~ /¥\d+/
    last_price = cache[id].to_s.force_encoding('utf-8')
    if item[:price] != last_price
      items << "#{item[:title]} (from:#{last_price} to:#{item[:price]})"
    end

    cache[id] = item[:price]
  end

  text = StringIO.new
  if items.empty?
    text.puts "Amazonほしい物リストで価格変動はありませんでした"
    message.reply(text.string) if message[:verbose]
  else
    text.puts "Amazonほしい物リストで次のタイトルに価格変動があったようですよ"
    text.puts endpoint
    text.puts
    items.each { |item| text.puts item }
    message.reply(text.string)
  end
rescue
  log(:error, 'Error fetching wishlist items')
end

#log(level, message) ⇒ Object



101
102
103
# File 'lib/ruboty/handlers/amazon_wishlist.rb', line 101

def log(level, message)
  Ruboty.logger.send(level, "[#{Time.now}] ruboty-amazon_wishlist: #{message}")
end