-
Notifications
You must be signed in to change notification settings - Fork 1
/
aftership_endpoint.rb
59 lines (48 loc) · 1.3 KB
/
aftership_endpoint.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Sinatra
require 'sinatra'
# Hash fix
require 'active_support/core_ext/hash'
# Spree endpoint
require 'endpoint_base'
# Aftership API client
require 'aftership'
# Aftership endpoint helpers
require './lib/aftership_helpers'
require './lib/aftership_service'
require './lib/post_shipment'
require './lib/get_trackings'
# Errors
require './lib/errors/aftership_error'
require './lib/errors/bad_response_error'
require './lib/errors/bad_tracking_number_error'
class AftershipEndpoint < EndpointBase::Sinatra::Base
endpoint_key ENV["ENDPOINT_KEY"]
helpers AftershipHelpers
set :logging, true
post '/add_shipment' do
process_request do
post_shipment
result 200, 'Successfully sent shipment to AfterShip.'
end
end
post '/update_shipment' do
process_request do
post_shipment
result 200, 'Successfully updated shipment to AfterShip.'
end
end
post '/get_trackings' do
process_request do
get_trackings = GetTrackings.new(@payload, @config)
@shipments = get_trackings.get!
@shipments.each do |shipment|
add_object :shipment, shipment.deep_symbolize_keys
end
if (count = @shipments.count) > 0
result 200, "Received #{count} #{"shipment".pluralize count} from AfterShip"
else
result 200
end
end
end
end