phaxio 0.4.0 → 2.1.0.pre

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 (85) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.travis.yml +11 -0
  4. data/Gemfile +11 -5
  5. data/README.md +296 -40
  6. data/Rakefile +7 -9
  7. data/lib/phaxio.rb +48 -3
  8. data/lib/phaxio/client.rb +151 -241
  9. data/lib/phaxio/config.rb +31 -0
  10. data/lib/phaxio/error.rb +13 -0
  11. data/lib/phaxio/helpers/mime_type_helper.rb +14 -0
  12. data/lib/phaxio/resource.rb +168 -0
  13. data/lib/phaxio/resources.rb +7 -0
  14. data/lib/phaxio/resources/account.rb +41 -0
  15. data/lib/phaxio/resources/ata.rb +219 -0
  16. data/lib/phaxio/resources/callback.rb +65 -0
  17. data/lib/phaxio/resources/fax.rb +310 -0
  18. data/lib/phaxio/resources/fax_recipient.rb +41 -0
  19. data/lib/phaxio/resources/phax_code.rb +89 -0
  20. data/lib/phaxio/resources/phone_number.rb +112 -0
  21. data/lib/phaxio/resources/public.rb +8 -0
  22. data/lib/phaxio/resources/public/area_code.rb +64 -0
  23. data/lib/phaxio/resources/public/country.rb +54 -0
  24. data/lib/phaxio/version.rb +1 -1
  25. data/phaxio.gemspec +10 -8
  26. data/spec/client_spec.rb +164 -0
  27. data/spec/helpers/mime_type_helper_spec.rb +11 -0
  28. data/spec/phaxio_spec.rb +20 -0
  29. data/spec/resources/account_spec.rb +24 -0
  30. data/spec/resources/callback_spec.rb +34 -0
  31. data/spec/resources/fax_spec.rb +248 -0
  32. data/spec/resources/phax_code_spec.rb +111 -0
  33. data/spec/resources/phone_number_spec.rb +89 -0
  34. data/spec/resources/public/area_code_spec.rb +24 -0
  35. data/spec/resources/public/country_spec.rb +24 -0
  36. data/spec/spec_helper.rb +6 -0
  37. data/spec/support/credentials.rb +7 -0
  38. data/spec/support/expectations.rb +9 -0
  39. data/spec/support/files/test.pdf +0 -0
  40. data/spec/support/files/test.txt +1 -0
  41. data/spec/support/vcr.rb +8 -0
  42. data/spec/support/vcr_cassettes/resources/account/status.yml +45 -0
  43. data/spec/support/vcr_cassettes/resources/fax/cancel.yml +47 -0
  44. data/spec/support/vcr_cassettes/resources/fax/create.yml +50 -0
  45. data/spec/support/vcr_cassettes/resources/fax/create_for_cancel.yml +50 -0
  46. data/spec/support/vcr_cassettes/resources/fax/create_for_delete.yml +50 -0
  47. data/spec/support/vcr_cassettes/resources/fax/create_for_delete_file.yml +50 -0
  48. data/spec/support/vcr_cassettes/resources/fax/create_for_download_file.yml +50 -0
  49. data/spec/support/vcr_cassettes/resources/fax/create_for_get.yml +50 -0
  50. data/spec/support/vcr_cassettes/resources/fax/create_for_reference.yml +50 -0
  51. data/spec/support/vcr_cassettes/resources/fax/create_for_resend.yml +50 -0
  52. data/spec/support/vcr_cassettes/resources/fax/delete.yml +45 -0
  53. data/spec/support/vcr_cassettes/resources/fax/delete_file.yml +45 -0
  54. data/spec/support/vcr_cassettes/resources/fax/file.yml +50 -0
  55. data/spec/support/vcr_cassettes/resources/fax/get.yml +45 -0
  56. data/spec/support/vcr_cassettes/resources/fax/list.yml +51 -0
  57. data/spec/support/vcr_cassettes/resources/fax/reference.yml +45 -0
  58. data/spec/support/vcr_cassettes/resources/fax/resend.yml +47 -0
  59. data/spec/support/vcr_cassettes/resources/fax/test_receive.yml +51 -0
  60. data/spec/support/vcr_cassettes/resources/phax_code/create.yml +47 -0
  61. data/spec/support/vcr_cassettes/resources/phax_code/create_png.yml +52 -0
  62. data/spec/support/vcr_cassettes/resources/phax_code/get.yml +45 -0
  63. data/spec/support/vcr_cassettes/resources/phax_code/get_id.yml +46 -0
  64. data/spec/support/vcr_cassettes/resources/phax_code/get_id_png.yml +50 -0
  65. data/spec/support/vcr_cassettes/resources/phax_code/get_png.yml +50 -0
  66. data/spec/support/vcr_cassettes/resources/phone_number/create.yml +48 -0
  67. data/spec/support/vcr_cassettes/resources/phone_number/get.yml +46 -0
  68. data/spec/support/vcr_cassettes/resources/phone_number/list.yml +60 -0
  69. data/spec/support/vcr_cassettes/resources/phone_number/release.yml +45 -0
  70. data/spec/support/vcr_cassettes/resources/public/area_codes/list.yml +79 -0
  71. data/spec/support/vcr_cassettes/resources/public/country/list.yml +55 -0
  72. metadata +103 -73
  73. data/test/support/responses/account_status.json +0 -9
  74. data/test/support/responses/cancel_success.json +0 -4
  75. data/test/support/responses/fax_status_success.json +0 -21
  76. data/test/support/responses/list_faxes.json +0 -68
  77. data/test/support/responses/list_numbers.json +0 -22
  78. data/test/support/responses/provision_number.json +0 -12
  79. data/test/support/responses/release_number.json +0 -7
  80. data/test/support/responses/send_failure.json +0 -8
  81. data/test/support/responses/send_success.json +0 -8
  82. data/test/support/responses/test.pdf +0 -0
  83. data/test/support/responses/test_receive.json +0 -4
  84. data/test/test_helper.rb +0 -53
  85. data/test/test_phaxio.rb +0 -72
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7f20cd3d8695312c8b64cf40cc01b8fe0e320b921a3527abc1ca005a9d140b11
4
+ data.tar.gz: '088662055ff2bd6ebbeb5055ad7316bd6d99acf9a93249383d9ad1aa08133a4b'
5
+ SHA512:
6
+ metadata.gz: 99cfc91fefd07016507eb20049f5c7afeca903d2c227941bcd55a1cd14913faa1b03e69a2ceea1798da457bf338a48e5ad7c212f107aba598378965210a0d24a
7
+ data.tar.gz: cf3b858ff97a2b005f50a21709ee524fd20b6fc12080a59a48d57842b9e295f18de91a20f48ba5f9f1b64d632e5a38d7d375b0af8e323cea2232df49421deb93
data/.gitignore CHANGED
@@ -16,3 +16,5 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  .DS_Store
19
+ .envrc
20
+ .env
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1
6
+ - 2.2
7
+ - 2.3
8
+ - 2.4
9
+ - jruby-19mode
10
+
11
+ sudo: false
data/Gemfile CHANGED
@@ -1,8 +1,14 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem "fakeweb", "~> 1.3.0"
4
- gem "httmultiparty", "~> 0.3.8"
5
- gem "rake", "~> 0.9.2.2"
6
-
7
- # Specify your gem's dependencies in phaxio.gemspec
8
3
  gemspec
4
+
5
+ group :development do
6
+ gem 'bundler'
7
+ gem 'rake'
8
+ gem 'rspec', '~> 3.7'
9
+ gem 'vcr'
10
+ gem 'webmock'
11
+ gem 'pry'
12
+ gem 'yard'
13
+ gem 'dotenv', require: false
14
+ end
data/README.md CHANGED
@@ -1,68 +1,324 @@
1
- # Phaxio
1
+ # 📠 Phaxio
2
2
 
3
- A Ruby gem for interacting with the [Phaxio API]( https://www.phaxio.com/docs ).
3
+ [![Build Status](https://travis-ci.org/phaxio/phaxio-ruby.svg?branch=master)](https://travis-ci.org/phaxio/phaxio-ruby)
4
4
 
5
- **Note: This gem only runs on Ruby version 1.9.+**
5
+ A Ruby gem for interacting with the [Phaxio API](https://www.phaxio.com/docs/api/v2).
6
6
 
7
7
  ## Installation
8
8
 
9
- Add this line to your application's Gemfile:
9
+ Add to your application's Gemfile:
10
10
 
11
- gem 'phaxio'
11
+ ``` ruby
12
+ gem 'phaxio', '~> 2.0.0'
13
+ ```
12
14
 
13
15
  And then execute:
14
16
 
15
- $ bundle
17
+ ``` sh
18
+ $ bundle install
19
+ ```
16
20
 
17
21
  Or install it yourself as:
18
22
 
19
- $ gem install phaxio
23
+ ``` sh
24
+ $ gem install phaxio
25
+ ```
20
26
 
21
27
  ## Usage
22
28
 
23
- Configure Phaxio with your api_key and api_secret:
29
+ Set up your API Key, API Secret, and, optionally, Callback Token.
24
30
 
25
- Phaxio.config do |config|
26
- config.api_key = "10987654321"
27
- config.api_secret = "12345678910"
28
- end
31
+ ``` ruby
32
+ require 'phaxio'
33
+
34
+ Phaxio.api_key = '11111'
35
+ Phaxio.api_secret = '22222'
36
+ Phaxio.callback_token = '33333'
37
+ ```
38
+
39
+ Try sending a fax:
40
+
41
+ ``` ruby
42
+ fax_file = File.open 'test.pdf', 'rb'
43
+ Phaxio::Fax.create to: '+15558675309', file: fax_file
44
+ ```
45
+
46
+ You can include `Phaxio::Resources` to pull in the resource classes for convenience:
29
47
 
30
- To send a fax:
48
+ ``` ruby
49
+ include Phaxio::Resources
50
+
51
+ fax_file = File.open 'test.pdf', 'rb'
52
+ Fax.create to: '+15558675309', file: fax_file
53
+ ```
31
54
 
32
- Phaxio.send_fax(to: "0123456789", filename: "test.pdf")
33
55
 
34
56
  ### Currently Supported API Calls
35
57
 
36
- * send_fax - `Phaxio.send_fax(to: "0123456789", filename: "test.pdf")`
37
- * test_receive - `Phaxio.test_receive(filename: "test_file.pdf")`
38
- * provision_number - `Phaxio.provision_number(area_code: 802)`
39
- * release_number - `Phaxio.release_number(number: "8021112222")`
40
- * list_numbers - `Phaxio.list_numbers(area_code: 802)`
41
- * get_fax_file - `Phaxio.get_fax_file(id: 123456, type: p)`
42
- * list_faxes - `Phaxio.list_numbers(area_code: 802)`
43
- * list_faxes - `Phaxio.list_faxes(start: Time.now - 48000,
44
- end: Time.now)`
45
- * get_fax_status - `Phaxio.get_fax_status(id: 123456)`
46
- * cancel_fax - `Phaxio.cancel_fax(id: 123456)`
47
- * get_account_status - `Phaxio.get_account_status`
48
-
49
- ### Example
50
-
51
- require 'phaxio'
52
-
53
- Phaxio.config do |config|
54
- config.api_key = "your_key"
55
- config.api_secret = "your_secret"
58
+ #### Faxes
59
+
60
+ ##### `Fax.create`
61
+
62
+ Create and send a fax.
63
+
64
+ ``` ruby
65
+ fax_file = File.open 'test.pdf', 'rb'
66
+ ref = Fax.create to: '+15558675309', file: fax_file
67
+ # => Fax::Reference(id: 1234)
68
+ fax = ref.get
69
+ # => Fax(id: 1234, num_pages: 1, ...)
70
+ ```
71
+
72
+ ##### `Fax.list`
73
+
74
+ List faxes in date range.
75
+
76
+ ``` ruby
77
+ start = 2.weeks.ago
78
+ stop = 1.week.ago
79
+ faxes = Fax.list created_after: start, created_before: stop
80
+ # => Phaxio::Resource::Collection([Fax(id: 1234, ...), ...])
81
+ faxes.length
82
+ # => 5
83
+ faxes.map(&:cost).inject(&:+)
84
+ # => 35
85
+ ```
86
+
87
+ ##### `Fax.get`
88
+
89
+ Get information about a specific fax.
90
+
91
+ ``` ruby
92
+ fax = Fax.get 1234
93
+ # => Fax(id: 1234, ...)
94
+ ```
95
+
96
+ ##### `Fax.cancel`
97
+
98
+ Cancel a fax.
99
+
100
+ ``` ruby
101
+ Fax.cancel 1234
102
+ # => Fax::Reference(id: 1234)
103
+ ```
104
+
105
+ ##### `Fax.resend`
106
+
107
+ Resend a fax.
108
+
109
+ ``` ruby
110
+ Fax.resend 1234
111
+ # => Fax::Reference(id: 5678)
112
+ ```
113
+
114
+ ##### `Fax.delete`
115
+
116
+ Delete a fax. Only test faxes are allowed to be deleted.
117
+
118
+ ``` ruby
119
+ Fax.delete 1234
120
+ # => true
121
+ ```
122
+
123
+ ##### `Fax.delete_file`
124
+
125
+ Delete fax file.
126
+
127
+ ``` ruby
128
+ Fax.delete_file 1234
129
+ # => true
130
+ ```
131
+
132
+ ##### `Fax.file`
133
+
134
+ ``` ruby
135
+ Fax.file 1234
136
+ # => File
137
+ ```
138
+
139
+ ##### `Fax.test_receive`
140
+
141
+ Test receiving a fax.
142
+
143
+ ``` ruby
144
+ fax_file = File.open 'test.pdf', 'rb'
145
+ Fax.test_receive file: fax_file
146
+ # => true
147
+ ```
148
+
149
+ #### Countries
150
+
151
+ ##### `Public::Country.list`
152
+
153
+ Get a list of supported countries.
154
+
155
+ ``` ruby
156
+ Public::Country.list
157
+ # => Phaxio::Resource::Collection([Public::Country(alpha2: 'US', ...), ...])
158
+ ```
159
+
160
+ #### Phone Numbers
161
+
162
+ ##### `PhoneNumber.create`
163
+
164
+ Provision a new phone number.
165
+
166
+ ``` ruby
167
+ PhoneNumber.create country_code: 1, area_code: 555
168
+ # => PhoneNumber(phone_number: '+15558675309', ...)
169
+ ```
170
+
171
+ ##### `PhoneNumber.list`
172
+
173
+ List phone numbers that you own on Phaxio.
174
+
175
+ ``` ruby
176
+ PhoneNumber.list
177
+ # => Phaxio::Resource::Collection([PhoneNumber(phone_number: '+15558675309', ...), ...])
178
+ ```
179
+
180
+ ##### `PhoneNumber.get`
181
+
182
+ Get information about a specific phone number.
183
+
184
+ ``` ruby
185
+ PhoneNumber.get '+15558675309'
186
+ # => PhoneNumber(phone_number: '+15558675309', ...)
187
+ ```
188
+
189
+ ##### `PhoneNumber.delete`
190
+
191
+ Release a phone number.
192
+
193
+ ``` ruby
194
+ PhoneNumber.delete '+15558675309'
195
+ # => true
196
+ ```
197
+
198
+ #### Area Codes
199
+
200
+ ##### `Public::AreaCode.list`
201
+
202
+ Lists available area codes for purchasing Phaxio numbers.
203
+
204
+ ``` ruby
205
+ area_codes = Public::AreaCode.list toll_free: true
206
+ # => Phaxio::Resource::Collection([Public::AreaCode(city: 'Toll Free Service', ...), ...], page: 1)
207
+ ```
208
+
209
+ #### PhaxCodes
210
+
211
+ ##### `PhaxCode.create`
212
+
213
+ Creates a PhaxCode. Returns data about the PhaxCode by default, or a .png file if `type: 'png'` is
214
+ passed.
215
+
216
+ ``` ruby
217
+ PhaxCode.create metadata: 'test_phax_code'
218
+ # => PhaxCode(identifier: 'phax-code-identifier')
219
+ PhaxCode.create type: 'png'
220
+ # => File
221
+ ```
222
+
223
+ ##### `PhaxCode.get`
224
+
225
+ Gets a PhaxCode. Returns data about the PhaxCode by default, or a .png file if `type: 'png'` is
226
+ passed.
227
+
228
+ ``` ruby
229
+ PhaxCode.get 'phax-code-identifier'
230
+ # => PhaxCode(identifier: 'phax-code-identifier', metadata: 'phax-code-metadata')
231
+ PhaxCode.get 'phax-code-identifier', type: 'png'
232
+ # => File
233
+ ```
234
+
235
+ #### Account
236
+
237
+ ##### `Account.get`
238
+
239
+ Get information about your Phaxio account.
240
+
241
+ ``` ruby
242
+ Account.get
243
+ # => Account(balance: 1000, faxes_today: 0, faxes_this_month: 100)
244
+ ```
245
+
246
+ #### Callback
247
+
248
+ ##### `Callback.valid_signature?`
249
+
250
+ Validate the callback signature sent with a Phaxio callback. Requires that Phaxio.callback_token be
251
+ set.
252
+
253
+ ``` ruby
254
+ Callback.valid_signature? received_signature, callback_url, received_params, received_files
255
+ # => true
256
+ ```
257
+
258
+ ## Callback Validation Example with Sinatra
259
+
260
+ ``` ruby
261
+ require 'sinatra/base'
262
+ require 'phaxio'
263
+
264
+ class PhaxioWebhookExample < Sinatra::Base
265
+ Phaxio.callback_token = 'YOUR WEBHOOK TOKEN HERE'
266
+
267
+ post '/webhook' do
268
+ signature = request.env['HTTP_X_PHAXIO_SIGNATURE']
269
+ url = request.url
270
+ file_params = params[:file]
271
+ if Phaxio::Callback.valid_signature? signature, url, webhook_params, file_params
272
+ 'Success'
273
+ else
274
+ 'Invalid webhook signature'
275
+ end
276
+ end
277
+
278
+ def webhook_params
279
+ params.select do |key, _value|
280
+ %w(success is_test direction fax metadata message event_type).include?(key)
281
+ end
282
+ end
283
+ end
284
+ ```
285
+
286
+ ## Webhook Validation Example with Rails Controller
287
+
288
+ ``` ruby
289
+ class WebhookController < ApplicationController
290
+ skip_before_action :verify_authenticity_token
291
+
292
+ def index
293
+ signature = request.headers['X-Phaxio-Signature']
294
+ Phaxio.callback_token = 'YOUR WEBHOOK TOKEN HERE'
295
+ url = request.original_url
296
+
297
+ Rails.logger.debug "URL: " + url
298
+ Rails.logger.debug "Signature: " + signature
299
+ Rails.logger.debug "params: " + params.inspect
300
+ Rails.logger.debug "webhook_params: " + webhook_params.to_h.inspect
301
+
302
+ if Phaxio::Callback.valid_signature? signature, url, webhook_params.to_h, file_params
303
+ Rails.logger.debug "Success"
304
+ render plain: 'Success'
305
+ else
306
+ Rails.logger.debug "Invalid callback signature"
307
+ render plain: 'Invalid callback signature'
56
308
  end
309
+ end
57
310
 
58
- @fax = Phaxio.send_fax(to: '15555555555', string_data: "hello world")
59
- Phaxio.get_fax_status(id: @fax["faxId"])
311
+ def webhook_params
312
+ params.permit(:success, :is_test, :direction, :fax, :metadata, :event_type, :message)
313
+ end
60
314
 
61
- # Get a Fax and save it as a PDF
62
- @pdf = Phaxio.get_fax_file(id: @fax["faxId"], type: "p")
63
- File.open("received_test.pdf", "w") do |file|
64
- file << @pdf
315
+ def file_params
316
+ if params[:file]
317
+ [{ :name => 'file', :tempfile => params[:file].tempfile }]
65
318
  end
319
+ end
320
+ end
321
+ ```
66
322
 
67
323
  ## Contributing
68
324
 
data/Rakefile CHANGED
@@ -1,13 +1,11 @@
1
1
  #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
3
4
 
4
- require 'rake/testtask'
5
-
6
- Rake::TestTask.new do |t|
7
- t.libs << 'test'
8
- t.test_files = FileList['test/test*.rb']
9
- t.verbose = true
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.pattern = Dir.glob('spec/**/*_spec.rb')
7
+ t.rspec_opts = '--format documentation'
10
8
  end
11
9
 
12
- desc "Run tests"
13
- task :default => :test
10
+ desc 'Run tests'
11
+ task :default => :spec
data/lib/phaxio.rb CHANGED
@@ -1,4 +1,49 @@
1
- require "httmultiparty"
1
+ require 'base64'
2
+ require 'json'
3
+ require 'tempfile'
4
+ require 'openssl'
5
+ require 'time'
6
+ require 'faraday'
7
+ require 'active_support/core_ext/hash/indifferent_access'
8
+ require 'mime/types/full'
9
+ require 'phaxio/version'
10
+ require 'phaxio/config'
11
+ require 'phaxio/client'
12
+ require 'phaxio/error'
13
+ require 'phaxio/resource'
14
+ require 'phaxio/resources'
2
15
 
3
- require "phaxio/version"
4
- require "phaxio/client"
16
+ Dir[File.expand_path(File.join('..', 'phaxio', 'helpers', '*.rb'), __FILE__)].each do |file|
17
+ require file
18
+ end
19
+
20
+ %w[fax_recipient fax account callback phax_code phone_number public ata].each do |filename|
21
+ require File.expand_path(File.join('..', 'phaxio', 'resources', filename), __FILE__)
22
+ end
23
+
24
+ module Phaxio
25
+ include Resources
26
+
27
+ class << self
28
+ # @!attribute api_key
29
+ # @see Config.api_key
30
+ # @!attribute api_secret
31
+ # @see Config.api_secret
32
+ # @!attribute callback_token
33
+ # @see Config.callback_token
34
+ # @!attribute api_endpoint
35
+ # @see Config.api_endpoint
36
+ %w(api_key api_secret callback_token api_endpoint).each do |config_attribute|
37
+ # Define getters
38
+ define_method(config_attribute) do
39
+ Config.public_send config_attribute
40
+ end
41
+
42
+ # Define setters
43
+ setter = "#{config_attribute}="
44
+ define_method(setter) do |value|
45
+ Config.public_send setter, value
46
+ end
47
+ end
48
+ end
49
+ end