Sha256: e8706c10f443642f950685fd6aae4d7abb18d1d250a949431e09b4774babbe4e

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 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 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

7 entries across 7 versions & 1 rubygems

Version Path
takuhai_status-1.5.4 lib/takuhai_status/ups.rb
takuhai_status-1.5.3 lib/takuhai_status/ups.rb
takuhai_status-1.5.2 lib/takuhai_status/ups.rb
takuhai_status-1.5.1 lib/takuhai_status/ups.rb
takuhai_status-1.5.0 lib/takuhai_status/ups.rb
takuhai_status-1.4.2 lib/takuhai_status/ups.rb
takuhai_status-1.4.1 lib/takuhai_status/ups.rb