-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
185 lines (157 loc) · 4.95 KB
/
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
require 'dotenv/load'
require 'active_support/all'
require 'sinatra'
require "sinatra/reloader" if development?
require "sinatra/json"
require 'httparty'
require 'byebug'
module OpenTransact
class Client
include HTTParty
base_uri ENV["OPENTRANSACT_BASE_URI"]
headers 'Accept' => 'application/vnd.api+json'
headers 'Content-Type' => 'application/vnd.api+json'
def self.application_id
ENV["OPENTRANSACT_APPLICATION_ID"]
end
def self.api_key
ENV["OPENTRANSACT_API_KEY"]
end
headers 'Authorization' => "Bearer #{api_key}"
def self.client_token_role_id
ENV["OPENTRANSACT_CLIENT_TOKEN_ROLE_ID"]
end
def self.valid_configuration?
base_uri && application_id && api_key && client_token_role_id
end
end
class Resource
attr_accessor :resource_id
attr_accessor :attributes
def initialize(json)
@resource_id = json["id"]
@attributes = OpenStruct.new(json["attributes"].deep_transform_keys! { |key| key.underscore })
end
def as_json
attributes.to_h.merge(id: resource_id)
end
end
class Profile < Resource
def self.all
response = Client.get('/v1/profiles', query: { filter: {'application-id': Client.application_id } } )
data = response.parsed_response["data"]
data.map do |item|
new(item)
end
end
def self.get(id)
response = Client.get("/v1/profiles/#{id}")
data = response.parsed_response["data"]
new(data)
end
def self.create(params)
body = {
data: {
type: "profiles",
attributes: {
owner: {
type: params[:owner_type],
'first-name': params[:first_name],
'last-name': params[:last_name]
}
},
relationships: {
application: {
data: {
type: "applications",
id: Client.application_id
}
}
}
}
}
response = Client.post("/v1/profiles", body: body.to_json)
data = response.parsed_response["data"]
new(data)
end
def name
[attributes.owner["first_name"], attributes.owner["last_name"]].join(" ")
end
end
class ClientToken < Resource
def self.create(profile_id)
body = {
data: {
type: "client-tokens",
attributes: {
},
relationships: {
roles: {
data: [{
type: "roles",
id: Client.client_token_role_id
}]
},
subject: {
data: {
type: "profiles",
id: profile_id
}
}
}
}
}
response = Client.post("/v1/client-tokens", body: body.to_json)
data = response.parsed_response["data"]
new(data)
end
end
class Transaction < Resource
def self.create(params)
response = Client.post("/v1/transactions", body: {data: params}.to_json)
data = response.parsed_response["data"]
new(data)
end
end
end
set :port, ENV["OPENTRANSACT_DEMO_PORT"] || 3000
helpers do
def api_endpoint
OpenTransact::Client.base_uri
end
def application_id
OpenTransact::Client.application_id
end
end
before do
redirect '/config' if request.path_info != "/config" && !OpenTransact::Client.valid_configuration?
end
get '/' do
@profiles = OpenTransact::Profile.all
erb :index
end
get '/config' do
if OpenTransact::Client.valid_configuration?
redirect '/'
else
erb :config
end
end
get '/profiles/:id' do
@profiles = OpenTransact::Profile.all
@profile = OpenTransact::Profile.get(params[:id])
erb :index
end
post '/profiles' do
@profile = OpenTransact::Profile.create(params)
redirect "/profiles/#{@profile.resource_id}"
end
post '/profiles/:profile_id/client-token' do
@client_token = OpenTransact::ClientToken.create(params[:profile_id])
json @client_token.as_json
end
post '/transactions' do
json = JSON.parse(request.body.read)
@transaction = OpenTransact::Transaction.create(json)
json @transaction.as_json
end