Sha256: fdfef8e1cfad06f851929690ce3b1b7587e0ab3c6e5d21f925f3e4fd03ff6a5f

Contents?: true

Size: 870 Bytes

Versions: 12

Compression:

Stored size: 870 Bytes

Contents

require 'faraday'
require 'nokogiri'

module TakuhaiStatus
	class USPS
		attr_reader :key, :time, :state
		TakuhaiStatus.add_service(self)

		def initialize(key)
			@key = key.strip
			raise NotMyKey.new('invalid key format') unless @key =~ /\A[a-zA-Z0-9]+\Z/
			@time, @state = check
		end

		def finish?
			return !!(@state =~ /^Delivered/)
		end

	private
		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
	end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
takuhai_status-1.9.2 lib/takuhai_status/usps.rb
takuhai_status-1.9.1 lib/takuhai_status/usps.rb
takuhai_status-1.9.0 lib/takuhai_status/usps.rb
takuhai_status-1.8.10 lib/takuhai_status/usps.rb
takuhai_status-1.8.9 lib/takuhai_status/usps.rb
takuhai_status-1.8.8 lib/takuhai_status/usps.rb
takuhai_status-1.8.7 lib/takuhai_status/usps.rb
takuhai_status-1.8.6 lib/takuhai_status/usps.rb
takuhai_status-1.8.5 lib/takuhai_status/usps.rb
takuhai_status-1.8.4 lib/takuhai_status/usps.rb
takuhai_status-1.8.3 lib/takuhai_status/usps.rb
takuhai_status-1.8.2 lib/takuhai_status/usps.rb