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 +4 -4
- data/.github/workflows/ruby-app.yml +25 -0
- data/lib/payload/arm/object.rb +96 -19
- data/lib/payload/arm/request.rb +69 -28
- data/lib/payload/arm/session.rb +53 -0
- data/lib/payload/objects.rb +5 -1
- data/lib/payload/version.rb +1 -1
- data/lib/payload.rb +34 -3
- data/spec/payload/arm/request_spec.rb +762 -0
- data/spec/payload/arm/session_spec.rb +144 -0
- metadata +6 -2
@@ -0,0 +1,144 @@
|
|
1
|
+
require "payload"
|
2
|
+
require "payload/arm/object"
|
3
|
+
|
4
|
+
|
5
|
+
RSpec.describe Payload::Session do
|
6
|
+
|
7
|
+
describe "#initialize" do
|
8
|
+
context "when the user initializes a session with only an API key" do
|
9
|
+
let(:instance1) { described_class.new('test_key') }
|
10
|
+
|
11
|
+
it "sets the api key and uses default url" do
|
12
|
+
expect(instance1.api_key).to eq('test_key')
|
13
|
+
expect(instance1.api_url).to eq('https://api.payload.co')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when the user initializes a session with an API key and a URL" do
|
18
|
+
let(:instance2) { described_class.new('test_key', 'https://api.hello.co') }
|
19
|
+
|
20
|
+
it "sets the api key and url" do
|
21
|
+
expect(instance2.api_key).to eq('test_key')
|
22
|
+
expect(instance2.api_url).to eq('https://api.hello.co')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#query" do
|
28
|
+
|
29
|
+
context "when the user queries an ARMObject with a session" do
|
30
|
+
it "builds the appropriate ARMRequest" do
|
31
|
+
|
32
|
+
$test_id = 'acct_' + rand(9000000...9999999).to_s
|
33
|
+
|
34
|
+
Payload::api_key = 'test_key'
|
35
|
+
instance = Payload::Session.new('session_key', 'https://sandbox.payload.co')
|
36
|
+
|
37
|
+
arm_request = instance.query(Payload::Customer)
|
38
|
+
|
39
|
+
expect(arm_request.instance_variable_get(:@cls)).to eq(Payload::Customer)
|
40
|
+
expect(arm_request.instance_variable_get(:@session)).to eq(instance)
|
41
|
+
|
42
|
+
expect(arm_request).to receive(:_execute_request) do |http, request|
|
43
|
+
expect(request.method).to eq("GET")
|
44
|
+
expect(http.address).to eq("sandbox.payload.co")
|
45
|
+
expect(Base64.decode64(request['authorization'].split(' ')[1]).split(':')[0]).to eq('session_key')
|
46
|
+
expect(request.path).to eq("/customers?fields=name%2Cage")
|
47
|
+
|
48
|
+
class MockResponse
|
49
|
+
def initialize
|
50
|
+
end
|
51
|
+
|
52
|
+
def code
|
53
|
+
'200'
|
54
|
+
end
|
55
|
+
|
56
|
+
def body
|
57
|
+
'{
|
58
|
+
"object": "list",
|
59
|
+
"values": [
|
60
|
+
{"id": "' + $test_id + '", "object": "customer", "name": "John Doe", "age": 42}
|
61
|
+
]
|
62
|
+
}'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
MockResponse.new
|
67
|
+
end
|
68
|
+
|
69
|
+
expect(Payload::Customer.class_variable_get(:@@cache).key?(instance.object_id)).to eq(false)
|
70
|
+
|
71
|
+
custs = arm_request.select('name', 'age').all()
|
72
|
+
|
73
|
+
expect(custs).to be_a(Array)
|
74
|
+
expect(custs.size).to eq(1)
|
75
|
+
expect(custs[0]).to be_a(Payload::Customer)
|
76
|
+
expect(custs[0].object).to eq('customer')
|
77
|
+
expect(custs[0].session).to eq(instance)
|
78
|
+
|
79
|
+
expect(Payload::Customer.class_variable_get(:@@cache)[instance.object_id][$test_id]['name']).to eq('John Doe')
|
80
|
+
expect(Payload::Customer.class_variable_get(:@@cache)[instance.object_id][$test_id]['age']).to eq(42)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#create" do
|
86
|
+
|
87
|
+
context "when the user creates an ARMObject with a session" do
|
88
|
+
|
89
|
+
it "builds the appropriate ARMRequest" do
|
90
|
+
Payload::api_key = 'test_key'
|
91
|
+
instance = Payload::Session.new('session_key', 'https://sandbox.payload.co')
|
92
|
+
|
93
|
+
cust = Payload::Customer.new({})
|
94
|
+
|
95
|
+
expect_any_instance_of(Payload::ARMRequest).to receive(:create) do |req, objects|
|
96
|
+
expect(req.instance_variable_get(:@session)).to eq(instance)
|
97
|
+
expect(objects).to eq(cust)
|
98
|
+
end
|
99
|
+
|
100
|
+
instance.create(cust)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "#update" do
|
106
|
+
|
107
|
+
context "when the user updates an ARMObject with a session" do
|
108
|
+
|
109
|
+
it "builds the appropriate ARMRequest" do
|
110
|
+
Payload::api_key = 'test_key'
|
111
|
+
instance = Payload::Session.new('session_key', 'https://sandbox.payload.co')
|
112
|
+
|
113
|
+
cust = Payload::Customer.new({})
|
114
|
+
|
115
|
+
expect_any_instance_of(Payload::ARMRequest).to receive(:update_all) do |req, objects|
|
116
|
+
expect(req.instance_variable_get(:@session)).to eq(instance)
|
117
|
+
expect(objects).to eq(cust)
|
118
|
+
end
|
119
|
+
|
120
|
+
instance.update(cust)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe "#delete" do
|
126
|
+
|
127
|
+
context "when the user deletes an ARMObject with a session" do
|
128
|
+
|
129
|
+
it "builds the appropriate ARMRequest" do
|
130
|
+
Payload::api_key = 'test_key'
|
131
|
+
instance = Payload::Session.new('session_key', 'https://sandbox.payload.co')
|
132
|
+
|
133
|
+
cust = Payload::Customer.new({})
|
134
|
+
|
135
|
+
expect_any_instance_of(Payload::ARMRequest).to receive(:delete_all) do |req, objects|
|
136
|
+
expect(req.instance_variable_get(:@session)).to eq(instance)
|
137
|
+
expect(objects).to eq(cust)
|
138
|
+
end
|
139
|
+
|
140
|
+
instance.delete(cust)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: payload-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Payload
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple library to interface with the Payload API. See https://docs.payload.co
|
14
14
|
for details.
|
@@ -17,16 +17,20 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- ".github/workflows/ruby-app.yml"
|
20
21
|
- LICENSE
|
21
22
|
- README.md
|
22
23
|
- lib/payload.rb
|
23
24
|
- lib/payload/arm/object.rb
|
24
25
|
- lib/payload/arm/request.rb
|
26
|
+
- lib/payload/arm/session.rb
|
25
27
|
- lib/payload/exceptions.rb
|
26
28
|
- lib/payload/objects.rb
|
27
29
|
- lib/payload/utils.rb
|
28
30
|
- lib/payload/version.rb
|
29
31
|
- payload.gemspec
|
32
|
+
- spec/payload/arm/request_spec.rb
|
33
|
+
- spec/payload/arm/session_spec.rb
|
30
34
|
homepage: https://docs.payload.co
|
31
35
|
licenses:
|
32
36
|
- MIT
|