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.
Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/.env.example +35 -0
  3. data/.github/dependabot.yml +44 -0
  4. data/.github/pull_request_template.md +15 -0
  5. data/.github/workflows/ci.yml +67 -0
  6. data/.github/workflows/lint.yml +28 -0
  7. data/.gitignore +18 -0
  8. data/.markdownlint.yaml +6 -0
  9. data/.rspec +1 -0
  10. data/.rubocop.yml +15 -0
  11. data/.yamllint.yml +6 -0
  12. data/.yardopts +6 -0
  13. data/CHANGELOG.md +166 -0
  14. data/CODEOWNERS +8 -0
  15. data/Gemfile +6 -0
  16. data/Guardfile +19 -0
  17. data/README.md +37 -0
  18. data/Rakefile +8 -0
  19. data/bin/bump +20 -0
  20. data/bin/console +12 -0
  21. data/bin/rspec +29 -0
  22. data/bin/setup +8 -0
  23. data/build_tools/changelog.rb +52 -0
  24. data/build_tools/spec/changelog_spec.rb +36 -0
  25. data/build_tools/spec/fixtures/CHANGELOG.md +11 -0
  26. data/docs/product_definition.md +48 -0
  27. data/lib/monokera/sdk.rb +18 -0
  28. data/lib/monokera/sdk/active_client.rb +44 -0
  29. data/lib/monokera/sdk/active_model.rb +150 -0
  30. data/lib/monokera/sdk/active_model_serializer.rb +11 -0
  31. data/lib/monokera/sdk/active_support_notifications.rb +27 -0
  32. data/lib/monokera/sdk/billing_service/client.rb +9 -0
  33. data/lib/monokera/sdk/billing_service/configuration.rb +13 -0
  34. data/lib/monokera/sdk/billing_service/models/installment_item.rb +17 -0
  35. data/lib/monokera/sdk/billing_service/models/plan_installment.rb +17 -0
  36. data/lib/monokera/sdk/cancellation_request.rb +18 -0
  37. data/lib/monokera/sdk/claim.rb +24 -0
  38. data/lib/monokera/sdk/claim_service/client.rb +9 -0
  39. data/lib/monokera/sdk/claim_service/configuration.rb +13 -0
  40. data/lib/monokera/sdk/claim_service/models/attendant.rb +14 -0
  41. data/lib/monokera/sdk/claim_service/models/setting.rb +17 -0
  42. data/lib/monokera/sdk/client_base.rb +113 -0
  43. data/lib/monokera/sdk/client_deprecation.rb +37 -0
  44. data/lib/monokera/sdk/client_service/client.rb +9 -0
  45. data/lib/monokera/sdk/client_service/configuration.rb +13 -0
  46. data/lib/monokera/sdk/config/gem_inflector.rb +17 -0
  47. data/lib/monokera/sdk/configuration.rb +57 -0
  48. data/lib/monokera/sdk/configurator.rb +15 -0
  49. data/lib/monokera/sdk/consolidated_pricing.rb +9 -0
  50. data/lib/monokera/sdk/coverage.rb +18 -0
  51. data/lib/monokera/sdk/credential.rb +18 -0
  52. data/lib/monokera/sdk/customer.rb +26 -0
  53. data/lib/monokera/sdk/customer_prospect.rb +24 -0
  54. data/lib/monokera/sdk/customer_service/client.rb +9 -0
  55. data/lib/monokera/sdk/customer_service/configuration.rb +13 -0
  56. data/lib/monokera/sdk/customer_service/models/address.rb +15 -0
  57. data/lib/monokera/sdk/document.rb +19 -0
  58. data/lib/monokera/sdk/document_service/client.rb +9 -0
  59. data/lib/monokera/sdk/document_service/configuration.rb +13 -0
  60. data/lib/monokera/sdk/endorsed_plan_preview.rb +9 -0
  61. data/lib/monokera/sdk/endorsement.rb +26 -0
  62. data/lib/monokera/sdk/error.rb +61 -0
  63. data/lib/monokera/sdk/good.rb +17 -0
  64. data/lib/monokera/sdk/hash_model.rb +29 -0
  65. data/lib/monokera/sdk/middleware/custom_logger_middleware.rb +89 -0
  66. data/lib/monokera/sdk/middleware/service_middleware.rb +25 -0
  67. data/lib/monokera/sdk/model.rb +49 -0
  68. data/lib/monokera/sdk/model/error.rb +14 -0
  69. data/lib/monokera/sdk/model/error_message.rb +14 -0
  70. data/lib/monokera/sdk/plan.rb +22 -0
  71. data/lib/monokera/sdk/policy.rb +32 -0
  72. data/lib/monokera/sdk/policy_service/client.rb +9 -0
  73. data/lib/monokera/sdk/policy_service/configuration.rb +13 -0
  74. data/lib/monokera/sdk/policy_service/models/address.rb +21 -0
  75. data/lib/monokera/sdk/policy_service/models/document.rb +15 -0
  76. data/lib/monokera/sdk/policy_service/models/endorsement_operation.rb +15 -0
  77. data/lib/monokera/sdk/policy_service/models/policy_holder.rb +22 -0
  78. data/lib/monokera/sdk/policy_service/models/pricing_history.rb +15 -0
  79. data/lib/monokera/sdk/policy_service/models/setting.rb +15 -0
  80. data/lib/monokera/sdk/pricing.rb +20 -0
  81. data/lib/monokera/sdk/pricing_service/client.rb +9 -0
  82. data/lib/monokera/sdk/pricing_service/configuration.rb +13 -0
  83. data/lib/monokera/sdk/pricing_service/models/setting.rb +15 -0
  84. data/lib/monokera/sdk/pricing_snapshot.rb +9 -0
  85. data/lib/monokera/sdk/product.rb +21 -0
  86. data/lib/monokera/sdk/product_definition/client.rb +9 -0
  87. data/lib/monokera/sdk/product_definition/configuration.rb +13 -0
  88. data/lib/monokera/sdk/product_definition/models/condition.rb +15 -0
  89. data/lib/monokera/sdk/product_definition/models/deductible.rb +14 -0
  90. data/lib/monokera/sdk/product_definition/models/limit.rb +16 -0
  91. data/lib/monokera/sdk/product_definition/models/rate.rb +15 -0
  92. data/lib/monokera/sdk/product_definition/models/setting.rb +20 -0
  93. data/lib/monokera/sdk/quote.rb +29 -0
  94. data/lib/monokera/sdk/quote_service/client.rb +9 -0
  95. data/lib/monokera/sdk/quote_service/configuration.rb +13 -0
  96. data/lib/monokera/sdk/quote_service/models/address.rb +21 -0
  97. data/lib/monokera/sdk/quote_service/models/customer_prospect.rb +23 -0
  98. data/lib/monokera/sdk/quote_service/models/setting.rb +15 -0
  99. data/lib/monokera/sdk/resource_not_found.rb +7 -0
  100. data/lib/monokera/sdk/validity.rb +18 -0
  101. data/lib/monokera/sdk/version.rb +7 -0
  102. data/monokera-sdk.gemspec +45 -0
  103. metadata +423 -0
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module ActiveModelSerializer
6
+ def read_attribute_for_serialization(name)
7
+ attributes[name]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require 'statsd-instrument'
5
+
6
+ ActiveSupport::Notifications.subscribe 'monokera-sdk.request.faraday' do |*args|
7
+ event = ActiveSupport::Notifications::Event.new(*args)
8
+
9
+ http_status = case event.payload[:exception_object]
10
+ when Faraday::TimeoutError then 'timeout'
11
+ when Faraday::ConnectionFailed then 'connection-failed'
12
+ when Faraday::ClientError then 'client-error'
13
+ when Faraday::ResourceNotFound then 'not-found'
14
+ else event.payload[:status]
15
+ end
16
+
17
+ service_name = event.payload[:service_name].to_s.downcase
18
+ origin_service_name = event.payload[:origin_service_name].to_s.downcase
19
+ http_method = event.payload[:method].to_s.upcase
20
+ request_host = event.payload[:url].host
21
+ request_path = event.payload[:url].path
22
+
23
+ tags = "type=rest,method=#{http_method},status=#{http_status},host=#{request_host},path=#{request_path},origin_service=#{origin_service_name},service=#{service_name}"
24
+
25
+ StatsD.increment("external-service.requested,#{tags}")
26
+ StatsD.measure("external-service.response-time,#{tags}", event.duration) if event.duration
27
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module BillingService
6
+ class Client < Monokera::SDK::ActiveClient; end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module BillingService
6
+ class Configuration < ::Monokera::SDK::Configuration
7
+ def service_name
8
+ 'billing-service'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module BillingService
6
+ module Models
7
+ class InstallmentItem < Monokera::SDK::Model
8
+ attribute :description
9
+ attribute :item_type
10
+ attribute :good_uid
11
+ attribute :coverage_uid
12
+ attribute :amount
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module BillingService
6
+ module Models
7
+ class PlanInstallment < Monokera::SDK::Model
8
+ attribute :amount
9
+ attribute :number
10
+ attribute :status
11
+ attribute :due_at
12
+ attribute :items, Array[BillingService::Models::InstallmentItem]
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ class CancellationRequest < Monokera::SDK::ActiveModel
6
+ attribute :protocol_number
7
+ attribute :motivation
8
+ attribute :status
9
+ attribute :additional_info
10
+
11
+ BASE_ENDPOINT = '/v1/policies/%{policy_number}/cancellations'
12
+
13
+ def self.client
14
+ @client ||= PolicyService::Client.new
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ class Claim < Monokera::SDK::ActiveModel
6
+ attribute :number, String
7
+ attribute :policy_number, String
8
+ attribute :description, String
9
+ attribute :status, String
10
+ attribute :occurred_at, DateTime
11
+ attribute :total_amount, Float
12
+ attribute :report_at, DateTime
13
+
14
+ attribute :attendant, ClaimService::Models::Attendant
15
+ attribute :settings, Array[ClaimService::Models::Setting]
16
+
17
+ BASE_ENDPOINT = '/v1/claims'
18
+
19
+ def self.client
20
+ @client ||= ClaimService::Client.new
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module ClaimService
6
+ class Client < Monokera::SDK::ActiveClient; end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module ClaimService
6
+ class Configuration < ::Monokera::SDK::Configuration
7
+ def service_name
8
+ 'claim-service'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module ClaimService
6
+ module Models
7
+ class Attendant < Monokera::SDK::Model
8
+ attribute :name, String
9
+ attribute :company, String
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module ClaimService
6
+ module Models
7
+ class Setting < Monokera::SDK::Model
8
+ attribute :coverage_uid, String
9
+ attribute :pretension_amount, Float
10
+ attribute :branch_uid, String
11
+ attribute :good_uid, String
12
+ attribute :amount, Float
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext/hash/keys'
5
+ require 'active_support/core_ext/numeric'
6
+ require 'active_support/core_ext/object/blank'
7
+ require 'faraday'
8
+ require 'faraday/request_id'
9
+ require 'faraday_middleware'
10
+ require 'monokera/sdk/middleware/custom_logger_middleware'
11
+
12
+ module Monokera
13
+ module SDK
14
+ class ClientBase
15
+ def configuration
16
+ @configuration ||= self.class.name.deconstantize.gsub(/::V\d$/, '').constantize::Configuration.instance
17
+ end
18
+
19
+ def connection_headers
20
+ {}
21
+ end
22
+
23
+ def filter_parameters
24
+ @filter_parameters ||= default_filter_parameters.dup
25
+ end
26
+
27
+ def connection
28
+ @connection ||= Faraday.new(configuration.host) do |builder|
29
+ builder.request :json
30
+ builder.response :json, content_type: /\bjson$/
31
+ builder.response :custom_logger, Configuration.instance.logger, service_name: configuration.service_name, filter_parameters: filter_parameters
32
+ builder.options[:timeout] = configuration.timeout.to_i
33
+ builder.options[:open_timeout] = configuration.open_timeout.to_i
34
+ builder.use :instrumentation
35
+ builder.use Faraday::RequestId
36
+ builder.use Monokera::SDK::Error::Middleware if raise_error?
37
+ builder.use Monokera::SDK::Middleware::ServiceMiddleware, service_name: configuration.service_name
38
+ builder.adapter Faraday.default_adapter
39
+ builder.headers.merge! connection_headers
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def raise_error?
46
+ false
47
+ end
48
+
49
+ def cached_request(cache_key, expires_in)
50
+ able_to_cache = cache_key && expires_in && defined?(Rails)
51
+ return yield unless able_to_cache
52
+
53
+ cached_result = Rails.cache.read(cache_key)
54
+
55
+ cached_result || yield.tap do |result|
56
+ Rails.cache.write(cache_key, result, expires_in: expires_in) if result.success?
57
+ end
58
+ end
59
+
60
+ def handle_error(errors, status, content_type, service_name)
61
+ if defined?(Rails)
62
+ Rails.logger.warn "RESPONSE STATUS: #{status}"
63
+ Rails.logger.warn "CLIENT NAME: #{service_name}"
64
+ Rails.logger.warn errors
65
+ end
66
+
67
+ OpenStruct.new(success?: false, status: status, body: { errors: errors }, content_type: content_type, client: configuration.service_name.upcase)
68
+ end
69
+
70
+ def process_response(response)
71
+ content_type = content_type_for(response)
72
+ result = process_body(response.body, content_type)
73
+
74
+ if response.success?
75
+ OpenStruct.new(success?: true, status: response.status, headers: response.headers, content_type: content_type, body: result)
76
+ else
77
+ handle_error(result, response.status, content_type, configuration.service_name)
78
+ end
79
+ end
80
+
81
+ def process_body(body, content_type)
82
+ return {} if body.blank?
83
+
84
+ if content_type == :json
85
+ body.is_a?(Array) ? body.map(&:deep_symbolize_keys) : body.deep_symbolize_keys
86
+ else
87
+ body
88
+ end
89
+ end
90
+
91
+ def content_type_for(response)
92
+ case response.headers['content-type']
93
+ when /json/i
94
+ :json
95
+ when /xml/i
96
+ :xml
97
+ when /html/i
98
+ :html
99
+ else
100
+ :plain_text
101
+ end
102
+ end
103
+
104
+ def default_filter_parameters
105
+ if defined?(Rails) && Rails.respond_to?(:application)
106
+ Rails.application.config.filter_parameters
107
+ else
108
+ []
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module ClientDeprecation
6
+ def self.included(klass)
7
+ raise NoMethodError, "#{klass.name} does not have configuration method" unless klass.instance_methods.include?(:configuration)
8
+
9
+ deprecated_initialize!(klass)
10
+
11
+ deprecated_methods = klass.public_instance_methods - Class.instance_methods - [:configuration]
12
+
13
+ deprecated_methods.each do |method_name|
14
+ deprecated_method!(method_name, klass)
15
+ end
16
+ end
17
+
18
+ def self.deprecated_initialize!(klass)
19
+ klass.send(:define_method, :initialize) do |*args|
20
+ super(*args)
21
+
22
+ configuration.logger.warn "[DEPRECATED] `#{klass.name}` is deprecated, please use a versioned client"
23
+ end
24
+ end
25
+
26
+ def self.deprecated_method!(method_name, klass)
27
+ klass.send :alias_method, "deprecated_#{method_name}", method_name
28
+
29
+ klass.send :define_method, method_name do |*arguments, &block|
30
+ configuration.logger.warn "[DEPRECATED] Using `#{method_name}` from `#{klass.name}`. Please migrate to a versioned client"
31
+
32
+ send "deprecated_#{method_name}", *arguments, &block
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module ClientService
6
+ class Client < Monokera::SDK::ActiveClient; end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module ClientService
6
+ class Configuration < ::Monokera::SDK::Configuration
7
+ def service_name
8
+ 'client-service'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monokera
4
+ module SDK
5
+ module Config
6
+ class GemInflector < Zeitwerk::Inflector
7
+ CAMELIZED_NAMES = {
8
+ sdk: 'SDK'
9
+ }.freeze
10
+
11
+ def camelize(basename, abspath)
12
+ CAMELIZED_NAMES[basename.to_sym] || super
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ module Monokera
6
+ module SDK
7
+ class Configuration
8
+ include Singleton
9
+
10
+ attr_writer :host, :token, :timeout, :open_timeout, :cache_duration
11
+
12
+ def service_name
13
+ raise('Service\'s name not defined')
14
+ end
15
+
16
+ def host
17
+ @host ||= ENV.fetch("#{service_name_configuration_prefix}_API_URL")
18
+ end
19
+
20
+ def token
21
+ @token ||= ENV.fetch("#{service_name_configuration_prefix}_API_TOKEN")
22
+ end
23
+
24
+ def timeout
25
+ @timeout ||= ENV.fetch("#{service_name_configuration_prefix}_API_TIMEOUT", 10).to_i
26
+ end
27
+
28
+ def open_timeout
29
+ @open_timeout ||= ENV.fetch("#{service_name_configuration_prefix}_API_OPEN_TIMEOUT", timeout).to_i
30
+ end
31
+
32
+ def cache_duration
33
+ @cache_duration ||= ENV.fetch("#{service_name_configuration_prefix}_CACHE_DURATION", 5).to_i.minutes
34
+ end
35
+
36
+ def logger
37
+ @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
38
+ end
39
+
40
+ def logger=(logger)
41
+ logger = Logger.new(logger) if logger.is_a?(String)
42
+ @logger = logger
43
+ end
44
+
45
+ def self.configure(&block)
46
+ yield(instance) if block.present?
47
+ instance
48
+ end
49
+
50
+ private
51
+
52
+ def service_name_configuration_prefix
53
+ service_name.underscore.upcase
54
+ end
55
+ end
56
+ end
57
+ end