payload-api 0.2.6 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby-app.yml +1 -1
- data/lib/payload/arm/object.rb +96 -19
- data/lib/payload/arm/request.rb +68 -14
- data/lib/payload/arm/session.rb +53 -0
- data/lib/payload/objects.rb +10 -1
- data/lib/payload/version.rb +1 -1
- data/lib/payload.rb +34 -3
- data/spec/payload/arm/request_spec.rb +726 -9
- data/spec/payload/arm/session_spec.rb +144 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bab3aa7202ecf90ba0e88cfcdaf91eda738ee593a1ea2d38a390a29fd87b9813
|
4
|
+
data.tar.gz: 4380c8d7ce309fc97aed9a2f07d1fa0d1516b3b9823de647df831483ded14f54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb8e9e90bb630c7dbccd23161e0dac207ff53068e6f95b8438a03dc9b3176a423b2da3e85b9b188f77a023ead8b583723528e50c83c35c143f08e8925580ac4f
|
7
|
+
data.tar.gz: b6d2f1d4a6923fc64ea7b08b8d89247fd9905ee212e90a93e80e6984d20ea20c93c88c573ff4f6ee31450d0af0460c2a4678c16bc809568b27076e7a71ef29fa
|
data/lib/payload/arm/object.rb
CHANGED
@@ -1,39 +1,92 @@
|
|
1
1
|
require "payload/arm/request"
|
2
2
|
|
3
3
|
module Payload
|
4
|
+
class ARMObjectWrapper
|
5
|
+
@cls
|
6
|
+
@session
|
7
|
+
|
8
|
+
def initialize(cls, session)
|
9
|
+
@cls = cls
|
10
|
+
@session = session
|
11
|
+
end
|
12
|
+
|
13
|
+
def new(data)
|
14
|
+
return @cls.new(data)
|
15
|
+
end
|
16
|
+
|
17
|
+
def all()
|
18
|
+
return @cls.all(:session => @session)
|
19
|
+
end
|
20
|
+
|
21
|
+
def filter_by(*args, **data)
|
22
|
+
return @cls.filter_by(*args, **data, :session => @session)
|
23
|
+
end
|
24
|
+
|
25
|
+
def create(*args, **data)
|
26
|
+
return @cls.create(*args, **data, :session => @session)
|
27
|
+
end
|
28
|
+
|
29
|
+
def get(id)
|
30
|
+
return @cls.get(id, :session => @session)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
4
35
|
class ARMObject
|
5
36
|
@poly = nil
|
6
37
|
@data = nil
|
7
38
|
@@cache = {}
|
39
|
+
@session = nil
|
8
40
|
|
9
41
|
class << self
|
10
|
-
attr_reader :spec, :poly, :data
|
42
|
+
attr_reader :spec, :poly, :data, :session
|
11
43
|
end
|
12
44
|
|
13
|
-
def initialize(data)
|
45
|
+
def initialize(data, session = nil)
|
14
46
|
self.set_data(data)
|
15
47
|
end
|
16
48
|
|
17
|
-
def self.new(data)
|
18
|
-
|
19
|
-
|
20
|
-
|
49
|
+
def self.new(data, session = nil)
|
50
|
+
|
51
|
+
session = session || Payload::Session.new(Payload::api_key, Payload::api_url)
|
52
|
+
session_key = session.object_id
|
53
|
+
|
54
|
+
if !@@cache.key?(session_key)
|
55
|
+
@@cache[session_key] = {}
|
56
|
+
end
|
57
|
+
|
58
|
+
session_cache = @@cache[session_key]
|
59
|
+
|
60
|
+
id = data.key?(:id) ? data[:id] : data.key?('id') ? data['id'] : nil
|
61
|
+
if id && session_cache.key?(id)
|
62
|
+
session_cache[id].set_data(data)
|
63
|
+
return session_cache[id]
|
21
64
|
else
|
22
65
|
inst = super
|
23
|
-
|
24
|
-
|
66
|
+
inst.set_session(session)
|
67
|
+
inst.set_data(data)
|
68
|
+
if id
|
69
|
+
session_cache[id] = inst
|
25
70
|
end
|
26
71
|
|
27
72
|
return inst
|
28
73
|
end
|
29
74
|
end
|
30
75
|
|
76
|
+
def session
|
77
|
+
@session
|
78
|
+
end
|
79
|
+
|
80
|
+
def set_session(session)
|
81
|
+
@session = session
|
82
|
+
end
|
83
|
+
|
31
84
|
def data
|
32
85
|
@data
|
33
86
|
end
|
34
87
|
|
35
88
|
def set_data(data)
|
36
|
-
@data = data
|
89
|
+
@data = data.transform_keys { |key| key.to_s }
|
37
90
|
end
|
38
91
|
|
39
92
|
def method_missing(name, *args)
|
@@ -49,37 +102,61 @@ module Payload
|
|
49
102
|
return @data[key]
|
50
103
|
end
|
51
104
|
|
105
|
+
def _get_request()
|
106
|
+
return Payload::ARMRequest.new(self.class, @session)
|
107
|
+
end
|
108
|
+
|
109
|
+
def self._get_request(session=nil)
|
110
|
+
return Payload::ARMRequest.new(self, session)
|
111
|
+
end
|
112
|
+
|
52
113
|
def self.select(*args, **data)
|
53
|
-
return
|
114
|
+
return self._get_request().select(*args, **data)
|
54
115
|
end
|
55
116
|
|
56
117
|
def self.filter_by(*args, **data)
|
57
|
-
|
118
|
+
session = data[:session]
|
119
|
+
data.delete(:session)
|
120
|
+
return self._get_request(session).filter_by(*args, **data)
|
58
121
|
end
|
59
122
|
|
60
123
|
def self.create(*args, **data)
|
124
|
+
session = data[:session]
|
125
|
+
data.delete(:session)
|
61
126
|
if args.length != 0
|
62
|
-
return
|
127
|
+
return self._get_request(session).create(args[0])
|
63
128
|
else
|
64
|
-
return
|
129
|
+
return self._get_request(session).create(data)
|
65
130
|
end
|
66
131
|
end
|
67
132
|
|
68
|
-
def self.get(id)
|
69
|
-
return
|
133
|
+
def self.get(id, session: nil)
|
134
|
+
return self._get_request(session).get(id)
|
70
135
|
end
|
71
136
|
|
72
137
|
def self.delete(objects)
|
73
|
-
return
|
138
|
+
return self._get_request().delete_all(objects)
|
139
|
+
end
|
140
|
+
|
141
|
+
def self.all(session: nil)
|
142
|
+
return self._get_request(session).all()
|
74
143
|
end
|
75
144
|
|
76
145
|
def update(**update)
|
77
|
-
return
|
146
|
+
return _get_request()._request('Put', id: self.id, json: update)
|
78
147
|
end
|
79
148
|
|
80
149
|
def delete()
|
81
|
-
return
|
150
|
+
return _get_request()._request('Delete', id: self.id)
|
151
|
+
end
|
152
|
+
|
153
|
+
def to_json(*args)
|
154
|
+
serialized = {}
|
155
|
+
if self.class.poly
|
156
|
+
serialized.merge!(self.class.poly)
|
157
|
+
end
|
158
|
+
serialized.merge!(@data)
|
159
|
+
return serialized.to_json(*args)
|
82
160
|
end
|
83
161
|
end
|
84
162
|
end
|
85
|
-
|
data/lib/payload/arm/request.rb
CHANGED
@@ -8,19 +8,20 @@ module Payload
|
|
8
8
|
class ARMRequest
|
9
9
|
@cls = nil
|
10
10
|
|
11
|
-
def initialize(cls=nil)
|
11
|
+
def initialize(cls=nil, session=nil)
|
12
12
|
@cls = cls
|
13
|
+
@session = session || Payload::Session.new(Payload::api_key, Payload::api_url)
|
13
14
|
@filters = {}
|
14
15
|
end
|
15
16
|
|
16
17
|
def select(*args, **data)
|
17
18
|
@filters['fields'] = args.map {|a| a.strip }.join(',')
|
18
|
-
|
19
|
+
|
19
20
|
return self
|
20
21
|
end
|
21
22
|
|
22
23
|
def filter_by(*args, **data)
|
23
|
-
if @cls.poly
|
24
|
+
if !@cls.nil? && @cls.poly
|
24
25
|
data = data.merge(@cls.poly)
|
25
26
|
end
|
26
27
|
|
@@ -46,9 +47,49 @@ module Payload
|
|
46
47
|
._request('Put', json: updates)
|
47
48
|
end
|
48
49
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
50
|
+
def update_all(updates)
|
51
|
+
updates.map do |obj, update|
|
52
|
+
if obj.kind_of?(ARMObject)
|
53
|
+
if @cls and not obj.instance_of?(@cls)
|
54
|
+
throw "All objects must be of the same type"
|
55
|
+
end
|
56
|
+
|
57
|
+
@cls = obj.class
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
updates = {
|
62
|
+
object: 'list',
|
63
|
+
values: updates.map do |obj, update|
|
64
|
+
update['id'] = obj.id
|
65
|
+
update
|
66
|
+
end
|
67
|
+
}
|
68
|
+
|
69
|
+
return self._request('Put', json: updates)
|
70
|
+
end
|
71
|
+
|
72
|
+
def delete_all(objects)
|
73
|
+
deletes = objects.map do |obj|
|
74
|
+
if obj.kind_of?(ARMObject)
|
75
|
+
if @cls and not obj.instance_of?(@cls)
|
76
|
+
throw "All objects must be of the same type"
|
77
|
+
end
|
78
|
+
|
79
|
+
@cls = obj.class
|
80
|
+
obj = { id: obj.id }
|
81
|
+
end
|
82
|
+
|
83
|
+
obj
|
84
|
+
end
|
85
|
+
|
86
|
+
data = { object: 'list', values: deletes }
|
87
|
+
|
88
|
+
return self._request('Delete', json: data)
|
89
|
+
end
|
90
|
+
|
91
|
+
def delete()
|
92
|
+
return self.filter_by(mode: 'query')
|
52
93
|
._request('Delete')
|
53
94
|
end
|
54
95
|
|
@@ -81,18 +122,30 @@ module Payload
|
|
81
122
|
return self._request('Post', json: data)
|
82
123
|
end
|
83
124
|
|
125
|
+
def _execute_request(http, request)
|
126
|
+
http.request(request)
|
127
|
+
end
|
128
|
+
|
84
129
|
def _request(method, id: nil, json: nil)
|
85
|
-
if
|
86
|
-
|
130
|
+
if !@cls.nil?
|
131
|
+
if @cls.spec.key?("endpoint")
|
132
|
+
endpoint = @cls.spec["endpoint"]
|
133
|
+
else
|
134
|
+
endpoint = "/"+@cls.spec["object"]+"s"
|
135
|
+
end
|
87
136
|
else
|
88
|
-
|
137
|
+
if json.is_a? Array
|
138
|
+
if json.all? {|obj| obj.key?("object") }
|
139
|
+
endpoint = json[0]["object"]+"s"
|
140
|
+
end
|
141
|
+
end
|
89
142
|
end
|
90
143
|
|
91
144
|
if id
|
92
145
|
endpoint = File.join(endpoint, id)
|
93
146
|
end
|
94
147
|
|
95
|
-
url = URI.join(
|
148
|
+
url = URI.join(@session.api_url, endpoint)
|
96
149
|
url.query = URI.encode_www_form(@filters)
|
97
150
|
|
98
151
|
http = Net::HTTP.new(url.host, url.port)
|
@@ -102,14 +155,14 @@ module Payload
|
|
102
155
|
end
|
103
156
|
|
104
157
|
request = Net::HTTP.const_get(method).new(url.request_uri)
|
105
|
-
request.basic_auth(
|
158
|
+
request.basic_auth(@session.api_key, '')
|
106
159
|
|
107
160
|
if json
|
108
161
|
request.body = json.to_json
|
109
162
|
request.add_field('Content-Type', 'application/json')
|
110
163
|
end
|
111
164
|
|
112
|
-
response =
|
165
|
+
response = self._execute_request(http, request)
|
113
166
|
|
114
167
|
begin
|
115
168
|
data = JSON.parse(response.body)
|
@@ -128,11 +181,12 @@ module Payload
|
|
128
181
|
if cls.nil?
|
129
182
|
obj
|
130
183
|
else
|
131
|
-
cls.new(obj)
|
184
|
+
ret = cls.new(obj, @session)
|
185
|
+
ret
|
132
186
|
end
|
133
187
|
end
|
134
188
|
else
|
135
|
-
return Payload::get_cls(data).new(data)
|
189
|
+
return Payload::get_cls(data).new(data, @session)
|
136
190
|
end
|
137
191
|
else
|
138
192
|
for error in Payload::subclasses(Payload::PayloadError)
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'payload/arm/request'
|
2
|
+
|
3
|
+
|
4
|
+
module Payload
|
5
|
+
|
6
|
+
class Session
|
7
|
+
attr_accessor :api_key, :api_url
|
8
|
+
|
9
|
+
def initialize(api_key, api_url=nil)
|
10
|
+
@api_key = api_key
|
11
|
+
@api_url = api_url || Payload.URL
|
12
|
+
|
13
|
+
Payload.constants.each do |c|
|
14
|
+
val = Payload.const_get(c)
|
15
|
+
if val.is_a?(Class) && val < Payload::ARMObject
|
16
|
+
define_singleton_method(c) { Payload::ARMObjectWrapper.new(val, self) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def _get_request(cls = nil)
|
22
|
+
return Payload::ARMRequest.new(cls, self)
|
23
|
+
end
|
24
|
+
|
25
|
+
def query(cls)
|
26
|
+
return self._get_request(cls)
|
27
|
+
end
|
28
|
+
|
29
|
+
def create(objects)
|
30
|
+
return self._get_request().create(objects)
|
31
|
+
end
|
32
|
+
|
33
|
+
def update(objects)
|
34
|
+
return self._get_request().update_all(objects)
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete(objects)
|
38
|
+
return self._get_request().delete_all(objects)
|
39
|
+
end
|
40
|
+
|
41
|
+
def ==(other)
|
42
|
+
return false unless other.is_a?(Session)
|
43
|
+
|
44
|
+
# Compare the attributes for equality
|
45
|
+
api_key == other.api_key &&
|
46
|
+
api_url == other.api_url
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_s
|
50
|
+
"#{api_key} @ #{api_url}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/payload/objects.rb
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
require "payload/arm/object"
|
2
2
|
|
3
3
|
module Payload
|
4
|
+
class AccessToken < ARMObject
|
5
|
+
@spec = { 'object' => 'account' }
|
6
|
+
end
|
7
|
+
|
8
|
+
class ClientToken < ARMObject
|
9
|
+
@spec = { 'object' => 'access_token' }
|
10
|
+
@poly = { 'type' => 'client' }
|
11
|
+
end
|
12
|
+
|
4
13
|
class Account < ARMObject
|
5
14
|
@spec = { 'object' => 'account' }
|
6
15
|
end
|
@@ -67,7 +76,7 @@ module Payload
|
|
67
76
|
|
68
77
|
class ChargeItem < ARMObject
|
69
78
|
@spec = { 'object' => 'line_item' }
|
70
|
-
@poly = { '
|
79
|
+
@poly = { 'entry_type' => 'charge' }
|
71
80
|
end
|
72
81
|
|
73
82
|
class PaymentItem < ARMObject
|
data/lib/payload/version.rb
CHANGED
data/lib/payload.rb
CHANGED
@@ -1,16 +1,47 @@
|
|
1
1
|
require "payload/version"
|
2
2
|
require "payload/objects"
|
3
|
+
require "payload/arm/session"
|
3
4
|
|
4
5
|
module Payload
|
5
6
|
@URL = "https://api.payload.co"
|
6
|
-
@api_url = @URL
|
7
|
-
@api_key = nil
|
8
7
|
|
9
8
|
class << self
|
10
|
-
|
9
|
+
def api_key=(value)
|
10
|
+
session.api_key = value
|
11
|
+
end
|
12
|
+
|
13
|
+
def api_key
|
14
|
+
session.api_key
|
15
|
+
end
|
16
|
+
|
17
|
+
def api_url=(value)
|
18
|
+
session.api_url = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def api_url
|
22
|
+
session.api_url
|
23
|
+
end
|
24
|
+
|
25
|
+
def URL
|
26
|
+
@URL
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def session
|
32
|
+
@session ||= Payload::Session.new(nil, @URL)
|
33
|
+
end
|
11
34
|
end
|
12
35
|
|
13
36
|
def self.create(objects)
|
14
37
|
return Payload::ARMRequest.new().create(objects)
|
15
38
|
end
|
39
|
+
|
40
|
+
def self.update(objects)
|
41
|
+
return Payload::ARMRequest.new().update_all(objects)
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.delete(objects)
|
45
|
+
return Payload::ARMRequest.new().delete_all(objects)
|
46
|
+
end
|
16
47
|
end
|