Sha256: b0a9db957013e046b1fc48f2534ce05d9e4b57e6cc6cebe42d213feb2f38844f

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

require 'open-uri'
require 'nokogiri'

module TakuhaiStatus
	class UPS
		attr_reader :key, :time, :state

		def initialize(key)
			@key = key.gsub(/[^a-zA-Z0-9]/, '')
			@time, @state = check
		end

		def finish?
			return !!(@state =~ /配達済み/)
		end

	private
		def check
			uri = "https://www.ups.com/WebTracking/processInputRequest?loc=ja_JP&Requester=NES&tracknum=#{@key}"
			html = open(uri, &:read)
			# HTML中に謎の大量ヌル文字が含まれていてnokogiriのパースが止まる対策
			html.gsub!(/\u0000/,'')
			doc = Nokogiri::HTML.parse(html, uri, "utf-8")

			begin
				state = doc.css('.newstatus #ttc_tt_spStatus h3')[0].text.strip
				begin
					time = "#{doc.css('.secHead ul li')[0].text.match(/\d{4}\/\d\d\/\d\d \d{1,2}:\d\d/)[0]}+0500)"
				rescue NoMethodError
					time = "#{doc.css('.secHead ul li')[0].text.match(/\d{4}\/\d\d\/\d\d/)[0]} 00:00:00+0500)"
				end
				return Time.parse(time).localtime, state
			rescue NoMethodError
				raise NotMyKey
			rescue ArgumentError
				return Time.now, ''
			end
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
takuhai_status-1.4.0 lib/takuhai_status/ups.rb
takuhai_status-1.3.2 lib/takuhai_status/ups.rb