privacygate 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.github/ISSUE_REQUEST_TEMPLATE/issue_request_template.md +36 -0
  3. data/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +51 -0
  4. data/.gitignore +51 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE +21 -0
  7. data/README.md +278 -0
  8. data/Rakefile +6 -0
  9. data/examples/charge.rb +38 -0
  10. data/examples/checkout.rb +61 -0
  11. data/examples/event.rb +26 -0
  12. data/examples/webhook.rb +35 -0
  13. data/lib/privacygate/api_errors.rb +157 -0
  14. data/lib/privacygate/api_resources/base/api_object.rb +206 -0
  15. data/lib/privacygate/api_resources/base/api_resource.rb +25 -0
  16. data/lib/privacygate/api_resources/base/create.rb +15 -0
  17. data/lib/privacygate/api_resources/base/delete.rb +16 -0
  18. data/lib/privacygate/api_resources/base/list.rb +25 -0
  19. data/lib/privacygate/api_resources/base/save.rb +18 -0
  20. data/lib/privacygate/api_resources/base/update.rb +15 -0
  21. data/lib/privacygate/api_resources/charge.rb +14 -0
  22. data/lib/privacygate/api_resources/checkout.rb +19 -0
  23. data/lib/privacygate/api_resources/event.rb +13 -0
  24. data/lib/privacygate/api_response.rb +48 -0
  25. data/lib/privacygate/client.rb +120 -0
  26. data/lib/privacygate/util.rb +59 -0
  27. data/lib/privacygate/version.rb +3 -0
  28. data/lib/privacygate/webhooks.rb +52 -0
  29. data/lib/privacygate.rb +42 -0
  30. data/privacygate.gemspec +28 -0
  31. data/spec/api_resources/base/api_object_spec.rb +156 -0
  32. data/spec/api_resources/base/api_resource_spec.rb +32 -0
  33. data/spec/api_resources/charge_spec.rb +19 -0
  34. data/spec/api_resources/checkout_spec.rb +31 -0
  35. data/spec/api_resources/event_spec.rb +12 -0
  36. data/spec/endpont_spec.rb +103 -0
  37. data/spec/error_spec.rb +58 -0
  38. data/spec/response_spec.rb +43 -0
  39. data/spec/spec_helper.rb +15 -0
  40. data/spec/webhook_spec.rb +36 -0
  41. metadata +161 -0
@@ -0,0 +1,59 @@
1
+ module PrivacyGate
2
+ module Util
3
+
4
+ def self.object_classes
5
+ # Class mappings for api responce fetching
6
+ @object_classes ||= {
7
+ # API Resources
8
+ APIResources::Checkout::OBJECT_NAME => APIResources::Checkout,
9
+ APIResources::Charge::OBJECT_NAME => APIResources::Charge,
10
+ APIResources::Event::OBJECT_NAME => APIResources::Event,
11
+ }
12
+ end
13
+
14
+
15
+ def self.convert_to_api_object(data, client = nil, klass = nil)
16
+ # Converts a hash of fields or an array of hashes into a
17
+ # appropriate APIResources of APIObjects form
18
+ case data
19
+ when Array
20
+ data.map {|i| convert_to_api_object(i, client, klass)}
21
+ when Hash
22
+ # If class received in params, create instance
23
+ if klass
24
+ klass.create_from(data, client)
25
+ else
26
+ # Try converting to a known object class.
27
+ # If none available, fall back to generic APIObject
28
+ klass = object_classes.fetch(data[:resource], APIResources::Base::APIObject)
29
+ # Provide client relation only for APIResource objects
30
+ klass != APIResources::Base::APIObject ? klass.create_from(data, client) : klass.create_from(data)
31
+ end
32
+ else
33
+ data
34
+ end
35
+ end
36
+
37
+ def self.symbolize_names(object)
38
+ # Convert object key and values to symbols if its possible
39
+ case object
40
+ when Hash
41
+ new_hash = {}
42
+ object.each do |key, value|
43
+ key = (
44
+ begin
45
+ key.to_sym
46
+ rescue StandardError
47
+ key
48
+ end) || key
49
+ new_hash[key] = symbolize_names(value)
50
+ end
51
+ new_hash
52
+ when Array
53
+ object.map {|value| symbolize_names(value)}
54
+ else
55
+ object
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module PrivacyGate
2
+ VERSION = "0.8.7"
3
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrivacyGate
4
+ module Webhook
5
+ # Analyze and construct appropriate event object based on webhook notification
6
+ def self.construct_event(payload, sig_header, secret)
7
+ data = JSON.parse(payload, symbolize_names: true)
8
+ if data.key?(:event)
9
+ WebhookSignature.verify_header(payload, sig_header, secret)
10
+ PrivacyGate::APIResources::Event.create_from(data[:event])
11
+ else
12
+ raise PrivacyGate::Errors::WebhookInvalidPayload.new("no event in payload",
13
+ sig_header, http_body: payload)
14
+ end
15
+ end
16
+
17
+ module WebhookSignature
18
+ def self.verify_header(payload, sig_header, secret)
19
+ unless [payload, sig_header, secret].all?
20
+ raise PrivacyGate::Errors::WebhookInvalidPayload.new(
21
+ "Missing payload or signature",
22
+ sig_header, http_body: payload)
23
+ end
24
+ expected_sig = compute_signature(payload, secret)
25
+ unless secure_compare(expected_sig, sig_header)
26
+ raise PrivacyGate::Errors::SignatureVerificationError.new(
27
+ "No signatures found matching the expected signature for payload",
28
+ sig_header, http_body: payload
29
+ )
30
+ end
31
+ true
32
+ end
33
+
34
+ def self.secure_compare(a, b)
35
+ return false unless a.bytesize == b.bytesize
36
+
37
+ l = a.unpack "C#{a.bytesize}"
38
+ res = 0
39
+ b.each_byte {|byte| res |= byte ^ l.shift}
40
+ res.zero?
41
+ end
42
+
43
+ private_class_method :secure_compare
44
+
45
+ def self.compute_signature(payload, secret)
46
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret, payload)
47
+ end
48
+
49
+ private_class_method :compute_signature
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,42 @@
1
+ # general
2
+ require "json"
3
+ require "uri"
4
+ require "faraday"
5
+ require "openssl"
6
+
7
+ # version
8
+ require "privacygate/version"
9
+
10
+ # client
11
+ require "privacygate/client"
12
+
13
+ # api response and errors
14
+ require "privacygate/api_errors"
15
+ require "privacygate/api_response"
16
+
17
+ # api base object
18
+ require "privacygate/api_resources/base/api_object"
19
+
20
+ # api resource base model
21
+ require "privacygate/api_resources/base/api_resource"
22
+
23
+ # api base operations
24
+ require "privacygate/api_resources/base/create"
25
+ require "privacygate/api_resources/base/update"
26
+ require "privacygate/api_resources/base/save"
27
+ require "privacygate/api_resources/base/list"
28
+ require "privacygate/api_resources/base/delete"
29
+
30
+ # api resources
31
+ require "privacygate/api_resources/checkout"
32
+ require "privacygate/api_resources/charge"
33
+ require "privacygate/api_resources/event"
34
+
35
+ # webhooks
36
+ require "privacygate/webhooks"
37
+
38
+ # utils
39
+ require "privacygate/util"
40
+
41
+ module PrivacyGate
42
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
4
+ require 'privacygate/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "privacygate"
8
+ gem.version = "1.1.1"
9
+ gem.license = "MIT"
10
+ gem.required_ruby_version = ">= 2.0.0"
11
+ gem.authors = ["PrivacyGate"]
12
+
13
+ gem.description = "Client library for PrivacyGate API"
14
+ gem.summary = "Client library for PrivacyGate API"
15
+ gem.homepage = "https://privacygate.io/docs/"
16
+
17
+ gem.files = `git ls-files`.sub("privacygate.gemspec", "privacygate.gemspec").split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(spec|gem|features)/})
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_dependency("faraday", "~> 0.10")
23
+
24
+ gem.add_development_dependency "rake"
25
+ gem.add_development_dependency "rspec"
26
+ gem.add_development_dependency "webmock"
27
+ gem.add_development_dependency "pry-byebug"
28
+ end
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe PrivacyGate::APIResources::Base::APIObject do
6
+ describe "#respond_to" do
7
+ it "should implement respond_to method" do
8
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(id: 1, foo: "bar")
9
+ expect(obj.respond_to?(:foo)).to be true
10
+ expect(obj.respond_to?(:baz)).to be false
11
+ end
12
+ end
13
+
14
+ describe "#serialize params" do
15
+ it "should serialize params on an empty object" do
16
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from({})
17
+ expect({}).to eq obj.serialize_params
18
+ end
19
+
20
+ it "should serialize params on a new object with a sub object" do
21
+ obj = PrivacyGate::APIResources::Base::APIObject.new
22
+ obj.metadata = {foo: "bar"}
23
+ expect({metadata: {foo: "bar"}}).to eq obj.serialize_params
24
+ end
25
+
26
+ it "should serialize params on a basic object" do
27
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(foo: nil)
28
+ obj.update_attributes(foo: "bar")
29
+ expect({foo: "bar"}).to eq obj.serialize_params
30
+ end
31
+
32
+ it "should serialize params on a complex object" do
33
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(
34
+ foo: PrivacyGate::APIResources::Base::APIObject.create_from(bar: nil, baz: nil))
35
+ obj.foo.bar = "newbar"
36
+ expect({foo: {bar: "newbar"}}).to eq obj.serialize_params
37
+ end
38
+
39
+ it "should serialize params on an array" do
40
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(foo: nil)
41
+ obj.foo = ["new-value"]
42
+ expect({foo: ["new-value"]}).to eq obj.serialize_params
43
+ end
44
+
45
+ it "should serialize params on an array that shortens" do
46
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(foo: ["0-index", "1-index", "2-index"])
47
+ obj.foo = ["new-value"]
48
+ expect({foo: ["new-value"]}).to eq obj.serialize_params
49
+ end
50
+
51
+ it "should serialize params on an array of hashes" do
52
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(foo: nil)
53
+ obj.foo = [
54
+ PrivacyGate::APIResources::Base::APIObject.create_from(bar: nil),
55
+ ]
56
+ obj.foo[0].bar = "baz"
57
+ expect({foo: [{bar: "baz"}]}).to eq obj.serialize_params
58
+ end
59
+
60
+ it "should serialize params doesn't include unchanged values" do
61
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(foo: nil)
62
+ expect({}).to eq obj.serialize_params
63
+ end
64
+
65
+ it "should serialize params with a APIObject" do
66
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from({})
67
+ obj.metadata = PrivacyGate::APIResources::Base::APIObject.create_from(foo: "bar")
68
+ serialized = obj.serialize_params
69
+ expect({foo: "bar"}).to eq serialized[:metadata]
70
+ end
71
+
72
+ it "should serialize params with APIObject that's been replaced" do
73
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(
74
+ source: PrivacyGate::APIResources::Base::APIObject.create_from(bar: "foo"))
75
+
76
+ obj.source = PrivacyGate::APIResources::Base::APIObject.create_from(baz: "foo")
77
+
78
+ serialized = obj.serialize_params
79
+ expect({baz: "foo"}).to eq serialized[:source]
80
+ end
81
+
82
+ it "should serialize params with an array of APIObjects" do
83
+ stub_request(:post, "#{@api_base}charges").to_return(body: {data: mock_item}.to_json)
84
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from({})
85
+ obj.metadata = [
86
+ PrivacyGate::APIResources::Base::APIObject.create_from(foo: "bar"),
87
+ ]
88
+
89
+ serialized = obj.serialize_params
90
+ expect([{foo: "bar"}]).to eq serialized[:metadata]
91
+ end
92
+
93
+ it "should serialize params with embed API resources" do
94
+ charge = PrivacyGate::APIResources::Charge.create_from(id: "charge_id")
95
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from({})
96
+ obj.charge = charge
97
+ expect(obj.charge).is_a? PrivacyGate::APIResources::Charge
98
+ end
99
+
100
+ it "should serialize params and not include API resources that have not been set" do
101
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(id: "cus_123")
102
+ obj2 = PrivacyGate::APIResources::Base::APIObject.create_from(obj: obj)
103
+ serialized = obj2.serialize_params
104
+ expect({}).to eq serialized
105
+ end
106
+
107
+ it "should serialize params takes a push option" do
108
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(
109
+ id: "id", metadata: PrivacyGate::APIResources::Base::APIObject.create_from(foo: "bar"))
110
+ serialized = obj.serialize_params(push: true)
111
+ expect({id: "id", metadata: {foo: "bar"}}).to eq serialized
112
+ end
113
+ end
114
+
115
+ describe "update attributes" do
116
+ it "should update attributes with a hash" do
117
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from({})
118
+ obj.update_attributes(metadata: {foo: "bar"})
119
+ expect(obj.metadata.class).to eq PrivacyGate::APIResources::Base::APIObject
120
+ end
121
+
122
+ it "should assign question mark accessors for booleans added after initialization" do
123
+ obj = PrivacyGate::APIResources::Base::APIObject.new
124
+ obj.bool = true
125
+ expect(obj.respond_to?(:bool?)).to be true
126
+ expect(obj.bool?).to be true
127
+ end
128
+
129
+ it "should assign question mark accessors for booleans" do
130
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from(id: 1, bool: true, not_bool: "bar")
131
+ expect(obj.respond_to?(:bool?)).to be true
132
+ expect(obj.respond_to?(:not_bool)).to be true
133
+ expect(obj.bool?).to be true
134
+ end
135
+
136
+ it "should create accessors when update attributes is called" do
137
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from({})
138
+ expect(obj.send(:metaclass).method_defined?(:foo)).to eq false
139
+ obj.update_attributes(foo: "bar")
140
+ expect(obj.send(:metaclass).method_defined?(:foo)).to eq true
141
+ end
142
+
143
+ it "should update attributes with a hash" do
144
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from({})
145
+ obj.update_attributes(metadata: {foo: "bar"})
146
+ expect(PrivacyGate::APIResources::Base::APIObject).to eq obj.metadata.class
147
+ end
148
+
149
+ it "should create accessors when update attributes is called" do
150
+ obj = PrivacyGate::APIResources::Base::APIObject.create_from({})
151
+ expect(obj.send(:metaclass).method_defined?(:foo)).to be false
152
+ obj.update_attributes(foo: "bar")
153
+ expect(obj.send(:metaclass).method_defined?(:foo)).to be true
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe PrivacyGate::APIResources::Base::APIResource do
4
+ before :all do
5
+ @client = PrivacyGate::Client.new(api_key: 'api_key')
6
+ @api_base = @client.instance_variable_get :@api_uri
7
+ end
8
+
9
+ it "shout create a new APIResource without request" do
10
+ PrivacyGate::APIResources::Base::APIResource.new("id")
11
+ assert_not_requested :get, %r{#{@api_base}/.*}
12
+ end
13
+
14
+ it "should create APIResource object form hash without request" do
15
+ PrivacyGate::APIResources::Base::APIResource.create_from(id: "some_id",
16
+ param: {id: "param_id"})
17
+ assert_not_requested :get, %r{#{@api_base}/.*}
18
+ end
19
+
20
+ it "setting an attribute should not cause a network request" do
21
+ c = PrivacyGate::APIResources::Base::APIResource.new("cus_123")
22
+ c.param = {id: "param_id"}
23
+ assert_not_requested :get, %r{#{@api_base}/.*}
24
+ assert_not_requested :post, %r{#{@api_base}/.*}
25
+ end
26
+
27
+ it "accessing id should not issue a fetch" do
28
+ c = PrivacyGate::APIResources::Base::APIResource.new("cus_123")
29
+ c.id
30
+ assert_not_requested :get, %r{#{@api_base}/.*}
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ describe PrivacyGate::APIResources::Charge do
2
+ before :all do
3
+ @client = PrivacyGate::Client.new(api_key: 'api_key')
4
+ @api_base = @client.instance_variable_get :@api_uri
5
+ end
6
+
7
+ it "making a POST request with parameters should have a body and no query string" do
8
+ stub_request(:post, "#{@api_base}#{PrivacyGate::APIResources::Charge::RESOURCE_PATH}")
9
+ .with(body: {:id => "id_value", :key => "key_value"})
10
+ .to_return(body: {data: {id: "id_value", key: "key_value"}}.to_json)
11
+ @client.charge.create(id: "id_value", key: "key_value")
12
+ end
13
+
14
+ it "making a GET request with parameters should have a query string and no body" do
15
+ stub_request(:get, "#{@api_base}#{PrivacyGate::APIResources::Charge::RESOURCE_PATH}")
16
+ .with(query: {limit: 5}).to_return(body: JSON.generate(data: [mock_list]))
17
+ @client.charge.list(limit: 5)
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ describe PrivacyGate::APIResources::Checkout do
2
+ before :all do
3
+ @client = PrivacyGate::Client.new(api_key: 'api_key')
4
+ @api_base = @client.instance_variable_get :@api_uri
5
+ end
6
+
7
+ it "checkout should save nothing if nothing changes" do
8
+ stub_request(:post, "#{@api_base}#{PrivacyGate::APIResources::Checkout::RESOURCE_PATH}")
9
+ .to_return(body: {data: {id: "id_value", key: "key_value"}}.to_json)
10
+ checkout = @client.checkout.create(id: "id_value", key: "key_value")
11
+
12
+ stub_request(:put, "#{@api_base}#{PrivacyGate::APIResources::Checkout::RESOURCE_PATH}/id_value")
13
+ .with(body: {})
14
+ .to_return(body: JSON.generate("id" => "id_value", "key" => "key_value"))
15
+
16
+ checkout.save
17
+ end
18
+
19
+ it "making a POST request with parameters should have a body and no query string" do
20
+ stub_request(:post, "#{@api_base}#{PrivacyGate::APIResources::Checkout::RESOURCE_PATH}")
21
+ .with(body: {:id => "id_value", :key => "key_value"})
22
+ .to_return(body: {data: {id: "id_value", key: "key_value"}}.to_json)
23
+ @client.checkout.create(id: "id_value", key: "key_value")
24
+ end
25
+
26
+ it "making a GET request with parameters should have a query string and no body" do
27
+ stub_request(:get, "#{@api_base}#{PrivacyGate::APIResources::Checkout::RESOURCE_PATH}")
28
+ .with(query: {limit: 5}).to_return(body: JSON.generate(data: [mock_list]))
29
+ @client.checkout.list(limit: 5)
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ describe PrivacyGate::APIResources::Event do
2
+ before :all do
3
+ @client = PrivacyGate::Client.new(api_key: 'api_key')
4
+ @api_base = @client.instance_variable_get :@api_uri
5
+ end
6
+
7
+ it "making a GET request with parameters should have a query string and no body" do
8
+ stub_request(:get, "#{@api_base}#{PrivacyGate::APIResources::Event::RESOURCE_PATH}")
9
+ .with(query: {limit: 5}).to_return(body: JSON.generate(data: [mock_list]))
10
+ @client.event.list(limit: 5)
11
+ end
12
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe PrivacyGate do
5
+ before :all do
6
+ @client = PrivacyGate::Client.new(api_key: 'api_key')
7
+ @api_base = @client.instance_variable_get :@api_uri
8
+ end
9
+
10
+ # test list resources
11
+ it "should get charges" do
12
+ stub_request(:get, "#{@api_base}charges").to_return(body: mock_list.to_json)
13
+ charge_list = @client.charge.list
14
+ expect(charge_list).is_a? @client.charge
15
+ expect(charge_list.data).is_a? Array
16
+ expect(charge_list.pagination).is_a? PrivacyGate::APIResources::Base::APIObject
17
+ expect(charge_list.data[0].to_hash).to eq mock_item
18
+ end
19
+
20
+ it "should get checkouts" do
21
+ stub_request(:get, "#{@api_base}checkouts").to_return(body: mock_list.to_json)
22
+ checkout_list = @client.checkout.list
23
+ expect(checkout_list).is_a? @client.checkout
24
+ expect(checkout_list.data).is_a? Array
25
+ expect(checkout_list.pagination).is_a? PrivacyGate::APIResources::Base::APIObject
26
+ expect(checkout_list.data[0].to_hash).to eq mock_item
27
+ end
28
+
29
+ it "should should get events" do
30
+ stub_request(:get, "#{@api_base}events").to_return(body: mock_list.to_json)
31
+ event_list = @client.event.list
32
+ expect(event_list).is_a? @client.checkout
33
+ expect(event_list.data).is_a? Array
34
+ expect(event_list.pagination).is_a? PrivacyGate::APIResources::Base::APIObject
35
+ expect(event_list.data[0].to_hash).to eq mock_item
36
+ end
37
+
38
+
39
+ # test single resources
40
+ it "should get charge" do
41
+ stub_request(:get, "#{@api_base}charges/key").to_return(body: {data: mock_item}.to_json)
42
+ charge = @client.charge.retrieve :key
43
+ expect(charge).is_a? @client.charge
44
+ expect(charge).is_a? PrivacyGate::APIResources::Base::APIObject
45
+ expect(charge.id).to eq mock_item[:id]
46
+ expect(charge.key).to eq mock_item[:key]
47
+ end
48
+
49
+ it "should get checkout" do
50
+ stub_request(:get, "#{@api_base}checkouts/key").to_return(body: {data: mock_item}.to_json)
51
+ checkout = @client.checkout.retrieve :key
52
+ expect(checkout).is_a? @client.checkout
53
+ expect(checkout).is_a? PrivacyGate::APIResources::Base::APIObject
54
+ expect(checkout.id).to eq mock_item[:id]
55
+ expect(checkout.key).to eq mock_item[:key]
56
+ end
57
+
58
+ it "should get event" do
59
+ stub_request(:get, "#{@api_base}events/key").to_return(body: {data: mock_item}.to_json)
60
+ event = @client.event.retrieve :key
61
+ expect(event).is_a? @client.charge
62
+ expect(event).is_a? PrivacyGate::APIResources::Base::APIObject
63
+ expect(event.id).to eq mock_item[:id]
64
+ expect(event.key).to eq mock_item[:key]
65
+ end
66
+
67
+ # test create resources
68
+ it "should create charge" do
69
+ stub_request(:post, "#{@api_base}charges").to_return(body: {data: mock_item}.to_json)
70
+ charge = @client.charge.create(mock_item)
71
+ expect(charge).is_a? @client.charge
72
+ expect(charge).is_a? PrivacyGate::APIResources::Base::APIObject
73
+ expect(charge.id).to eq mock_item[:id]
74
+ expect(charge.key).to eq mock_item[:key]
75
+ end
76
+
77
+ it "should create checkout" do
78
+ stub_request(:post, "#{@api_base}checkouts").to_return(body: {data: mock_item}.to_json)
79
+ checkout = @client.checkout.create(mock_item)
80
+ expect(checkout).is_a? @client.checkout
81
+ expect(checkout).is_a? PrivacyGate::APIResources::Base::APIObject
82
+ expect(checkout.id).to eq mock_item[:id]
83
+ expect(checkout.key).to eq mock_item[:key]
84
+ end
85
+
86
+ # test delete resources
87
+ it "should delete checkout" do
88
+ stub_request(:post, "#{@api_base}checkouts").to_return(body: {data: mock_item}.to_json)
89
+ stub_request(:delete, "#{@api_base}checkouts/val").to_return(body: {}.to_json)
90
+ checkout = @client.checkout.create(mock_item)
91
+ checkout.delete
92
+ end
93
+
94
+ # test save resources
95
+ it "should save checkout" do
96
+ stub_request(:post, "#{@api_base}checkouts").to_return(body: {data: mock_item}.to_json)
97
+ stub_request(:put, "#{@api_base}checkouts/val").to_return(body: {:edited => true}.to_json)
98
+ checkout = @client.checkout.create(mock_item)
99
+ checkout.key = "new value"
100
+ checkout.save
101
+ expect(checkout.edited).to eq true
102
+ end
103
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe PrivacyGate do
5
+ before :all do
6
+ @client = PrivacyGate::Client.new(api_key: 'api_key')
7
+ end
8
+
9
+ it "should get appropriate error when invalid response object" do
10
+ stub_request(:get, /.*/).to_return(body: {errors: [{id: "boo", message: "foo"}]}.to_json, status: 400)
11
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::APIError
12
+ end
13
+
14
+ it "should handle invalid request general" do
15
+ stub_request(:get, /.*/).to_return(body: {error: {type: "invalid_request", message: "test"}}.to_json, status: 400)
16
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::InvalidRequestError
17
+ end
18
+
19
+ it "should handle param required" do
20
+ stub_request(:get, /.*/).to_return(body: {error: {type: "param_required", message: "param_required"}}.to_json, status: 400)
21
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::ParamRequiredError
22
+ end
23
+
24
+ it "should handle validation error" do
25
+ stub_request(:get, /.*/).to_return(body: {error: {type: "validation_error", message: "validation_error"}}.to_json, status: 400)
26
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::ValidationError
27
+ end
28
+
29
+ it "should handle param_required" do
30
+ stub_request(:get, /.*/).to_return(body: {error: {type: "invalid_request", message: "invalid_request"}}.to_json, status: 400)
31
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::InvalidRequestError
32
+ end
33
+
34
+ it "should handle authentication error" do
35
+ stub_request(:get, /.*/).to_return(body: {error: {type: "auth_error", message: "auth_error"}}.to_json, status: 401)
36
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::AuthenticationError
37
+ end
38
+
39
+ it "should handle resource not found error" do
40
+ stub_request(:get, /.*/).to_return(body: {error: {type: "not_found", message: "not_found"}}.to_json, status: 404)
41
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::ResourceNotFoundError
42
+ end
43
+
44
+ it "should handle rate limit exceeded error" do
45
+ stub_request(:get, /.*/).to_return(body: {error: {type: "rate_limit_exceeded", message: "rate_limit_exceeded"}}.to_json, status: 429)
46
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::RateLimitExceededError
47
+ end
48
+
49
+ it "should handle internal server error" do
50
+ stub_request(:get, /.*/).to_return(body: {error: {type: "internal_error", message: "internal_error"}}.to_json, status: 500)
51
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::InternalServerError
52
+ end
53
+
54
+ it "should handle service unavailable error" do
55
+ stub_request(:get, /.*/).to_return(body: {error: {type: "service_unavailable", message: "service_unavailable"}}.to_json, status: 503)
56
+ expect {@client.checkout.list}.to raise_error PrivacyGate::Errors::ServiceUnavailableError
57
+ end
58
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe PrivacyGate::PrivacyGateResponse do
5
+ describe "from_faraday_hash" do
6
+ it "should initialize a PrivacyGateResponse object from a Hash like the kind" do
7
+ body = '{"foo": "bar"}'
8
+ headers = {"x-request-id" => "x-request-id"}
9
+ http_resp = {
10
+ body: body,
11
+ headers: headers,
12
+ status: 200,
13
+ }
14
+ resp = PrivacyGate::PrivacyGateResponse.from_faraday_hash(http_resp)
15
+
16
+ expect(JSON.parse(body, symbolize_names: true)).to eq resp.data
17
+ expect(body).to eq resp.http_body
18
+ expect(headers).to eq resp.http_headers
19
+ expect(200).to eq resp.http_status
20
+ expect("x-request-id").to eq resp.request_id
21
+ end
22
+ end
23
+
24
+ describe "from_faraday_response" do
25
+ it "should initialize a PrivacyGateResponse object from a Faraday HTTP response object." do
26
+ body = '{"foo": "bar"}'
27
+ headers = {"x-request-id" => "x-request-id"}
28
+ env = Faraday::Env.from(
29
+ status: 200, body: body,
30
+ response_headers: headers
31
+ )
32
+ http_resp = Faraday::Response.new(env)
33
+
34
+ resp = PrivacyGate::PrivacyGateResponse.from_faraday_response(http_resp)
35
+
36
+ expect(JSON.parse(body, symbolize_names: true)).to eq resp.data
37
+ expect(body).to eq resp.http_body
38
+ expect(headers).to eq resp.http_headers
39
+ expect(200).to eq resp.http_status
40
+ expect("x-request-id").to eq resp.request_id
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+ require 'webmock/rspec'
3
+ Bundler.setup
4
+ require 'privacygate'
5
+
6
+ def mock_item
7
+ {:id => "val", :key => "val"}
8
+ end
9
+
10
+ def mock_list
11
+ {
12
+ :pagination => mock_item,
13
+ :data => [mock_item, mock_item]
14
+ }
15
+ end