phaxio 0.5.0 → 2.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 (75) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.travis.yml +2 -1
  4. data/Gemfile +10 -0
  5. data/README.md +260 -73
  6. data/Rakefile +6 -16
  7. data/lib/phaxio.rb +47 -3
  8. data/lib/phaxio/client.rb +117 -484
  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/callback.rb +65 -0
  16. data/lib/phaxio/resources/fax.rb +310 -0
  17. data/lib/phaxio/resources/fax_recipient.rb +41 -0
  18. data/lib/phaxio/resources/phax_code.rb +89 -0
  19. data/lib/phaxio/resources/phone_number.rb +112 -0
  20. data/lib/phaxio/resources/public.rb +8 -0
  21. data/lib/phaxio/resources/public/area_code.rb +64 -0
  22. data/lib/phaxio/resources/public/country.rb +54 -0
  23. data/lib/phaxio/version.rb +1 -1
  24. data/phaxio.gemspec +9 -12
  25. data/spec/client_spec.rb +132 -0
  26. data/spec/helpers/mime_type_helper_spec.rb +11 -0
  27. data/spec/phaxio_spec.rb +20 -0
  28. data/spec/resources/account_spec.rb +24 -0
  29. data/spec/resources/callback_spec.rb +34 -0
  30. data/spec/resources/fax_spec.rb +227 -0
  31. data/spec/resources/phax_code_spec.rb +83 -0
  32. data/spec/resources/phone_number_spec.rb +89 -0
  33. data/spec/resources/public/area_code_spec.rb +24 -0
  34. data/spec/resources/public/country_spec.rb +24 -0
  35. data/spec/spec_helper.rb +6 -0
  36. data/spec/support/credentials.rb +7 -0
  37. data/spec/support/expectations.rb +9 -0
  38. data/spec/support/files/test.pdf +0 -0
  39. data/spec/support/vcr.rb +9 -0
  40. data/spec/support/vcr_cassettes/resources/account/status.yml +44 -0
  41. data/spec/support/vcr_cassettes/resources/fax/cancel.yml +46 -0
  42. data/spec/support/vcr_cassettes/resources/fax/create.yml +230 -0
  43. data/spec/support/vcr_cassettes/resources/fax/delete.yml +44 -0
  44. data/spec/support/vcr_cassettes/resources/fax/delete_file.yml +44 -0
  45. data/spec/support/vcr_cassettes/resources/fax/file.yml +251 -0
  46. data/spec/support/vcr_cassettes/resources/fax/get.yml +44 -0
  47. data/spec/support/vcr_cassettes/resources/fax/list.yml +56 -0
  48. data/spec/support/vcr_cassettes/resources/fax/resend.yml +46 -0
  49. data/spec/support/vcr_cassettes/resources/fax/test_receive.yml +231 -0
  50. data/spec/support/vcr_cassettes/resources/phax_code/create.yml +100 -0
  51. data/spec/support/vcr_cassettes/resources/phax_code/get.yml +190 -0
  52. data/spec/support/vcr_cassettes/resources/phone_number/create.yml +47 -0
  53. data/spec/support/vcr_cassettes/resources/phone_number/get.yml +45 -0
  54. data/spec/support/vcr_cassettes/resources/phone_number/list.yml +52 -0
  55. data/spec/support/vcr_cassettes/resources/phone_number/release.yml +44 -0
  56. data/spec/support/vcr_cassettes/resources/public/area_codes/list.yml +77 -0
  57. data/spec/support/vcr_cassettes/resources/public/country/list.yml +54 -0
  58. metadata +70 -82
  59. data/.ruby-version +0 -1
  60. data/CHANGELOG +0 -6
  61. data/test/files/test.pdf +0 -0
  62. data/test/integration/phaxio_integration_test.rb +0 -45
  63. data/test/support/responses/account_status.json +0 -9
  64. data/test/support/responses/cancel_success.json +0 -4
  65. data/test/support/responses/fax_status_success.json +0 -21
  66. data/test/support/responses/list_faxes.json +0 -68
  67. data/test/support/responses/list_numbers.json +0 -22
  68. data/test/support/responses/provision_number.json +0 -12
  69. data/test/support/responses/release_number.json +0 -7
  70. data/test/support/responses/send_failure.json +0 -8
  71. data/test/support/responses/send_success.json +0 -8
  72. data/test/support/responses/test.pdf +0 -0
  73. data/test/support/responses/test_receive.json +0 -4
  74. data/test/test_helper.rb +0 -57
  75. data/test/test_phaxio.rb +0 -128
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0ba3d3a1d93ed66b41b2f43e4830301b1cf636f
4
- data.tar.gz: 61dec61548a074dbaedd1e450fe8aa23bc729740
3
+ metadata.gz: 6a10449404bdb1df029a52a878f49202589e727d
4
+ data.tar.gz: f500188f94760b8300dc2f63b17eb4ad31edc5fa
5
5
  SHA512:
6
- metadata.gz: c04abdcfb62078eace54eb05a7560cd183aa4b70717f0c267833b5808f39130a89f6c448713b6f9036b038894909b8ae9cc97234208f713a2bcb665723f5b2f9
7
- data.tar.gz: 48ea7099e4556e635f0a66dcfd61d5f205594835230a7797effd4e823b7762d24affe7fed8dd3fb2c759fd85f9a015353b63532aaf17f2adaa1b6df1476e9643
6
+ metadata.gz: df06285d6e87fbc048f0ebcad0cd5ee608ccfb0b45c6813926f272c4302aa5dc60421445df11bacc6fae2e26a32951912ca201376a56c77bcd2e5fa71e0e5976
7
+ data.tar.gz: 9d5bcfa978e6e92317d17d4bb0a31e4cd2f33f57d96f8241721bc581be34c768623704d951d4a177f5634dc0eb6a7f2cff4c8db7588e6e286a61ba2aa5020dd3
data/.gitignore CHANGED
@@ -17,4 +17,4 @@ test/version_tmp
17
17
  tmp
18
18
  .DS_Store
19
19
  .envrc
20
- .idea
20
+ .env
@@ -1,10 +1,11 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 1.9.3
5
4
  - 2.0.0
6
5
  - 2.1
7
6
  - 2.2
7
+ - 2.3
8
+ - 2.4
8
9
  - jruby-19mode
9
10
 
10
11
  sudo: false
data/Gemfile CHANGED
@@ -1,3 +1,13 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :development do
6
+ gem 'bundler'
7
+ gem 'rake', '~> 10'
8
+ gem 'rspec', '~> 3.7'
9
+ gem 'vcr'
10
+ gem 'webmock'
11
+ gem 'pry'
12
+ gem 'yard'
13
+ end
data/README.md CHANGED
@@ -1,104 +1,291 @@
1
- # Phaxio
1
+ # 📠 Phaxio
2
2
 
3
3
  [![Build Status](https://travis-ci.org/phaxio/phaxio-ruby.svg?branch=master)](https://travis-ci.org/phaxio/phaxio-ruby)
4
4
 
5
- A Ruby gem for interacting with the [Phaxio API]( https://www.phaxio.com/docs ).
6
-
7
- **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).
8
6
 
9
7
  ## Installation
10
8
 
11
- Add this line to your application's Gemfile:
9
+ Add to your application's Gemfile:
12
10
 
13
- gem 'phaxio'
11
+ ``` ruby
12
+ gem 'phaxio', '~> 2.0.0'
13
+ ```
14
14
 
15
15
  And then execute:
16
16
 
17
- $ bundle
17
+ ``` sh
18
+ $ bundle install
19
+ ```
18
20
 
19
21
  Or install it yourself as:
20
22
 
21
- $ gem install phaxio
23
+ ``` sh
24
+ $ gem install phaxio
25
+ ```
22
26
 
23
27
  ## Usage
24
28
 
25
- Configure Phaxio with your api_key and api_secret:
29
+ Set up your API Key, API Secret, and, optionally, Callback Token.
26
30
 
27
- Phaxio.config do |config|
28
- config.api_key = "10987654321"
29
- config.api_secret = "12345678910"
30
- 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
+ ```
31
45
 
32
- To send a fax:
46
+ You can include `Phaxio::Resources` to pull in the resource classes for convenience:
47
+
48
+ ``` ruby
49
+ include Phaxio::Resources
50
+
51
+ fax_file = File.open 'test.pdf', 'rb'
52
+ Fax.create to: '+15558675309', file: fax_file
53
+ ```
33
54
 
34
- Phaxio.send_fax(to: "0123456789", filename: File.new("test.pdf"))
35
55
 
36
56
  ### Currently Supported API Calls
37
57
 
38
- * send_fax - `Phaxio.send_fax(to: "0123456789", filename: File.new("test.pdf"))`
39
- * resend_fax - `Phaxio.resend_fax(id: 1234)`
40
- * test_receive - `Phaxio.test_receive(filename: "test_file.pdf")`
41
- * provision_number - `Phaxio.provision_number(area_code: 802)`
42
- * release_number - `Phaxio.release_number(number: "8021112222")`
43
- * list_numbers - `Phaxio.list_numbers(area_code: 802)`
44
- * get_fax_file - `Phaxio.get_fax_file(id: 123456, type: p)`
45
- * list_faxes - `Phaxio.list_numbers(area_code: 802)`
46
- * list_faxes - `Phaxio.list_faxes(start: Time.now - 48000, end: Time.now)`
47
- * get_fax_status - `Phaxio.get_fax_status(id: 123456)`
48
- * cancel_fax - `Phaxio.cancel_fax(id: 123456)`
49
- * delete_fax - `Phaxio.delete_fax(id: 1234, files_only: true)`
50
- * get_account_status - `Phaxio.get_account_status`
51
- * attach_phaxcode_to_pdf - `Phaxio.attach_phaxcode_to_pdf(x: 10, y:10, File.new("input.pdf"))`
52
- * create_phaxcode - `Phaxio.create_phaxcode(metadata: "some metadata")`
53
- * supported_countries
54
- * area_codes
55
-
56
- ### Example
57
-
58
- require 'phaxio'
59
-
60
- Phaxio.config do |config|
61
- config.api_key = "your_key"
62
- config.api_secret = "your_secret"
63
- end
58
+ #### Faxes
64
59
 
65
- @fax = Phaxio.send_fax(to: '15555555555', string_data: "hello world")
66
- Phaxio.get_fax_status(id: @fax["faxId"])
60
+ ##### `Fax.create`
67
61
 
68
- # Get a Fax and save it as a PDF
69
- @pdf = Phaxio.get_fax_file(id: @fax["faxId"], type: "p")
70
- File.open("received_test.pdf", "w") do |file|
71
- file << @pdf
72
- end
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
+ ```
73
257
 
74
258
  ## Callback Validation Example with Sinatra
75
259
 
76
- require 'sinatra/base'
77
- require 'phaxio'
78
-
79
- class PhaxioCallbackExample < Sinatra::Base
80
- Phaxio.config do |config|
81
- config.api_key = '0123456789'
82
- config.api_secret = '0123456789'
83
- config.callback_token = '0123456789'
84
- end
85
-
86
- post '/phaxio_callback' do
87
- if Phaxio.valid_callback_signature?(
88
- request.env['HTTP_X_PHAXIO_SIGNATURE'],
89
- request.url, callback_params, params[:filename])
90
- 'Success'
91
- else
92
- 'Invalid callback signature'
93
- end
94
- end
95
-
96
- def callback_params
97
- params.select do |key, _value|
98
- %w(success is_test direction fax metadata).include?(key)
99
- end
100
- end
260
+ ``` ruby
261
+ require 'sinatra/base'
262
+ require 'phaxio'
263
+
264
+ class PhaxioCallbackExample < Sinatra::Base
265
+ Phaxio.config do |config|
266
+ config.api_key = '0123456789'
267
+ config.api_secret = '0123456789'
268
+ config.callback_token = '0123456789'
269
+ end
270
+
271
+ post '/phaxio_callback' do
272
+ signature = request.env['HTTP_X_PHAXIO_SIGNATURE']
273
+ url = request.url
274
+ file_params = params[:filename]
275
+ if Phaxio::Callback.valid_signature? signature, url, callback_params, file_params
276
+ 'Success'
277
+ else
278
+ 'Invalid callback signature'
279
+ end
280
+ end
281
+
282
+ def callback_params
283
+ params.select do |key, _value|
284
+ %w(success is_test direction fax metadata message).include?(key)
101
285
  end
286
+ end
287
+ end
288
+ ```
102
289
 
103
290
  ## Contributing
104
291
 
data/Rakefile CHANGED
@@ -1,21 +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
10
  desc 'Run tests'
13
- task :default => :test
14
-
15
- desc 'Run integration tests'
16
- task :integration_tests do
17
- integration_tests_path = File.expand_path(
18
- '../test/integration/*_integration_test.rb', __FILE__
19
- )
20
- FileList[integration_tests_path].each { |file| require file }
21
- end
11
+ task :default => :spec
@@ -1,4 +1,48 @@
1
- require "httmultiparty"
1
+ require 'json'
2
+ require 'tempfile'
3
+ require 'openssl'
4
+ require 'time'
5
+ require 'faraday'
6
+ require 'active_support/core_ext/hash/indifferent_access'
7
+ require 'mime/types/full'
8
+ require 'phaxio/version'
9
+ require 'phaxio/config'
10
+ require 'phaxio/client'
11
+ require 'phaxio/error'
12
+ require 'phaxio/resource'
13
+ require 'phaxio/resources'
2
14
 
3
- require "phaxio/version"
4
- require "phaxio/client"
15
+ Dir[File.expand_path(File.join('..', 'phaxio', 'helpers', '*.rb'), __FILE__)].each do |file|
16
+ require file
17
+ end
18
+
19
+ %w[fax_recipient fax account callback phax_code phone_number public].each do |filename|
20
+ require File.expand_path(File.join('..', 'phaxio', 'resources', filename), __FILE__)
21
+ end
22
+
23
+ module Phaxio
24
+ include Resources
25
+
26
+ class << self
27
+ # @!attribute api_key
28
+ # @see Config.api_key
29
+ # @!attribute api_secret
30
+ # @see Config.api_secret
31
+ # @!attribute callback_token
32
+ # @see Config.callback_token
33
+ # @!attribute api_endpoint
34
+ # @see Config.api_endpoint
35
+ %w(api_key api_secret callback_token api_endpoint).each do |config_attribute|
36
+ # Define getters
37
+ define_method(config_attribute) do
38
+ Config.public_send config_attribute
39
+ end
40
+
41
+ # Define setters
42
+ setter = "#{config_attribute}="
43
+ define_method(setter) do |value|
44
+ Config.public_send setter, value
45
+ end
46
+ end
47
+ end
48
+ end