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
@@ -1,107 +1,133 @@
1
1
  # frozen_string_literal: true
2
2
  # Demonstrates exclusive trunk/trunk group assignment on DIDs.
3
- # Assigning a trunk auto-nullifies the trunk group and vice versa.
3
+ # Assigning a trunk auto-nullifies the trunk group and vice versa: the SDK sends
4
+ # the other relationship as null in the same PATCH.
5
+ # At the end the DID's original voice routing and description are restored and
6
+ # the created trunk and trunk group are deleted, also when a step fails.
4
7
  #
5
8
  # Usage: DIDWW_API_KEY=your_api_key ruby examples/did_trunk_assignment.rb
6
9
 
7
10
  require 'bundler/setup'
8
11
  require 'didww'
12
+ require 'securerandom'
9
13
 
10
14
  DIDWW::Client.configure do |client|
11
15
  client.api_key = ENV.fetch('DIDWW_API_KEY') { abort 'Please set DIDWW_API_KEY' }
12
16
  client.api_mode = :sandbox
13
17
  end
14
18
 
15
- # Get a DID to work with
16
- puts '=== Finding DID ==='
17
- dids = DIDWW::Client.dids
18
- .includes(:voice_in_trunk, :voice_in_trunk_group)
19
- .all
20
-
21
- if dids.empty?
22
- puts 'No DIDs found. Please order a DID first.'
23
- exit 1
24
- end
25
-
26
- did = dids.first
27
- puts "Using DID: #{did.number} (#{did.id})"
28
-
29
- # Get a trunk
30
- puts "\n=== Finding Trunk ==="
31
- trunks = DIDWW::Client.voice_in_trunks.all
32
-
33
- if trunks.empty?
34
- puts 'No trunks found. Please create a trunk first.'
35
- exit 1
19
+ def print_did_assignment(did_id)
20
+ result = DIDWW::Client.dids
21
+ .includes(:voice_in_trunk, :voice_in_trunk_group)
22
+ .find(did_id)
23
+ .first
24
+ puts " trunk = #{result.voice_in_trunk&.id || 'null'}"
25
+ puts " group = #{result.voice_in_trunk_group&.id || 'null'}"
36
26
  end
37
27
 
38
- trunk = trunks.first
39
- puts "Selected trunk: #{trunk.name} (#{trunk.id})"
40
-
41
- # Get a trunk group
42
- puts "\n=== Finding Trunk Group ==="
43
- trunk_groups = DIDWW::Client.voice_in_trunk_groups.all
44
-
45
- if trunk_groups.empty?
46
- puts 'No trunk groups found. Please create a trunk group first.'
47
- exit 1
28
+ # Sends a PATCH with only the attributes and relationships set in the block
29
+ def update_did(did_id)
30
+ did = DIDWW::Resource::Did.load(id: did_id)
31
+ yield did
32
+ raise "Error updating DID: #{did.errors.full_messages}" unless did.save
48
33
  end
49
34
 
50
- trunk_group = trunk_groups.first
51
- puts "Selected trunk group: #{trunk_group.name} (#{trunk_group.id})"
35
+ # Get a DID
36
+ puts '=== Finding DID ==='
37
+ did = DIDWW::Client.dids
38
+ .includes(:voice_in_trunk, :voice_in_trunk_group)
39
+ .page(1).per(1)
40
+ .all
41
+ .first
42
+ abort 'No DIDs found. Please order a DID first.' unless did
43
+ puts "Using DID: #{did.number} (#{did.id})"
52
44
 
53
- def print_did_assignment(client, did_id)
54
- result = client.dids
55
- .includes(:voice_in_trunk, :voice_in_trunk_group)
56
- .find(did_id)
57
- .first
58
- trunk = result.voice_in_trunk ? result.voice_in_trunk.id : 'null'
59
- group = result.voice_in_trunk_group ? result.voice_in_trunk_group.id : 'null'
60
- puts " trunk = #{trunk}"
61
- puts " group = #{group}"
62
- end
45
+ # Remember the current routing and description to put them back at the end
46
+ original_trunk = did.voice_in_trunk
47
+ original_group = did.voice_in_trunk_group
48
+ original_description = did.description
49
+ puts "Current routing: trunk=#{original_trunk&.id || 'null'} group=#{original_group&.id || 'null'}"
63
50
 
51
+ suffix = SecureRandom.hex(4)
52
+ trunk = nil
53
+ trunk_group = nil
54
+ did_changed = false
64
55
  begin
56
+ # Create a trunk
57
+ trunk = DIDWW::Client.voice_in_trunks.new(
58
+ name: "Assignment Trunk #{suffix}",
59
+ pop: DIDWW::Resource::VoiceInTrunk::POP_FRA, # optional; leave it nil to let DIDWW choose
60
+ configuration: DIDWW::ComplexObject::SipConfiguration.new(
61
+ host: 'sip.example.com',
62
+ port: 5060,
63
+ codecs: [
64
+ DIDWW::ComplexObject::SipConfiguration::CODEC_PCMU,
65
+ DIDWW::ComplexObject::SipConfiguration::CODEC_PCMA
66
+ ],
67
+ transport_protocol_id: 1 # UDP
68
+ )
69
+ )
70
+ raise "Error creating trunk: #{trunk.errors.full_messages}" unless trunk.save
71
+ puts "Created trunk: #{trunk.id}"
72
+
73
+ # Create a trunk group
74
+ trunk_group = DIDWW::Client.voice_in_trunk_groups.new(name: "Assignment Group #{suffix}", capacity_limit: 10)
75
+ raise "Error creating trunk group: #{trunk_group.errors.full_messages}" unless trunk_group.save
76
+ puts "Created trunk group: #{trunk_group.id}"
77
+
65
78
  # 1. Assign trunk to DID (auto-nullifies trunk group)
66
- puts "\n=== 1. Assigning trunk to DID ==="
67
- did.voice_in_trunk = trunk
68
- if did.save
69
- print_did_assignment(DIDWW::Client, did.id)
70
- else
71
- puts "Error assigning trunk: #{did.errors.full_messages}"
72
- end
79
+ did_changed = true
80
+ update_did(did.id) { |d| d.voice_in_trunk = trunk }
81
+ puts "\n1. Assigned trunk:"
82
+ print_did_assignment(did.id)
73
83
 
74
84
  # 2. Assign trunk group to DID (auto-nullifies trunk)
75
- puts "\n=== 2. Assigning trunk group to DID ==="
76
- did_fresh = DIDWW::Client.dids.find(did.id).first
77
- did_fresh.voice_in_trunk_group = trunk_group
78
- if did_fresh.save
79
- print_did_assignment(DIDWW::Client, did.id)
80
- else
81
- puts "Error assigning trunk group: #{did_fresh.errors.full_messages}"
82
- end
85
+ update_did(did.id) { |d| d.voice_in_trunk_group = trunk_group }
86
+ puts "\n2. Assigned trunk group (the trunk is cleared):"
87
+ print_did_assignment(did.id)
83
88
 
84
89
  # 3. Re-assign trunk (auto-nullifies trunk group again)
85
- puts "\n=== 3. Re-assigning trunk ==="
86
- did_fresh = DIDWW::Client.dids.find(did.id).first
87
- did_fresh.voice_in_trunk = trunk
88
- if did_fresh.save
89
- print_did_assignment(DIDWW::Client, did.id)
90
- else
91
- puts "Error re-assigning trunk: #{did_fresh.errors.full_messages}"
90
+ update_did(did.id) { |d| d.voice_in_trunk = trunk }
91
+ puts "\n3. Re-assigned trunk:"
92
+ print_did_assignment(did.id)
93
+
94
+ # 4. Update the description only: only changed fields are sent, so the trunk stays
95
+ update_did(did.id) { |d| d.description = 'DID with trunk assigned' }
96
+ puts "\n4. Updated description only (trunk stays):"
97
+ print_did_assignment(did.id)
98
+ ensure
99
+ begin
100
+ # Restore the DID first, so the created trunk and group are no longer in use
101
+ if did_changed
102
+ update_did(did.id) do |d|
103
+ if original_trunk
104
+ d.voice_in_trunk = original_trunk
105
+ elsif original_group
106
+ d.voice_in_trunk_group = original_group
107
+ else
108
+ # nil unassigns only that relationship, so both are set
109
+ d.voice_in_trunk = nil
110
+ d.voice_in_trunk_group = nil
111
+ end
112
+ d.description = original_description
113
+ end
114
+ puts "\nRestored the DID's original routing:"
115
+ begin
116
+ print_did_assignment(did.id)
117
+ rescue StandardError => e
118
+ puts " (could not read the routing back: #{e.message})"
119
+ end
120
+ end
121
+ ensure
122
+ # Deleted also when the restore fails
123
+ { 'trunk' => trunk, 'trunk group' => trunk_group }.each do |label, resource|
124
+ next unless resource&.persisted?
125
+
126
+ if resource.destroy
127
+ puts "Deleted #{label} #{resource.id}"
128
+ else
129
+ puts "Error deleting #{label} #{resource.id}: #{resource.errors.full_messages}"
130
+ end
131
+ end
92
132
  end
93
-
94
- # 4. Update description only (trunk stays assigned)
95
- puts "\n=== 4. Updating description only (trunk stays) ==="
96
- did_fresh = DIDWW::Client.dids.find(did.id).first
97
- did_fresh.description = 'DID with trunk assigned'
98
- if did_fresh.save
99
- print_did_assignment(DIDWW::Client, did.id)
100
- else
101
- puts "Error updating description: #{did_fresh.errors.full_messages}"
102
- end
103
-
104
- puts "\nDemonstration complete!"
105
- rescue StandardError => e
106
- puts "Error: #{e.message}"
107
133
  end
@@ -2,7 +2,7 @@
2
2
  # Lists and cancels customer Emergency Calling Services (2026-04-16).
3
3
  #
4
4
  # An EmergencyCallingService represents a customer's 911/112 subscription
5
- # attached to one or more DIDs. It ties an address, identity, DID group type
5
+ # attached to one or more DIDs. It ties an address, identity, number type
6
6
  # and country together with a pricing snapshot (meta.setup_price, meta.monthly_price).
7
7
  #
8
8
  # Supported operations: index, show, destroy (cancel).
@@ -19,7 +19,7 @@ end
19
19
 
20
20
  puts '=== Emergency Calling Services ==='
21
21
  services = DIDWW::Client.emergency_calling_services
22
- .includes(:country, :did_group_type, :dids)
22
+ .includes(:country, :dids)
23
23
  .all
24
24
  puts "Found #{services.size} emergency calling services"
25
25
 
@@ -29,7 +29,7 @@ services.first(5).each do |svc|
29
29
  puts " Reference: #{svc.reference}"
30
30
  puts " Status: #{svc.status}"
31
31
  puts " Country: #{svc.country&.name}"
32
- puts " DID Group Type: #{svc.did_group_type&.name}"
32
+ puts " Number type: #{svc.number_type}"
33
33
  puts " Activated: #{svc.activated_at}"
34
34
  puts " Canceled: #{svc.canceled_at}" if svc.canceled_at
35
35
  puts " Renews: #{svc.renew_date}" if svc.renew_date
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- # Lists emergency service requirements for a country/did_group_type (2026-04-16).
2
+ # Lists emergency service requirements for a country/number_type.
3
3
  #
4
4
  # Emergency requirements describe what address precision, identity type,
5
5
  # and supporting fields an end-customer must provide to enable 911/112
@@ -18,14 +18,14 @@ end
18
18
 
19
19
  puts '=== Emergency Requirements ==='
20
20
  requirements = DIDWW::Client.emergency_requirements
21
- .includes(:country, :did_group_type)
21
+ .includes(:country)
22
22
  .all
23
23
  puts "Found #{requirements.size} emergency requirements"
24
24
 
25
25
  requirements.first(5).each do |req|
26
26
  puts "\nRequirement: #{req.id}"
27
27
  puts " Country: #{req.country&.name}"
28
- puts " DID Group Type: #{req.did_group_type&.name}"
28
+ puts " Number type: #{req.number_type}"
29
29
  puts " Identity type required: #{req.identity_type}"
30
30
  puts " Address area level: #{req.address_area_level}"
31
31
  puts " Address mandatory fields: #{req.address_mandatory_fields&.join(', ')}"
@@ -5,7 +5,7 @@
5
5
  # Calling Service:
6
6
  #
7
7
  # 1. Find a DID with the emergency feature that is not yet emergency-enabled.
8
- # 2. Look up emergency requirements for that DID's country + did_group_type.
8
+ # 2. Look up emergency requirements for that DID's country + number_type.
9
9
  # 3. Find an existing identity on the account.
10
10
  # 4. Find an existing address for that identity.
11
11
  # 5. Validate the (emergency_requirement, address, identity) triple.
@@ -56,8 +56,8 @@ abort "Failed to order DID: #{order.errors.full_messages}" unless order.save
56
56
 
57
57
  puts " Order: #{order.id} — #{order.status}"
58
58
 
59
- # Wait for order to complete
60
- 10.times do
59
+ # Wait up to 120 s for the order to complete
60
+ 24.times do
61
61
  break if order.status == 'completed'
62
62
  sleep 5
63
63
  order = DIDWW::Client.orders.find(order.id).first
@@ -69,7 +69,7 @@ puts ' Order completed'
69
69
  puts "\n=== Step 1: Find the newly ordered DID ==="
70
70
  dids = DIDWW::Client.dids
71
71
  .where('did_group.features': 'emergency', emergency_enabled: false)
72
- .includes(:did_group, 'did_group.country', 'did_group.did_group_type', :emergency_calling_service)
72
+ .includes(:did_group, 'did_group.country', :emergency_calling_service)
73
73
  .order('-created_at')
74
74
  .page(1).per(10)
75
75
  .all
@@ -80,21 +80,21 @@ abort 'No available DID without an existing Emergency Calling Service.' unless d
80
80
 
81
81
  did_group = did.did_group
82
82
  country = did_group.country
83
- dgt = did_group.did_group_type
83
+ number_type = did_group.number_type
84
84
 
85
85
  puts " DID: #{did.number} (#{did.id})"
86
86
  puts " DID Group: #{did_group.id}"
87
87
  puts " Country: #{country&.name} (#{country&.id})"
88
- puts " DID Group Type: #{dgt&.name} (#{dgt&.id})"
88
+ puts " Number type: #{number_type}"
89
89
 
90
90
  # === Step 2: Get emergency requirements ===
91
- puts "\n=== Step 2: Get emergency requirements for country + did_group_type ==="
91
+ puts "\n=== Step 2: Get emergency requirements for country + number_type ==="
92
92
  requirements = DIDWW::Client.emergency_requirements
93
- .where('country.id': country.id, 'did_group_type.id': dgt.id)
93
+ .where('country.id': country.id, number_type: number_type)
94
94
  .all
95
95
 
96
96
  requirement = requirements.first
97
- abort "No emergency requirements found for country #{country.name} / #{dgt.name}." unless requirement
97
+ abort "No emergency requirements found for country #{country.name} / #{number_type}." unless requirement
98
98
 
99
99
  puts " Emergency Requirement: #{requirement.id}"
100
100
  puts " Identity type: #{requirement.identity_type}"
@@ -182,7 +182,7 @@ ecs = fetched.emergency_calling_service
182
182
  if ecs
183
183
  # Re-fetch with includes for full details
184
184
  service = DIDWW::Client.emergency_calling_services
185
- .includes(:country, :did_group_type, :dids)
185
+ .includes(:country, :dids)
186
186
  .find(ecs.id)
187
187
  .first
188
188
 
@@ -191,7 +191,7 @@ if ecs
191
191
  puts " Reference: #{service.reference}"
192
192
  puts " Status: #{service.status}"
193
193
  puts " Country: #{service.country&.name}"
194
- puts " DID Group Type: #{service.did_group_type&.name}"
194
+ puts " Number type: #{service.number_type}"
195
195
  puts " Attached DIDs: #{service.dids.map(&:number).join(', ')}" if service.dids&.any?
196
196
  if service.meta
197
197
  puts " Setup price: #{service.meta[:setup_price]}" if service.meta[:setup_price]
@@ -50,11 +50,13 @@ puts "Found #{approved.size} approved verifications"
50
50
  #
51
51
  # suffix = SecureRandom.hex(4)
52
52
  # verification = DIDWW::Client.emergency_verifications.new(
53
- # address: DIDWW::Resource::Address.load(id: '<address-uuid>'),
54
- # emergency_calling_service:
55
- # DIDWW::Resource::EmergencyCallingService.load(id: '<ecs-uuid>'),
56
- # dids: [DIDWW::Resource::Did.load(id: '<did-uuid>')],
57
- # external_reference_id: "ruby-ev-#{suffix}"
53
+ # external_reference_id: "ruby-ev-#{suffix}",
54
+ # relationships: {
55
+ # address: DIDWW::Resource::Address.load(id: '<address-uuid>'),
56
+ # emergency_calling_service:
57
+ # DIDWW::Resource::EmergencyCallingService.load(id: '<ecs-uuid>'),
58
+ # dids: [DIDWW::Resource::Did.load(id: '<did-uuid>')]
59
+ # }
58
60
  # )
59
61
  #
60
62
  # if verification.save
data/examples/exports.rb CHANGED
@@ -60,7 +60,7 @@ else
60
60
  end
61
61
 
62
62
  # Find and inspect a specific export
63
- if !exports.empty?
63
+ unless exports.empty?
64
64
  puts "\n=== Specific Export Details ==="
65
65
  specific_export = DIDWW::Client.exports
66
66
  .find(exports.first.id)
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+ # Creates SMS exports (sms_in / sms_out) for yesterday and downloads the
3
+ # first completed one.
4
+ #
5
+ # Filters on SMS exports:
6
+ # * from, to (required) — "YYYY-MM-DD HH:MM:SS" UTC; from inclusive, to exclusive
7
+ # * source_address_eq / _cont, destination_address_eq / _cont, status, sms_id
8
+ # * sms_trunk_id — one id or an array, sent as sms_trunk.id
9
+ # * sms_in only: trunk_type, one or more of ExportFilters::TRUNK_TYPES
10
+ # * sms_out only: service_type (one of ExportFilters::SERVICE_TYPES), sender_id_verification_id
11
+ #
12
+ # Usage: DIDWW_API_KEY=your_api_key ruby examples/exports_sms.rb
13
+
14
+ require 'bundler/setup'
15
+ require 'didww'
16
+ require 'securerandom'
17
+
18
+ DIDWW::Client.configure do |client|
19
+ client.api_key = ENV.fetch('DIDWW_API_KEY') { abort 'Please set DIDWW_API_KEY' }
20
+ client.api_mode = :sandbox
21
+ end
22
+
23
+ today = Date.today
24
+ yesterday = today - 1
25
+ suffix = SecureRandom.hex(4)
26
+
27
+ puts '=== Creating SMS-Out Export (A2P, yesterday) ==='
28
+ sms_out_export = DIDWW::Client.exports.new(
29
+ export_type: DIDWW::Resource::Export::EXPORT_TYPE_SMS_OUT,
30
+ filters: DIDWW::ComplexObject::ExportFilters.new(
31
+ from: "#{yesterday} 00:00:00",
32
+ to: "#{today} 00:00:00",
33
+ service_type: DIDWW::ComplexObject::ExportFilters::SERVICE_TYPE_A2P
34
+ ),
35
+ external_reference_id: "ruby-sms-out-#{suffix}"
36
+ )
37
+ if sms_out_export.save
38
+ puts "Created SMS-Out export: #{sms_out_export.id} (status: #{sms_out_export.status})"
39
+ else
40
+ puts "Error creating SMS-Out export: #{sms_out_export.errors.full_messages}"
41
+ end
42
+
43
+ puts "\n=== Creating SMS-In Export (HTTP IN and email trunks, yesterday) ==="
44
+ sms_in_export = DIDWW::Client.exports.new(
45
+ export_type: DIDWW::Resource::Export::EXPORT_TYPE_SMS_IN,
46
+ filters: DIDWW::ComplexObject::ExportFilters.new(
47
+ from: "#{yesterday} 00:00:00",
48
+ to: "#{today} 00:00:00",
49
+ trunk_type: [
50
+ DIDWW::ComplexObject::ExportFilters::TRUNK_TYPE_HTTP_IN,
51
+ DIDWW::ComplexObject::ExportFilters::TRUNK_TYPE_SMS_TO_EMAIL
52
+ ]
53
+ ),
54
+ external_reference_id: "ruby-sms-in-#{suffix}"
55
+ )
56
+ if sms_in_export.save
57
+ puts "Created SMS-In export: #{sms_in_export.id} (status: #{sms_in_export.status})"
58
+ else
59
+ puts "Error creating SMS-In export: #{sms_in_export.errors.full_messages}"
60
+ end
61
+
62
+ puts "\n=== Completed SMS-Out exports ==="
63
+ completed = DIDWW::Client.exports
64
+ .where(export_type: DIDWW::Resource::Export::EXPORT_TYPE_SMS_OUT, status: DIDWW::Resource::Export::STATUS_COMPLETED)
65
+ .all
66
+ puts "Found #{completed.size} completed SMS-Out exports"
67
+
68
+ if (export = completed.first)
69
+ csv = export.csv
70
+ header = csv.gets
71
+ puts "Export #{export.id} (#{export.export_type}) columns: #{header}"
72
+ end
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+ # Creates an identity and an address, encrypts and uploads a file, attaches a
3
+ # proof to the identity and one to the address, prints their verified flags,
4
+ # and deletes the address and the identity.
5
+ #
6
+ # 1. Create a personal identity with a country and a birth country (2026-04-16)
7
+ # 2. Create an address linked to the identity
8
+ # 3. Pick a proof type for each: entity_type "personal" and "address"
9
+ # 4. Encrypt sample.pdf and upload it twice, one file per proof
10
+ # 5. Create the proofs; DIDWW reviews them and sets verified later
11
+ # 6. Clean up, also when a step fails
12
+ #
13
+ # Usage: DIDWW_API_KEY=your_api_key ruby examples/identity_address_proofs.rb
14
+
15
+ require 'bundler/setup'
16
+ require 'didww'
17
+ require 'stringio'
18
+
19
+ DIDWW::Client.configure do |client|
20
+ client.api_key = ENV.fetch('DIDWW_API_KEY') { abort 'Please set DIDWW_API_KEY' }
21
+ client.api_mode = :sandbox
22
+ end
23
+
24
+ # Encrypts content and uploads it; returns the encrypted file ID
25
+ def upload_encrypted(enc, content, name)
26
+ DIDWW::Resource::EncryptedFile.upload(
27
+ encryption_fingerprint: enc.encryption_fingerprint,
28
+ file: Faraday::Multipart::FilePart.new(StringIO.new(enc.encrypt(content)), 'application/octet-stream', "#{name}.enc"),
29
+ description: name
30
+ )
31
+ end
32
+
33
+ # entity is the identity or the address the proof is for
34
+ def create_proof(label, entity, proof_type, file_id)
35
+ proof = DIDWW::Client.proofs.new
36
+ proof.entity = entity
37
+ proof.proof_type = proof_type
38
+ proof.files = [DIDWW::Resource::EncryptedFile.load(id: file_id)]
39
+ abort "Error creating #{label} proof: #{proof.errors.full_messages}" unless proof.save
40
+ puts "Created #{label} proof: #{proof.id} (type=#{proof_type.name})"
41
+ end
42
+
43
+ country = DIDWW::Client.countries.where(iso: 'US').all.first
44
+ abort 'Country US not found' unless country
45
+ puts "Using country: #{country.name} (#{country.id})"
46
+
47
+ identity = nil
48
+ address = nil
49
+ file_ids = []
50
+ begin
51
+ puts "\n=== Creating Identity ==="
52
+ identity = DIDWW::Client.identities.new(
53
+ identity_type: DIDWW::Resource::Identity::IDENTITY_TYPE_PERSONAL,
54
+ first_name: 'John',
55
+ last_name: 'Doe',
56
+ phone_number: '12125551234'
57
+ )
58
+ identity.country = country
59
+ identity.birth_country = country # 2026-04-16
60
+ abort "Error creating identity: #{identity.errors.full_messages}" unless identity.save
61
+ puts "Created identity: #{identity.id} (#{identity.first_name} #{identity.last_name})"
62
+
63
+ puts "\n=== Creating Address ==="
64
+ address = DIDWW::Client.addresses.new(
65
+ city_name: 'New York',
66
+ postal_code: '10001',
67
+ address: '123 Main St'
68
+ )
69
+ address.identity = identity
70
+ address.country = country
71
+ abort "Error creating address: #{address.errors.full_messages}" unless address.save
72
+ puts "Created address: #{address.id} (#{address.address})"
73
+
74
+ puts "\n=== Proof Types ==="
75
+ proof_types = DIDWW::Client.proof_types.all
76
+ identity_proof_type = proof_types.find { |pt| pt.entity_type == DIDWW::Resource::Identity::IDENTITY_TYPE_PERSONAL }
77
+ address_proof_type = proof_types.find { |pt| pt.entity_type == 'address' }
78
+ abort 'No proof type for personal identities' unless identity_proof_type
79
+ abort 'No proof type for addresses' unless address_proof_type
80
+ puts "Identity proof type: #{identity_proof_type.name} (#{identity_proof_type.id})"
81
+ puts "Address proof type: #{address_proof_type.name} (#{address_proof_type.id})"
82
+
83
+ puts "\n=== Encrypting and Uploading Files ==="
84
+ content = File.binread(File.join(__dir__, 'sample.pdf'))
85
+ enc = DIDWW::Encrypt.new # fetches the public keys
86
+ file_ids << upload_encrypted(enc, content, 'identity_proof.pdf')
87
+ file_ids << upload_encrypted(enc, content, 'address_proof.pdf')
88
+ puts "Uploaded encrypted files: #{file_ids.join(', ')}"
89
+
90
+ puts "\n=== Creating Proofs ==="
91
+ create_proof('identity', identity, identity_proof_type, file_ids[0])
92
+ create_proof('address', address, address_proof_type, file_ids[1])
93
+
94
+ puts "\n=== Verified Flags ==="
95
+ puts "Identity #{identity.id}: verified=#{DIDWW::Client.identities.find(identity.id).first.verified}"
96
+ puts "Address #{address.id}: verified=#{DIDWW::Client.addresses.find(address.id).first.verified}"
97
+ ensure
98
+ puts "\n=== Cleaning Up ==="
99
+ { 'address' => address, 'identity' => identity }.each do |label, resource|
100
+ next unless resource&.persisted?
101
+
102
+ if resource.destroy
103
+ puts "Deleted #{label}: #{resource.id}"
104
+ else
105
+ puts "Error deleting #{label} #{resource.id}: #{resource.errors.full_messages}"
106
+ end
107
+ end
108
+ file_ids.each do |file_id|
109
+ file = DIDWW::Resource::EncryptedFile.load(id: file_id)
110
+ if file.destroy
111
+ puts "Deleted encrypted file: #{file_id}"
112
+ else
113
+ puts "Error deleting encrypted file #{file_id}: #{file.errors.full_messages}"
114
+ end
115
+ rescue JsonApiClient::Errors::NotFound
116
+ puts "Encrypted file #{file_id} was removed with its proof"
117
+ end
118
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+ # Reads resources with included relationships (JSON:API `include`):
3
+ # regions with their country, DID groups with their SKUs, available DIDs with
4
+ # a nested include, DIDs with their voice trunk. Then shows how the SDK exposes
5
+ # an included relationship and a not-included one.
6
+ #
7
+ # * Included: the response carries `data` for the relationship and the record
8
+ # in `included`; the reader returns that record without another request.
9
+ # An empty to-one relationship reads as nil.
10
+ # * Not included: the response carries only `links`; the reader GETs the
11
+ # relationship's `related` link, one request per record. Include what you read.
12
+ #
13
+ # Usage: DIDWW_API_KEY=your_api_key ruby examples/includes.rb
14
+
15
+ require 'bundler/setup'
16
+ require 'didww'
17
+
18
+ DIDWW::Client.configure do |client|
19
+ client.api_key = ENV.fetch('DIDWW_API_KEY') { abort 'Please set DIDWW_API_KEY' }
20
+ client.api_mode = :sandbox
21
+ end
22
+
23
+ # Regions with included country
24
+ puts '=== Regions with country ==='
25
+ regions = DIDWW::Client.regions.includes(:country).page(1).per(3).all
26
+ regions.each do |region|
27
+ puts " #{region.name} (#{region.iso}) - country: #{region.country&.name}"
28
+ end
29
+
30
+ # DID groups with included stock keeping units (to-many)
31
+ puts "\n=== DID groups with SKUs ==="
32
+ did_groups = DIDWW::Client.did_groups.includes(:stock_keeping_units).page(1).per(3).all
33
+ did_groups.each do |group|
34
+ skus = group.stock_keeping_units || []
35
+ puts " #{group.prefix} #{group.area_name} - #{skus.size} SKU(s)"
36
+ skus.each { |sku| puts " $#{sku.monthly_price&.to_s('F')}/mo, #{sku.channels_included_count} channels" }
37
+ end
38
+
39
+ # Available DIDs with a nested include: did_group and the group's SKUs
40
+ puts "\n=== Available DIDs with did_group.stock_keeping_units ==="
41
+ available_dids = DIDWW::Client.available_dids.includes('did_group.stock_keeping_units').page(1).per(3).all
42
+ available_dids.each do |available_did|
43
+ group = available_did.did_group
44
+ skus = group&.stock_keeping_units || []
45
+ puts " #{available_did.number} - group: #{group&.prefix} #{group&.area_name}, #{skus.size} SKU(s)"
46
+ end
47
+
48
+ # DIDs with included voice trunk; a DID without one reads nil
49
+ puts "\n=== DIDs with voice_in_trunk ==="
50
+ dids = DIDWW::Client.dids.includes(:voice_in_trunk).page(1).per(3).all
51
+ dids.each do |did|
52
+ puts " #{did.number} - trunk: #{did.voice_in_trunk&.name || '(none)'}"
53
+ end
54
+
55
+ # Included vs not included
56
+ puts "\n=== Included vs not included ==="
57
+ included = DIDWW::Client.regions.includes(:country).page(1).per(1).all.first
58
+ plain = DIDWW::Client.regions.page(1).per(1).all.first
59
+ if included && plain
60
+ # The raw relationship object of the response
61
+ puts " included: relationships.country has #{included.relationships.country.keys.sort.join(', ')}"
62
+ puts " not included: relationships.country has #{plain.relationships.country.keys.sort.join(', ')}"
63
+ puts " included: region.country = #{included.country&.name} (from the included data, no request)"
64
+ puts " not included: region.country = #{plain.country&.name} (GET #{plain.relationships.country.dig('links', 'related')})"
65
+ end
data/examples/orders.rb CHANGED
@@ -26,16 +26,18 @@ end
26
26
 
27
27
  # Create an order with DID order items
28
28
  puts "\n=== Creating DID Order ==="
29
- did_groups = DIDWW::Client.did_groups
30
- .includes(:stock_keeping_units)
31
- .all
29
+ # A DID group with numbers in stock, so that the order can be filled
30
+ did_group = DIDWW::Client.did_groups
31
+ .where(is_available: true)
32
+ .includes(:stock_keeping_units)
33
+ .all
34
+ .find { |group| group.stock_keeping_units&.any? }
32
35
 
33
- if did_groups.empty? || did_groups.first.stock_keeping_units.nil? || did_groups.first.stock_keeping_units.empty?
34
- puts 'No DID group with stock_keeping_units found'
36
+ unless did_group
37
+ puts 'No available DID group with stock_keeping_units found'
35
38
  exit 1
36
39
  end
37
40
 
38
- did_group = did_groups.first
39
41
  sku = did_group.stock_keeping_units.first
40
42
 
41
43
  did_item = DIDWW::ComplexObject::DidOrderItem.new(