Class: UCCard

Inherits:
Object
  • Object
show all
Defined in:
lib/uc_card.rb

Defined Under Namespace

Classes: ShoppingData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UCCard

Returns a new instance of UCCard.



28
29
30
31
32
33
34
35
36
# File 'lib/uc_card.rb', line 28

def initialize(options={})
  @user = options[:user] or raise ArgumentError('user')
  @password = options[:password] or raise ArgumentError('password')

  ## data
  @recent_shoppings = []

  create_agent
end

Instance Attribute Details

#recent_shoppingsObject (readonly)

Returns the value of attribute recent_shoppings.



7
8
9
# File 'lib/uc_card.rb', line 7

def recent_shoppings
  @recent_shoppings
end

Class Method Details

.start(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/uc_card.rb', line 9

def self.start(options={})
  instance=self.new(options)
  begin
    instance.
    instance.go_recent
    yield instance
  ensure
    instance.logout
  end
end

.start_with_pit(pit_key, &block) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/uc_card.rb', line 20

def self.start_with_pit(pit_key, &block)
  config = Pit.get(pit_key, :require => {
    :user => 'your name',
    :password => 'your password'
  })
  self.start(:user => config[:user], :password => config[:password], &block)
end

Instance Method Details

#go_recentObject



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

def go_recent
  @agent.get("https://atunet.uccard.co.jp/UCPc/USC0101BLC01NOW.do?r=7005820039568205309")
  @recent_shoppings = []
  Nokogiri(@agent.page.body).search("#mainWrap > table > tbody > tr")[2..-1].each{ |record|
    (date, klass, name, value, usecase, times, note, payment) = record.search("td").map{|e| e.text.strip}
    @recent_shoppings << ShoppingData.new(
      :date => date, :klass => klass, :name => name,
      :value => value, :usecase => usecase, :times => times,
      :note => note, :payment => payment)
  }
end

#loginObject

login / logout



39
40
41
42
43
44
45
46
# File 'lib/uc_card.rb', line 39

def 
  @agent.get("https://atunet.uccard.co.jp/UCPc/welcomeSCR.do")
  @agent.page.form_with(:name => '_USA01Form'){ |f|
    f.field_with(:name => 'inputId').value = @user
    f.field_with(:name => 'inputPassword').value = @password
    f.click_button
  }
end

#logoutObject



48
49
50
# File 'lib/uc_card.rb', line 48

def logout
  @agent.get('https://atunet.uccard.co.jp/UCPc/USL0100BLC01.do?r=8400550016797730123')
end

#reserved_paymentObject

api



65
66
67
68
69
# File 'lib/uc_card.rb', line 65

def reserved_payment
  recent_shoppings.inject(0) {|total, record|
    total += record.money
  }.to_i
end

#reserved_payment_this_monthObject



71
72
# File 'lib/uc_card.rb', line 71

def reserved_payment_this_month
end

#to_sObject



74
75
76
77
78
79
80
# File 'lib/uc_card.rb', line 74

def to_s
  recent_data = recent_shoppings.map(&:to_s).join($/)
  return <<-EOT
#{recent_data}
Total: #{reserved_payment}
  EOT
end