laa-fee-calculator-client 0.1.1
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/Gemfile +8 -0
- data/LICENSE +21 -0
- data/README.md +195 -0
- data/Rakefile +10 -0
- data/bin/console +21 -0
- data/bin/ruby_version_test +18 -0
- data/bin/setup +8 -0
- data/laa-fee-calculator-client.gemspec +40 -0
- data/lib/laa/fee/calculator/client.rb +3 -0
- data/lib/laa/fee_calculator.rb +39 -0
- data/lib/laa/fee_calculator/calculator.rb +33 -0
- data/lib/laa/fee_calculator/client.rb +35 -0
- data/lib/laa/fee_calculator/configuration.rb +17 -0
- data/lib/laa/fee_calculator/connection.rb +43 -0
- data/lib/laa/fee_calculator/errors.rb +35 -0
- data/lib/laa/fee_calculator/fee_scheme.rb +31 -0
- data/lib/laa/fee_calculator/has_manyable.rb +33 -0
- data/lib/laa/fee_calculator/raise_error.rb +30 -0
- data/lib/laa/fee_calculator/version.rb +8 -0
- metadata +215 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f8449f4bcfb343e3c9afab992dbfd671dfe11f955d94dd7e773d09130d3778d2
|
4
|
+
data.tar.gz: 1c09eb9194ad77223bf7ca4ec9ceab46cad12a65feb56b670ac9cb7d6bfc3c2a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4485644c9c9e46548968488f787a04cb4339d31d1819ef749a6bbe9dc8a499913f6a8fcda3edbfa15b599bc4ca9bf8d54e88f897b34caf98abbc428a47e277ab
|
7
|
+
data.tar.gz: 7eba04cf618951f3812953fe3f5fb4f06e19d8620a91fdbf35777c44bbec8f44f41037db33baea9fde216cd4b9b6ed6c5093f7bc24999b0fcdfbb3526883b917
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Ministry of Justice
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
[](https://travis-ci.org/ministryofjustice/laa-fee-calculator-client)
|
2
|
+
|
3
|
+
# Legal Aid Agency Fee Calculator Client
|
4
|
+
|
5
|
+
Ruby client for the [ministryofjustice/laa-fee-calculator](https://github.com/ministryofjustice/laa-fee-calculator). It enables transparent calling of this API's
|
6
|
+
endpoints, providing a simple interface for querying data, in particular, the primary function of the API, the `#calculate` endpoint.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'laa-fee-calculator-client'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install laa-fee-calculator-client
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
<a name="quick-start"></a>
|
27
|
+
##### Quick start
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# retrieve a calculated value for AGFS scheme 9
|
31
|
+
client = LAA::FeeCalculator.client
|
32
|
+
fee_scheme = client.fee_schemes(1)
|
33
|
+
|
34
|
+
# using a hash of options
|
35
|
+
fee_scheme.calculate(
|
36
|
+
scenario: 5,
|
37
|
+
advocate_type: 'JRALONE',
|
38
|
+
offence_class: 'E',
|
39
|
+
fee_type_code: 'AGFS_APPEAL_CON',
|
40
|
+
day: 1,
|
41
|
+
number_of_defendants: 1,
|
42
|
+
number_of_cases: 1
|
43
|
+
)
|
44
|
+
# => 130.0
|
45
|
+
|
46
|
+
# using a block to supply options
|
47
|
+
fee_scheme.calculate do |options|
|
48
|
+
options[:scenario] = 5
|
49
|
+
options[:offence_class] = 'E'
|
50
|
+
options[:advocate_type] = 'JRALONE'
|
51
|
+
options[:fee_type_code] = 'AGFS_APPEAL_CON'
|
52
|
+
options[:day] = 1
|
53
|
+
options[:number_of_defendants] = 1
|
54
|
+
options[:number_of_cases] = 1
|
55
|
+
end
|
56
|
+
# => 130.0
|
57
|
+
```
|
58
|
+
|
59
|
+
##### Lookup endpoints
|
60
|
+
|
61
|
+
Lookup endpoints (`advocate_types`, `offence_classes`, `scenarios`, `fee_types`, `modifier_types`, `units`) respond to a single argument for the id, or options hashes (where id can also be specified explicitly). Any option key specified, except `id:`, will be converted to a URI param in the resulting request. `id`s will become part of the URI path itself. Options specified should therefore be available on the [ministryofjustice/laa-fee-caclulator](https://github.com/ministryofjustice/laa-fee-calculator)
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# instantiate a client
|
65
|
+
client = LAA::FeeCalculator.client
|
66
|
+
```
|
67
|
+
|
68
|
+
###### Fee schemes
|
69
|
+
|
70
|
+
All other endpoints are queried based on fee scheme so you will need one of these first
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# by id
|
74
|
+
fee_scheme = client.fee_schemes(1)
|
75
|
+
# or
|
76
|
+
fee_scheme = client.fee_schemes(id: 1)
|
77
|
+
|
78
|
+
|
79
|
+
# by type and/or date came into force
|
80
|
+
fee_scheme = client.fee_schemes(type: 'AGFS', case_date: '2018-01-01')
|
81
|
+
# => AGFS scheme 9 object
|
82
|
+
|
83
|
+
fee_scheme = client.fee_schemes(type: 'AGFS', case_date: '2018-04-01')
|
84
|
+
# => AGFS scheme 10 object
|
85
|
+
|
86
|
+
```
|
87
|
+
note: supplier type and its args are to be changed (20/06/2018) in API
|
88
|
+
|
89
|
+
###### Lookup data
|
90
|
+
|
91
|
+
```
|
92
|
+
# all
|
93
|
+
fee_scheme.advocate_types
|
94
|
+
fee_scheme.scenarios
|
95
|
+
fee_scheme.offence_classes
|
96
|
+
fee_scheme.fee_types
|
97
|
+
fee_scheme.modifier_types
|
98
|
+
fee_scheme.units
|
99
|
+
|
100
|
+
# filtered
|
101
|
+
# by id
|
102
|
+
fee_scheme.advocate_types('JRALONE')
|
103
|
+
fee_scheme.modifier_types(1)
|
104
|
+
|
105
|
+
# by options
|
106
|
+
fee_scheme.modifier_types(fee_type_code: 'AGFS_APPEAL_CON')
|
107
|
+
```
|
108
|
+
|
109
|
+
###### Calculate
|
110
|
+
see the [Quick start](#quick-start)
|
111
|
+
|
112
|
+
|
113
|
+
## Development
|
114
|
+
|
115
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests.
|
116
|
+
|
117
|
+
|
118
|
+
To experiment with the code:
|
119
|
+
|
120
|
+
- clone the API's repo
|
121
|
+
```bash
|
122
|
+
git clone git@github.com:ministryofjustice/laa-fee-calculator.git
|
123
|
+
```
|
124
|
+
|
125
|
+
- setup and run the [API locally](https://github.com/ministryofjustice/laa-fee-calculator/blob/develop/docs/DEVELOPMENT.md)
|
126
|
+
- run `bin/console` for an interactive prompt in this repo's directory.
|
127
|
+
|
128
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
129
|
+
|
130
|
+
## Testing
|
131
|
+
|
132
|
+
The test suite makes extensive use of the [VCR](https://github.com/vcr/vcr) gem to stub requests to the external API. Any examples within a `describe` block with a `:vcr` tag will automatically use (or generate) VCR cassettes via an `around` block - see `spec_helper.rb`. All cassettes are stored in the `spec/vcr` directory and automatically named after the spec that produced them. Only new episodes/requests are created.
|
133
|
+
|
134
|
+
To recreate all cassettes:
|
135
|
+
|
136
|
+
- run the [laa-fee-calculator](https://github.com/ministryofjustice/laa-fee-calculator) API locally
|
137
|
+
- delete all files in `spec/vcr`
|
138
|
+
- `$ rspec`
|
139
|
+
|
140
|
+
Alternatively you can run tests against the local version of the API:
|
141
|
+
|
142
|
+
```bash
|
143
|
+
# run tests against local API
|
144
|
+
$ VCR_OFF=true rspec
|
145
|
+
```
|
146
|
+
|
147
|
+
You can also test against a hosted API by modifying the following in the `spec_helper.rb`
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
# run tests against a remote host
|
151
|
+
LAA::FeeCalculator.configure do |config|
|
152
|
+
config.host = LAA::FeeCalculator::Configuration::DEV_LAA_FEE_CALCULATOR_API_V1
|
153
|
+
end
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
## Contributing
|
158
|
+
|
159
|
+
Bug reports and pull requests are welcome on GitHub at [ministryofjustice/laa-fee-calculator-client](https://github.com/ministryofjustice/laa-fee-calculator-client)
|
160
|
+
|
161
|
+
## Publishing
|
162
|
+
|
163
|
+
1. Make required changes, run specs (rerecord vcr cassettes if necessary)
|
164
|
+
|
165
|
+
2. Run `bin/ruby_version_test` to test against ruby versions (2.4+ supported at present)
|
166
|
+
|
167
|
+
3. Update the version in `lib/laa/fee_calculator/version` using [semantic versioning](https://guides.rubygems.org/patterns/#semantic-versioning).
|
168
|
+
|
169
|
+
4. PR the change, code-review, merge etc.
|
170
|
+
|
171
|
+
5. Pull master and tag it with the new version number:
|
172
|
+
|
173
|
+
```
|
174
|
+
$ git tag -a v0.1.1 -m "version 0.1.1 - description"
|
175
|
+
```
|
176
|
+
|
177
|
+
```
|
178
|
+
$ git push origin v0.1.1
|
179
|
+
```
|
180
|
+
|
181
|
+
6. Build the gem
|
182
|
+
|
183
|
+
```
|
184
|
+
$ gem build laa-fee-calculator-client.gemspec
|
185
|
+
```
|
186
|
+
|
187
|
+
7. Publish to [Rubygems](https://rubygems.org)
|
188
|
+
|
189
|
+
```
|
190
|
+
$ gem push laa-fee-calculator-client-[VERSION].gem
|
191
|
+
```
|
192
|
+
|
193
|
+
## License
|
194
|
+
|
195
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "pry-byebug"
|
6
|
+
require "laa/fee_calculator"
|
7
|
+
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
10
|
+
|
11
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
12
|
+
|
13
|
+
# Pry.start
|
14
|
+
|
15
|
+
# configure for local development use
|
16
|
+
LAA::FeeCalculator.configure do |config|
|
17
|
+
config.host = 'http://localhost:8000/api/v1'
|
18
|
+
end
|
19
|
+
|
20
|
+
require "irb"
|
21
|
+
IRB.start(__FILE__)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
set -e
|
4
|
+
|
5
|
+
rubies=("ruby-2.4.4" "ruby-2.5.0" "ruby-2.5.1")
|
6
|
+
for i in "${rubies[@]}"
|
7
|
+
do
|
8
|
+
echo "====================================================="
|
9
|
+
echo "$i: Start Test"
|
10
|
+
echo "====================================================="
|
11
|
+
if [[ $(rvm list | grep $i) ]]; then echo "$i already installed!"; else rvm install $i; fi
|
12
|
+
rvm $i exec gem install bundler
|
13
|
+
rvm $i exec bundle install
|
14
|
+
rvm $i exec bundle exec rspec spec
|
15
|
+
echo "====================================================="
|
16
|
+
echo "$i: End Test"
|
17
|
+
echo "====================================================="
|
18
|
+
done
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'laa/fee_calculator/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'laa-fee-calculator-client'
|
9
|
+
spec.version = LAA::FeeCalculator::VERSION
|
10
|
+
spec.authors = ['Joel Sugarman', 'Ministry of Justice']
|
11
|
+
spec.email = ['joel.sugarman@digital.justice.gov.uk', 'tools@digital.justice.gov.uk']
|
12
|
+
spec.date = '2018-06-29'
|
13
|
+
spec.summary = 'Ruby client for the Legal Aid Agency fee calculator API'
|
14
|
+
spec.description = "Ruby client for the Ministry of Justices LAA fee calculator API. A simple interface for transparent calling of the API endpoints to query data and return calculated fee amounts."
|
15
|
+
spec.homepage = 'https://github.com/ministryofjustice/laa-fee-calculator-client'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
spec.extra_rdoc_files = ["LICENSE", "README.md"]
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test/|spec/|features/|\..*)})
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
# see bin/ruby_version_test
|
27
|
+
spec.required_ruby_version = '>= 2.4.0'
|
28
|
+
|
29
|
+
spec.add_runtime_dependency 'addressable', '~> 2.3', '>= 2.3.7'
|
30
|
+
spec.add_runtime_dependency 'faraday', '~> 0.9.2'
|
31
|
+
|
32
|
+
spec.add_development_dependency "awesome_print"
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
34
|
+
spec.add_development_dependency "pry-byebug"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
spec.add_development_dependency "rubocop", '~> 0.50'
|
38
|
+
spec.add_development_dependency "vcr"
|
39
|
+
spec.add_development_dependency "webmock"
|
40
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'laa/fee_calculator/version'
|
4
|
+
require 'laa/fee_calculator/configuration'
|
5
|
+
require 'laa/fee_calculator/raise_error'
|
6
|
+
require 'laa/fee_calculator/connection'
|
7
|
+
require 'laa/fee_calculator/fee_scheme'
|
8
|
+
require 'laa/fee_calculator/calculator'
|
9
|
+
require 'laa/fee_calculator/client'
|
10
|
+
|
11
|
+
module LAA
|
12
|
+
module FeeCalculator
|
13
|
+
class << self
|
14
|
+
def root
|
15
|
+
spec = Gem::Specification.find_by_name("laa-fee-calculator-client")
|
16
|
+
spec.gem_dir
|
17
|
+
end
|
18
|
+
|
19
|
+
def client
|
20
|
+
Client.new
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_writer :configuration
|
24
|
+
def configuration
|
25
|
+
@configuration ||= Configuration.new
|
26
|
+
end
|
27
|
+
alias config configuration
|
28
|
+
|
29
|
+
def configure
|
30
|
+
yield(configuration) if block_given?
|
31
|
+
configuration
|
32
|
+
end
|
33
|
+
|
34
|
+
def reset
|
35
|
+
@configuration = Configuration.new
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LAA
|
4
|
+
module FeeCalculator
|
5
|
+
class Calculator
|
6
|
+
extend Forwardable
|
7
|
+
def_delegators :connection, :get
|
8
|
+
|
9
|
+
attr_reader :scheme_pk, :options
|
10
|
+
|
11
|
+
def initialize(scheme_pk, options)
|
12
|
+
@scheme_pk = scheme_pk
|
13
|
+
@options = options
|
14
|
+
end
|
15
|
+
|
16
|
+
def call
|
17
|
+
uri.query_values = options.reject { |k, _v| k.eql?(:id) }
|
18
|
+
json = get(uri).body
|
19
|
+
JSON.parse(json)['amount']
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def uri
|
25
|
+
@uri ||= Addressable::URI.parse("fee-schemes/#{scheme_pk}/calculate/")
|
26
|
+
end
|
27
|
+
|
28
|
+
def connection
|
29
|
+
@connection ||= Connection.instance
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "addressable/uri"
|
2
|
+
|
3
|
+
module LAA
|
4
|
+
module FeeCalculator
|
5
|
+
class Client
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
attr_reader :connection
|
9
|
+
alias conn connection
|
10
|
+
|
11
|
+
attr_accessor :fee_scheme
|
12
|
+
|
13
|
+
def_delegators :connection, :host, :url_prefix, :port, :get, :ping
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@connection = Connection.instance
|
17
|
+
end
|
18
|
+
|
19
|
+
def fee_schemes(id = nil, **options)
|
20
|
+
uri = 'fee-schemes/'
|
21
|
+
id ||= options.fetch(:id, nil)
|
22
|
+
uri.concat(id.to_s, "/") if id
|
23
|
+
uri = Addressable::URI.parse(uri)
|
24
|
+
uri.query_values = options.reject { |k, _v| k.eql?(:id) }
|
25
|
+
|
26
|
+
json = get(uri).body
|
27
|
+
|
28
|
+
fstruct = JSON.parse(json, object_class: FeeScheme)
|
29
|
+
return fstruct unless fstruct.respond_to?(:results)
|
30
|
+
return fstruct.results.first if fstruct.results.size.eql?(1)
|
31
|
+
fstruct.results
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LAA
|
4
|
+
module FeeCalculator
|
5
|
+
class Configuration
|
6
|
+
DEV_LAA_FEE_CALCULATOR_API_V1 = 'https://laa-fee-calculator-dev.apps.non-production.k8s.integration.dsd.io/api/v1'
|
7
|
+
HEADERS = { 'Accept' => 'application/json', 'User-Agent' => USER_AGENT }.freeze
|
8
|
+
|
9
|
+
attr_accessor :host, :headers
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@host ||= host || DEV_LAA_FEE_CALCULATOR_API_V1
|
13
|
+
@headers ||= headers || HEADERS
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'singleton'
|
4
|
+
require 'faraday'
|
5
|
+
|
6
|
+
module LAA
|
7
|
+
module FeeCalculator
|
8
|
+
class Connection
|
9
|
+
include Singleton
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
attr_reader :conn
|
13
|
+
def_delegators :conn, :port, :headers, :url_prefix, :options, :ssl, :get
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@conn = Faraday.new(url: LAA::FeeCalculator.configuration.host, headers: default_headers) do |conn|
|
17
|
+
conn.use LAA::FeeCalculator::RaiseError
|
18
|
+
conn.use Faraday::Adapter::NetHttp
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def host
|
24
|
+
LAA::FeeCalculator.configuration.host
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def host
|
29
|
+
self.class.host
|
30
|
+
end
|
31
|
+
|
32
|
+
def ping
|
33
|
+
get('/ping.json')
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def default_headers
|
39
|
+
LAA::FeeCalculator.configuration.headers
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LAA
|
4
|
+
module FeeCalculator
|
5
|
+
# see: laa/fee_calculator/raise_error
|
6
|
+
|
7
|
+
# raised for failed response status between 400 and 599 and not handled
|
8
|
+
# by classes below
|
9
|
+
class ClientError < Faraday::Error::ClientError
|
10
|
+
# overide the e.message attribute
|
11
|
+
# e.g. raise LAA::FeeCalculator::ResponseError.new(response)
|
12
|
+
# => #<LAA::FeeCalculator::ResponseError: "`case_date` should be in the format YYYY-MM-DD">
|
13
|
+
def to_s
|
14
|
+
response[:body]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# raised when API response is bad request: 400
|
19
|
+
# e.g. body ["`case_date` should be in the format YYYY-MM-DD"]
|
20
|
+
class ResponseError < ClientError
|
21
|
+
end
|
22
|
+
|
23
|
+
# raised when API response is not found: 404:
|
24
|
+
# e.g. body { "detail": "Not found." }
|
25
|
+
# TODO: consider raising for empty results i.e. results: [] ??
|
26
|
+
class ResourceNotFound < ResponseError
|
27
|
+
def to_s
|
28
|
+
h = JSON.parse(response[:body])
|
29
|
+
h.each_with_object(+'') do |(k, v), message|
|
30
|
+
message.concat("#{k} #{v}")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'laa/fee_calculator/has_manyable'
|
4
|
+
|
5
|
+
module LAA
|
6
|
+
module FeeCalculator
|
7
|
+
class FeeScheme < OpenStruct
|
8
|
+
include HasManyable
|
9
|
+
extend Forwardable
|
10
|
+
def_delegators :connection, :get
|
11
|
+
|
12
|
+
has_many :advocate_types
|
13
|
+
has_many :scenarios
|
14
|
+
has_many :offence_classes
|
15
|
+
has_many :fee_types
|
16
|
+
has_many :units
|
17
|
+
has_many :modifier_types
|
18
|
+
|
19
|
+
def calculate(**options)
|
20
|
+
yield options if block_given?
|
21
|
+
LAA::FeeCalculator::Calculator.new(id, options).call
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def connection
|
27
|
+
@connection ||= Connection.instance
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module LAA
|
4
|
+
module FeeCalculator
|
5
|
+
module HasManyable
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def has_many(association)
|
12
|
+
define_method("#{association}_uri".to_sym) do |scheme_pk = nil, id = nil|
|
13
|
+
uri = scheme_pk.nil? ? "#{association.to_s.tr('_', '-')}/" : "fee-schemes/#{scheme_pk}/#{association.to_s.tr('_', '-')}/"
|
14
|
+
uri = uri.concat(id.to_s, '/') unless id.nil?
|
15
|
+
Addressable::URI.parse(uri)
|
16
|
+
end
|
17
|
+
|
18
|
+
define_method(association) do |id = nil, **options|
|
19
|
+
id ||= options&.fetch(:id, nil)
|
20
|
+
uri = self.send("#{association}_uri", self.id, id)
|
21
|
+
uri.query_values = options.reject { |k, _v| k.eql?(:id) }
|
22
|
+
|
23
|
+
json = get(uri).body
|
24
|
+
|
25
|
+
ostruct = JSON.parse(json, object_class: OpenStruct)
|
26
|
+
return ostruct unless ostruct.respond_to?(:results)
|
27
|
+
ostruct.results
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require 'laa/fee_calculator/errors'
|
5
|
+
|
6
|
+
module LAA
|
7
|
+
module FeeCalculator
|
8
|
+
# Wrap Faraday adapter error handling with
|
9
|
+
# gem specific handlers.
|
10
|
+
#
|
11
|
+
class RaiseError < Faraday::Response::Middleware
|
12
|
+
CLIENT_ERROR_STATUSES = 400...600
|
13
|
+
|
14
|
+
def on_complete(env)
|
15
|
+
case env[:status]
|
16
|
+
when 400
|
17
|
+
raise LAA::FeeCalculator::ResponseError.new(response_values(env))
|
18
|
+
when 404
|
19
|
+
raise LAA::FeeCalculator::ResourceNotFound.new(response_values(env))
|
20
|
+
when CLIENT_ERROR_STATUSES
|
21
|
+
raise LAA::FeeCalculator::ClientError.new(response_values(env))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def response_values(env)
|
26
|
+
{ status: env.status, headers: env.response_headers, body: env.body }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: laa-fee-calculator-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel Sugarman
|
8
|
+
- Ministry of Justice
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-06-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: addressable
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '2.3'
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.7
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - "~>"
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '2.3'
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.3.7
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: faraday
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.9.2
|
41
|
+
type: :runtime
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.9.2
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: awesome_print
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bundler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.16'
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.16'
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: pry-byebug
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
- !ruby/object:Gem::Dependency
|
91
|
+
name: rake
|
92
|
+
requirement: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
type: :development
|
98
|
+
prerelease: false
|
99
|
+
version_requirements: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '10.0'
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: rspec
|
106
|
+
requirement: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
type: :development
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.0'
|
118
|
+
- !ruby/object:Gem::Dependency
|
119
|
+
name: rubocop
|
120
|
+
requirement: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.50'
|
125
|
+
type: :development
|
126
|
+
prerelease: false
|
127
|
+
version_requirements: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0.50'
|
132
|
+
- !ruby/object:Gem::Dependency
|
133
|
+
name: vcr
|
134
|
+
requirement: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
type: :development
|
140
|
+
prerelease: false
|
141
|
+
version_requirements: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
- !ruby/object:Gem::Dependency
|
147
|
+
name: webmock
|
148
|
+
requirement: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
type: :development
|
154
|
+
prerelease: false
|
155
|
+
version_requirements: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
description: Ruby client for the Ministry of Justices LAA fee calculator API. A simple
|
161
|
+
interface for transparent calling of the API endpoints to query data and return
|
162
|
+
calculated fee amounts.
|
163
|
+
email:
|
164
|
+
- joel.sugarman@digital.justice.gov.uk
|
165
|
+
- tools@digital.justice.gov.uk
|
166
|
+
executables: []
|
167
|
+
extensions: []
|
168
|
+
extra_rdoc_files:
|
169
|
+
- LICENSE
|
170
|
+
- README.md
|
171
|
+
files:
|
172
|
+
- Gemfile
|
173
|
+
- LICENSE
|
174
|
+
- README.md
|
175
|
+
- Rakefile
|
176
|
+
- bin/console
|
177
|
+
- bin/ruby_version_test
|
178
|
+
- bin/setup
|
179
|
+
- laa-fee-calculator-client.gemspec
|
180
|
+
- lib/laa/fee/calculator/client.rb
|
181
|
+
- lib/laa/fee_calculator.rb
|
182
|
+
- lib/laa/fee_calculator/calculator.rb
|
183
|
+
- lib/laa/fee_calculator/client.rb
|
184
|
+
- lib/laa/fee_calculator/configuration.rb
|
185
|
+
- lib/laa/fee_calculator/connection.rb
|
186
|
+
- lib/laa/fee_calculator/errors.rb
|
187
|
+
- lib/laa/fee_calculator/fee_scheme.rb
|
188
|
+
- lib/laa/fee_calculator/has_manyable.rb
|
189
|
+
- lib/laa/fee_calculator/raise_error.rb
|
190
|
+
- lib/laa/fee_calculator/version.rb
|
191
|
+
homepage: https://github.com/ministryofjustice/laa-fee-calculator-client
|
192
|
+
licenses:
|
193
|
+
- MIT
|
194
|
+
metadata: {}
|
195
|
+
post_install_message:
|
196
|
+
rdoc_options: []
|
197
|
+
require_paths:
|
198
|
+
- lib
|
199
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
200
|
+
requirements:
|
201
|
+
- - ">="
|
202
|
+
- !ruby/object:Gem::Version
|
203
|
+
version: 2.4.0
|
204
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
205
|
+
requirements:
|
206
|
+
- - ">="
|
207
|
+
- !ruby/object:Gem::Version
|
208
|
+
version: '0'
|
209
|
+
requirements: []
|
210
|
+
rubyforge_project:
|
211
|
+
rubygems_version: 2.7.6
|
212
|
+
signing_key:
|
213
|
+
specification_version: 4
|
214
|
+
summary: Ruby client for the Legal Aid Agency fee calculator API
|
215
|
+
test_files: []
|