class TakuhaiStatus::USPS

Attributes

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

Public Class Methods

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

Public Instance Methods

finish?() click to toggle source
# File lib/takuhai_status/usps.rb, line 15
def finish?
        return !!(@state =~ /^Delivered/)
end

Private Instance Methods

check() click to toggle source
# File lib/takuhai_status/usps.rb, line 20
def check
        url = "https://tools.usps.com/go/TrackConfirmAction?tLabels=#{@key}"
        html = Faraday.new(url: url).get.body
        date, status, detail = Nokogiri(html).css('div.status_feed p');
        date_str = date ? date.text.strip : ''
        status_str = status ? status.text.gsub(/\u00A0/, ' ').strip : ''
        detail_str = detail ? detail.text.gsub(/\u00A0/, ' ').strip : ''

        unless date_str.empty?
                return Time.parse(date_str), "#{status_str} (#{detail_str})"
        else
                raise NotMyKey
        end
end