class TakuhaiStatus::KuronekoYamato

Attributes

key[R]
state[R]
time[R]

Public Class Methods

new(key) click to toggle source
# File lib/takuhai_status/kuronekoyamato.rb, line 9
def initialize(key)
        @key = key.strip
        raise NotMyKey.new('invalid key format') unless @key =~ /\A[0-9]+\Z/
        @time, @state = check
end

Public Instance Methods

finish?() click to toggle source
# File lib/takuhai_status/kuronekoyamato.rb, line 15
def finish?
        return !!(@state =~ /^(お客様引渡|配達|投函)完了|返品/)
end

Private Instance Methods

check() click to toggle source
# File lib/takuhai_status/kuronekoyamato.rb, line 20
def check
        conn = Faraday.new(url: 'http://toi.kuronekoyamato.co.jp/')
        res = conn.post('/cgi-bin/tneko', {number00: '1', number01: @key})
        doc = Nokogiri(res.body)

        begin
                tr = doc.css('.meisai')[0].css('tr').last
                state = tr.css('td')[1].text
                sday = tr.css('td')[2].text
                stime = tr.css('td')[3].text
                time = Time.parse("#{sday} #{stime}")

                if state == '国内到着'
                        begin
                                time, state = global_state(doc)
                        rescue
                                $stderr.puts "error in yamato global, about #{key}"
                        end
                end

                return time, state
        rescue NoMethodError
                raise NotMyKey
        rescue ArgumentError
                return Time.now, state || ''
        end
end
global_state(doc) click to toggle source
# File lib/takuhai_status/kuronekoyamato.rb, line 48
def global_state(doc)
        form = doc.css('form[method=POST]')
        values = form.css('input[type=hidden]').map{|x| [x[:name], x[:value]]}.flatten
        params = Hash[*values]

        res = Faraday.new.post(
                'http://globaltoi.kuronekoyamato.co.jp/Global/outside',
                params
        )
        html = res.body.force_encoding('Shift_JIS').encode('UTF-8')
        tr = Nokogiri(html).css('table.detail tr')
        state = tr[tr.size-1].css('td')[1].text
        loc = tr[tr.size-1].css('td')[4].text.strip.chop.chop
        stime = "#{tr[tr.size-1].css('td')[2].text} #{tr[tr.size-1].css('td')[3].text}"
        return Time.parse(stime), "#{state}[#{loc}]"
end