Sha256: 701d14d271d991cdab9e7122d55654c40deeabe195373119e113cd1f70df67e7

Contents?: true

Size: 1.27 KB

Versions: 11

Compression:

Stored size: 1.27 KB

Contents

require 'faraday'
require 'nokogiri'

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

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

		def finish?
			return !!(@state =~ /営業所へお問い合わせ下さい。|配達は終了致しました。$|^配達完了|引渡完了/)
		end

	private
		def check
			conn = Faraday.new(url: 'http://k2k.sagawa-exp.co.jp')
			res = conn.post('/p/web/okurijosearch.do', {okurijoNo: @key})
			doc = Nokogiri(res.body)

			begin
				cells = doc.css('.table_okurijo_detail2').css('tr').last.css('td')
				state = "#{cells[0].text.strip} [#{cells[2].text.strip}]".sub(/^./, '')
				time = Time.parse(cells[1].text.strip)
				return time, state
			rescue NoMethodError # detail2 table not found, use detail table
				begin
					time = Time.now
					state = doc.css('.table_okurijo_detail').first.css('tr').last.css('td').text.strip
					if state =~ /恐れ入りますが、お問い合せ送り状NOをお確かめください。|お荷物データが登録されておりません。/
						raise NotMyKey.new('invalid key')
					end
					return time, state
				rescue NoMethodError
					raise NotMyKey.new('invalid response')
				end
			rescue ArgumentError
				return Time.now, ''
			end
		end
	end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
takuhai_status-1.7.4 lib/takuhai_status/sagawa.rb
takuhai_status-1.7.3 lib/takuhai_status/sagawa.rb
takuhai_status-1.7.2 lib/takuhai_status/sagawa.rb
takuhai_status-1.7.1 lib/takuhai_status/sagawa.rb
takuhai_status-1.7.0 lib/takuhai_status/sagawa.rb
takuhai_status-1.6.0 lib/takuhai_status/sagawa.rb
takuhai_status-1.5.5 lib/takuhai_status/sagawa.rb
takuhai_status-1.5.4 lib/takuhai_status/sagawa.rb
takuhai_status-1.5.3 lib/takuhai_status/sagawa.rb
takuhai_status-1.5.2 lib/takuhai_status/sagawa.rb
takuhai_status-1.5.1 lib/takuhai_status/sagawa.rb