didww-v3 6.2.0 → 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.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +8 -6
- data/.rubocop.yml +0 -110
- data/CHANGELOG.md +65 -0
- data/FILTERS.md +36 -12
- data/Gemfile +0 -2
- data/README.md +431 -58
- data/didww-v3.gemspec +11 -8
- data/docs/resource_relationships.md +35 -15
- data/examples/README.md +54 -61
- data/examples/capacity_pools.rb +28 -10
- data/examples/countries.rb +1 -1
- data/examples/did_groups.rb +1 -1
- data/examples/did_reservations.rb +35 -37
- data/examples/did_sms_routing.rb +91 -0
- data/examples/did_trunk_assignment.rb +106 -80
- data/examples/emergency_calling_services.rb +3 -3
- data/examples/emergency_requirements.rb +3 -3
- data/examples/emergency_scenario.rb +11 -11
- data/examples/emergency_verifications.rb +7 -5
- data/examples/exports.rb +1 -1
- data/examples/exports_sms.rb +72 -0
- data/examples/identity_address_proofs.rb +118 -0
- data/examples/includes.rb +65 -0
- data/examples/orders.rb +8 -6
- data/examples/orders_all_item_types.rb +63 -125
- data/examples/orders_emergency.rb +3 -3
- data/examples/{orders_by_sku.rb → orders_sku.rb} +12 -10
- data/examples/prices_download.rb +45 -0
- data/examples/prices_lookup.rb +43 -0
- data/examples/regions.rb +1 -1
- data/examples/sample.pdf +14 -0
- data/examples/sender_id_verifications.rb +63 -0
- data/examples/shared_capacity_groups.rb +44 -35
- data/examples/sms_trunk_groups.rb +71 -0
- data/examples/sms_trunks.rb +84 -0
- data/examples/test_readme.rb +1216 -0
- data/examples/trunks.rb +115 -0
- data/examples/upload_file.rb +44 -0
- data/examples/voice_in_trunk_groups.rb +57 -33
- data/examples/voice_in_trunk_sip_registration.rb +4 -1
- data/examples/voice_out_trunks.rb +11 -1
- data/examples/webhook.rb +51 -0
- data/lib/didww/auth/bearer_middleware.rb +74 -0
- data/lib/didww/auth/memory_store.rb +40 -0
- data/lib/didww/auth/token.rb +45 -0
- data/lib/didww/auth/token_provider.rb +215 -0
- data/lib/didww/base_middleware.rb +7 -8
- data/lib/didww/callback/request_validator.rb +7 -5
- data/lib/didww/client.rb +122 -13
- data/lib/didww/complex_objects/authentication_methods/generic.rb +6 -0
- data/lib/didww/complex_objects/configurations/base.rb +0 -18
- data/lib/didww/complex_objects/configurations/pstn_configuration.rb +0 -4
- data/lib/didww/complex_objects/configurations/sip_configuration.rb +86 -38
- data/lib/didww/complex_objects/emergency_order_item.rb +1 -1
- data/lib/didww/complex_objects/export_filters.rb +72 -6
- data/lib/didww/complex_objects/generic_order_item.rb +15 -0
- data/lib/didww/complex_objects/sms_trunk_configuration.rb +33 -0
- data/lib/didww/complex_objects/sms_trunk_configurations/base.rb +26 -0
- data/lib/didww/complex_objects/sms_trunk_configurations/generic.rb +32 -0
- data/lib/didww/complex_objects/sms_trunk_configurations/http_in.rb +75 -0
- data/lib/didww/complex_objects/sms_trunk_configurations/http_out.rb +35 -0
- data/lib/didww/complex_objects/sms_trunk_configurations/smpp_esme.rb +37 -0
- data/lib/didww/complex_objects/sms_trunk_configurations/smpp_smsc.rb +56 -0
- data/lib/didww/complex_objects/sms_trunk_configurations/sms_to_email.rb +27 -0
- data/lib/didww/errors.rb +103 -0
- data/lib/didww/http.rb +34 -0
- data/lib/didww/prices/lookup_result.rb +37 -0
- data/lib/didww/prices/price.rb +56 -0
- data/lib/didww/prices/price_file.rb +51 -0
- data/lib/didww/prices/service.rb +248 -0
- data/lib/didww/prices/sms_in_price.rb +43 -0
- data/lib/didww/prices/sms_out_price.rb +52 -0
- data/lib/didww/prices/voice_in_price.rb +48 -0
- data/lib/didww/prices/voice_out_price.rb +88 -0
- data/lib/didww/prices.rb +82 -0
- data/lib/didww/resource/address_requirement.rb +13 -3
- data/lib/didww/resource/base.rb +5 -2
- data/lib/didww/resource/concerns/number_types.rb +24 -0
- data/lib/didww/resource/did.rb +42 -3
- data/lib/didww/resource/did_group.rb +12 -1
- data/lib/didww/resource/emergency_calling_service.rb +10 -5
- data/lib/didww/resource/emergency_requirement.rb +27 -14
- data/lib/didww/resource/encrypted_file.rb +19 -6
- data/lib/didww/resource/export.rb +59 -12
- data/lib/didww/resource/identity.rb +9 -4
- data/lib/didww/resource/order.rb +1 -0
- data/lib/didww/resource/proof_type.rb +1 -1
- data/lib/didww/resource/sender_id_verification.rb +114 -0
- data/lib/didww/resource/sms_trunk.rb +76 -0
- data/lib/didww/resource/sms_trunk_group.rb +31 -0
- data/lib/didww/resource/sms_trunk_regenerate_credential.rb +16 -0
- data/lib/didww/resource/voice_in_trunk.rb +24 -1
- data/lib/didww/resource/voice_out_trunk.rb +72 -0
- data/lib/didww/types/ip_addresses.rb +1 -1
- data/lib/didww/types/strings.rb +1 -1
- data/lib/didww/types.rb +3 -1
- data/lib/didww/version.rb +1 -1
- data/lib/didww.rb +7 -0
- metadata +63 -25
- data/examples/identities_and_proofs.rb +0 -67
- data/examples/voice_in_trunks.rb +0 -108
- data/lib/didww/resource/did_group_type.rb +0 -10
- data/lib/didww/resource/exclusive_relationship.rb +0 -54
- data/lib/didww/resource/pop.rb +0 -10
data/lib/didww/errors.rb
ADDED
|
@@ -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
|
|
@@ -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
|