Last active
August 29, 2015 13:59
-
-
Save Igneous/10807892 to your computer and use it in GitHub Desktop.
start of a simple boxoh tracker module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'open-uri' | |
class MyPackage | |
# MyPackage.new :boxoh_api_key => "something", | |
def initialize(p={}) | |
@api_key = p[:boxoh_api_key] | |
end | |
def get_pkg(tracking_number) | |
pkg_data = open("http://api.boxoh.com/v2/rest/key/#{@api_key}/track/#{tracking_number}") | |
JSON.parse(pkg_data.read)['data'] | |
end; private :get_pkg | |
def groom(pkg_data) | |
if pkg_data['isValid'] | |
pkg = pkg_data['tracking'].first | |
else | |
pkg = Hash.new | |
pkg['time'] = "\u221E" | |
pkg['desc'] = "Package awaiting shipment." | |
pkg['loc'] = { 'city' => 'Somewhere', | |
'territory' => 'Someplace' } | |
end | |
return pkg | |
end; private :groom | |
# MyPackage.track "9405510200829153788476" | |
def track(tracking_number) | |
pkg = get_pkg(tracking_number) | |
return groom(pkg) | |
end | |
end | |
if __FILE__ == $0 | |
config = File.open("#{Dir.home}/.config/tracked-packages.json","r") | |
packages = JSON.parse(config.read)['packages'] | |
config.close | |
boxoh = MyPackage.new :boxoh_api_key => 'SNIP' | |
packages.each do |package| | |
pkg = boxoh.track package['num'] | |
puts "#{package['num']} (#{package['desc']}):" | |
puts " (#{pkg['time']}) #{pkg['desc']} @ #{pkg['loc']['city']}, #{pkg['loc']['territory']}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment