telnyx 0.0.6 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ab5e38d59a795014bb8cd179a7b2d621730070a140fb0a089c8bd873be92cf3
4
- data.tar.gz: 1070f9b6389bb74a793c47e0926533a69cf188859e8fb21c5d10e9224e1c5b75
3
+ metadata.gz: 954fc20c0162c4c8d7a2a4452e5d892b1ab4a46b2cb080af7b4fd3d4d9fed258
4
+ data.tar.gz: afac25a5c6384f6ad5aacefacbfa0a6eb5927a8d19744865eba79328e9560108
5
5
  SHA512:
6
- metadata.gz: 16eb703601a65504320838551bdec62e07014a015e4817d5fa8a372e3d24cc35bd5ba8f2a583b8c015379bda0754a09e9d54776fe7e0fd893d60fc17db590f55
7
- data.tar.gz: '08351961080c07cffe34d3d882b6a1b0df5013a37a6e19092ddd9579eb7ce6246f9f3fbcfbc92d11c180fabe561ae15c841c8513ed9917c66f8bfec62e034775'
6
+ metadata.gz: e284d6bf79df563365f76e9355dc7009e58bbbcbb984f440e45bdf55d297fdbb9b742484418dec64de4d5a99c86b626fec113093a83d02f39f2f65d11a0a813f
7
+ data.tar.gz: 6a233a3042db655c72511203c9e79d3b1b80ae64b3ec38dacf5c1b31a8c3ee417291428ce57a3b7c2d9e35ba3b0a81ad66529a781322937da044eb0bb26fa2c7
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -46,7 +46,9 @@ require "telnyx/messaging_phone_number"
46
46
  require "telnyx/messaging_profile"
47
47
  require "telnyx/number_order"
48
48
  require "telnyx/number_reservation"
49
+ require "telnyx/phone_number"
49
50
  require "telnyx/public_key"
51
+ require "telnyx/sim_card.rb"
50
52
 
51
53
  module Telnyx
52
54
  @app_info = nil
@@ -6,11 +6,12 @@ module Telnyx
6
6
  # that it's possible to do so from a static context (i.e. without a
7
7
  # pre-existing collection of subresources on the parent).
8
8
  #
9
- # For examle, a transfer gains the static methods for reversals so that the
9
+ # For example, a transfer gains the static methods for reversals so that the
10
10
  # methods `.create_reversal`, `.retrieve_reversal`, `.update_reversal`,
11
11
  # etc. all become available.
12
12
  # rubocop:disable Metrics/AbcSize
13
13
  # rubocop:disable Metrics/MethodLength
14
+ # rubocop:disable Metrics/CyclomaticComplexity
14
15
  module NestedResource
15
16
  def nested_resource_class_methods(resource, path: nil, operations: nil, instance_methods: {})
16
17
  path ||= "#{resource}s"
@@ -29,8 +30,19 @@ module Telnyx
29
30
  define_instance_method = lambda do |target_name, operation|
30
31
  return unless instance_methods.keys.include? operation
31
32
 
32
- define_method(instance_methods[operation] || target_name) do |*opts|
33
- self.class.send(target_name, id, *opts)
33
+ case operation
34
+ when :create, :list
35
+ define_method(instance_methods[operation] || target_name) do |*opts|
36
+ self.class.send(target_name, id, *opts)
37
+ end
38
+ when :retrieve
39
+ define_method(instance_methods[operation] || target_name) do |nested_id|
40
+ self.class.send(target_name, id, nested_id)
41
+ end
42
+ when :update, :delete
43
+ define_method(instance_methods[operation] || target_name) do |nested_id, *opts|
44
+ self.class.send(target_name, id, nested_id, *opts)
45
+ end
34
46
  end
35
47
  end
36
48
 
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telnyx
4
+ class PhoneNumber < APIResource
5
+ include Telnyx::APIOperations::Save
6
+ include Telnyx::APIOperations::Delete
7
+ extend Telnyx::APIOperations::List
8
+ extend Telnyx::APIOperations::NestedResource
9
+
10
+ nested_resource_class_methods "voice",
11
+ path: ["voice"],
12
+ operations: %i[update list],
13
+ instance_methods: { list: "voice" }
14
+ nested_resource_class_methods "messaging",
15
+ path: ["messaging"],
16
+ operations: %i[update list],
17
+ instance_methods: { list: "messaging" }
18
+
19
+ def update_messaging(opts)
20
+ self.class.update_messaging(id, nil, opts)
21
+ end
22
+
23
+ def update_voice(opts)
24
+ self.class.update_voice(id, nil, opts)
25
+ end
26
+
27
+ OBJECT_NAME = "phone_number".freeze
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telnyx
4
+ class SimCard < APIResource
5
+ include Telnyx::APIOperations::Save
6
+ extend APIOperations::NestedResource
7
+ extend APIOperations::List
8
+
9
+ ACTIONS = %w[deactivate activate].freeze
10
+ ACTIONS.each do |action|
11
+ nested_resource_class_methods action,
12
+ path: %W[actions #{action}],
13
+ operations: [:create],
14
+ instance_methods: { create: action }
15
+ end
16
+
17
+ OBJECT_NAME = "sim_card".freeze
18
+ end
19
+ end
@@ -26,7 +26,7 @@ module Telnyx
26
26
  # This may throw JSON::ParserError if the response body is not valid JSON.
27
27
  def self.from_faraday_hash(http_resp)
28
28
  resp = TelnyxResponse.new
29
- resp.data = JSON.parse(http_resp[:body], symbolize_names: true)
29
+ resp.data = JSON.parse(preprocess_response(http_resp[:body]), symbolize_names: true)
30
30
  resp.http_body = http_resp[:body]
31
31
  resp.http_headers = http_resp[:headers]
32
32
  resp.http_status = http_resp[:status]
@@ -39,12 +39,21 @@ module Telnyx
39
39
  # This may throw JSON::ParserError if the response body is not valid JSON.
40
40
  def self.from_faraday_response(http_resp)
41
41
  resp = TelnyxResponse.new
42
- resp.data = JSON.parse(http_resp.body, symbolize_names: true)
42
+ resp.data = JSON.parse(preprocess_response(http_resp.body), symbolize_names: true)
43
43
  resp.http_body = http_resp.body
44
44
  resp.http_headers = http_resp.headers
45
45
  resp.http_status = http_resp.status
46
46
  resp.request_id = http_resp.headers["X-Request-Id"]
47
47
  resp
48
48
  end
49
+
50
+ class << self
51
+ private
52
+
53
+ # Helper to handle when the server responds with a blank body (as is the case with SimCards).
54
+ def preprocess_response(resp)
55
+ resp.empty? ? "{}" : resp
56
+ end
57
+ end
49
58
  end
50
59
  end
@@ -49,6 +49,8 @@ module Telnyx
49
49
  NumberOrder::OBJECT_NAME => NumberOrder,
50
50
  NumberReservation::OBJECT_NAME => NumberReservation,
51
51
  PublicKey::OBJECT_NAME => PublicKey,
52
+ PhoneNumber::OBJECT_NAME => PhoneNumber,
53
+ SimCard::OBJECT_NAME => SimCard,
52
54
  }
53
55
  end
54
56
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Telnyx
4
- VERSION = "0.0.6".freeze
4
+ VERSION = "0.0.7".freeze
5
5
  end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../test_helper"
4
+ module Telnyx
5
+ class PhoneNumberTest < Test::Unit::TestCase
6
+ should "list resources" do
7
+ stub = stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers")
8
+ .to_return(body: JSON.generate(data: (1..5).map { |i| mock_response(i) }))
9
+ phone_numbers = Telnyx::PhoneNumber.list
10
+ assert_requested stub
11
+ assert_kind_of Telnyx::ListObject, phone_numbers
12
+ assert_kind_of Telnyx::PhoneNumber, phone_numbers.first
13
+ end
14
+
15
+ should "get resource" do
16
+ stub = stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers/123")
17
+ .to_return(body: JSON.generate(data: mock_response("123")))
18
+ phone_number = Telnyx::PhoneNumber.retrieve("123")
19
+ assert_kind_of Telnyx::PhoneNumber, phone_number
20
+ assert_requested stub
21
+ assert_equal phone_number.id, "123"
22
+ end
23
+
24
+ should "update resource" do
25
+ stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers/123")
26
+ .to_return(body: JSON.generate(data: mock_response("123")))
27
+ stub = stub_request(:patch, "#{Telnyx.api_base}/v2/phone_numbers/123")
28
+ .to_return(body: JSON.generate(data: mock_response("123")))
29
+ phone_number = Telnyx::PhoneNumber.retrieve("123")
30
+ phone_number.save
31
+ assert_requested stub
32
+ end
33
+
34
+ should "delete resource" do
35
+ stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers/123")
36
+ .to_return(body: JSON.generate(data: mock_response("123")))
37
+ stub = stub_request(:delete, "#{Telnyx.api_base}/v2/phone_numbers/123")
38
+ .to_return(body: JSON.generate(data: mock_response("123")))
39
+ phone_number = Telnyx::PhoneNumber.retrieve("123")
40
+ phone_number.delete
41
+ assert_requested stub
42
+ end
43
+
44
+ context "nested commands" do
45
+ should "update voice" do
46
+ stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers/123")
47
+ .to_return(body: JSON.generate(data: mock_response("123")))
48
+ command_stub = stub_request(:patch, "#{Telnyx.api_base}/v2/phone_numbers/123/voice")
49
+ .with(body: { connection_id: 123 })
50
+ .to_return(body: JSON.generate(data: mock_voice_response))
51
+ phone_number = Telnyx::PhoneNumber.retrieve("123")
52
+ phone_number.update_voice(connection_id: 123)
53
+ assert_requested command_stub
54
+ end
55
+
56
+ should "get voice" do
57
+ stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers/123")
58
+ .to_return(body: JSON.generate(data: mock_response("123")))
59
+ command_stub = stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers/123/voice")
60
+ .to_return(body: JSON.generate(data: mock_voice_response("456")))
61
+ phone_number = Telnyx::PhoneNumber.retrieve("123")
62
+ voice = phone_number.voice
63
+ assert_requested command_stub
64
+ assert_equal voice.data.connection_id, "456"
65
+ end
66
+
67
+ should "get messaging" do
68
+ stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers/123")
69
+ .to_return(body: JSON.generate(data: mock_response("123")))
70
+ command_stub = stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers/123/messaging")
71
+ .to_return(body: JSON.generate(data: mock_messaging_response("456")))
72
+ phone_number = Telnyx::PhoneNumber.retrieve("123")
73
+ phone_number.messaging
74
+ assert_requested command_stub
75
+ end
76
+
77
+ should "update messaging" do
78
+ stub_request(:get, "#{Telnyx.api_base}/v2/phone_numbers/123")
79
+ .to_return(body: JSON.generate(data: mock_response("123")))
80
+ command_stub = stub_request(:patch, "#{Telnyx.api_base}/v2/phone_numbers/123/messaging")
81
+ .with(body: { messaging_profile_id: "12345", messaging_product: "P2P" })
82
+ .to_return(body: JSON.generate(data: mock_messaging_response))
83
+ phone_number = Telnyx::PhoneNumber.retrieve("123")
84
+ phone_number.update_messaging(messaging_profile_id: "12345", messaging_product: "P2P")
85
+ assert_requested command_stub
86
+ end
87
+ end
88
+
89
+ def mock_response(id = nil)
90
+ {
91
+ id: (id || rand(0...1_000_000_000_000)),
92
+ record_type: :phone_number,
93
+ status: rand(1..10),
94
+ phone_number: "+15555555555",
95
+ address_id: rand(0...1_000_000_000_000),
96
+ }
97
+ end
98
+
99
+ def mock_voice_response(id = nil)
100
+ {
101
+ connection_id: (id || rand(0...1_000_000_000_000)),
102
+ connection_name: "Telnyx connection",
103
+ }
104
+ end
105
+
106
+ def mock_messaging_response(_id = nil)
107
+ {
108
+ record_type: "messaging_phone_number",
109
+ phone_number: "+18005550001",
110
+ id: "+18665550001",
111
+ }
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../test_helper"
4
+
5
+ module Telnyx
6
+ class SimCardTest < Test::Unit::TestCase
7
+ should "retrieve sim card" do
8
+ stub = stub_request(:get, "#{Telnyx.api_base}/v2/sim_cards/123")
9
+ .to_return(body: JSON.generate(data: { record_type: "sim_card", id: "123" }))
10
+ sim = Telnyx::SimCard.retrieve "123"
11
+ assert_requested stub
12
+ assert_kind_of Telnyx::SimCard, sim
13
+ end
14
+
15
+ should "list sim cards" do
16
+ stub = stub_request(:get, "#{Telnyx.api_base}/v2/sim_cards")
17
+ .to_return(body: JSON.generate(data: (1..5).map { |i| { record_type: "sim_card", id: i } }))
18
+ simlist = Telnyx::SimCard.list
19
+ assert_requested stub
20
+ assert_kind_of Telnyx::ListObject, simlist
21
+ end
22
+
23
+ should "save sim card" do
24
+ stub_request(:get, "#{Telnyx.api_base}/v2/sim_cards/123")
25
+ .to_return(body: JSON.generate(data: { record_type: "sim_card", id: "123" }))
26
+ stub = stub_request(:patch, "#{Telnyx.api_base}/v2/sim_cards/123")
27
+ .to_return(body: JSON.generate(data: { record_type: "sim_card", id: "123" }))
28
+ sim = Telnyx::SimCard.retrieve "123"
29
+ sim.save
30
+ assert_requested stub
31
+ end
32
+
33
+ context "actions" do
34
+ should "deactivate" do
35
+ stub_request(:get, "#{Telnyx.api_base}/v2/sim_cards/123")
36
+ .to_return(body: JSON.generate(data: { record_type: "sim_card", id: "123" }))
37
+
38
+ stub = stub_request(:post, "#{Telnyx.api_base}/v2/sim_cards/123/actions/deactivate")
39
+ .to_return(body: JSON.generate(errors: []), status: 202)
40
+
41
+ sim = Telnyx::SimCard.retrieve "123"
42
+ sim.deactivate
43
+ assert_requested stub
44
+ end
45
+
46
+ should "activate" do
47
+ stub_request(:get, "#{Telnyx.api_base}/v2/sim_cards/123")
48
+ .to_return(body: JSON.generate(data: { record_type: "sim_card", id: "123" }))
49
+
50
+ stub = stub_request(:post, "#{Telnyx.api_base}/v2/sim_cards/123/actions/activate")
51
+ .to_return(body: JSON.generate(errors: []), status: 202)
52
+
53
+ sim = Telnyx::SimCard.retrieve "123"
54
+ sim.activate
55
+ assert_requested stub
56
+ end
57
+ end
58
+ end
59
+ end
@@ -324,17 +324,18 @@ module Telnyx
324
324
  assert_equal 'Invalid response object from API: "" (HTTP response code was 500)', e.message
325
325
  end
326
326
 
327
- should "handle success response with empty body" do
328
- stub_request(:post, "#{Telnyx.api_base}/v2/messaging_profiles")
329
- .to_return(body: "", status: 200)
330
-
331
- client = TelnyxClient.new
332
- e = assert_raises Telnyx::APIError do
333
- client.execute_request(:post, "/v2/messaging_profiles")
334
- end
335
-
336
- assert_equal 'Invalid response object from API: "" (HTTP response code was 200)', e.message
337
- end
327
+ # Disabled this due to incompatibility with sim endpont actions.
328
+ # should "handle success response with empty body" do
329
+ # stub_request(:post, "#{Telnyx.api_base}/v2/messaging_profiles")
330
+ # .to_return(body: "", status: 200)
331
+
332
+ # client = TelnyxClient.new
333
+ # e = assert_raises Telnyx::APIError do
334
+ # client.execute_request(:post, "/v2/messaging_profiles")
335
+ # end
336
+
337
+ # assert_equal 'Invalid response object from API: "" (HTTP response code was 200)', e.message
338
+ # end
338
339
 
339
340
  should "handle error response with unknown value" do
340
341
  stub_request(:post, "#{Telnyx.api_base}/v2/messaging_profiles")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telnyx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Telnyx
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-08 00:00:00.000000000 Z
11
+ date: 2019-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -112,7 +112,9 @@ files:
112
112
  - lib/telnyx/messaging_profile.rb
113
113
  - lib/telnyx/number_order.rb
114
114
  - lib/telnyx/number_reservation.rb
115
+ - lib/telnyx/phone_number.rb
115
116
  - lib/telnyx/public_key.rb
117
+ - lib/telnyx/sim_card.rb
116
118
  - lib/telnyx/singleton_api_resource.rb
117
119
  - lib/telnyx/telnyx_client.rb
118
120
  - lib/telnyx/telnyx_object.rb
@@ -136,7 +138,9 @@ files:
136
138
  - test/telnyx/messaging_profile_test.rb
137
139
  - test/telnyx/number_order_test.rb
138
140
  - test/telnyx/number_reservation_test.rb
141
+ - test/telnyx/phone_number_test.rb
139
142
  - test/telnyx/public_key_test.rb
143
+ - test/telnyx/sim_card_test.rb
140
144
  - test/telnyx/telnyx_client_test.rb
141
145
  - test/telnyx/telnyx_object_test.rb
142
146
  - test/telnyx/telnyx_response_test.rb
@@ -189,7 +193,9 @@ test_files:
189
193
  - test/telnyx/messaging_profile_test.rb
190
194
  - test/telnyx/number_order_test.rb
191
195
  - test/telnyx/number_reservation_test.rb
196
+ - test/telnyx/phone_number_test.rb
192
197
  - test/telnyx/public_key_test.rb
198
+ - test/telnyx/sim_card_test.rb
193
199
  - test/telnyx/telnyx_client_test.rb
194
200
  - test/telnyx/telnyx_object_test.rb
195
201
  - test/telnyx/telnyx_response_test.rb