class TakuhaiStatus::FedEx

Attributes

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

Public Class Methods

new(key) click to toggle source
# File lib/takuhai_status/fedex.rb, line 9
def initialize(key)
        @key = key
        @time, @state = check
end

Public Instance Methods

finish?() click to toggle source
# File lib/takuhai_status/fedex.rb, line 14
def finish?
        return !!(@state =~ /認可済み業者に委託|配達完了|貨物情報はFedExに送信されました/)
end

Private Instance Methods

check() click to toggle source
# File lib/takuhai_status/fedex.rb, line 19
def check
        data = {
                "TrackPackagesRequest": {
                        "appType": "WTRK",
                        "uniqueKey": "",
                        "processingParameters": {},
                        "trackingInfoList": [
                                {
                                        "trackNumberInfo": {
                                                "trackingNumber": @key,
                                                "trackingQualifier": "",
                                                "trackingCarrier": ""
                                        }
                                }
                        ]
                }
        }
        conn = Faraday.new(url: 'https://www.fedex.com'){|builder|
                builder.request :url_encoded
                builder.adapter :net_http
        }
        res = conn.post('/trackingCal/track', {
                data: data.to_json,
                action: 'trackpackages',
                locale: 'ja_JP',
                version: '1',
                format: 'json'
        })

        package = JSON.parse(res.body)["TrackPackagesResponse"]
        unless package["successful"]
                raise NotMyKey.new(package["errorList"].first["message"])
        end

        current = package["packageList"].first["scanEventList"].first

        t_str = "#{current['date']} #{current['time']}"
        raise NotMyKey.new('no time status in the package') if t_str.size == 1
        time = Time.parse("#{t_str}+09:00")

        state = "#{current['status']}"
        state = "#{state}(#{current['scanDetails']})" if current['scanDetails'].size > 0
        state = "#{state} - #{current['scanLocation']}" if current['scanLocation'].size > 0

        return time, state
end