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
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+ require 'time'
3
+
4
+ module DIDWW
5
+ module Prices
6
+ class PriceFile
7
+ STATUS_OK = :ok
8
+ STATUS_NOT_MODIFIED = :not_modified
9
+ FILENAME_PATTERN = /filename="?([^";]+)"?/
10
+
11
+ attr_reader :status, :last_modified, :content_length, :filename, :bytes_written
12
+
13
+ # @param response [Faraday::Response] a 200 or 304 answer of GET or HEAD
14
+ # @param bytes_written [Integer,nil] bytes of the file written by a download
15
+ # @return [DIDWW::Prices::PriceFile]
16
+ def self.from_response(response, bytes_written: nil)
17
+ headers = response.headers
18
+ new(
19
+ status: response.status == 304 ? STATUS_NOT_MODIFIED : STATUS_OK,
20
+ last_modified: parse_http_date(headers['Last-Modified']),
21
+ content_length: headers['Content-Length']&.to_i,
22
+ filename: headers['Content-Disposition'].to_s[FILENAME_PATTERN, 1],
23
+ bytes_written: bytes_written
24
+ )
25
+ end
26
+
27
+ def self.parse_http_date(value)
28
+ Time.httpdate(value) if value
29
+ rescue ArgumentError
30
+ nil
31
+ end
32
+
33
+ def initialize(status:, last_modified: nil, content_length: nil, filename: nil, bytes_written: nil)
34
+ @status = status
35
+ @last_modified = last_modified
36
+ @content_length = content_length
37
+ @filename = filename
38
+ @bytes_written = bytes_written
39
+ end
40
+
41
+ # The file changed after the If-Modified-Since date sent, or none was sent.
42
+ def modified?
43
+ status == STATUS_OK
44
+ end
45
+
46
+ def not_modified?
47
+ status == STATUS_NOT_MODIFIED
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,248 @@
1
+ # frozen_string_literal: true
2
+ require 'fileutils'
3
+ require 'json'
4
+ require 'pathname'
5
+ require 'securerandom'
6
+ require 'active_support/core_ext/object/blank'
7
+ require 'didww/errors'
8
+ require 'didww/http'
9
+ require 'didww/prices/lookup_result'
10
+ require 'didww/prices/price_file'
11
+
12
+ module DIDWW
13
+ module Prices
14
+ class Service
15
+ JSON_API_MEDIA_TYPE = 'application/vnd.api+json'
16
+ FILE_PATH = 'prices.csv.gz'
17
+ LOOKUP_PATH = 'lookup/prices'
18
+ PRICE_PLAN_NOT_ASSIGNED = 'price_plan_not_assigned'
19
+ TRANSPORT_ERRORS = [
20
+ Faraday::ConnectionFailed,
21
+ Faraday::TimeoutError,
22
+ Faraday::SSLError
23
+ ].freeze
24
+ MAX_UNAVAILABLE_RETRIES = 2
25
+ MAX_RETRY_AFTER = 60
26
+ RETRY_SPREAD_RATIO = 0.2
27
+ MIN_RETRY_SPREAD = 1
28
+ ERROR_CLASSES = {
29
+ 400 => BadRequest,
30
+ 401 => NotAuthorized,
31
+ 403 => AccessDenied,
32
+ 406 => NotAcceptable,
33
+ 429 => TooManyRequests,
34
+ 503 => ServiceUnavailable
35
+ }.freeze
36
+ NOT_FOUND_ERROR_CLASSES = {
37
+ 'price_not_found' => PriceNotFound,
38
+ PRICE_PLAN_NOT_ASSIGNED => PricePlanNotAssigned
39
+ }.freeze
40
+
41
+ # Streams a 200 body into the IO and keeps any other body for the error.
42
+ # A failed write is kept too: the adapter reports it as a connection failure.
43
+ class Sink
44
+ attr_reader :bytes_written, :error_body, :write_error
45
+
46
+ def initialize(io)
47
+ @io = io
48
+ @bytes_written = 0
49
+ @error_body = +''
50
+ end
51
+
52
+ def to_proc
53
+ proc do |chunk, received, env|
54
+ if env.status == 200
55
+ write(chunk)
56
+ else
57
+ @error_body.clear if received == chunk.bytesize
58
+ @error_body << chunk
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def write(chunk)
66
+ @bytes_written += @io.write(chunk)
67
+ rescue StandardError => e
68
+ @write_error = e
69
+ raise
70
+ end
71
+ end
72
+ private_constant :Sink
73
+
74
+ attr_reader :host, :path_prefix, :price_class, :lookup_filters
75
+
76
+ # @param host [Symbol] :voice_in, :voice_out or :sms
77
+ # @param path_prefix [String] price domain path below /v3/, such as 'sms_out/a2p/'
78
+ # @param price_class [Class] DIDWW::Prices::Price subclass of the lookup rows
79
+ # @param lookup_filters [Hash] filter names under :required, :optional and :any_of
80
+ # @param sleeper [#call] waits the Retry-After of a 503 on a price file, plus a random spread
81
+ def initialize(host:, path_prefix:, price_class:, lookup_filters:, sleeper: ->(seconds) { sleep(seconds) })
82
+ @host = host
83
+ @path_prefix = path_prefix
84
+ @price_class = price_class
85
+ @lookup_filters = lookup_filters
86
+ @sleeper = sleeper
87
+ end
88
+
89
+ # @param filters [Hash] lookup filters, such as number: '18005550123'
90
+ # @return [DIDWW::Prices::LookupResult]
91
+ # @raise [ArgumentError] a filter is missing or not allowed
92
+ # @raise [DIDWW::Prices::Error]
93
+ def lookup(**filters)
94
+ filters = filters.transform_keys(&:to_s).reject { |_name, value| value.blank? }.transform_values(&:to_s)
95
+ validate_filters!(filters)
96
+ response = transport! { connection.get(lookup_path, { 'filter' => filters }, 'Accept' => JSON_API_MEDIA_TYPE) }
97
+ raise_error!(response, response.body) unless response.status == 200
98
+
99
+ LookupResult.from_json_api(JSON.parse(response.body), price_class)
100
+ end
101
+
102
+ # @param if_modified_since [Time,String,nil] Last-Modified of the copy held
103
+ # @return [DIDWW::Prices::PriceFile]
104
+ # @raise [DIDWW::Prices::Error]
105
+ def head(if_modified_since: nil)
106
+ PriceFile.from_response(file_request!(:head, if_modified_since))
107
+ end
108
+
109
+ # Writes the gzipped file unchanged. A path is written through a temp file next to it and
110
+ # replaced only on 200, so a 304 or an error leaves an existing file as it was. The new
111
+ # file gets the Last-Modified of the response as its modification time.
112
+ # @param to [IO,String,Pathname] binary IO or file path
113
+ # @param if_modified_since [Time,String,nil] Last-Modified of the copy held
114
+ # @return [DIDWW::Prices::PriceFile]
115
+ # @raise [DIDWW::Prices::Error]
116
+ def download(to:, if_modified_since: nil)
117
+ if to.is_a?(String) || to.is_a?(Pathname)
118
+ download_to_path!(to.to_s, if_modified_since)
119
+ else
120
+ download_to_io!(to, if_modified_since)
121
+ end
122
+ end
123
+
124
+ # Downloads the file to path unless the copy there is current: the modification time of
125
+ # the file at path is sent as If-Modified-Since.
126
+ # @param path [String,Pathname]
127
+ # @return [DIDWW::Prices::PriceFile]
128
+ # @raise [DIDWW::Prices::Error]
129
+ def fetch(path)
130
+ path = path.to_s
131
+ if_modified_since = File.mtime(path) if File.exist?(path)
132
+ download_to_path!(path, if_modified_since)
133
+ end
134
+
135
+ def file_path
136
+ "#{path_prefix}#{FILE_PATH}"
137
+ end
138
+
139
+ def lookup_path
140
+ "#{path_prefix}#{LOOKUP_PATH}"
141
+ end
142
+
143
+ private
144
+
145
+ def connection
146
+ DIDWW::Http.connection(
147
+ "#{DIDWW::Client.prices_base_url(host).chomp('/')}/",
148
+ audience: DIDWW::Client.prices_audience(host)
149
+ )
150
+ end
151
+
152
+ def validate_filters!(filters)
153
+ allowed = lookup_filters.values.flatten
154
+ unknown = filters.keys - allowed
155
+ missing = lookup_filters.fetch(:required, []) - filters.keys
156
+ any_of = lookup_filters.fetch(:any_of, [])
157
+ if unknown.any?
158
+ raise ArgumentError.new("Filter #{unknown.join(', ')} is not allowed on #{lookup_path} (allowed: #{allowed.join(', ')}).")
159
+ elsif missing.any?
160
+ raise ArgumentError.new("Filter #{missing.join(', ')} is required on #{lookup_path}.")
161
+ elsif any_of.any? && (any_of & filters.keys).empty?
162
+ raise ArgumentError.new("One of filters #{any_of.join(', ')} is required on #{lookup_path}.")
163
+ end
164
+ end
165
+
166
+ def download_to_io!(io, if_modified_since)
167
+ sink = Sink.new(io)
168
+ PriceFile.from_response(file_request!(:get, if_modified_since, sink), bytes_written: sink.bytes_written)
169
+ end
170
+
171
+ def download_to_path!(path, if_modified_since)
172
+ tmp_path = File.join(File.dirname(path), ".didww-#{SecureRandom.hex(8)}.tmp")
173
+ price_file = File.open(tmp_path, 'wbx') do |file|
174
+ download_to_io!(file, if_modified_since).tap { |downloaded| file.fsync if downloaded.modified? }
175
+ end
176
+ if price_file.modified?
177
+ File.utime(price_file.last_modified, price_file.last_modified, tmp_path) if price_file.last_modified
178
+ File.rename(tmp_path, path)
179
+ end
180
+ price_file
181
+ ensure
182
+ FileUtils.rm_f(tmp_path) if tmp_path
183
+ end
184
+
185
+ def file_request!(method, if_modified_since, sink = nil, attempt = 1)
186
+ response = transport!(sink) do
187
+ connection.run_request(method, file_path, nil, file_headers(if_modified_since)) do |request|
188
+ request.options.on_data = sink.to_proc if sink
189
+ end
190
+ end
191
+ retry_after = retry_after(response)
192
+ if [200, 304].include?(response.status)
193
+ response
194
+ elsif response.status == 503 && attempt <= MAX_UNAVAILABLE_RETRIES && retry_after && retry_after <= MAX_RETRY_AFTER
195
+ @sleeper.call(retry_delay(retry_after))
196
+ file_request!(method, if_modified_since, sink, attempt + 1)
197
+ else
198
+ raise_error!(response, sink ? sink.error_body : response.body)
199
+ end
200
+ end
201
+
202
+ def transport!(sink = nil)
203
+ yield
204
+ rescue *TRANSPORT_ERRORS => e
205
+ raise sink.write_error if sink&.write_error
206
+
207
+ raise ConnectionError.new(e.message)
208
+ end
209
+
210
+ def file_headers(if_modified_since)
211
+ if if_modified_since
212
+ { 'If-Modified-Since' => if_modified_since.respond_to?(:httpdate) ? if_modified_since.httpdate : if_modified_since.to_s }
213
+ else
214
+ {}
215
+ end
216
+ end
217
+
218
+ def raise_error!(response, body)
219
+ errors = parse_errors(body)
220
+ code = errors.first&.fetch('code', nil)
221
+ if response.status == 404
222
+ code ||= PRICE_PLAN_NOT_ASSIGNED
223
+ error_class = NOT_FOUND_ERROR_CLASSES.fetch(code, PricePlanNotAssigned)
224
+ else
225
+ error_class = ERROR_CLASSES.fetch(response.status, UnexpectedStatus)
226
+ end
227
+ raise error_class.new(status: response.status, code: code, errors: errors, retry_after: retry_after(response))
228
+ end
229
+
230
+ def parse_errors(body)
231
+ document = JSON.parse(body.to_s)
232
+ document.is_a?(Hash) ? Array(document['errors']).grep(Hash) : []
233
+ rescue JSON::ParserError
234
+ []
235
+ end
236
+
237
+ def retry_after(response)
238
+ RetryAfter.seconds(response.headers['Retry-After'])
239
+ end
240
+
241
+ # Spreads the retries of clients that got the same Retry-After.
242
+ def retry_delay(retry_after)
243
+ seconds = [retry_after, 1].max
244
+ seconds + (rand * [MIN_RETRY_SPREAD, RETRY_SPREAD_RATIO * seconds].max)
245
+ end
246
+ end
247
+ end
248
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ require 'didww/prices/price'
3
+
4
+ module DIDWW
5
+ module Prices
6
+ class SmsInPrice < Price
7
+ attribute :source_prefix
8
+ # Type: String
9
+ # Nullable: true
10
+ # Description: Prefix of the sending number; null for any number
11
+
12
+ attribute :source_number_length_min, :integer
13
+ # Type: Integer
14
+ # Description: Shortest sending number, in digits
15
+
16
+ attribute :source_number_length_max, :integer
17
+ # Type: Integer
18
+ # Description: Longest sending number, in digits
19
+
20
+ attribute :destination_prefix
21
+ # Type: String
22
+ # Nullable: true
23
+ # Description: Prefix of the receiving number; null for any destination
24
+
25
+ attribute :destination_number_length_min, :integer
26
+ # Type: Integer
27
+ # Description: Shortest receiving number, in digits
28
+
29
+ attribute :destination_number_length_max, :integer
30
+ # Type: Integer
31
+ # Description: Longest receiving number, in digits
32
+
33
+ attribute :country
34
+ # Type: String
35
+ # Nullable: true
36
+ # Description: Country of the receiving number
37
+
38
+ attribute :price, :decimal
39
+ # Type: BigDecimal
40
+ # Description: Price per SMS in USD, VAT excluded
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+ require 'didww/prices/price'
3
+
4
+ module DIDWW
5
+ module Prices
6
+ class SmsOutPrice < Price
7
+ attribute :source_prefix
8
+ # Type: String
9
+ # Nullable: true
10
+ # Description: Prefix of the sending number; null for any number
11
+
12
+ attribute :destination_prefix
13
+ # Type: String
14
+ # Nullable: true
15
+ # Description: Destination prefix; null for any destination
16
+
17
+ attribute :country
18
+ # Type: String
19
+ # Nullable: true
20
+ # Description: Destination country name
21
+
22
+ attribute :mcc, :integer
23
+ # Type: Integer
24
+ # Nullable: true
25
+ # Description: Mobile country code; null for any network
26
+
27
+ attribute :mcc_name
28
+ # Type: String
29
+ # Nullable: true
30
+ # Description: Name of the mobile country code
31
+
32
+ attribute :mnc, :integer
33
+ # Type: Integer
34
+ # Nullable: true
35
+ # Description: Mobile network code; null for any network of the country
36
+
37
+ attribute :mnc_name
38
+ # Type: String
39
+ # Nullable: true
40
+ # Description: Name of the mobile network
41
+
42
+ attribute :reject_sms, :boolean
43
+ # Type: Boolean
44
+ # Description: The plan rejects SMS to this destination
45
+
46
+ attribute :price, :decimal
47
+ # Type: BigDecimal
48
+ # Nullable: true
49
+ # Description: Price per SMS in USD, VAT excluded; null when reject_sms is true
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+ require 'didww/prices/price'
3
+
4
+ module DIDWW
5
+ module Prices
6
+ class VoiceInPrice < Price
7
+ attribute :destination
8
+ # Type: String
9
+ # Description: Prefix of the plan that priced the number
10
+
11
+ attribute :country
12
+ # Type: String
13
+ # Nullable: true
14
+ # Description: Destination country name
15
+
16
+ attribute :src_country_name
17
+ # Type: String
18
+ # Nullable: true
19
+ # Description: Source country name; null for a price of calls from any country
20
+
21
+ attribute :source_type
22
+ # Type: String
23
+ # Description: Calling party category, such as "cellular" or "fixed"
24
+
25
+ attribute :rate, :decimal
26
+ # Type: BigDecimal
27
+ # Nullable: true
28
+ # Description: Per-minute rate in USD, VAT excluded; null when reject_calls is true
29
+
30
+ attribute :connection_fee, :decimal
31
+ # Type: BigDecimal
32
+ # Nullable: true
33
+ # Description: Fee charged once per call in USD, VAT excluded; null when reject_calls is true
34
+
35
+ attribute :initial_interval, :integer
36
+ # Type: Integer
37
+ # Description: First billing increment, in seconds
38
+
39
+ attribute :next_interval, :integer
40
+ # Type: Integer
41
+ # Description: Each following billing increment, in seconds
42
+
43
+ attribute :reject_calls, :boolean
44
+ # Type: Boolean
45
+ # Description: The plan rejects calls to this destination
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+ require 'didww/prices/price'
3
+
4
+ module DIDWW
5
+ module Prices
6
+ class VoiceOutPrice < Price
7
+ CATEGORY_INTERNATIONAL = 'International'
8
+ CATEGORY_ORIGIN_BASED = 'Origin based'
9
+ CATEGORY_LOCAL = 'Local'
10
+ CATEGORIES = [
11
+ CATEGORY_INTERNATIONAL,
12
+ CATEGORY_ORIGIN_BASED,
13
+ CATEGORY_LOCAL
14
+ ].freeze
15
+
16
+ attribute :prefix
17
+ # Type: String
18
+ # Description: Destination prefix of the plan that priced the number, plain digits
19
+
20
+ attribute :country
21
+ # Type: String
22
+ # Nullable: true
23
+ # Description: Destination country name
24
+
25
+ attribute :network
26
+ # Type: String
27
+ # Nullable: true
28
+ # Description: Destination network name
29
+
30
+ attribute :rate, :decimal
31
+ # Type: BigDecimal
32
+ # Nullable: true
33
+ # Description: Per-minute rate in USD, VAT excluded; null when reject_calls is true
34
+
35
+ attribute :connection_fee, :decimal
36
+ # Type: BigDecimal
37
+ # Nullable: true
38
+ # Description: Fee charged once per call in USD, VAT excluded; null when reject_calls is true
39
+
40
+ attribute :initial_increment, :integer
41
+ # Type: Integer
42
+ # Description: First billing increment, in seconds
43
+
44
+ attribute :recurring_increment, :integer
45
+ # Type: Integer
46
+ # Description: Each following billing increment, in seconds
47
+
48
+ attribute :next_rate, :decimal
49
+ # Type: BigDecimal
50
+ # Nullable: true
51
+ # Description: Per-minute rate from effective_at on
52
+
53
+ attribute :next_connection_fee, :decimal
54
+ # Type: BigDecimal
55
+ # Nullable: true
56
+ # Description: Connection fee from effective_at on
57
+
58
+ attribute :next_initial_increment, :integer
59
+ # Type: Integer
60
+ # Nullable: true
61
+ # Description: First billing increment from effective_at on
62
+
63
+ attribute :next_recurring_increment, :integer
64
+ # Type: Integer
65
+ # Nullable: true
66
+ # Description: Each following billing increment from effective_at on
67
+
68
+ attribute :effective_at, :time
69
+ # Type: Time
70
+ # Nullable: true
71
+ # Description: When the change named by comment takes effect
72
+
73
+ attribute :comment
74
+ # Type: String
75
+ # Nullable: true
76
+ # Description: Scheduled change, such as "Increase", "Connection fee change", "Billing increment change",
77
+ # "No Change", "Reject calls", "Removed" or "New Breakout"
78
+
79
+ attribute :reject_calls, :boolean
80
+ # Type: Boolean
81
+ # Description: The plan rejects calls to this destination
82
+
83
+ attribute :category
84
+ # Type: String
85
+ # Description: One of CATEGORIES
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+ require 'csv'
3
+ require 'zlib'
4
+ require 'didww/errors'
5
+ require 'didww/prices/price'
6
+ require 'didww/prices/voice_in_price'
7
+ require 'didww/prices/voice_out_price'
8
+ require 'didww/prices/sms_out_price'
9
+ require 'didww/prices/sms_in_price'
10
+ require 'didww/prices/lookup_result'
11
+ require 'didww/prices/price_file'
12
+ require 'didww/prices/service'
13
+
14
+ module DIDWW
15
+ module Prices
16
+ VOICE_IN_FILTERS = {
17
+ required: %w[number],
18
+ optional: %w[src_country_iso]
19
+ }.freeze
20
+ VOICE_OUT_FILTERS = {
21
+ required: %w[number]
22
+ }.freeze
23
+ SMS_FILTERS = {
24
+ any_of: %w[dst_number dst_country_iso]
25
+ }.freeze
26
+ SERVICES = {
27
+ voice_in: {
28
+ host: :voice_in,
29
+ path_prefix: '',
30
+ price_class: VoiceInPrice,
31
+ lookup_filters: VOICE_IN_FILTERS
32
+ },
33
+ voice_out: {
34
+ host: :voice_out,
35
+ path_prefix: '',
36
+ price_class: VoiceOutPrice,
37
+ lookup_filters: VOICE_OUT_FILTERS
38
+ },
39
+ sms_out_a2p: {
40
+ host: :sms,
41
+ path_prefix: 'sms_out/a2p/',
42
+ price_class: SmsOutPrice,
43
+ lookup_filters: SMS_FILTERS
44
+ },
45
+ sms_out_p2p: {
46
+ host: :sms,
47
+ path_prefix: 'sms_out/p2p/',
48
+ price_class: SmsOutPrice,
49
+ lookup_filters: SMS_FILTERS
50
+ },
51
+ sms_out_otp: {
52
+ host: :sms,
53
+ path_prefix: 'sms_out/otp/',
54
+ price_class: SmsOutPrice,
55
+ lookup_filters: SMS_FILTERS
56
+ },
57
+ sms_in: {
58
+ host: :sms,
59
+ path_prefix: 'sms_in/',
60
+ price_class: SmsInPrice,
61
+ lookup_filters: SMS_FILTERS
62
+ }
63
+ }.freeze
64
+
65
+ # DIDWW::Prices.voice_in, .voice_out, .sms_out_a2p, .sms_out_p2p, .sms_out_otp and .sms_in
66
+ # @return [DIDWW::Prices::Service]
67
+ SERVICES.each do |name, options|
68
+ define_singleton_method(name) { |**overrides| Service.new(**options, **overrides) }
69
+ end
70
+
71
+ # Reads a downloaded price file, which stays gzipped on disk.
72
+ # @param path [String,Pathname] path of a *.csv.gz price file
73
+ # @yieldparam row [CSV::Row] one row, keyed by the header line
74
+ # @return [Enumerator] when no block is given
75
+ def self.each_row(path, &block)
76
+ return enum_for(:each_row, path) unless block
77
+
78
+ Zlib::GzipReader.open(path.to_s, encoding: Encoding::UTF_8) { |gzip| CSV.new(gzip, headers: true).each(&block) }
79
+ nil
80
+ end
81
+ end
82
+ end
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
+ require 'didww/resource/concerns/number_types'
3
+
2
4
  module DIDWW
3
5
  module Resource
4
6
  class AddressRequirement < Base
7
+ include NumberTypes
5
8
 
6
9
  has_one :country, class_name: 'Country'
7
- has_one :did_group_type, class_name: 'DidGroupType', relation_name: :group_type
8
10
  has_one :personal_permanent_document, class_name: 'SupportingDocumentTemplate'
9
11
  has_one :business_permanent_document, class_name: 'SupportingDocumentTemplate'
10
12
  has_one :personal_onetime_document, class_name: 'SupportingDocumentTemplate'
@@ -43,11 +45,15 @@ module DIDWW
43
45
 
44
46
  property :personal_mandatory_fields, type: :array
45
47
  # Type: String[]
46
- # Description:
48
+ # Description: Identity fields that must be filled in for a personal identity, among
49
+ # birth_date, country, birth_country, id_number, personal_tax_id and contact_email.
50
+ # Empty when none are.
47
51
 
48
52
  property :business_mandatory_fields, type: :array
49
53
  # Type: String[]
50
- # Description:
54
+ # Description: Identity fields that must be filled in for a business identity, among
55
+ # id_number, tax_id (Identity#vat_id), country, company_reg_number, personal_tax_id and
56
+ # contact_email. Empty when none are.
51
57
 
52
58
  property :service_description_required, type: :boolean
53
59
  # Type: Boolean
@@ -56,6 +62,10 @@ module DIDWW
56
62
  property :restriction_message, type: :string
57
63
  # Type: String
58
64
  # Description:
65
+
66
+ property :number_type, type: :string
67
+ # Type: String
68
+ # Description: Number type the rules apply to, one of NUMBER_TYPES.
59
69
  end
60
70
  end
61
71
  end
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
+ require 'didww/errors'
2
3
  require 'didww/jsonapi_middleware'
3
4
 
4
5
  module DIDWW
5
6
  module Resource
6
7
  class Base < JsonApiClient::Resource
8
+ self.connection_options = connection_options.merge(status_handlers: DIDWW::TooManyRequests::STATUS_HANDLERS)
9
+
7
10
  def as_json_api(*args)
8
11
  serialize_complex_attributes(super(*args))
9
12
  end
@@ -13,7 +16,7 @@ module DIDWW
13
16
  def serialize_complex_attributes(hash)
14
17
  # Replace complex objects with their json_api representation
15
18
  attributes = hash[:attributes]
16
- hash[:attributes] = Hash[attributes.map do |k, v|
19
+ hash[:attributes] = attributes.to_h do |k, v|
17
20
  if v.respond_to?(:as_json_api)
18
21
  [k, v.as_json_api]
19
22
  elsif v.is_a?(Array)
@@ -21,7 +24,7 @@ module DIDWW
21
24
  else
22
25
  [k, v]
23
26
  end
24
- end].with_indifferent_access
27
+ end.with_indifferent_access
25
28
  return hash
26
29
  end
27
30
  end