didww-v3 6.2.1 → 7.0.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 (105) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/tests.yml +8 -6
  3. data/.rubocop.yml +0 -110
  4. data/CHANGELOG.md +61 -0
  5. data/FILTERS.md +36 -12
  6. data/Gemfile +0 -2
  7. data/README.md +427 -56
  8. data/didww-v3.gemspec +11 -8
  9. data/docs/resource_relationships.md +35 -15
  10. data/examples/README.md +54 -61
  11. data/examples/capacity_pools.rb +28 -10
  12. data/examples/countries.rb +1 -1
  13. data/examples/did_groups.rb +1 -1
  14. data/examples/did_reservations.rb +35 -37
  15. data/examples/did_sms_routing.rb +91 -0
  16. data/examples/did_trunk_assignment.rb +106 -80
  17. data/examples/emergency_calling_services.rb +3 -3
  18. data/examples/emergency_requirements.rb +3 -3
  19. data/examples/emergency_scenario.rb +11 -11
  20. data/examples/emergency_verifications.rb +7 -5
  21. data/examples/exports.rb +1 -1
  22. data/examples/exports_sms.rb +72 -0
  23. data/examples/identity_address_proofs.rb +118 -0
  24. data/examples/includes.rb +65 -0
  25. data/examples/orders.rb +8 -6
  26. data/examples/orders_all_item_types.rb +63 -125
  27. data/examples/orders_emergency.rb +3 -3
  28. data/examples/{orders_by_sku.rb → orders_sku.rb} +12 -10
  29. data/examples/prices_download.rb +45 -0
  30. data/examples/prices_lookup.rb +43 -0
  31. data/examples/regions.rb +1 -1
  32. data/examples/sample.pdf +14 -0
  33. data/examples/sender_id_verifications.rb +63 -0
  34. data/examples/shared_capacity_groups.rb +44 -35
  35. data/examples/sms_trunk_groups.rb +71 -0
  36. data/examples/sms_trunks.rb +84 -0
  37. data/examples/test_readme.rb +1216 -0
  38. data/examples/trunks.rb +115 -0
  39. data/examples/upload_file.rb +44 -0
  40. data/examples/voice_in_trunk_groups.rb +57 -33
  41. data/examples/voice_in_trunk_sip_registration.rb +4 -1
  42. data/examples/voice_out_trunks.rb +11 -1
  43. data/examples/webhook.rb +51 -0
  44. data/lib/didww/auth/bearer_middleware.rb +74 -0
  45. data/lib/didww/auth/memory_store.rb +40 -0
  46. data/lib/didww/auth/token.rb +45 -0
  47. data/lib/didww/auth/token_provider.rb +215 -0
  48. data/lib/didww/base_middleware.rb +7 -8
  49. data/lib/didww/callback/request_validator.rb +2 -1
  50. data/lib/didww/client.rb +122 -13
  51. data/lib/didww/complex_objects/authentication_methods/generic.rb +6 -0
  52. data/lib/didww/complex_objects/configurations/base.rb +0 -18
  53. data/lib/didww/complex_objects/configurations/pstn_configuration.rb +0 -4
  54. data/lib/didww/complex_objects/configurations/sip_configuration.rb +86 -38
  55. data/lib/didww/complex_objects/emergency_order_item.rb +1 -1
  56. data/lib/didww/complex_objects/export_filters.rb +72 -6
  57. data/lib/didww/complex_objects/generic_order_item.rb +15 -0
  58. data/lib/didww/complex_objects/sms_trunk_configuration.rb +33 -0
  59. data/lib/didww/complex_objects/sms_trunk_configurations/base.rb +26 -0
  60. data/lib/didww/complex_objects/sms_trunk_configurations/generic.rb +32 -0
  61. data/lib/didww/complex_objects/sms_trunk_configurations/http_in.rb +75 -0
  62. data/lib/didww/complex_objects/sms_trunk_configurations/http_out.rb +35 -0
  63. data/lib/didww/complex_objects/sms_trunk_configurations/smpp_esme.rb +37 -0
  64. data/lib/didww/complex_objects/sms_trunk_configurations/smpp_smsc.rb +56 -0
  65. data/lib/didww/complex_objects/sms_trunk_configurations/sms_to_email.rb +27 -0
  66. data/lib/didww/errors.rb +103 -0
  67. data/lib/didww/http.rb +34 -0
  68. data/lib/didww/prices/lookup_result.rb +37 -0
  69. data/lib/didww/prices/price.rb +56 -0
  70. data/lib/didww/prices/price_file.rb +51 -0
  71. data/lib/didww/prices/service.rb +248 -0
  72. data/lib/didww/prices/sms_in_price.rb +43 -0
  73. data/lib/didww/prices/sms_out_price.rb +52 -0
  74. data/lib/didww/prices/voice_in_price.rb +48 -0
  75. data/lib/didww/prices/voice_out_price.rb +88 -0
  76. data/lib/didww/prices.rb +82 -0
  77. data/lib/didww/resource/address_requirement.rb +13 -3
  78. data/lib/didww/resource/base.rb +5 -2
  79. data/lib/didww/resource/concerns/number_types.rb +24 -0
  80. data/lib/didww/resource/did.rb +42 -3
  81. data/lib/didww/resource/did_group.rb +12 -1
  82. data/lib/didww/resource/emergency_calling_service.rb +10 -5
  83. data/lib/didww/resource/emergency_requirement.rb +27 -14
  84. data/lib/didww/resource/encrypted_file.rb +19 -6
  85. data/lib/didww/resource/export.rb +59 -12
  86. data/lib/didww/resource/identity.rb +9 -4
  87. data/lib/didww/resource/order.rb +1 -0
  88. data/lib/didww/resource/proof_type.rb +1 -1
  89. data/lib/didww/resource/sender_id_verification.rb +114 -0
  90. data/lib/didww/resource/sms_trunk.rb +76 -0
  91. data/lib/didww/resource/sms_trunk_group.rb +31 -0
  92. data/lib/didww/resource/sms_trunk_regenerate_credential.rb +16 -0
  93. data/lib/didww/resource/voice_in_trunk.rb +24 -1
  94. data/lib/didww/resource/voice_out_trunk.rb +72 -0
  95. data/lib/didww/types/ip_addresses.rb +1 -1
  96. data/lib/didww/types/strings.rb +1 -1
  97. data/lib/didww/types.rb +3 -1
  98. data/lib/didww/version.rb +1 -1
  99. data/lib/didww.rb +7 -0
  100. metadata +62 -24
  101. data/examples/identities_and_proofs.rb +0 -67
  102. data/examples/voice_in_trunks.rb +0 -108
  103. data/lib/didww/resource/did_group_type.rb +0 -10
  104. data/lib/didww/resource/exclusive_relationship.rb +0 -54
  105. data/lib/didww/resource/pop.rb +0 -10
@@ -2,7 +2,7 @@
2
2
  module DIDWW
3
3
  module ComplexObject
4
4
  class EmergencyOrderItem < Base
5
- # passed at order creation
5
+ # returned; the API creates emergency orders, POST /orders does not accept these items
6
6
  property :qty, type: :int
7
7
  property :emergency_calling_service_id, type: :string
8
8
 
@@ -4,20 +4,86 @@ require 'json_api_client/schema'
4
4
  module DIDWW
5
5
  module ComplexObject
6
6
  class ExportFilters < Base
7
+ # Allowed values for trunk_type (sms_in only)
8
+ TRUNK_TYPE_SMPP_SMSC = 'smpp_smsc'
9
+ TRUNK_TYPE_SMPP_ESME = 'smpp_esme'
10
+ TRUNK_TYPE_SMS_TO_EMAIL = 'sms_to_email'
11
+ TRUNK_TYPE_HTTP_IN = 'http_in'
12
+
13
+ TRUNK_TYPES = [
14
+ TRUNK_TYPE_SMPP_SMSC,
15
+ TRUNK_TYPE_SMPP_ESME,
16
+ TRUNK_TYPE_SMS_TO_EMAIL,
17
+ TRUNK_TYPE_HTTP_IN
18
+ ].freeze
19
+
20
+ # Allowed values for service_type (sms_out only)
21
+ SERVICE_TYPE_A2P = 'a2p'
22
+ SERVICE_TYPE_P2P = 'p2p'
23
+
24
+ SERVICE_TYPES = [
25
+ SERVICE_TYPE_A2P,
26
+ SERVICE_TYPE_P2P
27
+ ].freeze
28
+
29
+ # Ruby-friendly property names for the dotted keys on the wire
30
+ DOTTED_KEYS = {
31
+ 'voice_out_trunk_id' => 'voice_out_trunk.id',
32
+ 'sms_trunk_id' => 'sms_trunk.id',
33
+ 'sender_id_verification_id' => 'sender_id_verification.id'
34
+ }.freeze
35
+
7
36
  # Type casting for JsonApiClient parser/setters
8
37
  def self.cast_single_object(hash)
9
38
  new(hash)
10
39
  end
11
40
 
12
- property :from, type: :string # ISO 8601 / "YYYY-MM-DD HH:MM:SS" lower bound, INCLUSIVE (time_start >= from). cdr_in / cdr_out only.
13
- property :to, type: :string # ISO 8601 / "YYYY-MM-DD HH:MM:SS" upper bound, EXCLUSIVE (time_start < to). cdr_in / cdr_out only.
14
- property :did_number, type: :string # only for CDR in
15
- property :voice_out_trunk_id, type: :string # only for CDR Out
41
+ # Every export type
42
+ property :from, type: :string # "YYYY-MM-DD HH:MM:SS" UTC lower bound, INCLUSIVE (time_start >= from). Required.
43
+ property :to, type: :string # "YYYY-MM-DD HH:MM:SS" UTC upper bound, EXCLUSIVE (time_start < to). Required.
44
+
45
+ # cdr_in
46
+ property :did_number, type: :string
47
+ property :source_number, type: :string
48
+ property :source_number_out, type: :string
49
+ property :source_country_iso, type: :string
50
+
51
+ # cdr_out
52
+ property :voice_out_trunk_id, type: :string # sent as voice_out_trunk.id
53
+ property :source, type: :string
54
+ property :cli, type: :string
55
+ property :destination, type: :string
56
+
57
+ # sms_in and sms_out
58
+ property :source_address_eq, type: :string
59
+ property :source_address_cont, type: :string
60
+ property :destination_address_eq, type: :string
61
+ property :destination_address_cont, type: :string
62
+ # sms_in: success, failed, queued; sms_out: routing_error, in_progress, queued, failed, success
63
+ property :status, type: :string
64
+ property :sms_trunk_id # String or Array of Strings, sent as sms_trunk.id
65
+ property :sms_id, type: :string
66
+
67
+ # sms_in only
68
+ property :trunk_type # String or Array of Strings from TRUNK_TYPES
69
+
70
+ # sms_out only
71
+ property :service_type, type: :string # one of SERVICE_TYPES, lowercase
72
+ property :sender_id_verification_id # String or Array of Strings, sent as sender_id_verification.id
73
+
74
+ # Returned on exports created at earlier API versions only
75
+ property :year, type: :string, read_only: true
76
+ property :month, type: :string, read_only: true
77
+ property :day, type: :string, read_only: true
78
+
79
+ def initialize(params = {})
80
+ super(params.to_h.transform_keys { |key| DOTTED_KEYS.key(key.to_s) || key })
81
+ end
16
82
 
17
83
  def as_json(*)
18
84
  result = attributes.as_json.with_indifferent_access
19
- result[:'voice_out_trunk.id'] = result.delete(:voice_out_trunk_id) if result.key?(:voice_out_trunk_id)
20
- result
85
+ DOTTED_KEYS.each { |name, wire_key| result[wire_key] = result.delete(name) if result.key?(name) }
86
+ result.except(*self.class.read_only_attributes)
21
87
  end
22
88
 
23
89
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ module DIDWW
3
+ module ComplexObject
4
+ # An order item for any other charge, such as a Sender ID verification.
5
+ # It is returned only; orders are not created with it.
6
+ class GenericOrderItem < Base
7
+ property :qty, type: :int
8
+ property :nrc, type: :decimal
9
+ property :mrc, type: :decimal
10
+ property :prorated_mrc, type: :boolean
11
+ property :billed_from, type: :string
12
+ property :billed_to, type: :string
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+ require 'didww/complex_objects/sms_trunk_configurations/base'
3
+ require 'didww/complex_objects/sms_trunk_configurations/generic'
4
+ require 'didww/complex_objects/sms_trunk_configurations/sms_to_email'
5
+ require 'didww/complex_objects/sms_trunk_configurations/http_in'
6
+ require 'didww/complex_objects/sms_trunk_configurations/http_out'
7
+ require 'didww/complex_objects/sms_trunk_configurations/smpp_esme'
8
+ require 'didww/complex_objects/sms_trunk_configurations/smpp_smsc'
9
+
10
+ module DIDWW
11
+ module ComplexObject
12
+ module SmsTrunkConfiguration
13
+ TYPE_CLASSES = [
14
+ SmsToEmail,
15
+ HttpIn,
16
+ HttpOut,
17
+ SmppEsme,
18
+ SmppSmsc
19
+ ].to_h { |klass| [klass.type, klass] }.freeze
20
+
21
+ TYPES = TYPE_CLASSES.keys.freeze
22
+
23
+ # Production accepts nyc and ams; the sandbox accepts nyc only.
24
+ SERVER_LOCATION_NYC = 'nyc'
25
+ SERVER_LOCATION_AMS = 'ams'
26
+
27
+ SERVER_LOCATIONS = [
28
+ SERVER_LOCATION_NYC,
29
+ SERVER_LOCATION_AMS
30
+ ].freeze
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DIDWW
4
+ module ComplexObject
5
+ module SmsTrunkConfiguration
6
+ class Base < ComplexObject::Base
7
+ class << self
8
+ protected
9
+
10
+ # Resolves the subclass from the explicit TYPE_CLASSES map: the wire types
11
+ # (sms_to_email, http_in, ...) are not pluralized class names. An unknown
12
+ # type is wrapped in Generic so it round-trips through PATCH.
13
+ def cast_single_object(hash)
14
+ hash = hash.with_indifferent_access
15
+ klass = SmsTrunkConfiguration::TYPE_CLASSES[hash[:type]]
16
+ if klass
17
+ klass.new(hash[:attributes] || {})
18
+ else
19
+ Generic.new(hash[:attributes] || {}, hash[:type])
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DIDWW
4
+ module ComplexObject
5
+ module SmsTrunkConfiguration
6
+ # Fallback for a configuration type this gem version does not know yet.
7
+ # Keeps the type and attributes as received.
8
+ class Generic < Base
9
+ attr_reader :stored_type
10
+
11
+ def initialize(params = {}, stored_type = nil)
12
+ @stored_type = stored_type
13
+ params.each { |k, v| attributes[k] = v }
14
+ end
15
+
16
+ def type
17
+ stored_type || self.class.type
18
+ end
19
+
20
+ def as_json(*)
21
+ { type: type, attributes: attributes.as_json }.with_indifferent_access
22
+ end
23
+
24
+ # Every value is redacted: which attributes of an unknown type carry credentials is not known.
25
+ def inspect
26
+ formatted = attributes.map { |k, v| "#{k}=#{(v.nil? ? v : FILTERED).inspect}" }
27
+ "#<#{self.class.name} type=#{type.inspect} #{formatted.join(' ')}>"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DIDWW
4
+ module ComplexObject
5
+ module SmsTrunkConfiguration
6
+ # Inbound messages are delivered as an HTTP request. Query parameters, headers
7
+ # and body may use the placeholders {SMS_TEXT}, {SMS_TEXT_BASE64_ENCODED},
8
+ # {SMS_SRC_ADDR}, {SMS_DST_ADDR} and {SMS_TIME}.
9
+ class HttpIn < Base
10
+ HTTP_METHOD_GET = 'get'
11
+ HTTP_METHOD_POST = 'post'
12
+ HTTP_METHOD_PUT = 'put'
13
+
14
+ HTTP_METHODS = [
15
+ HTTP_METHOD_GET,
16
+ HTTP_METHOD_POST,
17
+ HTTP_METHOD_PUT
18
+ ].freeze
19
+
20
+ BODY_TYPE_JSON = 'json'
21
+ BODY_TYPE_HTML_MULTIPART = 'html_multipart'
22
+ BODY_TYPE_HTML_URL_ENCODED = 'html_url_encoded'
23
+ BODY_TYPE_RAW = 'raw'
24
+
25
+ BODY_TYPES = [
26
+ BODY_TYPE_JSON,
27
+ BODY_TYPE_HTML_MULTIPART,
28
+ BODY_TYPE_HTML_URL_ENCODED,
29
+ BODY_TYPE_RAW
30
+ ].freeze
31
+
32
+ def self.type
33
+ 'http_in'
34
+ end
35
+
36
+ property :url, type: :string
37
+ # Type: String
38
+ # Description: Endpoint the request is sent to. Required when the trunk is created.
39
+
40
+ property :http_method, type: :string
41
+ # Type: String
42
+ # Description: One of HTTP_METHODS. Required when the trunk is created.
43
+
44
+ property :query_parameters
45
+ # Type: Hash
46
+ # Nullable: Yes
47
+ # Description: Query parameters added to the request
48
+
49
+ property :headers, sensitive: true
50
+ # Type: Hash
51
+ # Nullable: Yes
52
+ # Description: Headers added to the request. Redacted in #inspect, as they may carry credentials.
53
+
54
+ property :body_type, type: :string
55
+ # Type: String
56
+ # Nullable: Yes
57
+ # Description: One of BODY_TYPES. Required for post and put, refused for get.
58
+
59
+ property :body
60
+ # Type: String or Hash
61
+ # Nullable: Yes
62
+ # Description: Request body template: a JSON string for json, a Hash for the form
63
+ # encodings, a String for raw. Required for post and put, refused for get.
64
+
65
+ property :follow_redirect, type: :boolean
66
+ # Type: Boolean
67
+ # Description: Follow 3xx responses
68
+
69
+ property :validate_https_certificate, type: :boolean
70
+ # Type: Boolean
71
+ # Description: Reject an untrusted TLS certificate
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DIDWW
4
+ module ComplexObject
5
+ module SmsTrunkConfiguration
6
+ # Outbound messages are submitted to DIDWW over HTTP. The submit URL is
7
+ # returned in the trunk's meta as host.
8
+ class HttpOut < Base
9
+ def self.type
10
+ 'http_out'
11
+ end
12
+
13
+ property :callback_url, type: :string
14
+ # Type: String
15
+ # Nullable: Yes
16
+ # Description: URL the status events of submitted messages are sent to
17
+
18
+ property :allowed_ips, type: :strings
19
+ # Type: Array of Strings
20
+ # Nullable: Yes
21
+ # Description: Addresses messages are accepted from
22
+
23
+ property :auth_name, type: :string, read_only: true, sensitive: true
24
+ # Type: String
25
+ # Nullable: Yes
26
+ # Description: Generated HTTP username
27
+
28
+ property :auth_password, type: :string, read_only: true, sensitive: true
29
+ # Type: String
30
+ # Nullable: Yes
31
+ # Description: Generated HTTP password
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DIDWW
4
+ module ComplexObject
5
+ module SmsTrunkConfiguration
6
+ # The customer connects to DIDWW as an SMPP ESME.
7
+ class SmppEsme < Base
8
+ def self.type
9
+ 'smpp_esme'
10
+ end
11
+
12
+ property :system_type, type: :string
13
+ # Type: String
14
+ # Nullable: Yes
15
+ # Description: SMPP system_type, up to 12 characters
16
+
17
+ property :allowed_ips, type: :strings
18
+ # Type: Array of Strings
19
+ # Nullable: Yes
20
+ # Description: Addresses the ESME may bind from
21
+
22
+ property :server_location, type: :string
23
+ # Type: String
24
+ # Description: DIDWW SMPP server location, one of SERVER_LOCATIONS. Required when
25
+ # the trunk is created.
26
+
27
+ property :system_id, type: :string, read_only: true, sensitive: true
28
+ # Type: String
29
+ # Description: Generated SMPP system_id to bind with
30
+
31
+ property :password, type: :string, read_only: true, sensitive: true
32
+ # Type: String
33
+ # Description: Generated SMPP bind password
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DIDWW
4
+ module ComplexObject
5
+ module SmsTrunkConfiguration
6
+ # DIDWW connects to the customer as an SMPP client.
7
+ # host, system_id, password and server_location are required when the trunk is created.
8
+ class SmppSmsc < Base
9
+ def self.type
10
+ 'smpp_smsc'
11
+ end
12
+
13
+ property :host, type: :string
14
+ # Type: String
15
+ # Description: SMSC host DIDWW binds to
16
+
17
+ property :tx_port, type: :integer
18
+ # Type: Integer
19
+ # Description: Transmitter port. Defaults to 2775.
20
+
21
+ property :rx_port, type: :integer
22
+ # Type: Integer
23
+ # Description: Receiver port. Defaults to 2776.
24
+
25
+ property :transceiver_mode, type: :boolean
26
+ # Type: Boolean
27
+ # Description: Bind as a transceiver instead of separate tx/rx
28
+
29
+ property :connection_timeout, type: :integer
30
+ # Type: Integer
31
+ # Description: Bind timeout, seconds
32
+
33
+ property :use_ssl, type: :boolean
34
+ # Type: Boolean
35
+ # Description: Wrap the connection in TLS
36
+
37
+ property :system_id, type: :string
38
+ # Type: String
39
+ # Description: SMPP system_id DIDWW binds with, up to 15 characters
40
+
41
+ property :system_type, type: :string
42
+ # Type: String
43
+ # Nullable: Yes
44
+ # Description: SMPP system_type, up to 12 characters
45
+
46
+ property :password, type: :string, sensitive: true
47
+ # Type: String
48
+ # Description: SMPP bind password, up to 8 characters
49
+
50
+ property :server_location, type: :string
51
+ # Type: String
52
+ # Description: DIDWW SMPP server location, one of SERVER_LOCATIONS
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DIDWW
4
+ module ComplexObject
5
+ module SmsTrunkConfiguration
6
+ # Inbound messages are forwarded as email.
7
+ # send_to, subject and message are required when the trunk is created.
8
+ class SmsToEmail < Base
9
+ def self.type
10
+ 'sms_to_email'
11
+ end
12
+
13
+ property :send_to, type: :string
14
+ # Type: String
15
+ # Description: Recipient address
16
+
17
+ property :subject, type: :string
18
+ # Type: String
19
+ # Description: Subject template
20
+
21
+ property :message, type: :string
22
+ # Type: String
23
+ # Description: Body template
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'json_api_client'
4
+
5
+ module DIDWW
6
+ class Error < StandardError
7
+ end
8
+
9
+ # @api private
10
+ module RetryAfter
11
+ # @param value [String,nil] the Retry-After header
12
+ # @return [Integer,nil] the delay in seconds, nil when the header is missing or is an HTTP date
13
+ def self.seconds(value)
14
+ value.to_i if value&.match?(/\A\d+\z/)
15
+ end
16
+ end
17
+
18
+ # A 429 from the API, raised by resource calls and Export#download. It is not retried.
19
+ class TooManyRequests < JsonApiClient::Errors::TooManyRequests
20
+ STATUS_HANDLERS = { 429 => ->(env) { raise DIDWW::TooManyRequests, env } }.freeze
21
+
22
+ # @return [Integer,nil] seconds to wait before the next request, from Retry-After
23
+ def retry_after
24
+ RetryAfter.seconds(env.response_headers['Retry-After']) if env
25
+ end
26
+ end
27
+
28
+ module Auth
29
+ class Error < DIDWW::Error
30
+ attr_reader :status, :error, :error_description, :retry_after
31
+
32
+ def initialize(message = nil, status: nil, error: nil, error_description: nil, retry_after: nil)
33
+ @status = status
34
+ @error = error
35
+ @error_description = error_description
36
+ @retry_after = retry_after
37
+ super(message || [error, error_description].compact.join(': ').presence || "Token service responded with status #{status}")
38
+ end
39
+ end
40
+
41
+ class InvalidClient < Auth::Error
42
+ end
43
+
44
+ class InvalidRequest < Auth::Error
45
+ end
46
+
47
+ class RateLimited < Auth::Error
48
+ end
49
+
50
+ class ServerError < Auth::Error
51
+ end
52
+
53
+ class ConnectionError < Auth::Error
54
+ end
55
+
56
+ class InvalidGrant < Auth::Error
57
+ end
58
+ end
59
+
60
+ module Prices
61
+ class Error < DIDWW::Error
62
+ attr_reader :status, :code, :errors, :retry_after
63
+
64
+ def initialize(message = nil, status: nil, code: nil, errors: [], retry_after: nil)
65
+ @status = status
66
+ @code = code
67
+ @errors = errors
68
+ @retry_after = retry_after
69
+ super(message || errors.map { |e| e['detail'] || e['title'] }.compact.join('; ').presence || "Prices service responded with status #{status}")
70
+ end
71
+ end
72
+
73
+ class BadRequest < Prices::Error
74
+ end
75
+
76
+ class NotAuthorized < Prices::Error
77
+ end
78
+
79
+ class AccessDenied < Prices::Error
80
+ end
81
+
82
+ class PricePlanNotAssigned < Prices::Error
83
+ end
84
+
85
+ class PriceNotFound < Prices::Error
86
+ end
87
+
88
+ class NotAcceptable < Prices::Error
89
+ end
90
+
91
+ class TooManyRequests < Prices::Error
92
+ end
93
+
94
+ class ServiceUnavailable < Prices::Error
95
+ end
96
+
97
+ class ConnectionError < Prices::Error
98
+ end
99
+
100
+ class UnexpectedStatus < Prices::Error
101
+ end
102
+ end
103
+ end
data/lib/didww/http.rb ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ require 'faraday'
3
+ require 'didww/version'
4
+
5
+ module DIDWW
6
+ module Http
7
+ USER_AGENT = "didww-v3 Ruby gem v#{DIDWW::VERSION}"
8
+ OPEN_TIMEOUT = 10
9
+ TIMEOUT = 60
10
+ FILTERED_HEADERS = /^(Api-Key|Authorization): .*$/i
11
+
12
+ # The block receives the connection before the bearer middleware is added,
13
+ # so middleware registered there wraps the authenticated request.
14
+ # @param url [String]
15
+ # @param audience [String,nil] token audience; nil sends no Authorization header
16
+ # @param timeout [Integer] read timeout in seconds
17
+ # @return [Faraday::Connection]
18
+ def self.connection(url, audience: nil, timeout: TIMEOUT)
19
+ Faraday.new(url: url, headers: { 'User-Agent' => USER_AGENT }) do |connection|
20
+ connection.options.open_timeout = OPEN_TIMEOUT
21
+ connection.options.timeout = timeout
22
+ if DIDWW::Client.http_verbose?
23
+ connection.response :logger, nil, headers: true do |formatter|
24
+ formatter.filter(FILTERED_HEADERS, '\1: [FILTERED]')
25
+ end
26
+ end
27
+ yield(connection) if block_given?
28
+ connection.use DIDWW::Auth::BearerMiddleware, audience: audience if audience
29
+ connection.adapter Faraday.default_adapter
30
+ DIDWW::Client._customize_faraday_block&.call(connection)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DIDWW
4
+ module Prices
5
+ class LookupResult
6
+ include Enumerable
7
+
8
+ attr_reader :total_records
9
+
10
+ # @param document [Hash] parsed JSON:API collection document
11
+ # @param price_class [Class] DIDWW::Prices::Price subclass of the rows
12
+ # @return [DIDWW::Prices::LookupResult]
13
+ def self.from_json_api(document, price_class)
14
+ new(
15
+ document.fetch('data').map { |data| price_class.from_json_api(data) },
16
+ total_records: document.dig('meta', 'total_records')
17
+ )
18
+ end
19
+
20
+ def initialize(prices, total_records:)
21
+ @prices = prices
22
+ @total_records = total_records
23
+ end
24
+
25
+ def each(&block)
26
+ return enum_for(:each) unless block_given?
27
+
28
+ @prices.each(&block)
29
+ self
30
+ end
31
+
32
+ def size
33
+ @prices.size
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+ require 'bigdecimal'
3
+ require 'time'
4
+
5
+ module DIDWW
6
+ module Prices
7
+ # Every lookup answers JSON:API type "prices", so the row class is chosen by endpoint.
8
+ class Price
9
+ class << self
10
+ def attributes
11
+ @attributes ||= {}
12
+ end
13
+
14
+ # @param data [Hash] JSON:API resource object
15
+ # @return [DIDWW::Prices::Price]
16
+ def from_json_api(data)
17
+ new(id: data['id'], attributes: data.fetch('attributes', {}))
18
+ end
19
+
20
+ private
21
+
22
+ def attribute(name, type = :string)
23
+ attributes[name] = type
24
+ attr_reader name
25
+ end
26
+ end
27
+
28
+ attr_reader :id
29
+
30
+ def initialize(id: nil, attributes: {})
31
+ @id = id
32
+ values = attributes.transform_keys(&:to_sym)
33
+ self.class.attributes.each { |name, type| instance_variable_set("@#{name}", coerce(values[name], type)) }
34
+ end
35
+
36
+ # @return [Hash]
37
+ def to_h
38
+ self.class.attributes.keys.each_with_object({ id: id }) { |name, hash| hash[name] = public_send(name) }
39
+ end
40
+
41
+ private
42
+
43
+ def coerce(value, type)
44
+ if value.nil?
45
+ nil
46
+ elsif type == :decimal
47
+ BigDecimal(value.to_s)
48
+ elsif type == :time
49
+ Time.iso8601(value)
50
+ else
51
+ value
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end