payload-api 0.2.6 → 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: 34f564177aca2508a3ab7864c3c86b2750fbe7b6d678bcf821d4f802da7d59b5
4
- data.tar.gz: 626a0ecbdfff120ca85a8e8bef68af08fdfe7627826baeb6f88bfe10e1818197
3
+ metadata.gz: 74d5e99e6b4b79f89e876de732d0b202c52652091467f975de5d91aedc7c1b2b
4
+ data.tar.gz: beed06286c333c72bb34841414e394e2dd15b971f3dbff5bb5e12141611eac91
5
5
  SHA512:
6
- metadata.gz: bbdfbe194bc9a878426bc74cd6c59107852a557a5a2fc1c9c5353bcca935ba189e64fc5a88d4a13076eb6ab104b6d57f337a7326fa9be58ab1974d595ada2a87
7
- data.tar.gz: 5b9d5a44b34ecd605e94e3c6910b9f2dcef895f84ac29987bfd84234490befe616904322e2c3859ef4e062ccd1d5d11945bb33822dc8e58395576cd408fc2b3e
6
+ metadata.gz: '009d5bd388d06f3ddff528ac3a7ddb46ee211d1094733bbe096ec4044706f1e1757fb2b064532fe40517e53e5f6eef694b18862b154221869d9fff868386371c'
7
+ data.tar.gz: 646a54524964916d4f9f405e01db6496b33247d1f4af22f6604dc050d543bf1850c983cf01a8a88e43bf1ea250365495a1252e981ef58c3a4f3c8891a48ed085
@@ -13,7 +13,7 @@ jobs:
13
13
  steps:
14
14
  - name: Checkout code
15
15
  uses: actions/checkout@v2
16
-
16
+
17
17
  - name: Set up Ruby
18
18
  uses: ruby/setup-ruby@v1
19
19
  with:
@@ -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,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 delete(objects)
50
- deletes = objects.map {|o| o.id }.join('|')
51
- 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')
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 @cls.spec.key?("endpoint")
86
- endpoint = @cls.spec["endpoint"]
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
- 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
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(Payload::api_url, endpoint)
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(Payload::api_key, '')
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 = http.request(request)
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Payload
2
- VERSION = '0.2.6'
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