phaxio 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +2 -1
  3. data/README.md +53 -20
  4. data/lib/phaxio/client.rb +46 -16
  5. data/lib/phaxio/config.rb +7 -5
  6. data/lib/phaxio/resource.rb +11 -5
  7. data/lib/phaxio/resources/account.rb +1 -1
  8. data/lib/phaxio/resources/ata.rb +285 -0
  9. data/lib/phaxio/resources/callback.rb +10 -56
  10. data/lib/phaxio/resources/fax.rb +11 -11
  11. data/lib/phaxio/resources/phax_code.rb +2 -2
  12. data/lib/phaxio/resources/phone_number.rb +31 -5
  13. data/lib/phaxio/resources/port_number.rb +42 -0
  14. data/lib/phaxio/resources/port_number_note.rb +51 -0
  15. data/lib/phaxio/resources/port_order.rb +172 -0
  16. data/lib/phaxio/resources/public/area_code.rb +1 -1
  17. data/lib/phaxio/resources/public/country.rb +1 -1
  18. data/lib/phaxio/resources/webhook.rb +65 -0
  19. data/lib/phaxio/version.rb +1 -1
  20. data/lib/phaxio.rb +13 -3
  21. data/phaxio.gemspec +2 -1
  22. data/spec/client_spec.rb +38 -6
  23. data/spec/phaxio_spec.rb +7 -1
  24. data/spec/resources/account_spec.rb +1 -7
  25. data/spec/resources/ata_spec.rb +207 -0
  26. data/spec/resources/fax_spec.rb +42 -89
  27. data/spec/resources/phax_code_spec.rb +26 -32
  28. data/spec/resources/phone_number_spec.rb +19 -30
  29. data/spec/resources/port_number_note_spec.rb +33 -0
  30. data/spec/resources/port_number_spec.rb +32 -0
  31. data/spec/resources/port_order_spec.rb +76 -0
  32. data/spec/resources/public/area_code_spec.rb +1 -1
  33. data/spec/resources/public/country_spec.rb +1 -7
  34. data/spec/resources/{callback_spec.rb → webhook_spec.rb} +8 -8
  35. data/spec/spec_helper.rb +1 -0
  36. data/spec/support/{vcr_cassettes/resources/account/status.yml → cassettes/account/get.yml} +13 -14
  37. data/spec/support/cassettes/ata/add_phone_number.yml +89 -0
  38. data/spec/support/cassettes/ata/create.yml +46 -0
  39. data/spec/support/cassettes/ata/delete.yml +86 -0
  40. data/spec/support/cassettes/ata/get.yml +87 -0
  41. data/spec/support/cassettes/ata/list.yml +67 -0
  42. data/spec/support/cassettes/ata/provisioning_urls.yml +43 -0
  43. data/spec/support/cassettes/ata/reference.yml +87 -0
  44. data/spec/support/cassettes/ata/regenerate.yml +89 -0
  45. data/spec/support/cassettes/ata/remove_phone_number.yml +130 -0
  46. data/spec/support/cassettes/ata/update.yml +89 -0
  47. data/spec/support/cassettes/fax/cancel.yml +90 -0
  48. data/spec/support/cassettes/fax/create.yml +48 -0
  49. data/spec/support/cassettes/fax/delete.yml +88 -0
  50. data/spec/support/cassettes/fax/delete_file.yml +88 -0
  51. data/spec/support/cassettes/fax/file.yml +93 -0
  52. data/spec/support/cassettes/fax/get.yml +88 -0
  53. data/spec/support/cassettes/fax/list.yml +49 -0
  54. data/spec/support/cassettes/fax/reference.yml +88 -0
  55. data/spec/support/cassettes/fax/resend.yml +90 -0
  56. data/spec/support/cassettes/fax/test_receive.yml +49 -0
  57. data/spec/support/{vcr_cassettes/resources/fax/resend.yml → cassettes/phax_code/create.yml} +14 -15
  58. data/spec/support/cassettes/phax_code/create_png.yml +50 -0
  59. data/spec/support/cassettes/phax_code/get_by_id.yml +86 -0
  60. data/spec/support/{vcr_cassettes/resources/fax/delete.yml → cassettes/phax_code/get_default.yml} +14 -15
  61. data/spec/support/cassettes/phax_code/get_default_png.yml +48 -0
  62. data/spec/support/cassettes/phax_code/get_png_by_id.yml +90 -0
  63. data/spec/support/{vcr_cassettes/resources → cassettes}/phone_number/create.yml +15 -16
  64. data/spec/support/cassettes/phone_number/get.yml +44 -0
  65. data/spec/support/cassettes/phone_number/list.yml +72 -0
  66. data/spec/support/cassettes/phone_number/reference.yml +45 -0
  67. data/spec/support/cassettes/phone_number/release.yml +86 -0
  68. data/spec/support/cassettes/port_number/get.yml +88 -0
  69. data/spec/support/cassettes/port_number_note/list.yml +88 -0
  70. data/spec/support/cassettes/port_order/create.yml +48 -0
  71. data/spec/support/cassettes/port_order/get.yml +91 -0
  72. data/spec/support/cassettes/port_order/list.yml +52 -0
  73. data/spec/support/{vcr_cassettes/resources/public/area_codes → cassettes/public/area_code}/list.yml +18 -18
  74. data/spec/support/{vcr_cassettes/resources → cassettes}/public/country/list.yml +14 -15
  75. data/spec/support/files/test.txt +1 -0
  76. data/spec/support/vcr.rb +25 -8
  77. metadata +125 -31
  78. data/spec/support/credentials.rb +0 -7
  79. data/spec/support/vcr_cassettes/resources/fax/cancel.yml +0 -46
  80. data/spec/support/vcr_cassettes/resources/fax/create.yml +0 -230
  81. data/spec/support/vcr_cassettes/resources/fax/delete_file.yml +0 -44
  82. data/spec/support/vcr_cassettes/resources/fax/file.yml +0 -251
  83. data/spec/support/vcr_cassettes/resources/fax/get.yml +0 -44
  84. data/spec/support/vcr_cassettes/resources/fax/list.yml +0 -56
  85. data/spec/support/vcr_cassettes/resources/fax/test_receive.yml +0 -231
  86. data/spec/support/vcr_cassettes/resources/phax_code/create.yml +0 -100
  87. data/spec/support/vcr_cassettes/resources/phax_code/get.yml +0 -190
  88. data/spec/support/vcr_cassettes/resources/phone_number/get.yml +0 -45
  89. data/spec/support/vcr_cassettes/resources/phone_number/list.yml +0 -52
  90. data/spec/support/vcr_cassettes/resources/phone_number/release.yml +0 -44
@@ -84,7 +84,7 @@ module Phaxio
84
84
  attr_accessor :id
85
85
 
86
86
  # Gets the referenced fax.
87
- # @return [Phaxio::Resource::Fax]
87
+ # @return [Phaxio::Resources::Fax]
88
88
  # The referenced Fax.
89
89
  def get
90
90
  Fax.get self
@@ -120,7 +120,7 @@ module Phaxio
120
120
  # @return [Phaxio::Resource::Collection<Phaxio::Resources::Fax>]
121
121
  # The collection of faxes matching your request.
122
122
  # @raise [Phaxio::Error::PhaxioError]
123
- # @see https://www.phaxio.com/docs/api/v2/faxes/list_faxes
123
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/list_faxes
124
124
  def list params = {}
125
125
  response = Client.request :get, faxes_endpoint, params
126
126
  response_collection response
@@ -159,8 +159,8 @@ module Phaxio
159
159
  # @return [Phaxio::Resources::Fax]
160
160
  # The created fax.
161
161
  # @raise [Phaxio::Error::PhaxioError]
162
- # @see https://www.phaxio.com/docs/api/v2/faxes/create_and_send_fax
163
- # @see https://www.phaxio.com/docs/api/v2/faxes/batching
162
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/create_and_send_fax
163
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/batching
164
164
  def create params = {}
165
165
  response = Client.request :post, faxes_endpoint, params
166
166
  response_reference response
@@ -174,7 +174,7 @@ module Phaxio
174
174
  # A hash of parameters to send to Phaxio. This action takes no unique parameters.
175
175
  # @return [Phaxio::Resource::Fax] The requested fax.
176
176
  # @raise [Phaxio::Error::PhaxioError]
177
- # @see https://www.phaxio.com/docs/api/v2/faxes/get_fax
177
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/get_fax
178
178
  def get id, params = {}
179
179
  response = Client.request :get, fax_endpoint(id.to_i), params
180
180
  response_record response
@@ -191,7 +191,7 @@ module Phaxio
191
191
  # @return [Phaxio::Resources::Fax::Reference]
192
192
  # A reference to the canceled fax.
193
193
  # @raise [Phaxio::Error::PhaxioError]
194
- # @see https://www.phaxio.com/docs/api/v2/faxes/cancel
194
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/cancel
195
195
  def cancel id, params = {}
196
196
  response = Client.request :post, cancel_fax_endpoint(id), params
197
197
  response_reference response
@@ -207,7 +207,7 @@ module Phaxio
207
207
  # @return [Phaxio::Resources::Fax::Reference]
208
208
  # A reference to the resent fax.
209
209
  # @raise [Phaxio::Error::PhaxioError]
210
- # @see https://www.phaxio.com/docs/api/v2/faxes/resend
210
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/resend
211
211
  def resend id, params = {}
212
212
  response = Client.request :post, resend_fax_endpoint(id), params
213
213
  response_reference response
@@ -221,7 +221,7 @@ module Phaxio
221
221
  # parameters.
222
222
  # @return [true]
223
223
  # @raise [Phaxio::Error::PhaxioError]
224
- # @see https://www.phaxio.com/docs/api/v2/faxes/delete_fax
224
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/delete_fax
225
225
  def delete id, params = {}
226
226
  Client.request :delete, fax_endpoint(id), params
227
227
  true
@@ -235,7 +235,7 @@ module Phaxio
235
235
  # parameters.
236
236
  # @return [true]
237
237
  # @raise [Phaxio::Error::PhaxioError]
238
- # @see https://www.phaxio.com/docs/api/v2/faxes/delete_fax_file
238
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/delete_fax_file
239
239
  def delete_file id, params = {}
240
240
  Client.request :delete, fax_file_endpoint(id), params
241
241
  true
@@ -252,7 +252,7 @@ module Phaxio
252
252
  # @return [File]
253
253
  # The requested fax file.
254
254
  # @raise [Phaxio::Error::PhaxioError]
255
- # @see https://www.phaxio.com/docs/api/v2/faxes/get_fax_file
255
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/get_fax_file
256
256
  def file id, params = {}
257
257
  Client.request :get, fax_file_endpoint(id), params
258
258
  end
@@ -269,7 +269,7 @@ module Phaxio
269
269
  # number. Default is the public Phaxio phone number.
270
270
  # @return [true]
271
271
  # @raise [Phaxio::Error::PhaxioError]
272
- # @see https://www.phaxio.com/docs/api/v2/faxes/test_receive
272
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/test_receive
273
273
  def test_receive params = {}
274
274
  Client.request :post, faxes_endpoint, test_receive_params(params)
275
275
  true
@@ -28,7 +28,7 @@ module Phaxio
28
28
  # file instead of a PhaxCode object.
29
29
  # @return [Phaxio::Resources::PhaxCode | File] The created PhaxCode
30
30
  # @raise [Phaxio::Error::PhaxioError]
31
- # @see https://www.phaxio.com/docs/api/v2/phaxcodes/create_phax_code
31
+ # @see https://www.phaxio.com/docs/api/v2.1/phaxcodes/create_phax_code
32
32
  def create params = {}
33
33
  endpoint = case (params[:type] || params['type']).to_s
34
34
  when 'png' then phax_codes_endpoint_png
@@ -48,7 +48,7 @@ module Phaxio
48
48
  # PhaxCode object.
49
49
  # @return [Phaxio::Resources::PhaxCode | File]
50
50
  # @raise [Phaxio::Error::PhaxioError]
51
- # @see https://www.phaxio.com/docs/api/v2/phaxcodes/retrieve_phax_code
51
+ # @see https://www.phaxio.com/docs/api/v2.1/phaxcodes/retrieve_phax_code
52
52
  def get params = {}
53
53
  identifier = params[:identifier] || params['identifier']
54
54
  endpoint = case (identifier)
@@ -33,6 +33,32 @@ module Phaxio
33
33
 
34
34
  has_time_attributes %w(provisioned_at last_billed_at)
35
35
 
36
+ # A reference to a phone number, returned by some actions.
37
+ class Reference
38
+ # @return [String]
39
+ # The phone number in E.164 format.
40
+ attr_accessor :phone_number
41
+
42
+ def to_s
43
+ phone_number
44
+ end
45
+
46
+ # Gets the referenced phone number.
47
+ # @return [Phaxio::Resources::PhoneNumber]
48
+ # The referenced phone number.
49
+ def get
50
+ PhoneNumber.get self
51
+ end
52
+ alias :retrieve :get
53
+ alias :find :get
54
+
55
+ private
56
+
57
+ def initialize phone_number
58
+ self.phone_number = phone_number
59
+ end
60
+ end
61
+
36
62
  private
37
63
 
38
64
  class << self
@@ -44,7 +70,7 @@ module Phaxio
44
70
  # - *callback_url* [String] - A callback URL that we'll post to when a fax is received by this number. This will override the global receive callback URL, if you have one specified.
45
71
  # @return [Phaxio::Resources::PhoneNumber] The newly provisioned number.
46
72
  # @raise [Phaxio::Error::PhaxioError]
47
- # @see https://www.phaxio.com/docs/api/v2/phone_numbers/provision
73
+ # @see https://www.phaxio.com/docs/api/v2.1/phone_numbers/provision
48
74
  def create params = {}
49
75
  response = Client.request :post, phone_numbers_endpoint, params
50
76
  response_record response
@@ -58,9 +84,9 @@ module Phaxio
58
84
  # A hash of parameters to send to Phaxio. This action has no unique parameters.
59
85
  # @return [Phaxio::Resources::PhoneNumber] The requested number.
60
86
  # @raise [Phaxio::Error::PhaxioError]
61
- # @see https://www.phaxio.com/docs/api/v2/phone_numbers/get_number
87
+ # @see https://www.phaxio.com/docs/api/v2.1/phone_numbers/get_number
62
88
  def get phone_number, params = {}
63
- response = Client.request :get, phone_number_endpoint(phone_number), params
89
+ response = Client.request :get, phone_number_endpoint(phone_number.to_s), params
64
90
  response_record response
65
91
  end
66
92
  alias :find :get
@@ -76,7 +102,7 @@ module Phaxio
76
102
  # @return [Phaxio::Resource::Collection<Phaxio::Resources::PhoneNumber>]
77
103
  # A collection of phone numbers.
78
104
  # @raise [Phaxio::Error::PhaxioError]
79
- # @see https://www.phaxio.com/docs/api/v2/phone_numbers/list
105
+ # @see https://www.phaxio.com/docs/api/v2.1/phone_numbers/list
80
106
  def list params = {}
81
107
  response = Client.request :get, phone_numbers_endpoint, params
82
108
  response_collection response
@@ -90,7 +116,7 @@ module Phaxio
90
116
  # A hash of parameters to send to Phaxio. This action has no unique parameters.
91
117
  # @return true
92
118
  # @raise [Phaxio::Error::PhaxioError]
93
- # @see https://www.phaxio.com/docs/api/v2/phone_numbers/release
119
+ # @see https://www.phaxio.com/docs/api/v2.1/phone_numbers/release
94
120
  def delete phone_number, params = {}
95
121
  Client.request :delete, phone_number_endpoint(phone_number), params
96
122
  true
@@ -0,0 +1,42 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Provides functionality for viewing port numbers.
4
+ class PortNumber < Resource
5
+ PORT_NUMBERS_PATH = 'port_numbers'.freeze
6
+ private_constant :PORT_NUMBERS_PATH
7
+
8
+ # @return [Integer] the ID associated with this number.
9
+ # @!attribute id
10
+
11
+ # @return [String] the E.164 number.
12
+ # @!attribute port_number
13
+
14
+ # @return [String] the status of this number.
15
+ # @!attribute status
16
+
17
+ has_normal_attributes %w(id port_number status)
18
+
19
+ class << self
20
+ # Get port number info.
21
+ # @param id [Integer] The ID of the number to retrieve.
22
+ # @param params [Hash]
23
+ # A hash of parameters to send to Phaxio. This action takes no unique parameters.
24
+ # @return [Phaxio::Resource::PortNumber]
25
+ # @raise [Phaxio::Error::PhaxioError]
26
+ # @see https://www.phaxio.com/docs/api/v2/port_numbers/get_port_number
27
+ def get id, params = {}
28
+ response = Client.request :get, port_number_endpoint(id.to_i), params
29
+ response_record response
30
+ end
31
+ alias :retrieve :get
32
+ alias :find :get
33
+
34
+ private
35
+
36
+ def port_number_endpoint id
37
+ "#{PORT_NUMBERS_PATH}/#{id}"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,51 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Provides functionality for viewing port number notes.
4
+ class PortNumberNote < Resource
5
+ PORT_NUMBERS_PATH = 'port_numbers'.freeze
6
+ private_constant :PORT_NUMBERS_PATH
7
+ NOTES_PATH = 'notes'.freeze
8
+ private_constant :NOTES_PATH
9
+
10
+ # @return [Integer] the ID associated with this note.
11
+ # @!attribute id
12
+
13
+ # @return [String] the author of this note.
14
+ # @!attribute author
15
+
16
+ # @return [String] the content of the note
17
+ # @!attribute note
18
+
19
+ has_normal_attributes %w[id note author]
20
+
21
+ # @return [Time] the time this note was created.
22
+ # @!attribute created_at
23
+
24
+ # @return [Time] the time this note was updated.
25
+ # @!attribute updated_at
26
+
27
+ has_time_attributes %w[created_at updated_at]
28
+
29
+ class << self
30
+ # List notes for a port number.
31
+ # @param port_number_id [Integer]
32
+ # The ID of the port number to list notes for.
33
+ # @param params [Hash]
34
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
35
+ # @return [Phaxio::Resource::Collection<Phaxio::Resources::PortNumberNote>]
36
+ # @raise [Phaxio::Error::PhaxioError]
37
+ # @see https://www.phaxio.com/docs/api/v2/port_number_notes/list_notes
38
+ def list port_number_id, params = {}
39
+ response = Client.request :get, port_number_notes_endpoint(port_number_id.to_i), params
40
+ response_collection response
41
+ end
42
+
43
+ private
44
+
45
+ def port_number_notes_endpoint port_number_id
46
+ "#{PORT_NUMBERS_PATH}/#{port_number_id}/#{NOTES_PATH}"
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,172 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Provides functionality for viewing and managing port orders.
4
+ class PortOrder < Resource
5
+ PORT_ORDERS_PATH = 'port_orders'.freeze
6
+ private_constant :PORT_ORDERS_PATH
7
+
8
+ # @return [Integer] the ID associated with this order.
9
+ # @!attribute id
10
+
11
+ # @return [String] the status of this order.
12
+ # @!attribute status
13
+
14
+ # @return [String] the E.164 contact number.
15
+ # @!attribute contact_number
16
+
17
+ # @return [String] The email address for the order.
18
+ # @!attribute contact_email
19
+
20
+ # @return [String] The identifier for the account associated with the numbers.
21
+ # @!attribute account_identifier
22
+
23
+ # @return [String] Type of numbers to be ported. Either "business" or "residential".
24
+ # @!attribute port_type
25
+
26
+ # @return [String] The PIN or password needed to port out the numbers.
27
+ # @!attribute port_out_pin
28
+
29
+ # @return [String] the name associated with the account.
30
+ # @!attribute name_on_account
31
+
32
+ # @return [String] the name of the business.
33
+ # @!attribute name_of_business
34
+
35
+ # @return [String] the provider name.
36
+ # @!attribute provider_name
37
+
38
+ # @return [String] the E.164 billing number.
39
+ # @!attribute billing_number
40
+
41
+ # @return [String] the first billing address line.
42
+ # @!attribute billing_address1
43
+
44
+ # @return [String] the second billing address line.
45
+ # @!attribute billing_address2
46
+
47
+ # @return [String] the billing address city.
48
+ # @!attribute billing_city
49
+
50
+ # @return [String] the billing address state.
51
+ # @!attribute billing_state
52
+
53
+ # @return [String] the billing address zip.
54
+ # @!attribute billing_zip
55
+
56
+ # @return [String] the electronic signature used to sign the order.
57
+ # @!attribute esig
58
+
59
+ # @return [true | false]
60
+ # whether or not a bill will be provided. (only present on new orders)
61
+ # @!attribute has_bill
62
+
63
+ # @return [true | false]
64
+ # whether or not the legal agreement is accepted. (only present on new orders)
65
+ # @!attribute legal_agreement
66
+
67
+ has_normal_attributes %w[
68
+ id contact_number contact_email name_on_account name_of_business provider_name
69
+ billing_number billing_address1 billing_address2 account_identifier
70
+ billing_city billing_state billing_zip esig legal_agreement port_type port_out_pin
71
+ status
72
+ ]
73
+
74
+ # @return [Time] the time the order was created.
75
+ # @!attribute created_at
76
+
77
+ # @return [Time] the time the order was updated.
78
+ # @!attribute updated_at
79
+
80
+ # @return [Time] the time the bill for the order was received.
81
+ # @!attribute bill_received_at
82
+
83
+ # @return [Time] the time the order was requested for.
84
+ # @!attribute requested_for
85
+
86
+ # @return [Time] the time the order was completed.
87
+ # @!attribute completed_at
88
+
89
+ has_time_attributes %w[
90
+ created_at updated_at bill_received_at requested_for completed_at
91
+ ]
92
+
93
+ # @return [Phaxio::Resource::Collection<Phaxio::Resources::PortNumber>]
94
+ # a collection of port numbers associated with the order
95
+ # @!attribute port_numbers
96
+
97
+ has_collection_attributes({port_numbers: PortNumber})
98
+
99
+ class << self
100
+ # @macro paging
101
+ # List port orders in date range.
102
+ # @param params [Hash]
103
+ # Any parameters to send to Phaxio.
104
+ # - *created_before* [String] - RFC 3339 timestamp representing the end of the range.
105
+ # - *created_after* [String] - RFC 3339 timestamp representing the beginning of the range.
106
+ # @return [Phaxio::Resource::Collection<Phaxio::Resources::PortOrder>]
107
+ # The collection of port orders matching your request.
108
+ # @raise [Phaxio::Error::PhaxioError
109
+ # @see https://www.phaxio.com/docs/api/v2/port_orders/list_port_orders
110
+ def list params = {}
111
+ response = Client.request :get, port_orders_endpoint, params
112
+ response_collection response
113
+ end
114
+
115
+ # Create a port order.
116
+ # @param params [Hash]
117
+ # Any parameters to send to Phaxio.
118
+ # - *port_numbers* [Array<String>] - Numbers to port.
119
+ # - *contact_number* [String] - Number to contact about the port.
120
+ # - *contact_email* [String] - The email address for the order.
121
+ # - *account_identifier* [String] - The identifier for the account associated with the numbers.
122
+ # - *port_type* [String] - Type of numbers to be ported. Either "business" or "residential".
123
+ # - *port_out_pin* [String] - The PIN or password needed to port out the numbers.
124
+ # - *name_on_account* [String] - Name on the account for the port.
125
+ # - *name_of_business* [String] - Name of business associated with the port.
126
+ # - *provider_name* [String] - Name of provider for the port.
127
+ # - *esig* [String] - Electronic signature used to sign the order.
128
+ # - *legal_agreement* [true | false] - Indicates acceptance of the legal agreement.
129
+ # - *has_bill* [true | false] - Indicates whether or not a bill will be provided. If true, billing number
130
+ # and address fields are not needed.
131
+ # - *billing_number* [String] - Billing number.
132
+ # - *billing_address1* [String] - Billing address line 1
133
+ # - *billing_address2* [String] - Billing address line 2
134
+ # - *billing_city* [String] - Billing address city.
135
+ # - *billing_state* [String] - Billing address state.
136
+ # - *billing_zip* [String] - Billing address zip.
137
+ # @return [Phaxio::Resources::PortOrder]
138
+ # @raise [Phaxio::Error::PhaxioError]
139
+ # @see https://www.phaxio.com/docs/api/v2/port_orders/create_port_order
140
+ def create params = {}
141
+ response = Client.request :post, port_orders_endpoint, params
142
+ response_record response
143
+ end
144
+
145
+ # Get port order info.
146
+ # @param id [Integer]
147
+ # The ID of the order to retrieve.
148
+ # @param params [Hash]
149
+ # A hash of parameters to send to Phaxio. This action takes no unique parameters.
150
+ # @return [Phaxio::Resource::PortOrder]
151
+ # @raise [Phaxio::Error::PhaxioError]
152
+ # @see https://www.phaxio.com/docs/api/v2/port_orders/get_port_order
153
+ def get id, params = {}
154
+ response = Client.request :get, port_order_endpoint(id.to_i), params
155
+ response_record response
156
+ end
157
+ alias :retrieve :get
158
+ alias :find :get
159
+
160
+ private
161
+
162
+ def port_orders_endpoint
163
+ PORT_ORDERS_PATH
164
+ end
165
+
166
+ def port_order_endpoint id
167
+ "#{PORT_ORDERS_PATH}/#{id}"
168
+ end
169
+ end
170
+ end
171
+ end
172
+ end
@@ -46,7 +46,7 @@ module Phaxio
46
46
  # @return [Phaxio::Resource::Collection<Phaxio::Resources::AreaCode>] A collection of
47
47
  # AreaCode objects.
48
48
  # @raise [Phaxio::Error::PhaxioError]
49
- # @see https://www.phaxio.com/docs/api/v2/public/list_area_codes
49
+ # @see https://www.phaxio.com/docs/api/v2.1/public/list_area_codes
50
50
  def list params = {}
51
51
  response = Client.request :get, available_area_codes_endpoint, params
52
52
  AreaCode.response_collection response
@@ -36,7 +36,7 @@ module Phaxio
36
36
  # @return [Phaxio::Resource::Collection<Phaxio::Resources::Country>]
37
37
  # A collection of supported countries.
38
38
  # @raise Phaxio::Error::PhaxioError
39
- # @see https://www.phaxio.com/docs/api/v2/public/list_countries
39
+ # @see https://www.phaxio.com/docs/api/v2.1/public/list_countries
40
40
  def list params = {}
41
41
  response = Client.request :get, supported_countries_endpoint, params
42
42
  Country.response_collection response
@@ -0,0 +1,65 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Provides utilities for working with webhooks.
4
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/send_webhook
5
+ # @see https://www.phaxio.com/docs/api/v2.1/faxes/receive_webhooks
6
+ class Webhook
7
+ DIGEST = OpenSSL::Digest.new('sha1')
8
+ private_constant :DIGEST
9
+
10
+ class << self
11
+ # Determines whether or not the passed signature is valid for the given
12
+ # url, params, and files.
13
+ # @param signature [String]
14
+ # The signature received from Phaxio.
15
+ # @param url [String]
16
+ # The webhook URL used in this request.
17
+ # @param params [Hash]
18
+ # The parameters received with the webhook, excluding files.
19
+ # @param files [Array<File>]
20
+ # The files received with the webhook, if any.
21
+ # @return [true, false]
22
+ # @raise [Phaxio::Error::PhaxioError]
23
+ # @see https://www.phaxio.com/docs/security/callbacks
24
+ def valid_signature? signature, url, params, files = []
25
+ check_signature = generate_check_signature url, params, files
26
+ check_signature == signature
27
+ end
28
+
29
+ private
30
+
31
+ def generate_check_signature url, params, files = []
32
+ params_string = generate_params_string(params)
33
+ files_string = generate_files_string(files)
34
+ webhook_data = "#{url}#{params_string}#{files_string}"
35
+ OpenSSL::HMAC.hexdigest(DIGEST, webhook_token, webhook_data)
36
+ end
37
+
38
+ def webhook_token
39
+ Phaxio.webhook_token or raise(Error::PhaxioError, 'No webhook token has been set')
40
+ end
41
+
42
+ def generate_params_string(params)
43
+ sorted_params = params.sort_by { |key, _value| key }
44
+ params_strings = sorted_params.map { |key, value| "#{key}#{value}" }
45
+ params_strings.join
46
+ end
47
+
48
+ def generate_files_string(files)
49
+ files_array = files_to_array(files).reject(&:nil?)
50
+ sorted_files = files_array.sort_by { |file| file[:name] }
51
+ files_strings = sorted_files.map { |file| generate_file_string(file) }
52
+ files_strings.join
53
+ end
54
+
55
+ def files_to_array(files)
56
+ files.is_a?(Array) ? files : [files]
57
+ end
58
+
59
+ def generate_file_string(file)
60
+ file[:name] + DIGEST.hexdigest(file[:tempfile].read)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,3 +1,3 @@
1
1
  module Phaxio
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
data/lib/phaxio.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'base64'
1
2
  require 'json'
2
3
  require 'tempfile'
3
4
  require 'openssl'
@@ -16,7 +17,10 @@ Dir[File.expand_path(File.join('..', 'phaxio', 'helpers', '*.rb'), __FILE__)].ea
16
17
  require file
17
18
  end
18
19
 
19
- %w[fax_recipient fax account callback phax_code phone_number public].each do |filename|
20
+ %w[
21
+ fax_recipient fax account callback webhook phax_code phone_number public ata
22
+ port_number_note port_number port_order
23
+ ].each do |filename|
20
24
  require File.expand_path(File.join('..', 'phaxio', 'resources', filename), __FILE__)
21
25
  end
22
26
 
@@ -28,11 +32,13 @@ module Phaxio
28
32
  # @see Config.api_key
29
33
  # @!attribute api_secret
30
34
  # @see Config.api_secret
35
+ # @!attribute webhook_token
36
+ # @see Config.webhook_token
31
37
  # @!attribute callback_token
32
- # @see Config.callback_token
38
+ # @see Config.webhook_token
33
39
  # @!attribute api_endpoint
34
40
  # @see Config.api_endpoint
35
- %w(api_key api_secret callback_token api_endpoint).each do |config_attribute|
41
+ %w(api_key api_secret webhook_token callback_token api_endpoint).each do |config_attribute|
36
42
  # Define getters
37
43
  define_method(config_attribute) do
38
44
  Config.public_send config_attribute
@@ -44,5 +50,9 @@ module Phaxio
44
50
  Config.public_send setter, value
45
51
  end
46
52
  end
53
+
54
+ # for backwards compatibility
55
+ alias callback_token webhook_token
56
+ alias callback_token= webhook_token=
47
57
  end
48
58
  end
data/phaxio.gemspec CHANGED
@@ -14,9 +14,10 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "phaxio"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Phaxio::VERSION
17
+ gem.licenses = ['MIT']
17
18
 
18
19
  gem.required_ruby_version = '>= 2.0'
19
- gem.add_dependency 'faraday', '~> 0.10'
20
+ gem.add_dependency 'faraday', '>= 0.10', '< 2.0'
20
21
  gem.add_dependency 'mime-types', '~> 3.0'
21
22
  gem.add_dependency 'activesupport'
22
23
  end
data/spec/client_spec.rb CHANGED
@@ -32,10 +32,42 @@ RSpec.describe Phaxio::Client do
32
32
  client.request :get, endpoint, params
33
33
  end
34
34
 
35
+ it 'handles a single file param' do
36
+ file_path = File.expand_path File.join('..', 'support', 'files', 'test.pdf'), __FILE__
37
+ params = {to: '+12258888888', file: File.open(file_path)}
38
+ expect(test_connection).to receive(:post) do |request_endpoint, request_params|
39
+ expect(request_params[:file]).to be_a(Faraday::UploadIO)
40
+ expect(request_params[:file].original_filename).to eq(File.basename(file_path))
41
+ test_response 200
42
+ end
43
+
44
+ client.request :post, 'faxes', params
45
+ end
46
+
47
+ it 'handles multiple file params' do
48
+ file_names = ['test.pdf', 'test.txt']
49
+ file_paths = file_names.map do |file_name|
50
+ File.expand_path File.join('..', 'support', 'files', file_name), __FILE__
51
+ end
52
+ params = {to: '+12258888888', file: file_paths.map { |file_path| File.open(file_path) }}
53
+
54
+ expect(test_connection).to receive(:post) do |request_endpoint, request_params|
55
+ expect(request_params[:file]).to be_a(Array)
56
+ correct_file_params = request_params[:file].all? do |file_param|
57
+ file_param.is_a? Faraday::UploadIO
58
+ end
59
+ expect(correct_file_params).to eq(true)
60
+ expect(request_params[:file].map(&:original_filename)).to eq(file_names)
61
+ test_response 200
62
+ end
63
+
64
+ client.request :post, 'faxes', params
65
+ end
66
+
35
67
  it 'uses the configured API key and secret by default' do
36
- expect(test_connection).to receive(:get) do |_endpoint, request_params|
37
- expect(request_params[:api_key]).to eq(Phaxio.api_key)
38
- expect(request_params[:api_secret]).to eq(Phaxio.api_secret)
68
+ expect(test_connection).to receive(:get) do |_endpoint, _request_params, request_headers|
69
+ auth_header = "Basic #{Base64.strict_encode64("#{Phaxio.api_key}:#{Phaxio.api_secret}")}"
70
+ expect(request_headers['Authorization']).to eq(auth_header)
39
71
  test_response 200
40
72
  end
41
73
  client.request :get, 'test'
@@ -44,9 +76,9 @@ RSpec.describe Phaxio::Client do
44
76
  it 'uses the api key specified in the params hash' do
45
77
  custom_api_key = 'custom-api-key'
46
78
  custom_api_secret = 'custom-api-secret'
47
- expect(test_connection).to receive(:get) do |_endpoint, request_params|
48
- expect(request_params[:api_key]).to eq(custom_api_key)
49
- expect(request_params[:api_secret]).to eq(custom_api_secret)
79
+ expect(test_connection).to receive(:get) do |_endpoint, _request_params, request_headers|
80
+ auth_header = "Basic #{Base64.strict_encode64('custom-api-key:custom-api-secret')}"
81
+ expect(request_headers['Authorization']).to eq(auth_header)
50
82
  test_response 200
51
83
  end
52
84
  client.request :get, 'test', api_key: custom_api_key, api_secret: custom_api_secret
data/spec/phaxio_spec.rb CHANGED
@@ -13,8 +13,14 @@ RSpec.describe Phaxio do
13
13
  expect(subject.api_secret).to eq('test-api-secret')
14
14
  end
15
15
 
16
- it 'sets the callback token' do
16
+ it 'sets the webhook token' do
17
+ subject.webhook_token = 'test-webhook-token'
18
+ expect(subject.webhook_token).to eq('test-webhook-token')
19
+ end
20
+
21
+ it 'sets and gets the webhook token as callback_token' do
17
22
  subject.callback_token = 'test-callback-token'
18
23
  expect(subject.callback_token).to eq('test-callback-token')
24
+ expect(subject.webhook_token).to eq('test-callback-token')
19
25
  end
20
26
  end