forked from GeekOnCoffee/sinatra_integration_example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathship_app.rb
98 lines (79 loc) · 2.28 KB
/
ship_app.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'sinatra'
require 'json'
require 'active_support/core_ext/hash/indifferent_access'
class ShipApp < Sinatra::Base
attr_reader :payload
before do
@payload = JSON.parse(request.body.read).with_indifferent_access
end
post "/get_shipments" do
content_type :json
request_id = payload[:request_id]
shipments = Service.new(payload).shipments_since
{ request_id: request_id, shipments: shipments }.to_json
end
post "/add_shipment" do
content_type :json
request_id = payload[:request_id]
shipment = Service.new(payload).create
{ request_id: request_id, summary: "Shipment #{shipment} was added" }.to_json
end
post "/update_shipment" do
content_type :json
request_id = payload[:request_id]
shipment = Service.new(payload).update
{ request_id: request_id, summary: "Shipment #{shipment} was updated" }.to_json
end
post "/get_picked_up" do
content_type :json
request_id = payload[:request_id]
shipments = Service.new(payload).picked_up
{ request_id: request_id, shipments: shipments }.to_json
end
# Custom webhook
post "/cancel_shipment" do
content_type :json
request_id = payload[:request_id]
shipment = Service.new(payload).cancel
{ request_id: request_id, summary: "Shipment #{shipment} was canceled" }.to_json
end
end
class Service
attr_reader :payload
def initialize(payload = {})
@payload = payload
end
# Search for shipments after a given timestamp, e.g. payload[:created_after]
def shipments_since
[
{
"id" => "12836",
"status" => "shipped",
"tracking" => "12345678"
}
]
end
# Talk to your shipment api, e.g.
# FedEx.get_picked_up payload
def picked_up
[
{ "id" => "12836", "status" => "picked_up", "picked_up_at" => "2014-02-03T17:29:15.219Z" },
{ "id" => "13243", "status" => "picked_up", "picked_up_at" => "2014-02-03T17:03:15.219Z" }
]
end
# Talk to your shipment api, e.g.
# FedEx.create_shipment payload
def create
payload[:shipment][:id]
end
# Talk to your shipment api, e.g.
# FedEx.create_shipment payload
def update
payload[:shipment][:id]
end
# Talk to your shipment api, e.g.
# FedEx.cancel_shipment payload
def cancel
payload[:shipment][:id]
end
end