Sha256: 5fe36bcd07a7579b224d5091bd22b6a6600b6755b9aee986760b12c6264d977c

Contents?: true

Size: 1002 Bytes

Versions: 1

Compression:

Stored size: 1002 Bytes

Contents

require 'faraday'
require 'faraday/cookie_jar'
require 'nokogiri'

module TakuhaiStatus
	class TMGCargo
		attr_reader :key, :time, :state
		TakuhaiStatus.add_service(self)
		@@conn = nil

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

		def finish?
			return !!(@state =~ /配達完了|返品/)
		end

	private
		def check
			@@conn = Faraday.new(url: 'https://track-a.tmg-tms.com'){|builder|
				builder.use :cookie_jar
				builder.request :url_encoded
				builder.adapter :net_http
			} unless @@conn
			@@conn.get('/cts/TmgCargoSearchAction.do')
			res = @@conn.post('/cts/TmgCargoSearchAction.do', {
				'inputData[0].inq_no' => @key,
				'method_id' => 'POPUPSEA'
			})
			doc = Nokogiri(res.body)
			begin
				state = doc.css('#list tr td')[2].text.strip
				raise if state =~ /お荷物情報が見つかりません/
				return Time.now, state
			rescue
				raise NotMyKey
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
takuhai_status-1.8.5 lib/takuhai_status/tmg_cargo.rb