defra_ruby_govpay 0.1.2
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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +17 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +90 -0
- data/README.md +84 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/defra_ruby_govpay.gemspec +25 -0
- data/lib/defra_ruby_govpay/api.rb +76 -0
- data/lib/defra_ruby_govpay/configuration.rb +10 -0
- data/lib/defra_ruby_govpay/version.rb +5 -0
- data/lib/defra_ruby_govpay.rb +19 -0
- data/sig/defra_ruby_govpay.rbs +4 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 985dbbc4e62af28fb16d7af3ff829f171dd570f41014b2408eb07e686d3776a3
|
4
|
+
data.tar.gz: 6605967ad08002c8633f82cef44f3910dbf1c5e66546ad6a2565ec92dd2792f2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 24b85aa48412e7e55e33f3ed8a8c31536370f6fb8c8f34a0460c39c17b582812752f53f50328729ce09aea05c167a9e7d80b2b4f3825e844dd262530ab89b4df
|
7
|
+
data.tar.gz: 22092a2f738956cec84167e3dc3d834024e69e1c7d5812049f2a40a1bf7c0409e425fa42b0abae151140c98916a1f15c7112e7586b1ca3e1f54a87a9b254de9d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
14
|
+
|
15
|
+
Metrics/BlockLength:
|
16
|
+
Exclude:
|
17
|
+
- 'spec/**/*'
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in govpay_integration.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "rubocop", "~> 1.21"
|
13
|
+
|
14
|
+
gem "webmock", "~> 3.4"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
defra_ruby_govpay (0.1.0)
|
5
|
+
rest-client (~> 2.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.8.5)
|
11
|
+
public_suffix (>= 2.0.2, < 6.0)
|
12
|
+
ast (2.4.2)
|
13
|
+
crack (0.4.5)
|
14
|
+
rexml
|
15
|
+
diff-lcs (1.5.0)
|
16
|
+
domain_name (0.5.20190701)
|
17
|
+
unf (>= 0.0.5, < 1.0.0)
|
18
|
+
hashdiff (1.0.1)
|
19
|
+
http-accept (1.7.0)
|
20
|
+
http-cookie (1.0.5)
|
21
|
+
domain_name (~> 0.5)
|
22
|
+
json (2.6.3)
|
23
|
+
language_server-protocol (3.17.0.3)
|
24
|
+
mime-types (3.5.1)
|
25
|
+
mime-types-data (~> 3.2015)
|
26
|
+
mime-types-data (3.2023.0808)
|
27
|
+
netrc (0.11.0)
|
28
|
+
parallel (1.23.0)
|
29
|
+
parser (3.2.2.3)
|
30
|
+
ast (~> 2.4.1)
|
31
|
+
racc
|
32
|
+
public_suffix (5.0.3)
|
33
|
+
racc (1.7.1)
|
34
|
+
rainbow (3.1.1)
|
35
|
+
rake (13.0.6)
|
36
|
+
regexp_parser (2.8.1)
|
37
|
+
rest-client (2.1.0)
|
38
|
+
http-accept (>= 1.7.0, < 2.0)
|
39
|
+
http-cookie (>= 1.0.2, < 2.0)
|
40
|
+
mime-types (>= 1.16, < 4.0)
|
41
|
+
netrc (~> 0.8)
|
42
|
+
rexml (3.2.5)
|
43
|
+
rspec (3.12.0)
|
44
|
+
rspec-core (~> 3.12.0)
|
45
|
+
rspec-expectations (~> 3.12.0)
|
46
|
+
rspec-mocks (~> 3.12.0)
|
47
|
+
rspec-core (3.12.2)
|
48
|
+
rspec-support (~> 3.12.0)
|
49
|
+
rspec-expectations (3.12.3)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.12.0)
|
52
|
+
rspec-mocks (3.12.5)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.12.0)
|
55
|
+
rspec-support (3.12.1)
|
56
|
+
rubocop (1.54.1)
|
57
|
+
json (~> 2.3)
|
58
|
+
language_server-protocol (>= 3.17.0)
|
59
|
+
parallel (~> 1.10)
|
60
|
+
parser (>= 3.2.2.3)
|
61
|
+
rainbow (>= 2.2.2, < 4.0)
|
62
|
+
regexp_parser (>= 1.8, < 3.0)
|
63
|
+
rexml (>= 3.2.5, < 4.0)
|
64
|
+
rubocop-ast (>= 1.28.0, < 2.0)
|
65
|
+
ruby-progressbar (~> 1.7)
|
66
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
67
|
+
rubocop-ast (1.29.0)
|
68
|
+
parser (>= 3.2.1.0)
|
69
|
+
ruby-progressbar (1.13.0)
|
70
|
+
unf (0.1.4)
|
71
|
+
unf_ext
|
72
|
+
unf_ext (0.0.8.2)
|
73
|
+
unicode-display_width (2.4.2)
|
74
|
+
webmock (3.19.1)
|
75
|
+
addressable (>= 2.8.0)
|
76
|
+
crack (>= 0.3.2)
|
77
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
78
|
+
|
79
|
+
PLATFORMS
|
80
|
+
x86_64-darwin-22
|
81
|
+
|
82
|
+
DEPENDENCIES
|
83
|
+
defra_ruby_govpay!
|
84
|
+
rake (~> 13.0)
|
85
|
+
rspec (~> 3.0)
|
86
|
+
rubocop (~> 1.21)
|
87
|
+
webmock (~> 3.4)
|
88
|
+
|
89
|
+
BUNDLED WITH
|
90
|
+
2.4.8
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Defra Ruby Govpay Ruby Gem
|
2
|
+
|
3
|
+
The `defra-ruby-govpay` gem facilitates seamless integration with GovPay services, specifically tailored for DEFRA's WCR and WEX applications. It aims to abstract the integration code, offering a flexible and adaptable solution that requires minimal assumptions about the application's data models.
|
4
|
+
|
5
|
+
## Table of Contents
|
6
|
+
|
7
|
+
1. [Installation](#installation)
|
8
|
+
2. [Configuration](#configuration)
|
9
|
+
3. [Usage](#usage)
|
10
|
+
4. [Error Handling](#error-handling)
|
11
|
+
5. [Testing](#testing)
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'defra-ruby-govpay'
|
19
|
+
```
|
20
|
+
And then execute
|
21
|
+
```sh
|
22
|
+
bundle install
|
23
|
+
```
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
```sh
|
27
|
+
gem install defra-ruby-govpay
|
28
|
+
```
|
29
|
+
|
30
|
+
## Configuration
|
31
|
+
|
32
|
+
Before you start using the gem, you need to configure it according to your requirements. Create an initializer file (e.g., `config/initializers/govpay.rb`) and set the necessary parameters:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
DefraRubyGovpay.configure do |config|
|
36
|
+
config.govpay_url = 'https://your-govpay-url.com'
|
37
|
+
config.govpay_front_office_api_token = 'your-front-office-token'
|
38
|
+
config.govpay_back_office_api_token = 'your-back-office-token'
|
39
|
+
config.host_is_back_office = false
|
40
|
+
# ... any other configurations
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
Here is a detailed guide on how to use the various components of the `defra-ruby-govpay` gem in your application:
|
47
|
+
|
48
|
+
## Sending a Request
|
49
|
+
|
50
|
+
You can send requests to the GovPay API using the `send_request` method. Here's an example:
|
51
|
+
|
52
|
+
after having followed the configuration step:
|
53
|
+
```ruby
|
54
|
+
govpay_api = DefraRubyGovpay::API.new
|
55
|
+
|
56
|
+
begin
|
57
|
+
response = govpay_api.send_request(
|
58
|
+
method: :get,
|
59
|
+
path: '/path/to/endpoint',
|
60
|
+
params: { param1: 'value1', param2: 'value2' },
|
61
|
+
is_moto: false
|
62
|
+
)
|
63
|
+
puts "Response received: #{response}"
|
64
|
+
rescue DefraRubyGovpay::GovpayApiError => e
|
65
|
+
puts "An error occurred: #{e.message}"
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
## Error Handling
|
70
|
+
|
71
|
+
Errors are handled through the DefraRubyGovpay::GovpayApiError class. Here's an example of how you can handle errors:
|
72
|
+
```ruby
|
73
|
+
begin
|
74
|
+
# some code that might raise an error
|
75
|
+
rescue DefraRubyGovpay::GovpayApiError => e
|
76
|
+
puts "An error occurred: #{e.message}"
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
## Testing
|
81
|
+
|
82
|
+
```
|
83
|
+
bundle exec rspec
|
84
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "govpay_integration"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/defra_ruby_govpay/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "defra_ruby_govpay"
|
7
|
+
spec.version = DefraRubyGovpay::VERSION
|
8
|
+
spec.authors = ["Jerome Pratt"]
|
9
|
+
spec.email = ["jerome.pratt@defra.gov.uk"]
|
10
|
+
|
11
|
+
spec.summary = "A Ruby gem facilitating integration with Govpay services in ruby applications."
|
12
|
+
spec.description = "This gem abstracts the Govpay integration code, facilitating " \
|
13
|
+
"integration within defra ruby applications."
|
14
|
+
spec.homepage = "https://github.com/DEFRA/defra-ruby-govpay"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/DEFRA/defra-ruby-govpay"
|
18
|
+
|
19
|
+
spec.add_dependency "rest-client", "~> 2.1"
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rest-client"
|
4
|
+
|
5
|
+
module DefraRubyGovpay
|
6
|
+
# Custom error class to handle Govpay API errors
|
7
|
+
class GovpayApiError < StandardError
|
8
|
+
def initialize(msg = "Govpay API error")
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# The API class is responsible for making requests to the Govpay API.
|
14
|
+
# It handles the construction of request URLs, headers, and payload,
|
15
|
+
# and also deals with any errors that occur during the API request.
|
16
|
+
class API
|
17
|
+
def initialize(config = DefraRubyGovpay.configuration)
|
18
|
+
@config = config
|
19
|
+
@is_back_office = config.host_is_back_office
|
20
|
+
@front_office_token = config.govpay_front_office_api_token
|
21
|
+
@back_office_token = config.govpay_back_office_api_token
|
22
|
+
end
|
23
|
+
|
24
|
+
def send_request(method:, path:, params: nil, is_moto: false)
|
25
|
+
puts build_log_message(method, path, params, is_moto)
|
26
|
+
|
27
|
+
begin
|
28
|
+
response = execute_request(method, path, params, is_moto)
|
29
|
+
puts "Received response from Govpay: #{response}"
|
30
|
+
response
|
31
|
+
rescue StandardError => error
|
32
|
+
handle_error(error, method, path, params)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def build_log_message(method, path, params, is_moto)
|
39
|
+
"#{self.class} sending #{method} request to govpay (#{path}), params: #{params}, moto: #{is_moto}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def execute_request(method, path, params, is_moto)
|
43
|
+
RestClient::Request.execute(
|
44
|
+
method: method,
|
45
|
+
url: url(path),
|
46
|
+
payload: payload(params),
|
47
|
+
headers: {
|
48
|
+
"Authorization" => "Bearer #{bearer_token(is_moto)}",
|
49
|
+
"Content-Type" => "application/json"
|
50
|
+
}
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
def handle_error(error, method, path, params)
|
55
|
+
error_message = "Error sending request to govpay (#{method} #{path}, params: #{params}): #{error}"
|
56
|
+
puts error_message
|
57
|
+
raise GovpayApiError, error_message
|
58
|
+
end
|
59
|
+
|
60
|
+
def url(path)
|
61
|
+
"#{@config.govpay_url}#{path}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def bearer_token(is_moto)
|
65
|
+
return @front_office_token unless @is_back_office
|
66
|
+
|
67
|
+
is_moto ? @back_office_token : @front_office_token
|
68
|
+
end
|
69
|
+
|
70
|
+
def payload(params)
|
71
|
+
return nil if params.nil? || params.empty?
|
72
|
+
|
73
|
+
params.compact.to_json
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DefraRubyGovpay
|
4
|
+
# The Configuration class is responsible for storing configurable settings
|
5
|
+
# for the DefraRubyGovpay module. You can set different options like
|
6
|
+
# API tokens, host preferences, and other necessary configurations here.
|
7
|
+
class Configuration
|
8
|
+
attr_accessor :govpay_url, :govpay_front_office_api_token, :govpay_back_office_api_token, :host_is_back_office
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require_relative "defra_ruby_govpay/version"
|
5
|
+
require_relative "defra_ruby_govpay/configuration"
|
6
|
+
require_relative "defra_ruby_govpay/api"
|
7
|
+
|
8
|
+
# The DefraRubyGovpay module facilitates integration with Govpay services.
|
9
|
+
# It provides a convenient and configurable way to interact with Govpay APIs in Defra's ruby applications.
|
10
|
+
module DefraRubyGovpay
|
11
|
+
class << self
|
12
|
+
attr_accessor :configuration
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
self.configuration ||= Configuration.new
|
17
|
+
yield(configuration)
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: defra_ruby_govpay
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jerome Pratt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-09-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.1'
|
27
|
+
description: This gem abstracts the Govpay integration code, facilitating integration
|
28
|
+
within defra ruby applications.
|
29
|
+
email:
|
30
|
+
- jerome.pratt@defra.gov.uk
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- ".rubocop.yml"
|
38
|
+
- CHANGELOG.md
|
39
|
+
- Gemfile
|
40
|
+
- Gemfile.lock
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- bin/console
|
44
|
+
- bin/setup
|
45
|
+
- defra_ruby_govpay.gemspec
|
46
|
+
- lib/defra_ruby_govpay.rb
|
47
|
+
- lib/defra_ruby_govpay/api.rb
|
48
|
+
- lib/defra_ruby_govpay/configuration.rb
|
49
|
+
- lib/defra_ruby_govpay/version.rb
|
50
|
+
- sig/defra_ruby_govpay.rbs
|
51
|
+
homepage: https://github.com/DEFRA/defra-ruby-govpay
|
52
|
+
licenses: []
|
53
|
+
metadata:
|
54
|
+
source_code_uri: https://github.com/DEFRA/defra-ruby-govpay
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 2.6.0
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: A Ruby gem facilitating integration with Govpay services in ruby applications.
|
74
|
+
test_files: []
|