lib/takuhai_status/fedex.rb in takuhai_status-1.5.2 vs lib/takuhai_status/fedex.rb in takuhai_status-1.5.3
- old
+ new
@@ -4,16 +4,16 @@
module TakuhaiStatus
class FedEx
attr_reader :key, :time, :state
def initialize(key)
- @key = key.gsub(/[^0-9]/, '')
+ @key = key
@time, @state = check
end
def finish?
- return !!(@state =~ /認可済み業者に委託/)
+ return !!(@state =~ /認可済み業者に委託|配達完了/)
end
private
def check
data = {
@@ -41,31 +41,25 @@
action: 'trackpackages',
locale: 'ja_JP',
version: '1',
format: 'json'
})
- begin
- data = JSON.parse(res.body)
- raise unless data["TrackPackagesResponse"]["successful"]
- package = data["TrackPackagesResponse"]["packageList"].first
- current = package["scanEventList"].first
+ package = JSON.parse(res.body)["TrackPackagesResponse"]
+ unless package["successful"]
+ raise NotMyKey.new(package["errorList"].first["message"])
+ end
- begin
- time = Time.parse("#{current['date']} #{current['time']}#{current['+09:00']}")
- rescue ArgumentError
- raise NotMyKey.new('no time status in the package');
- end
+ current = package["packageList"].first["scanEventList"].first
- state = "#{current['status']}"
- state = "#{state}(#{current['scanDetails']})" if current['scanDetails'].size > 0
- state = "#{state} - #{current['scanLocation']}" if current['scanLocation'].size > 0
+ t_str = "#{current['date']} #{current['time']}"
+ raise NotMyKey.new('no time status in the package') if t_str.size == 1
+ time = Time.parse("#{t_str}+09:00")
- return time, state
- rescue NotMyKey
- raise
- rescue
- return (@time || Time.now), (@state || '')
- end
+ state = "#{current['status']}"
+ state = "#{state}(#{current['scanDetails']})" if current['scanDetails'].size > 0
+ state = "#{state} - #{current['scanLocation']}" if current['scanLocation'].size > 0
+
+ return time, state
end
end
end