email-assistant/parsers/package-tracking/main.rb

46 lines
1.4 KiB
Ruby
Raw Normal View History

2018-02-02 19:23:52 +00:00
require 'sinatra'
require 'tracking_number'
set :bind, '0.0.0.0'
2018-02-02 19:23:52 +00:00
set :port, 3000
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',
}
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)
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,
:type => 'SHIPPING',
:metadata => {
:carrier_name => tracker.courier_name,
:tracking_url => get_tracking_url(tracker),
}
2018-02-02 19:23:52 +00:00
})
end
JSON.dump(results)
end