payload-api 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5783328f0a31aea836478841802815c16aaef86b22ff101c698b61603f1437cd
4
- data.tar.gz: 4e606c81caaec76f7410e7e29c55c77261ae28b2c5b69c9191b62eb297a82d3f
3
+ metadata.gz: 74d5e99e6b4b79f89e876de732d0b202c52652091467f975de5d91aedc7c1b2b
4
+ data.tar.gz: beed06286c333c72bb34841414e394e2dd15b971f3dbff5bb5e12141611eac91
5
5
  SHA512:
6
- metadata.gz: 5aa4c8bbc4bb6a15feb4473378289b1f9d7c7ea510cca774a77cce4fcc3cd30a7aece820aaabfa5f24c54dd6fc3f09bf9f146955f5e1b05975947f04d1870fc8
7
- data.tar.gz: 5e7ded315bac9487390cdaa23b18a59770f5f1b3eb1896f99d6a33e270bb3c0906b40e0df22dc58b37c45000564bb78c9def03ff36ecdac909c0667492d83fc7
6
+ metadata.gz: '009d5bd388d06f3ddff528ac3a7ddb46ee211d1094733bbe096ec4044706f1e1757fb2b064532fe40517e53e5f6eef694b18862b154221869d9fff868386371c'
7
+ data.tar.gz: 646a54524964916d4f9f405e01db6496b33247d1f4af22f6604dc050d543bf1850c983cf01a8a88e43bf1ea250365495a1252e981ef58c3a4f3c8891a48ed085
@@ -0,0 +1,25 @@
1
+ name: RSpec Tests
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ rspec:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - name: Checkout code
15
+ uses: actions/checkout@v2
16
+
17
+ - name: Set up Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: 3.0.1
21
+
22
+ - name: Run RSpec tests
23
+ run: |
24
+ gem install rspec
25
+ rspec spec
@@ -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
- if data.key?('id') and @@cache.key?(data['id'])
19
- @@cache[data['id']].set_data(data)
20
- return @@cache[data['id']]
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
- if data.key?('id') and not data['id'].nil? and not data['id'].empty?
24
- @@cache[data['id']] = inst
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 Payload::ARMRequest.new(self).select(*args, **data)
114
+ return self._get_request().select(*args, **data)
54
115
  end
55
116
 
56
117
  def self.filter_by(*args, **data)
57
- return Payload::ARMRequest.new(self).filter_by(*args, **data)
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 Payload::ARMRequest.new(self).create(args[0])
127
+ return self._get_request(session).create(args[0])
63
128
  else
64
- return Payload::ARMRequest.new(self).create(data)
129
+ return self._get_request(session).create(data)
65
130
  end
66
131
  end
67
132
 
68
- def self.get(id)
69
- return Payload::ARMRequest.new(self).get(id)
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 Payload::ARMRequest.new(self).delete(objects)
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 Payload::ARMRequest.new(self.class)._request('Put', id: self.id, json: update)
146
+ return _get_request()._request('Put', id: self.id, json: update)
78
147
  end
79
148
 
80
149
  def delete()
81
- return Payload::ARMRequest.new(self.class)._request('Delete', id: self.id)
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
-
@@ -8,24 +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
- @selected_fields = ''
13
+ @session = session || Payload::Session.new(Payload::api_key, Payload::api_url)
14
14
  @filters = {}
15
15
  end
16
16
 
17
17
  def select(*args, **data)
18
- if @cls.poly
19
- data = data.merge(@cls.poly)
20
- end
21
-
22
- @selected_fields = args.join(',')
23
-
18
+ @filters['fields'] = args.map {|a| a.strip }.join(',')
19
+
24
20
  return self
25
21
  end
26
22
 
27
23
  def filter_by(*args, **data)
28
- if @cls.poly
24
+ if !@cls.nil? && @cls.poly
29
25
  data = data.merge(@cls.poly)
30
26
  end
31
27
 
@@ -51,9 +47,49 @@ module Payload
51
47
  ._request('Put', json: updates)
52
48
  end
53
49
 
54
- def delete(objects)
55
- deletes = objects.map {|o| o.id }.join('|')
56
- return self.filter_by(mode: 'query', id: deletes)
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')
57
93
  ._request('Delete')
58
94
  end
59
95
 
@@ -86,26 +122,30 @@ module Payload
86
122
  return self._request('Post', json: data)
87
123
  end
88
124
 
89
- def _request(method, id: nil, json: nil)
90
-
91
- # Update @filters['fields'] with @selected_fields
92
- fields_list = []
93
- fields_list << @filters['fields'] if @filters['fields'] && !@filters['fields'].empty?
94
- fields_list << @selected_fields if @selected_fields && !@selected_fields.empty?
95
- @filters['fields'] = fields_list.join(',') if fields_list.any?
96
- @filters['fields'] ||= '*'
125
+ def _execute_request(http, request)
126
+ http.request(request)
127
+ end
97
128
 
98
- if @cls.spec.key?("endpoint")
99
- endpoint = @cls.spec["endpoint"]
129
+ def _request(method, id: nil, json: nil)
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
100
136
  else
101
- endpoint = "/"+@cls.spec["object"]+"s"
137
+ if json.is_a? Array
138
+ if json.all? {|obj| obj.key?("object") }
139
+ endpoint = json[0]["object"]+"s"
140
+ end
141
+ end
102
142
  end
103
143
 
104
144
  if id
105
145
  endpoint = File.join(endpoint, id)
106
146
  end
107
147
 
108
- url = URI.join(Payload::api_url, endpoint)
148
+ url = URI.join(@session.api_url, endpoint)
109
149
  url.query = URI.encode_www_form(@filters)
110
150
 
111
151
  http = Net::HTTP.new(url.host, url.port)
@@ -115,14 +155,14 @@ module Payload
115
155
  end
116
156
 
117
157
  request = Net::HTTP.const_get(method).new(url.request_uri)
118
- request.basic_auth(Payload::api_key, '')
158
+ request.basic_auth(@session.api_key, '')
119
159
 
120
160
  if json
121
161
  request.body = json.to_json
122
162
  request.add_field('Content-Type', 'application/json')
123
163
  end
124
164
 
125
- response = http.request(request)
165
+ response = self._execute_request(http, request)
126
166
 
127
167
  begin
128
168
  data = JSON.parse(response.body)
@@ -141,11 +181,12 @@ module Payload
141
181
  if cls.nil?
142
182
  obj
143
183
  else
144
- cls.new(obj)
184
+ ret = cls.new(obj, @session)
185
+ ret
145
186
  end
146
187
  end
147
188
  else
148
- return Payload::get_cls(data).new(data)
189
+ return Payload::get_cls(data).new(data, @session)
149
190
  end
150
191
  else
151
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
@@ -67,7 +67,7 @@ module Payload
67
67
 
68
68
  class ChargeItem < ARMObject
69
69
  @spec = { 'object' => 'line_item' }
70
- @poly = { 'type' => 'charge' }
70
+ @poly = { 'entry_type' => 'charge' }
71
71
  end
72
72
 
73
73
  class PaymentItem < ARMObject
@@ -82,4 +82,8 @@ module Payload
82
82
  class Webhook < ARMObject
83
83
  @spec = { 'object' => 'webhook' }
84
84
  end
85
+
86
+ class PaymentLink < ARMObject
87
+ @spec = { 'object' => 'payment_link' }
88
+ end
85
89
  end
@@ -1,3 +1,3 @@
1
1
  module Payload
2
- VERSION = '0.2.5'
2
+ VERSION = '0.3.0'
3
3
  end
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
- attr_accessor :api_key, :api_url
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