Sha256: c68a08d12d9d11e041853df2a940a7f54a9e12a760f24a5e5bed08a1ab259232

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require 'open-uri'
require 'nokogiri'

module TakuhaiStatus
	class JapanPost
		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 =~ /差出人に返送済み|お届け済み|コンビニエンスストアに引渡|窓口でお渡し|転送/)
		end

	private
		def check
			uri = "https://trackings.post.japanpost.jp/services/srv/search/direct?reqCodeNo1=#{@key}"
			doc = Nokogiri(open(uri, &:read))

			begin
				begin
					# japanese baggage
					cols = doc.css('.tableType01')[1].css('tr')
					col = cols[cols.size - 2]
					stime = col.css('td')[0].text
					time = Time.parse(stime)
					station = " [#{col.css('td')[3].text}]"
					station = " [#{col.css('td')[4].text.strip}]" if station.size <= 4
					station = "" if station.size <= 4
					state = "#{col.css('td')[1].text}#{station}"
				rescue NoMethodError
					# international baggage
					cols = doc.css('.tableType01 tr')
					col = cols[cols.size - 2]
					stime = col.css('td')[2].text
					time = Time.parse(stime)
					station = " [#{col.css('td')[4].text}/#{col.css('td')[5].text}]"
					state = "#{col.css('td')[3].text}#{station}"
				end

				return time, state
			rescue NoMethodError
				raise NotMyKey
			rescue ArgumentError
				return Time.now, ''
			end
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
takuhai_status-1.8.3 lib/takuhai_status/japanpost.rb
takuhai_status-1.8.2 lib/takuhai_status/japanpost.rb
takuhai_status-1.8.1 lib/takuhai_status/japanpost.rb