2018-02-02 19:23:52 +00:00
|
|
|
require 'sinatra'
|
|
|
|
require 'tracking_number'
|
|
|
|
|
2018-02-06 18:23:40 +00:00
|
|
|
set :bind, '0.0.0.0'
|
2018-02-02 19:23:52 +00:00
|
|
|
set :port, 3000
|
|
|
|
|
2018-02-06 18:23:40 +00:00
|
|
|
|
|
|
|
def get_tracking_url(tracker)
|
|
|
|
tracking_urls = {
|
|
|
|
:ups => 'https://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=%s',
|
2018-02-06 18:59:19 +00:00
|
|
|
:dhl => 'http://www.dhl.com/en/express/tracking.html?brand=DHL&AWB=%s',
|
|
|
|
:fedex => 'https://www.fedex.com/apps/fedextrack/?tracknumbers=%s',
|
|
|
|
:ontrac => 'http://www.ontrac.com/trackingres.asp?tracking_number=%s',
|
|
|
|
:usps => 'https://m.usps.com/m/TrackConfirmAction_detail?tLabels=%s',
|
2018-02-06 18:23:40 +00:00
|
|
|
}
|
|
|
|
tracking_url = tracking_urls[tracker.courier_code]
|
|
|
|
if tracking_url != nil
|
|
|
|
tracking_url = tracking_url % tracker.tracking_number
|
|
|
|
end
|
|
|
|
return tracking_url
|
|
|
|
end
|
|
|
|
|
2018-02-02 19:23:52 +00:00
|
|
|
# Simple status endpoint on root
|
|
|
|
get '/' do
|
|
|
|
'OK'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Standard parser api receives PUT {"message": "Email body"} /parse
|
|
|
|
# Returns [{"token": "extracted token", "type": "token type", "metadata": {}]
|
|
|
|
post '/parse' do
|
|
|
|
body = JSON.parse(request.body.read)
|
2018-02-06 18:23:40 +00:00
|
|
|
trackers = TrackingNumber.search(body['message'])
|
2018-02-02 19:23:52 +00:00
|
|
|
results = []
|
|
|
|
for tracker in trackers do
|
|
|
|
results.push({
|
|
|
|
:token => tracker.tracking_number,
|
2018-02-06 18:23:40 +00:00
|
|
|
:type => 'SHIPPING',
|
2018-02-06 01:52:39 +00:00
|
|
|
:metadata => {
|
|
|
|
:carrier_name => tracker.courier_name,
|
2018-02-06 18:23:40 +00:00
|
|
|
:tracking_url => get_tracking_url(tracker),
|
2018-02-06 01:52:39 +00:00
|
|
|
}
|
2018-02-02 19:23:52 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
JSON.dump(results)
|
|
|
|
end
|