Sha256: fe8c4d68f0e0cf43aedc4293e7b034a4b4b188de4eecf90f28f9a9e70e52eccf

Contents?: true

Size: 993 Bytes

Versions: 11

Compression:

Stored size: 993 Bytes

Contents

require 'open-uri'
require 'nokogiri'

module TakuhaiStatus
	class JapanPost
		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://trackings.post.japanpost.jp/services/srv/search/direct?reqCodeNo1=#{@key}"
			doc = Nokogiri(open(uri, &:read))

			begin
				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}"

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
takuhai_status-1.4.1 lib/takuhai_status/japanpost.rb
takuhai_status-1.4.0 lib/takuhai_status/japanpost.rb
takuhai_status-1.3.2 lib/takuhai_status/japanpost.rb
takuhai_status-1.3.1 lib/takuhai_status/japanpost.rb
takuhai_status-1.3.0 lib/takuhai_status/japanpost.rb
takuhai_status-1.2.0 lib/takuhai_status/japanpost.rb
takuhai_status-1.1.3 lib/takuhai_status/japanpost.rb
takuhai_status-1.1.2 lib/takuhai_status/japanpost.rb
takuhai_status-1.1.1 lib/takuhai_status/japanpost.rb
takuhai_status-1.1.0 lib/takuhai_status/japanpost.rb
takuhai_status-1.0.0 lib/takuhai_status/japanpost.rb