simpal 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Simpal
4
+ module Middleware
5
+ # Allows the use of convenience header names which are then mapped into the real header name.
6
+ #
7
+ class Headers < Faraday::Middleware
8
+ # @return [Array] The request headers which must have the 'PayPal-' prefix applied to them.
9
+ #
10
+ PAYPAL_HEADERS = %w[
11
+ Request-Id
12
+ Client-Metadata-Id
13
+ Partner-Attribution-Id
14
+ Auth-Assertion
15
+ ].freeze
16
+
17
+ def on_request(env)
18
+ headers = env[:request_headers]
19
+ return unless headers.is_a?(Hash)
20
+
21
+ # Prefer full representations, instead of the minimal response by default.
22
+ headers['Prefer'] = 'return=representation' unless headers.key?('Prefer')
23
+
24
+ PAYPAL_HEADERS.each do |header|
25
+ headers["PayPal-#{header}"] = headers.delete(header) if headers.key?(header)
26
+ end
27
+
28
+ env[:request_headers] = headers
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Simpal
4
+ # @see Simpal::API::Orders
5
+ #
6
+ module Order
7
+ # Create an order.
8
+ #
9
+ # @see Simpal::API::Orders.create
10
+ # @param params [Hash] The parameters for the create request.
11
+ # @param headers [Hash] The custom headers to add to the request.
12
+ # @param client [Simpal::Client] The API client to make the request with.
13
+ # @return [Simpal::PayPalObject] An object representing the order.
14
+ #
15
+ def self.create(params = {}, headers: {}, client: nil)
16
+ resource = API::Orders.create(params, headers: headers, client: client)
17
+ PayPalObject.new(resource)
18
+ end
19
+
20
+ # Update an order.
21
+ #
22
+ # @see Simpal::API::Orders.update
23
+ # @param id [String] The ID of an existing order.
24
+ # @param params [Array<Hash>] The collection of patches to apply to the order.
25
+ # @param headers [Hash] The custom headers to add to the request.
26
+ # @param client [Simpal::Client] The API client to make the request with.
27
+ # @return [Boolean] `true` if the order was updated, else an exception is raised.
28
+ #
29
+ def self.update(id, params = [], headers: {}, client: nil)
30
+ API::Orders.update(id, params, headers: headers, client: client)
31
+ true
32
+ end
33
+
34
+ # Retrieve an order.
35
+ #
36
+ # @see Simpal::API::Orders.retrieve
37
+ # @param id [String] The ID of an existing order.
38
+ # @param headers [Hash] The custom headers to add to the request.
39
+ # @param client [Simpal::Client] The API client to make the request with.
40
+ # @return [Hash] A Hash representing the order.
41
+ #
42
+ def self.retrieve(id, headers: {}, client: nil)
43
+ resource = API::Orders.retrieve(id, headers: headers, client: client)
44
+ PayPalObject.new(resource)
45
+ end
46
+
47
+ # Authorize the payment for an order.
48
+ #
49
+ # @see Simpal::API::Orders.authorize
50
+ # @param id [String] The ID of an existing order.
51
+ # @param params [Hash] The parameters for the authorize request.
52
+ # @param headers [Hash] The custom headers to add to the request.
53
+ # @param client [Simpal::Client] The API client to make the request with.
54
+ # @return [Hash] A Hash representing the order.
55
+ #
56
+ def self.authorize(id, params = {}, headers: {}, client: nil)
57
+ resource = API::Orders.authorize(id, params, headers: headers, client: client)
58
+ PayPalObject.new(resource)
59
+ end
60
+
61
+ # Capture the payment for an order.
62
+ #
63
+ # @see Simpal::API::Orders.capture
64
+ # @param id [String] The ID of an existing order.
65
+ # @param params [Hash] The parameters for the capture request.
66
+ # @param headers [Hash] The custom headers to add to the request.
67
+ # @param client [Simpal::Client] The API client to make the request with.
68
+ # @return [Hash] A Hash representing the order.
69
+ #
70
+ def self.capture(id, params = {}, headers: {}, client: nil)
71
+ resource = API::Orders.capture(id, params, headers: headers, client: client)
72
+ PayPalObject.new(resource)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Simpal
4
+ # Represents an API resource.
5
+ #
6
+ class PayPalObject
7
+ # Create a new object representing the provided resource.
8
+ #
9
+ # @param resource [Hash] The resource to represent.
10
+ #
11
+ def initialize(resource = {})
12
+ transform = proc do |key, value|
13
+ case value
14
+ when Array then value.map { |v| transform.call(nil, v) }
15
+ when Hash then PayPalObject.new(value)
16
+ else
17
+ if key && (key.end_with?('date') || key.end_with?('time'))
18
+ Time.parse(value)
19
+ else
20
+ value
21
+ end
22
+ end
23
+ end
24
+
25
+ @values = resource.each_with_object({}) do |(key, value), values|
26
+ values[key.to_s] = transform.call(key.to_s, value)
27
+ add_reader(key)
28
+ end
29
+ end
30
+
31
+ # @return [Hash] A hash representation of the PayPal object.
32
+ #
33
+ def to_hash
34
+ transform = proc do |value|
35
+ case value
36
+ when Array then value.map { |v| transform.call(v) }
37
+ when PayPalObject then value.to_hash
38
+ else value.is_a?(Time) ? value.iso8601 : value
39
+ end
40
+ end
41
+
42
+ @values.transform_values(&transform)
43
+ end
44
+
45
+ # @see Simpal::PayPalObject#to_hash
46
+ #
47
+ alias to_h to_hash
48
+
49
+ # @return [String] A JSON string representation of the PayPal object.
50
+ #
51
+ def inspect
52
+ paypal_id = respond_to?(:id) && id ? " id=#{id}" : ''
53
+ json = JSON.pretty_generate(@values)
54
+ "#<#{self.class}:0x#{object_id.to_s(16)}#{paypal_id}> JSON: #{json}"
55
+ end
56
+
57
+ # @return [Boolean] `true` to indicate that all methods can be responded to.
58
+ #
59
+ def respond_to_missing?(_name, _include_all)
60
+ true
61
+ end
62
+
63
+ # @return [NilClass] `nil` instead of raising an exception.
64
+ #
65
+ def method_missing(_name, *args); end
66
+
67
+ private
68
+
69
+ # Add a read-only attribute for the specified value.
70
+ #
71
+ # @param name [String, Symbol] The name of the attribute to define.
72
+ #
73
+ def add_reader(name)
74
+ metaclass.instance_eval do
75
+ name = name.to_s
76
+ if name.to_sym == :method
77
+ define_method(name.to_sym) { |*args| args.empty? ? @values[name] : super(*args) }
78
+ else
79
+ define_method(name.to_sym) { @values[name] }
80
+ end
81
+ end
82
+ end
83
+
84
+ # @return [Class] The metaclass which can be used to define methods on this instance only.
85
+ #
86
+ def metaclass
87
+ class << self
88
+ self
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Simpal
4
+ # @see Simpal::API::Payments
5
+ #
6
+ module Payment
7
+ # @see Simpal::API::Payments::Authorizations
8
+ #
9
+ module Authorization
10
+ # Retrieve an authorized payment.
11
+ #
12
+ # @param id [String] The ID of an authorized payment.
13
+ # @param headers [Hash] The custom headers to add to the request.
14
+ # @param client [Simpal::Client] The API client to make the request with.
15
+ # @return [Simpal::PayPalObject] An object representing the authorized payment.
16
+ #
17
+ def self.retrieve(id, headers: {}, client: nil)
18
+ resource = API::Payments::Authorizations.retrieve(id, headers: headers, client: client)
19
+ PayPalObject.new(resource)
20
+ end
21
+
22
+ # Capture an authorized payment.
23
+ #
24
+ # @param id [String] The ID of an authorized payment.
25
+ # @param params [Hash] The parameters for the capture request.
26
+ # @param headers [Hash] The custom headers to add to the request.
27
+ # @param client [Simpal::Client] The API client to make the request with.
28
+ # @return [Simpal::PayPalObject] An object representing the captured payment.
29
+ #
30
+ def self.capture(id, params = {}, headers: {}, client: nil)
31
+ resource = API::Payments::Authorizations.capture(id, params, headers: headers, client: client)
32
+ PayPalObject.new(resource)
33
+ end
34
+
35
+ # Reauthorize an authorized payment.
36
+ #
37
+ # @param id [String] The ID of an authorized payment.
38
+ # @param params [Hash] The parameters for the reauthorize request.
39
+ # @param headers [Hash] The custom headers to add to the request.
40
+ # @param client [Simpal::Client] The API client to make the request with.
41
+ # @return [Simpal::PayPalObject] An object representing the reauthorized payment.
42
+ #
43
+ def self.reauthorize(id, params = {}, headers: {}, client: nil)
44
+ resource = API::Payments::Authorizations.reauthorize(id, params, headers: headers, client: client)
45
+ PayPalObject.new(resource)
46
+ end
47
+
48
+ # Void an authorized payment.
49
+ #
50
+ # @param id [String] The ID of an authorized payment.
51
+ # @param headers [Hash] The custom headers to add to the request.
52
+ # @param client [Simpal::Client] The API client to make the request with.
53
+ # @return [Boolean] `true` if the authorization was voided, else an exception is raised.
54
+ #
55
+ def self.void(id, headers: {}, client: nil)
56
+ API::Payments::Authorizations.void(id, headers: headers, client: client)
57
+ true
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Simpal
4
+ # @see Simpal::API::Payments
5
+ #
6
+ module Payment
7
+ # @see Simpal::API::Payments::Captures
8
+ #
9
+ module Capture
10
+ # Retrieve an captured payment.
11
+ #
12
+ # @param id [String] The ID of a captured payment.
13
+ # @param headers [Hash] The custom headers to add to the request.
14
+ # @param client [Simpal::Client] The API client to make the request with.
15
+ # @return [Simpal::PayPalObject] An object representing the captured payment.
16
+ #
17
+ def self.retrieve(id, headers: {}, client: nil)
18
+ resource = API::Payments::Captures.retrieve(id, headers: headers, client: client)
19
+ PayPalObject.new(resource)
20
+ end
21
+
22
+ # Refund a captured payment.
23
+ #
24
+ # @param id [String] The ID of a captured payment.
25
+ # @param params [Hash] The parameters for the refund request.
26
+ # @param headers [Hash] The custom headers to add to the request.
27
+ # @param client [Simpal::Client] The API client to make the request with.
28
+ # @return [Simpal::PayPalObject] An object representing the refunded payment.
29
+ #
30
+ def self.refund(id, params = {}, headers: {}, client: nil)
31
+ resource = API::Payments::Captures.capture(id, params, headers: headers, client: client)
32
+ PayPalObject.new(resource)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Simpal
4
+ # @see Simpal::API::Payments
5
+ #
6
+ module Payment
7
+ # @see Simpal::API::Payments::Refunds
8
+ #
9
+ module Refund
10
+ # Retrieve a refund for a payment.
11
+ #
12
+ # @see Simpal::API::Payments::Refunds.retrieve
13
+ # @param id [String] The ID of a refuned payment.
14
+ # @param headers [Hash] The custom headers to add to the request.
15
+ # @param client [Simpal::Client] The API client to make the request with.
16
+ # @return [Simpal::PayPalObject] An object representing the refunded payment.
17
+ #
18
+ def self.retrieve(id, headers: {}, client: nil)
19
+ resource = API::Payments::Refunds.retrieve(id, headers: headers, client: client)
20
+ PayPalObject.new(resource)
21
+ end
22
+ end
23
+ end
24
+ end
data/simpal.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/simpal/constants'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'simpal'
7
+ spec.version = Simpal::VERSION
8
+ spec.authors = ['Nialto Services']
9
+ spec.email = ['support@nialtoservices.co.uk']
10
+
11
+ spec.summary = "A simple, lightweight wrapper around PayPal's REST API."
12
+ spec.homepage = 'https://github.com/nialtoservices/simpal'
13
+ spec.license = 'Apache-2.0'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/nialtoservices/simpal'
18
+ spec.metadata['changelog_uri'] = 'https://github.com/NialtoServices/simpal/blob/main/CHANGELOG.md'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'faraday', '~> 1.4'
31
+ spec.add_dependency 'faraday_middleware', '~> 1.0'
32
+
33
+ spec.add_development_dependency 'pry'
34
+ spec.add_development_dependency 'rake', '~> 13.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'rubocop', '~> 1.7'
37
+ spec.add_development_dependency 'rubocop-rake', '~> 0.5.1'
38
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.3'
39
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simpal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nialto Services
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.7'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.5.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.5.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.3'
125
+ description:
126
+ email:
127
+ - support@nialtoservices.co.uk
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".github/workflows/ruby.yml"
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - CHANGELOG.md
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - LICENSE
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - lib/simpal.rb
145
+ - lib/simpal/api/orders.rb
146
+ - lib/simpal/api/payments/authorizations.rb
147
+ - lib/simpal/api/payments/captures.rb
148
+ - lib/simpal/api/payments/refunds.rb
149
+ - lib/simpal/client.rb
150
+ - lib/simpal/client_error.rb
151
+ - lib/simpal/constants.rb
152
+ - lib/simpal/middleware/authorization.rb
153
+ - lib/simpal/middleware/headers.rb
154
+ - lib/simpal/order.rb
155
+ - lib/simpal/pay_pal_object.rb
156
+ - lib/simpal/payment/authorization.rb
157
+ - lib/simpal/payment/capture.rb
158
+ - lib/simpal/payment/refund.rb
159
+ - simpal.gemspec
160
+ homepage: https://github.com/nialtoservices/simpal
161
+ licenses:
162
+ - Apache-2.0
163
+ metadata:
164
+ homepage_uri: https://github.com/nialtoservices/simpal
165
+ source_code_uri: https://github.com/nialtoservices/simpal
166
+ changelog_uri: https://github.com/NialtoServices/simpal/blob/main/CHANGELOG.md
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: 2.5.0
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubygems_version: 3.2.15
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: A simple, lightweight wrapper around PayPal's REST API.
186
+ test_files: []