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.
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 +65 -0
  5. data/FILTERS.md +36 -12
  6. data/Gemfile +0 -2
  7. data/README.md +431 -58
  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 +7 -5
  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 +63 -25
  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,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
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DIDWW
4
+ module Resource
5
+ # Possible values for number_type
6
+ module NumberTypes
7
+ NUMBER_TYPE_LOCAL = 'local'
8
+ NUMBER_TYPE_NATIONAL = 'national'
9
+ NUMBER_TYPE_MOBILE = 'mobile'
10
+ NUMBER_TYPE_TOLL_FREE = 'toll_free'
11
+ NUMBER_TYPE_SHARED_COST = 'shared_cost'
12
+ NUMBER_TYPE_GLOBAL = 'global'
13
+
14
+ NUMBER_TYPES = [
15
+ NUMBER_TYPE_LOCAL,
16
+ NUMBER_TYPE_NATIONAL,
17
+ NUMBER_TYPE_MOBILE,
18
+ NUMBER_TYPE_TOLL_FREE,
19
+ NUMBER_TYPE_SHARED_COST,
20
+ NUMBER_TYPE_GLOBAL
21
+ ].freeze
22
+ end
23
+ end
24
+ end
@@ -2,12 +2,53 @@
2
2
  module DIDWW
3
3
  module Resource
4
4
  class Did < Base
5
- include ExclusiveRelationship
5
+ # voice_in_trunk and voice_in_trunk_group are mutually exclusive, and so are
6
+ # sms_trunk and sms_trunk_group: assigning one clears the other, matching the
7
+ # server's own constraint.
8
+ class Relations < JsonApiClient::Relationships::Relations
9
+ EXCLUSIONS = {
10
+ 'voice_in_trunk' => 'voice_in_trunk_group',
11
+ 'voice_in_trunk_group' => 'voice_in_trunk',
12
+ 'sms_trunk' => 'sms_trunk_group',
13
+ 'sms_trunk_group' => 'sms_trunk'
14
+ }.freeze
15
+
16
+ # Bulk assignment (construction, or the server response applied after save)
17
+ # mirrors the server state as-is; only a single assignment clears its pair.
18
+ def attributes=(attrs = {})
19
+ @_bulk_assigning = true
20
+ super
21
+ ensure
22
+ @_bulk_assigning = false
23
+ end
24
+
25
+ def set_attribute(name, value)
26
+ super
27
+ return if @_bulk_assigning
28
+ exclusive = EXCLUSIONS[name.to_s]
29
+ super(exclusive, nil) if exclusive && !value.nil?
30
+ end
31
+ end
32
+
33
+ def relationships
34
+ @relationships ||= Relations.new(self.class, {})
35
+ end
36
+
37
+ def relationships=(rels)
38
+ attrs = case rels
39
+ when JsonApiClient::Relationships::Relations then rels.attributes
40
+ when Hash then rels
41
+ else rels || {}
42
+ end
43
+ @relationships = Relations.new(self.class, attrs)
44
+ end
6
45
 
7
46
  has_one :did_group
8
47
  has_one :order
9
48
  has_one :voice_in_trunk
10
49
  has_one :voice_in_trunk_group
50
+ has_one :sms_trunk, class_name: 'SmsTrunk'
51
+ has_one :sms_trunk_group, class_name: 'SmsTrunkGroup'
11
52
  has_one :capacity_pool
12
53
  has_one :shared_capacity_group
13
54
  has_one :address_verification
@@ -15,8 +56,6 @@ module DIDWW
15
56
  has_one :emergency_verification, class_name: 'EmergencyVerification'
16
57
  has_one :identity, class_name: 'Identity'
17
58
 
18
- exclusive_relationships(voice_in_trunk: :voice_in_trunk_group)
19
-
20
59
  property :blocked, type: :boolean
21
60
  # Type: Boolean
22
61
  # Description: Identifier for a blocked DID. Blocked DIDs are numbers that have expired, have been canceled or have been suspended by DIDWW.
@@ -1,7 +1,11 @@
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 DidGroup < Base
7
+ include NumberTypes
8
+
5
9
  # Possible values for did_group.features array
6
10
  FEATURE_VOICE_IN = 'voice_in'
7
11
  FEATURE_VOICE_OUT = 'voice_out'
@@ -14,7 +18,6 @@ module DIDWW
14
18
 
15
19
  has_one :country, class_name: 'Country'
16
20
  has_one :city, class_name: 'City'
17
- has_one :did_group_type, class_name: 'DidGroupType'
18
21
  has_one :region, class_name: 'Region'
19
22
  has_many :stock_keeping_units, class_name: 'StockKeepingUnit'
20
23
  has_one :address_requirement, class_name: 'AddressRequirement'
@@ -44,6 +47,14 @@ module DIDWW
44
47
  # Description: Service restriction message associated with this DID Group, if any.
45
48
  # Null when no restrictions apply. (API 2026-04-16)
46
49
 
50
+ property :number_type, type: :string
51
+ # Type: String
52
+ # Description: Number type the group offers, one of NUMBER_TYPES.
53
+
54
+ property :is_demo, type: :boolean
55
+ # Type: Boolean
56
+ # Description: True when the group is flagged as a demo group.
57
+
47
58
  # TODO
48
59
  # Meta attributes
49
60
  #
@@ -1,18 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'didww/resource/concerns/has_status_helpers'
4
+ require 'didww/resource/concerns/number_types'
4
5
 
5
6
  module DIDWW
6
7
  module Resource
7
8
  # Customer-owned subscription to emergency calling on one or more DIDs.
8
9
  # Supported operations: index, show, destroy. Introduced in API 2026-04-16.
9
10
  #
10
- # Server-side filters: name, reference, country.id, did_group_type.id,
11
+ # Server-side filters: name, reference, country.id, number_type,
11
12
  # address.id, identity.id, status.
12
13
  #
13
14
  # meta carries: setup_price, monthly_price.
14
15
  class EmergencyCallingService < Base
15
16
  include HasStatusHelpers
17
+ include NumberTypes
16
18
 
17
19
  STATUS_ACTIVE = 'active'
18
20
  STATUS_CANCELED = 'canceled'
@@ -31,7 +33,6 @@ module DIDWW
31
33
  ].freeze
32
34
 
33
35
  has_one :country, class_name: 'Country'
34
- has_one :did_group_type, class_name: 'DidGroupType'
35
36
  has_one :order, class_name: 'Order'
36
37
  has_one :address, class_name: 'Address'
37
38
  has_one :emergency_requirement, class_name: 'EmergencyRequirement'
@@ -62,9 +63,13 @@ module DIDWW
62
63
  property :created_at, type: :time
63
64
  # Type: Time
64
65
 
65
- property :renew_date, type: :time
66
- # Type: Time
67
- # Description: Next renewal date. nil when canceled.
66
+ property :renew_date, type: :string
67
+ # Type: String
68
+ # Description: Next renewal date, "YYYY-MM-DD". nil when canceled.
69
+
70
+ property :number_type, type: :string
71
+ # Type: String
72
+ # Description: Number type the subscription covers, one of NUMBER_TYPES.
68
73
 
69
74
  status_helper :active, STATUS_ACTIVE
70
75
  status_helper :canceled, STATUS_CANCELED
@@ -1,17 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'didww/resource/concerns/number_types'
4
+
3
5
  module DIDWW
4
6
  module Resource
5
7
  # Requirements that must be satisfied before ordering an emergency
6
- # calling service for a given country/did_group_type. Introduced in API 2026-04-16.
8
+ # calling service for a given country/number_type. Introduced in API 2026-04-16.
7
9
  #
8
10
  # Each record also reports a price preview in `meta` (setup_price,
9
- # monthly_price) when the customer has a matching emergency plan.
11
+ # monthly_price), both decimal strings, when the customer has a matching
12
+ # emergency plan.
10
13
  #
11
- # Server-side filters: country.id, did_group_type.id
14
+ # Server-side filters: country.id, number_type
12
15
  class EmergencyRequirement < Base
16
+ include NumberTypes
17
+
13
18
  has_one :country, class_name: 'Country'
14
- has_one :did_group_type, class_name: 'DidGroupType'
15
19
 
16
20
  property :identity_type, type: :string
17
21
  # Type: String
@@ -20,36 +24,45 @@ module DIDWW
20
24
 
21
25
  property :address_area_level, type: :string
22
26
  # Type: String
23
- # Description: How precise the service address must be (city, region, etc.).
27
+ # Description: How precise the service address must be: country, area or city.
24
28
 
25
29
  property :personal_area_level, type: :string
26
- # Type: String
27
- # Description: How precise a personal identity address must be.
30
+ # Type: String, nullable
31
+ # Description: How precise a personal identity address must be: world_wide or country.
32
+ # Nil when the country does not accept a personal identity for emergency calling.
28
33
 
29
34
  property :business_area_level, type: :string
30
- # Type: String
31
- # Description: How precise a business identity address must be.
35
+ # Type: String, nullable
36
+ # Description: How precise a business identity address must be: world_wide or country.
37
+ # Nil when the country does not accept a business identity for emergency calling.
32
38
 
33
39
  property :address_mandatory_fields, type: :array
34
40
  # Type: String[]
35
- # Description: Fields the service address must contain.
41
+ # Description: Fields the service address must contain (area). Empty when none are.
36
42
 
37
43
  property :personal_mandatory_fields, type: :array
38
44
  # Type: String[]
39
- # Description: Fields a personal identity must contain.
45
+ # Description: Fields a personal identity must contain, among birth_date, country,
46
+ # id_number, personal_tax_id and contact_email. Empty when none are.
40
47
 
41
48
  property :business_mandatory_fields, type: :array
42
49
  # Type: String[]
43
- # Description: Fields a business identity must contain.
50
+ # Description: Fields a business identity must contain, among id_number,
51
+ # tax_id (Identity#vat_id), country, company_reg_number, personal_tax_id, contact_email
52
+ # and birth_date. Empty when none are.
44
53
 
45
54
  property :estimate_setup_time, type: :string
46
55
  # Type: String
47
56
  # Description: Estimated time before emergency calling is enabled (e.g. "7-14 days").
48
57
 
49
58
  property :requirement_restriction_message, type: :string
50
- # Type: String
59
+ # Type: String, nullable
51
60
  # Description: Human-readable message describing any restrictions that
52
- # apply to ordering the service. May be null.
61
+ # apply to ordering the service. Nil when the country places none.
62
+
63
+ property :number_type, type: :string
64
+ # Type: String
65
+ # Description: Number type the rules apply to, one of NUMBER_TYPES.
53
66
  end
54
67
  end
55
68
  end
@@ -1,10 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
  require 'faraday/multipart'
3
+ require 'didww/errors'
3
4
 
4
5
  module DIDWW
5
6
  module Resource
6
7
  class EncryptedFile < Base
7
- class UploadError < StandardError
8
+ class UploadError < DIDWW::Error
9
+ attr_reader :status, :retry_after
10
+
11
+ # @param status [Integer,nil] HTTP status of the response
12
+ # @param retry_after [Integer,nil] Retry-After seconds of a 429
13
+ def initialize(message = nil, status: nil, retry_after: nil)
14
+ @status = status
15
+ @retry_after = retry_after
16
+ super(message)
17
+ end
8
18
  end
9
19
 
10
20
  property :description, type: :string
@@ -17,11 +27,9 @@ module DIDWW
17
27
 
18
28
  # @return [Faraday::Connection]
19
29
  def self.upload_connection
20
- Faraday.new(url: site) do |connection|
30
+ DIDWW::Http.connection(DIDWW::Client.api_base_url, audience: DIDWW::Client::API_AUDIENCE) do |connection|
21
31
  connection.request :multipart
22
32
  connection.request :url_encoded
23
- connection.adapter Faraday.default_adapter
24
- connection.use DIDWW::BaseMiddleware
25
33
  end
26
34
  end
27
35
 
@@ -47,11 +55,16 @@ module DIDWW
47
55
  # @raise [DIDWW::Resource::EncryptedFile::UploadError]
48
56
  def self.upload(payload)
49
57
  connection = upload_connection
50
- response = connection.post('/v3/encrypted_files', encrypted_files: payload)
58
+ response = connection.post('encrypted_files', encrypted_files: payload)
51
59
  if response.status == 201
52
60
  JSON.parse(response.body, symbolize_names: true).dig(:data, :id)
53
61
  else
54
- raise UploadError, "Code: #{response.status} #{response.body}"
62
+ retry_after = RetryAfter.seconds(response.headers['Retry-After']) if response.status == 429
63
+ raise UploadError.new(
64
+ "Code: #{response.status} #{response.body}",
65
+ status: response.status,
66
+ retry_after: retry_after
67
+ )
55
68
  end
56
69
  end
57
70
  end
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
  require 'forwardable'
3
3
  require 'didww/resource/concerns/has_status_helpers'
4
- require 'down/http'
4
+ require 'tempfile'
5
5
  require 'zlib'
6
6
  require 'stringio'
7
+ require 'uri'
7
8
  require 'didww/callback/const'
8
9
 
9
10
  module DIDWW
@@ -25,15 +26,19 @@ module DIDWW
25
26
 
26
27
  EXPORT_TYPE_CDR_IN = 'cdr_in'
27
28
  EXPORT_TYPE_CDR_OUT = 'cdr_out'
29
+ EXPORT_TYPE_SMS_IN = 'sms_in'
30
+ EXPORT_TYPE_SMS_OUT = 'sms_out'
28
31
  EXPORT_TYPES = [
29
32
  EXPORT_TYPE_CDR_IN,
30
- EXPORT_TYPE_CDR_OUT
33
+ EXPORT_TYPE_CDR_OUT,
34
+ EXPORT_TYPE_SMS_IN,
35
+ EXPORT_TYPE_SMS_OUT
31
36
  ].freeze
32
37
 
33
38
  property :filters, type: :export_filters
34
- # Type: CDR Export Filters Object
39
+ # Type: Export Filters Object
35
40
  # Nullable: No
36
- # Description: Filters
41
+ # Description: Filters; the accepted keys depend on export_type (see ComplexObject::ExportFilters)
37
42
 
38
43
  property :status, type: :string
39
44
  # Type: String
@@ -59,6 +64,8 @@ module DIDWW
59
64
  # Description: GET or POST
60
65
 
61
66
  property :export_type, type: :string
67
+ # Type: String
68
+ # Description: One of EXPORT_TYPES
62
69
 
63
70
  property :external_reference_id, type: :string
64
71
  # Type: String
@@ -68,24 +75,34 @@ module DIDWW
68
75
  super params.reverse_merge(filters: {})
69
76
  end
70
77
 
78
+ # @return [Tempfile,nil] the gzipped CSV, rewound and opened in binary mode
79
+ # @raise [ArgumentError] url is not on the scheme, host and port of the API base URL or of the canonical API URL of the mode
80
+ # @raise [JsonApiClient::Errors::ApiError]
71
81
  def download
72
82
  return unless url.present?
73
- Down::Http.new(headers: {
74
- 'Api-Key' => DIDWW::Client.api_key,
75
- 'X-DIDWW-API-Version' => DIDWW::Client.api_version,
76
- 'User-Agent' => "didww-v3 Ruby gem v#{DIDWW::VERSION}"
77
- }).open(url)
83
+ verify_url_on_api_host!
84
+ file = Tempfile.new(['export', '.csv.gz'], binmode: true)
85
+ response = download_connection.get do |request|
86
+ request.options.on_data = proc { |chunk, _received, env| file.write(chunk) if env.status == 200 }
87
+ end
88
+ raise JsonApiClient::Errors::UnexpectedStatus.new(response.status, url) unless response.status == 200
89
+
90
+ file.rewind
91
+ file
92
+ rescue StandardError
93
+ file&.close!
94
+ raise
78
95
  end
79
96
 
80
97
  def csv
81
98
  file = download
82
99
  return unless file
83
- gz = Zlib::GzipReader.new(file)
84
100
  begin
101
+ gz = Zlib::GzipReader.new(file)
85
102
  StringIO.new(gz.read)
86
103
  ensure
87
- gz.close unless gz.closed?
88
- file.close if file.respond_to?(:close) && !file.closed?
104
+ gz.close if gz && !gz.closed?
105
+ file.close!
89
106
  end
90
107
  end
91
108
 
@@ -93,6 +110,36 @@ module DIDWW
93
110
  status_helper :processing, STATUS_PROCESSING
94
111
  status_helper :complete, STATUS_COMPLETED
95
112
  alias_method :completed?, :complete?
113
+
114
+ private
115
+
116
+ # The download sends the API bearer token, so it goes only to the API host:
117
+ # that of the API base URL, or the canonical one of the mode, which the API builds export urls from.
118
+ def verify_url_on_api_host!
119
+ allowed = [DIDWW::Client.api_base_url, DIDWW::Client::BASE_URLS[DIDWW::Client.api_mode]].map { |base| URI.parse(base) }
120
+ target = begin
121
+ URI.parse(url)
122
+ rescue URI::InvalidURIError
123
+ nil
124
+ end
125
+ return if target && allowed.any? { |api| same_origin?(target, api) }
126
+
127
+ hosts = allowed.map { |api| "#{api.scheme}://#{api.host}#{":#{api.port}" unless api.port == api.default_port}" }.uniq
128
+ raise ArgumentError.new("Export url must be on the API host #{hosts.join(' or ')}.")
129
+ end
130
+
131
+ def same_origin?(target, api)
132
+ target.is_a?(URI::HTTP) && target.host.present? &&
133
+ target.scheme.casecmp?(api.scheme) &&
134
+ target.host.chomp('.').casecmp?(api.host.chomp('.')) &&
135
+ target.port == api.port
136
+ end
137
+
138
+ def download_connection
139
+ DIDWW::Http.connection(url, audience: DIDWW::Client::API_AUDIENCE) do |connection|
140
+ connection.use JsonApiClient::Middleware::Status, { custom_handlers: DIDWW::TooManyRequests::STATUS_HANDLERS }
141
+ end
142
+ end
96
143
  end
97
144
  end
98
145
  end