monokera-sdk 0.27.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.
- checksums.yaml +7 -0
- data/.env.example +35 -0
- data/.github/dependabot.yml +44 -0
- data/.github/pull_request_template.md +15 -0
- data/.github/workflows/ci.yml +67 -0
- data/.github/workflows/lint.yml +28 -0
- data/.gitignore +18 -0
- data/.markdownlint.yaml +6 -0
- data/.rspec +1 -0
- data/.rubocop.yml +15 -0
- data/.yamllint.yml +6 -0
- data/.yardopts +6 -0
- data/CHANGELOG.md +166 -0
- data/CODEOWNERS +8 -0
- data/Gemfile +6 -0
- data/Guardfile +19 -0
- data/README.md +37 -0
- data/Rakefile +8 -0
- data/bin/bump +20 -0
- data/bin/console +12 -0
- data/bin/rspec +29 -0
- data/bin/setup +8 -0
- data/build_tools/changelog.rb +52 -0
- data/build_tools/spec/changelog_spec.rb +36 -0
- data/build_tools/spec/fixtures/CHANGELOG.md +11 -0
- data/docs/product_definition.md +48 -0
- data/lib/monokera/sdk.rb +18 -0
- data/lib/monokera/sdk/active_client.rb +44 -0
- data/lib/monokera/sdk/active_model.rb +150 -0
- data/lib/monokera/sdk/active_model_serializer.rb +11 -0
- data/lib/monokera/sdk/active_support_notifications.rb +27 -0
- data/lib/monokera/sdk/billing_service/client.rb +9 -0
- data/lib/monokera/sdk/billing_service/configuration.rb +13 -0
- data/lib/monokera/sdk/billing_service/models/installment_item.rb +17 -0
- data/lib/monokera/sdk/billing_service/models/plan_installment.rb +17 -0
- data/lib/monokera/sdk/cancellation_request.rb +18 -0
- data/lib/monokera/sdk/claim.rb +24 -0
- data/lib/monokera/sdk/claim_service/client.rb +9 -0
- data/lib/monokera/sdk/claim_service/configuration.rb +13 -0
- data/lib/monokera/sdk/claim_service/models/attendant.rb +14 -0
- data/lib/monokera/sdk/claim_service/models/setting.rb +17 -0
- data/lib/monokera/sdk/client_base.rb +113 -0
- data/lib/monokera/sdk/client_deprecation.rb +37 -0
- data/lib/monokera/sdk/client_service/client.rb +9 -0
- data/lib/monokera/sdk/client_service/configuration.rb +13 -0
- data/lib/monokera/sdk/config/gem_inflector.rb +17 -0
- data/lib/monokera/sdk/configuration.rb +57 -0
- data/lib/monokera/sdk/configurator.rb +15 -0
- data/lib/monokera/sdk/consolidated_pricing.rb +9 -0
- data/lib/monokera/sdk/coverage.rb +18 -0
- data/lib/monokera/sdk/credential.rb +18 -0
- data/lib/monokera/sdk/customer.rb +26 -0
- data/lib/monokera/sdk/customer_prospect.rb +24 -0
- data/lib/monokera/sdk/customer_service/client.rb +9 -0
- data/lib/monokera/sdk/customer_service/configuration.rb +13 -0
- data/lib/monokera/sdk/customer_service/models/address.rb +15 -0
- data/lib/monokera/sdk/document.rb +19 -0
- data/lib/monokera/sdk/document_service/client.rb +9 -0
- data/lib/monokera/sdk/document_service/configuration.rb +13 -0
- data/lib/monokera/sdk/endorsed_plan_preview.rb +9 -0
- data/lib/monokera/sdk/endorsement.rb +26 -0
- data/lib/monokera/sdk/error.rb +61 -0
- data/lib/monokera/sdk/good.rb +17 -0
- data/lib/monokera/sdk/hash_model.rb +29 -0
- data/lib/monokera/sdk/middleware/custom_logger_middleware.rb +89 -0
- data/lib/monokera/sdk/middleware/service_middleware.rb +25 -0
- data/lib/monokera/sdk/model.rb +49 -0
- data/lib/monokera/sdk/model/error.rb +14 -0
- data/lib/monokera/sdk/model/error_message.rb +14 -0
- data/lib/monokera/sdk/plan.rb +22 -0
- data/lib/monokera/sdk/policy.rb +32 -0
- data/lib/monokera/sdk/policy_service/client.rb +9 -0
- data/lib/monokera/sdk/policy_service/configuration.rb +13 -0
- data/lib/monokera/sdk/policy_service/models/address.rb +21 -0
- data/lib/monokera/sdk/policy_service/models/document.rb +15 -0
- data/lib/monokera/sdk/policy_service/models/endorsement_operation.rb +15 -0
- data/lib/monokera/sdk/policy_service/models/policy_holder.rb +22 -0
- data/lib/monokera/sdk/policy_service/models/pricing_history.rb +15 -0
- data/lib/monokera/sdk/policy_service/models/setting.rb +15 -0
- data/lib/monokera/sdk/pricing.rb +20 -0
- data/lib/monokera/sdk/pricing_service/client.rb +9 -0
- data/lib/monokera/sdk/pricing_service/configuration.rb +13 -0
- data/lib/monokera/sdk/pricing_service/models/setting.rb +15 -0
- data/lib/monokera/sdk/pricing_snapshot.rb +9 -0
- data/lib/monokera/sdk/product.rb +21 -0
- data/lib/monokera/sdk/product_definition/client.rb +9 -0
- data/lib/monokera/sdk/product_definition/configuration.rb +13 -0
- data/lib/monokera/sdk/product_definition/models/condition.rb +15 -0
- data/lib/monokera/sdk/product_definition/models/deductible.rb +14 -0
- data/lib/monokera/sdk/product_definition/models/limit.rb +16 -0
- data/lib/monokera/sdk/product_definition/models/rate.rb +15 -0
- data/lib/monokera/sdk/product_definition/models/setting.rb +20 -0
- data/lib/monokera/sdk/quote.rb +29 -0
- data/lib/monokera/sdk/quote_service/client.rb +9 -0
- data/lib/monokera/sdk/quote_service/configuration.rb +13 -0
- data/lib/monokera/sdk/quote_service/models/address.rb +21 -0
- data/lib/monokera/sdk/quote_service/models/customer_prospect.rb +23 -0
- data/lib/monokera/sdk/quote_service/models/setting.rb +15 -0
- data/lib/monokera/sdk/resource_not_found.rb +7 -0
- data/lib/monokera/sdk/validity.rb +18 -0
- data/lib/monokera/sdk/version.rb +7 -0
- data/monokera-sdk.gemspec +45 -0
- metadata +423 -0
data/Guardfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
|
4
|
+
require 'guard/rspec/dsl'
|
|
5
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
|
6
|
+
|
|
7
|
+
# RSpec files
|
|
8
|
+
rspec = dsl.rspec
|
|
9
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
|
10
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
|
11
|
+
watch(rspec.spec_files)
|
|
12
|
+
|
|
13
|
+
# Ruby files
|
|
14
|
+
ruby = dsl.ruby
|
|
15
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
|
16
|
+
|
|
17
|
+
watch(/^spec\/(.+)\/(.+)\/operations\/(.+)_example.rb$/) { |m| "spec/#{ m[1] }/#{ m[2] }/client_spec.rb" }
|
|
18
|
+
watch(/^lib\/(.+)\/(.+)\/operations\/(.+).rb$/) { |m| "spec/#{ m[1] }/#{ m[2] }/client_spec.rb" }
|
|
19
|
+
end
|
data/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Gem Monokera::SDK
|
|
2
|
+
|
|
3
|
+
## Resources available in this SDK
|
|
4
|
+
|
|
5
|
+
| Service Name | Service Class | API Version | Docs |
|
|
6
|
+
| ------------------------| ----------------------| ----------- |--------------------------------------|
|
|
7
|
+
| Document Service | Document | v1 | [Docs](docs/document_service.md) |
|
|
8
|
+
| Policy Service | Policy | v1 | [Docs](docs/policy_service.md) |
|
|
9
|
+
| Pricing Service | Pricing | v1 | TODO: add reference to documentation |
|
|
10
|
+
| Product Definition | ProductDefinition | v1 | [Docs](docs/product_definition.md) |
|
|
11
|
+
|
|
12
|
+
## Development
|
|
13
|
+
|
|
14
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
15
|
+
|
|
16
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
17
|
+
|
|
18
|
+
Include the changes description just below `[Unreleased]` title.
|
|
19
|
+
|
|
20
|
+
## Versioning
|
|
21
|
+
|
|
22
|
+
This project uses [semantic versioning](http://semver.org/spec/v2.0.0.html). You can safely
|
|
23
|
+
express a dependency on a major version and expect all minor and patch versions
|
|
24
|
+
to be backwards compatible.
|
|
25
|
+
|
|
26
|
+
## How to release a new version
|
|
27
|
+
|
|
28
|
+
**NOTE:** Avoid updating `CHANGELOG.md` and `version.rb` files manually to release the new version.
|
|
29
|
+
|
|
30
|
+
After squash and merge your changes to master, you need to follow these steps:
|
|
31
|
+
|
|
32
|
+
* Run `$ bin/bump minor`
|
|
33
|
+
* To release a **major** or a **patch** version you can pass `major` or `patch` argument to `bin/bump`
|
|
34
|
+
* Ex: `$ bin/bump patch` or `$ bin/bump major`
|
|
35
|
+
* Open a Pull Request named `Bump version to x.y.z` and tag it as `release`
|
|
36
|
+
|
|
37
|
+
If any error is found updating `CHANGELOG.md`, any change related with versioning is reverted.
|
data/Rakefile
ADDED
data/bin/bump
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
|
|
6
|
+
require 'gem/release/files/version'
|
|
7
|
+
require_relative '../build_tools/changelog'
|
|
8
|
+
|
|
9
|
+
target = ARGV.fetch(0, 'minor').to_sym
|
|
10
|
+
|
|
11
|
+
begin
|
|
12
|
+
release = Gem::Release::Files::Version.new 'monokera-sdk', target, {}
|
|
13
|
+
release.version = release.from.split('-').first if target == :promote
|
|
14
|
+
release.bump
|
|
15
|
+
|
|
16
|
+
Changelog.new(release).bump
|
|
17
|
+
rescue Changelog::CorruptedFileError => error
|
|
18
|
+
`git checkout lib/monokera/sdk/version.rb CHANGELOG.md`
|
|
19
|
+
puts error
|
|
20
|
+
end
|
data/bin/console
ADDED
data/bin/rspec
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
#
|
|
5
|
+
# This file was generated by Bundler.
|
|
6
|
+
#
|
|
7
|
+
# The application 'rspec' is installed as part of a gem, and
|
|
8
|
+
# this file is here to facilitate running it.
|
|
9
|
+
#
|
|
10
|
+
|
|
11
|
+
require "pathname"
|
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
|
13
|
+
Pathname.new(__FILE__).realpath)
|
|
14
|
+
|
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
|
16
|
+
|
|
17
|
+
if File.file?(bundle_binstub)
|
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
|
19
|
+
load(bundle_binstub)
|
|
20
|
+
else
|
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
require "rubygems"
|
|
27
|
+
require "bundler/setup"
|
|
28
|
+
|
|
29
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/setup
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'date'
|
|
4
|
+
|
|
5
|
+
class Changelog
|
|
6
|
+
class CorruptedFileError < StandardError; end
|
|
7
|
+
|
|
8
|
+
attr_reader :version, :body
|
|
9
|
+
|
|
10
|
+
def initialize(version, changelog_path = 'CHANGELOG.md')
|
|
11
|
+
@version = version
|
|
12
|
+
@changelog_path = changelog_path
|
|
13
|
+
@body = File.read(changelog_path)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def bump
|
|
17
|
+
raise CorruptedFileError unless valid_content?
|
|
18
|
+
|
|
19
|
+
update_title!
|
|
20
|
+
update_links_section!
|
|
21
|
+
|
|
22
|
+
File.write(@changelog_path, body)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def valid_content?
|
|
28
|
+
body.include?(compare_head_section) && body.include?(unreleased_title)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def update_title!
|
|
32
|
+
@body.gsub!(unreleased_title, "## [Unreleased]\n\n## [#{ version.to }] - #{ Date.today }")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def update_links_section!
|
|
36
|
+
@body.gsub!(compare_head_section, "v#{ version.to }...HEAD\n#{ new_version_diff_link }")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def new_version_diff_link
|
|
40
|
+
# rubocop:disable Layout/LineLength
|
|
41
|
+
"[#{ version.to }]: https://github.com/monokera-tech/monokera-sdk-ruby/compare/v#{ version.from }...v#{ version.to }"
|
|
42
|
+
# rubocop:enable Layout/LineLength
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def unreleased_title
|
|
46
|
+
'## [Unreleased]'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def compare_head_section
|
|
50
|
+
"v#{ version.from }...HEAD"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rspec'
|
|
4
|
+
require_relative '../changelog'
|
|
5
|
+
|
|
6
|
+
RSpec.describe Changelog do
|
|
7
|
+
subject { Changelog.new version, changelog_path }
|
|
8
|
+
|
|
9
|
+
let(:changelog_path) { './build_tools/spec/fixtures/CHANGELOG.md' }
|
|
10
|
+
let(:version) { double(to: '2.0.0', from: '1.0.0') }
|
|
11
|
+
|
|
12
|
+
before do
|
|
13
|
+
allow(Date).to receive(:today).and_return(Date.new(2018, 1, 23))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
describe '#bump' do
|
|
17
|
+
it 'creates a new version section with the Unreleased information' do
|
|
18
|
+
expect(File).to receive(:write)
|
|
19
|
+
.with(changelog_path, start_with("## [Unreleased]\n\n## [2.0.0] - 2018-01-23\n### [Added]\n- bump bin"))
|
|
20
|
+
|
|
21
|
+
subject.bump
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'creates new Unreleased in diff link section' do
|
|
25
|
+
expect(File).to receive(:write).with(changelog_path, include('[Unreleased]: https://github.com/monokera-tech/monokera-sdk-ruby/compare/v2.0.0...HEAD'))
|
|
26
|
+
|
|
27
|
+
subject.bump
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'creates new version link in diff link section' do
|
|
31
|
+
expect(File).to receive(:write).with(changelog_path, include('[2.0.0]: https://github.com/monokera-tech/monokera-sdk-ruby/compare/v1.0.0...v2.0.0'))
|
|
32
|
+
|
|
33
|
+
subject.bump
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# [Unreleased]
|
|
2
|
+
|
|
3
|
+
- Add new attribute "agreements" in models (Assistance and Service)
|
|
4
|
+
- bump bin
|
|
5
|
+
|
|
6
|
+
## [0.1.0] - 2020-04-22
|
|
7
|
+
|
|
8
|
+
- First release.
|
|
9
|
+
|
|
10
|
+
[Unreleased]: https://github.com/monokera-tech/monokera-sdk-ruby/compare/v0.1.0...HEAD
|
|
11
|
+
[0.1.0]: https://github.com/monokera-tech/monokera-sdk-ruby/compare/v0.0.0...v0.1.0
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Monokera::SDK - ProductDefinition
|
|
2
|
+
|
|
3
|
+
## Usage
|
|
4
|
+
|
|
5
|
+
```rb
|
|
6
|
+
require 'monokera/sdk/product'
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
It is recommended to create a file in the initializers of the configuration of each project with the name of monokera_sdk.rb to require the gem's own resources.
|
|
10
|
+
|
|
11
|
+
## Configuration
|
|
12
|
+
|
|
13
|
+
To use this client you should set the environment variables:
|
|
14
|
+
|
|
15
|
+
* `PRODUCT_DEFINITION_API_URL`
|
|
16
|
+
* `PRODUCT_DEFINITION_API_TIMEOUT` (optional, default 30)
|
|
17
|
+
|
|
18
|
+
## resource operations
|
|
19
|
+
|
|
20
|
+
### Monokera::SDK::Product.find(product_uid)
|
|
21
|
+
|
|
22
|
+
Get the input data of a product as long as its uid is supplied.
|
|
23
|
+
|
|
24
|
+
| Parameter | Type | Description |
|
|
25
|
+
|-----------------|--------|-------------------------------------------------------|
|
|
26
|
+
| uid | String | product uid ('col:product:basic') |
|
|
27
|
+
|
|
28
|
+
Example.
|
|
29
|
+
|
|
30
|
+
```rb
|
|
31
|
+
Monokera::SDK::Product.find('basic_product')
|
|
32
|
+
# => #<Monokera::SDK::Product:0x000055cef2cb4488 @name='hello', @goods=[#<ProductDefinition::Good:0x000055cef2c92ba8 @uid="col:good"...>
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Monokera::SDK::Product.all
|
|
36
|
+
|
|
37
|
+
Gets all products.
|
|
38
|
+
|
|
39
|
+
Example
|
|
40
|
+
|
|
41
|
+
```rb
|
|
42
|
+
Monokera::SDK::Product.all
|
|
43
|
+
# => [
|
|
44
|
+
#<Monokera::SDK::Product:0x000055cef2cb4488 @name='hello', @goods=[#<ProductDefinition::Good:0x000055cef2c92ba8 @uid="col:good"...>,
|
|
45
|
+
#<Monokera::SDK::Product:0x000055cef2cb4489 @name='product', @goods=[#<ProductDefinition::Good:0x000055cef2c92ba9 @uid="col:good_product"...>,
|
|
46
|
+
#<Monokera::SDK::Product:0x000055cef2cb4490 @name='product_2', @goods=[#<ProductDefinition::Good:0x000055cef2c92ba2 @uid="col:good_product_2"...>]
|
|
47
|
+
]
|
|
48
|
+
```
|
data/lib/monokera/sdk.rb
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
|
|
5
|
+
require 'zeitwerk'
|
|
6
|
+
require_relative 'sdk/config/gem_inflector'
|
|
7
|
+
require_relative 'sdk/active_support_notifications'
|
|
8
|
+
|
|
9
|
+
loader = Zeitwerk::Loader.new
|
|
10
|
+
loader.push_dir("#{ __dir__ }/..")
|
|
11
|
+
loader.tag = 'monokera-sdk'
|
|
12
|
+
loader.inflector = Monokera::SDK::Config::GemInflector.new
|
|
13
|
+
loader.setup
|
|
14
|
+
|
|
15
|
+
module Monokera
|
|
16
|
+
module SDK
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Monokera
|
|
4
|
+
module SDK
|
|
5
|
+
class ActiveClient < Monokera::SDK::ClientBase
|
|
6
|
+
def self.for(service)
|
|
7
|
+
new(configuration: Monokera::SDK::Configurator.new(service))
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def initialize(configuration: nil)
|
|
11
|
+
@configuration = configuration
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def get(url, query_options = {})
|
|
15
|
+
http_response = connection.get(url, query_options, client_headers)
|
|
16
|
+
process_response(http_response)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def post(url, params, headers = {})
|
|
20
|
+
http_response = connection.post(url, params, client_headers.merge(headers))
|
|
21
|
+
process_response(http_response)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def put(url, params)
|
|
25
|
+
http_response = connection.put(url, params, client_headers)
|
|
26
|
+
process_response(http_response)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def destroy(url)
|
|
30
|
+
http_response = connection.delete(url, nil, client_headers)
|
|
31
|
+
process_response(http_response)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def client_headers
|
|
37
|
+
{
|
|
38
|
+
timezone: Time.zone.name,
|
|
39
|
+
tenant_name: Apartment::Tenant.current
|
|
40
|
+
}
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Monokera
|
|
4
|
+
module SDK
|
|
5
|
+
class ActiveModel < Model
|
|
6
|
+
def update(attrs = {})
|
|
7
|
+
url_params = { country_code: attributes[:country_code] }.merge(attrs)
|
|
8
|
+
response = self.class.client.put(self.class.resource_endpoint(id, url_params), attrs)
|
|
9
|
+
if response.success?
|
|
10
|
+
self.attributes = response.body
|
|
11
|
+
true
|
|
12
|
+
else
|
|
13
|
+
self.class.raven_context(response)
|
|
14
|
+
false
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def update!(attrs)
|
|
19
|
+
update(attrs) || raise(ClientError, errors)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def save!
|
|
23
|
+
if id
|
|
24
|
+
update!(as_json)
|
|
25
|
+
else
|
|
26
|
+
self.class.create!(as_json)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def save
|
|
31
|
+
create_or_update
|
|
32
|
+
rescue ClientError
|
|
33
|
+
false
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def destroy
|
|
37
|
+
response = self.class.client.destroy(self.class.resource_endpoint(id))
|
|
38
|
+
|
|
39
|
+
if response.success?
|
|
40
|
+
true
|
|
41
|
+
else
|
|
42
|
+
self.class.raven_context(response)
|
|
43
|
+
false
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def new_record?
|
|
48
|
+
id.blank?
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def persisted?
|
|
52
|
+
!new_record?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def create_or_update
|
|
58
|
+
if persisted?
|
|
59
|
+
update(attributes)
|
|
60
|
+
else
|
|
61
|
+
resource = self.class.create(as_json)
|
|
62
|
+
self.attributes = resource.attributes
|
|
63
|
+
resource.persisted?
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
class << self
|
|
68
|
+
def all(query_options = {})
|
|
69
|
+
response = client.get(collection_endpoint(query_options), query_options)
|
|
70
|
+
|
|
71
|
+
if response.success?
|
|
72
|
+
build_data(response.body)
|
|
73
|
+
else
|
|
74
|
+
raven_context(response)
|
|
75
|
+
raise ClientError, response.body[:errors]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def find(id, params = {})
|
|
80
|
+
response = client.get(resource_endpoint(id, params), params)
|
|
81
|
+
|
|
82
|
+
if response.success?
|
|
83
|
+
build(response.body)
|
|
84
|
+
else
|
|
85
|
+
raven_context(response)
|
|
86
|
+
raise ResourceNotFound, response.body[:errors]
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def create(attrs, headers = {})
|
|
91
|
+
response = client.post(collection_endpoint(attrs), attrs, headers)
|
|
92
|
+
|
|
93
|
+
if response.success?
|
|
94
|
+
build(response.body)
|
|
95
|
+
else
|
|
96
|
+
new(response.body[:errors])
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def create!(attrs, headers = {})
|
|
101
|
+
resource = create(attrs, headers)
|
|
102
|
+
|
|
103
|
+
if resource.persisted?
|
|
104
|
+
resource
|
|
105
|
+
else
|
|
106
|
+
raven_context(resource)
|
|
107
|
+
raise ClientError, resource.errors
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def collection_endpoint(options = {})
|
|
112
|
+
self::BASE_ENDPOINT % options.to_hash.symbolize_keys
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def resource_endpoint(id, options = {})
|
|
116
|
+
endpoint = options.present? ? collection_endpoint(options) : self::BASE_ENDPOINT
|
|
117
|
+
[endpoint, id].join('/')
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def build(body)
|
|
121
|
+
new(body)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def build_data(body)
|
|
125
|
+
if const_defined?(:ADAPTER) && self::ADAPTER == :json_api
|
|
126
|
+
body[:data].map { |resource_body| new(serialize_keys_json_api(resource_body[:attributes])) }
|
|
127
|
+
else
|
|
128
|
+
body.map { |resource_body| new(resource_body) }
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def serialize_keys_json_api(data)
|
|
133
|
+
data.deep_transform_keys! { |key| key.to_s.underscore }
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def raven_context(response)
|
|
137
|
+
Raven.extra_context(sdk_response: response.to_hash) if defined?(Raven)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
private
|
|
141
|
+
|
|
142
|
+
def contains_pagination_headers?(headers)
|
|
143
|
+
headers.include?('x-total-pages')
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
class ClientError < StandardError; end
|
|
149
|
+
end
|
|
150
|
+
end
|