Class: MizuhoBank

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

Defined Under Namespace

Classes: MizuhoAccount, MizuhoCacheFlow, MizuhoDirectInfo, RepeatDate, RepeatDateRange

Constant Summary collapse

TOP_URL =
"https://web.ib.mizuhobank.co.jp/servlet/mib?xtr=Emf00000"
CACHEFLOW_URL =
'/servlet/mib?xtr=Emf04000&NLS=JP'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keiyaku_no, password, aikotoba_dict = {}, logger = nil, &block) ⇒ MizuhoBank

Returns a new instance of MizuhoBank.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mizuho_bank.rb', line 38

def initialize(keiyaku_no, password, aikotoba_dict={}, logger=nil, &block)
  @agent = nil
  @logger = Logger.new(nil)
  @logger = logger unless logger.nil?

  unless MizuhoBank.is_available?
    raise "MizuhoBank is not available"
  end

  if (keiyaku_no, password, aikotoba_dict)
    block.call(self) if block_given?
  end
  self
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



36
37
38
# File 'lib/mizuho_bank.rb', line 36

def info
  @info
end

Class Method Details

.get_jikangai_ranges(context = Time.now) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/mizuho_bank.rb', line 190

def self.get_jikangai_ranges(context = Time.now)
  url = "http://www.mizuhobank.co.jp/direct/jikangai.html"
  body = open(url).read.toutf8
  doc = Nokogiri(body)
  unless body.include? "以下の時間帯は、インターネットバンキングをご利用いただけません。"
    raise "page format error"
  end

  dates = doc.search("#contents > div.section > div.inner > table :first-child > th > ul.normal > li > strong").map(&:text)

  ranges = []
  if dates.first =~ /土曜日(\d+)時(\d+)分~翌日曜日(\d+)時(\d+)分/
    range = RepeatDateRange.new
    range.start.wday = "last saturday"
    range.start.hour = Regexp.last_match(1).to_i
    range.start.minute = Regexp.last_match(2).to_i
    range.end.wday = "next sunday"
    range.end.hour = Regexp.last_match(3).to_i
    range.end.minute = Regexp.last_match(4).to_i
    ranges << range
  end

  if dates.second =~ /第1・第4土曜日(\d+)時(\d+)分~(\d+)時(\d+)分/
    range = RepeatDateRange.new
    range.start.wday = "1st saturday in this month"
    range.start.hour = Regexp.last_match(1).to_i
    range.start.minute = Regexp.last_match(2).to_i
    range.end.wday = nil
    range.end.hour = Regexp.last_match(3).to_i
    range.end.minute = Regexp.last_match(4).to_i
    ranges << range

    range = RepeatDateRange.new
    range.start.wday = "4th saturday in this month"
    range.start.hour = Regexp.last_match(1).to_i
    range.start.minute = Regexp.last_match(2).to_i
    range.end.wday = nil
    range.end.hour = Regexp.last_match(3).to_i
    range.end.minute = Regexp.last_match(4).to_i
    ranges << range
  end

  ranges
end

.is_available?(time = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
# File 'lib/mizuho_bank.rb', line 118

def self.is_available?(time = Time.now)
  get_jikangai_ranges(time).each{ |range|
    if range.cover? time
      return false
    end
  }
  return true
end

.new_with_pit(logger = nil, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mizuho_bank.rb', line 53

def self.new_with_pit(logger = nil, &block)
  pit = Pit.get("MizuhoBank",
                :require => {
                  "keiyaku_no" => "keiyaku_no",
                  "password" => "password",
                  "aikotoba1_question" => "aikotoba1_question",
                  "aikotoba2_question" => "aikotoba2_question",
                  "aikotoba3_question" => "aikotoba3_question",
                  "aikotoba1_answer" => "aikotoba1_answer",
                  "aikotoba2_answer" => "aikotoba2_answer",
                  "aikotoba3_answer" => "aikotoba3_answer",
                })

  aikotoba_dict = {
    pit['aikotoba1_question'] => pit['aikotoba1_answer'],
    pit['aikotoba2_question'] => pit['aikotoba2_answer'],
    pit['aikotoba3_question'] => pit['aikotoba3_answer']
  }
  self.new(pit['keiyaku_no'].to_s, pit['password'].to_s, aikotoba_dict, logger){ |bank|
    block.call(bank)
  }
end

Instance Method Details

#login(keiyaku_no, password, aikotoba_dict = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mizuho_bank.rb', line 76

def (keiyaku_no, password, aikotoba_dict={})
  @login_result = false
  Proc.new do
    @agent = Mechanize.new{ |agent|
      agent.user_agent_alias = "Windows IE 7"
    }

    auth_keiyaku_no(keiyaku_no)

    while true
      page_id = what_is_this_page?
      @logger.info "page: #{page_id.inspect}"

      case page_id
      when :auth_keiyaku_no
        auth_keiyaku_no(keiyaku_no)
      when :auth_aikotoba
        aikotoba_theme = extract_aikotoba_theme
        aikotoba_answer = aikotoba_dict[aikotoba_theme]
        auth_aikotoba(aikotoba_theme, aikotoba_answer)
      when :auth_password
        auth_password(password)
      when :logout_fail_info
        raise "Logout fail info page!!"
      when :not_available_page
        raise "not available page"
      when :unknown
        @logger.info "unknown page reached: #{@agent.page.title}"
        @login_result = true

        @info = load_bank_page(@agent.page.body.toutf8)
        go_cacheflow_page
        @info. = load_cacheflow_page(@agent.page.body.toutf8)
        break
      when :not_connection
        @logger.info "not connection"
      end
    end
  end.retry(:logger => @logger, :accept_exception => StandardError, :wait => 5, :max => 5)
  @login_result
end