class Ruboty::Handlers::Rakuten_rental

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/ruboty/handlers/rakuten_rental.rb, line 15
def initialize(*args)
  super
  @agent = Mechanize.new
end

Public Instance Methods

cache() click to toggle source
# File lib/ruboty/handlers/rakuten_rental.rb, line 111
def cache
  robot.brain.data[Ruboty::Rakuten_rental::NAMESPACE] ||= {}
end
list(message) click to toggle source
# File lib/ruboty/handlers/rakuten_rental.rb, line 20
def list(message)
  items = []
  list_items.each do |id, item|
    items << item
    cache[id] = item[:status]
  end

  text = StringIO.new
  if items.empty?
    text.puts "楽天レンタルに登録されているタイトルがないですね"
    message.reply(text.string) if message[:verbose]
  else
    text.puts "楽天レンタルに次のタイトルが登録されていますね"
    text.puts "http://rental.rakuten.jp/member/my_list/list"
    text.puts
    items.each do |item| 
      text.print "[#{item[:type]}] #{item[:title]} "
      text.print "(#{item[:artist]}) " if item[:artist]
      text.puts  "- #{item[:price]} #{item[:status]}"
    end
    message.reply(text.string)
  end
end
list_items() click to toggle source
# File lib/ruboty/handlers/rakuten_rental.rb, line 72
def list_items
  items = {}

  login do |page|
    page.search('table[@class="myListTable"]/tr').each do |tr|
      td = tr.search('td')
      next if td.empty?

      item = Hash.new
      item[:type]   = td[1].text.strip
      item[:status] = td[2].text.strip
      item[:title]  = td[3].at_xpath('a').text.strip
      item[:artist] = td[3].at_xpath('div').text.strip if td[3].at_xpath('div')
      item[:price]  = td[4].text.strip

      id = td[0].at_xpath('input').get_attribute('name')
      items[id] = item
    end
  end

  items
end
list_updated(message) click to toggle source
# File lib/ruboty/handlers/rakuten_rental.rb, line 44
def list_updated(message)
  items = []
  list_items.each do |id, item|
    last_status = cache[id].to_s.force_encoding('utf-8')
    if item[:status] == "レンタル可能" && last_status != "レンタル可能"
      items << item
    end

    cache[id] = item[:status]
  end

  text = StringIO.new
  if items.empty?
    text.puts "楽天レンタルで利用可能になったタイトルはないですね"
    message.reply(text.string) if message[:verbose]
  else
    text.puts "楽天レンタルで次のタイトルが利用可能になったようですよ"
    text.puts "http://rental.rakuten.jp/member/my_list/list"
    text.puts
    items.each do |item|
      text.print "[#{item[:type]}] #{item[:title]} "
      text.print "(#{item[:artist]}) " if item[:artist]
      text.puts  "- #{item[:price]}"
    end
    message.reply(text.string)
  end
end
login() { |page| ... } click to toggle source
# File lib/ruboty/handlers/rakuten_rental.rb, line 95
def login
  @agent.get(Ruboty::Rakuten_rental::MYLIST_URL) do |page|
    page = page.forms.first.submit # redirect
    page.form_with(name: "LoginForm") do |form|
      form.field_with(name: 'u').value = Ruboty::Rakuten_rental::USERNAME
      form.field_with(name: 'p').value = Ruboty::Rakuten_rental::PASSWORD
    end.submit
  end

  @agent.get(Ruboty::Rakuten_rental::MYLIST_URL) do |page|
    yield page
  end

  @agent.get(Ruboty::Rakuten_rental::LOGOUT_URL)
end