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.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.travis.yml +2 -1
- data/Gemfile +10 -0
- data/README.md +260 -73
- data/Rakefile +6 -16
- data/lib/phaxio.rb +47 -3
- data/lib/phaxio/client.rb +117 -484
- data/lib/phaxio/config.rb +31 -0
- data/lib/phaxio/error.rb +13 -0
- data/lib/phaxio/helpers/mime_type_helper.rb +14 -0
- data/lib/phaxio/resource.rb +168 -0
- data/lib/phaxio/resources.rb +7 -0
- data/lib/phaxio/resources/account.rb +41 -0
- data/lib/phaxio/resources/callback.rb +65 -0
- data/lib/phaxio/resources/fax.rb +310 -0
- data/lib/phaxio/resources/fax_recipient.rb +41 -0
- data/lib/phaxio/resources/phax_code.rb +89 -0
- data/lib/phaxio/resources/phone_number.rb +112 -0
- data/lib/phaxio/resources/public.rb +8 -0
- data/lib/phaxio/resources/public/area_code.rb +64 -0
- data/lib/phaxio/resources/public/country.rb +54 -0
- data/lib/phaxio/version.rb +1 -1
- data/phaxio.gemspec +9 -12
- data/spec/client_spec.rb +132 -0
- data/spec/helpers/mime_type_helper_spec.rb +11 -0
- data/spec/phaxio_spec.rb +20 -0
- data/spec/resources/account_spec.rb +24 -0
- data/spec/resources/callback_spec.rb +34 -0
- data/spec/resources/fax_spec.rb +227 -0
- data/spec/resources/phax_code_spec.rb +83 -0
- data/spec/resources/phone_number_spec.rb +89 -0
- data/spec/resources/public/area_code_spec.rb +24 -0
- data/spec/resources/public/country_spec.rb +24 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/credentials.rb +7 -0
- data/spec/support/expectations.rb +9 -0
- data/spec/support/files/test.pdf +0 -0
- data/spec/support/vcr.rb +9 -0
- data/spec/support/vcr_cassettes/resources/account/status.yml +44 -0
- data/spec/support/vcr_cassettes/resources/fax/cancel.yml +46 -0
- data/spec/support/vcr_cassettes/resources/fax/create.yml +230 -0
- data/spec/support/vcr_cassettes/resources/fax/delete.yml +44 -0
- data/spec/support/vcr_cassettes/resources/fax/delete_file.yml +44 -0
- data/spec/support/vcr_cassettes/resources/fax/file.yml +251 -0
- data/spec/support/vcr_cassettes/resources/fax/get.yml +44 -0
- data/spec/support/vcr_cassettes/resources/fax/list.yml +56 -0
- data/spec/support/vcr_cassettes/resources/fax/resend.yml +46 -0
- data/spec/support/vcr_cassettes/resources/fax/test_receive.yml +231 -0
- data/spec/support/vcr_cassettes/resources/phax_code/create.yml +100 -0
- data/spec/support/vcr_cassettes/resources/phax_code/get.yml +190 -0
- data/spec/support/vcr_cassettes/resources/phone_number/create.yml +47 -0
- data/spec/support/vcr_cassettes/resources/phone_number/get.yml +45 -0
- data/spec/support/vcr_cassettes/resources/phone_number/list.yml +52 -0
- data/spec/support/vcr_cassettes/resources/phone_number/release.yml +44 -0
- data/spec/support/vcr_cassettes/resources/public/area_codes/list.yml +77 -0
- data/spec/support/vcr_cassettes/resources/public/country/list.yml +54 -0
- metadata +70 -82
- data/.ruby-version +0 -1
- data/CHANGELOG +0 -6
- data/test/files/test.pdf +0 -0
- data/test/integration/phaxio_integration_test.rb +0 -45
- data/test/support/responses/account_status.json +0 -9
- data/test/support/responses/cancel_success.json +0 -4
- data/test/support/responses/fax_status_success.json +0 -21
- data/test/support/responses/list_faxes.json +0 -68
- data/test/support/responses/list_numbers.json +0 -22
- data/test/support/responses/provision_number.json +0 -12
- data/test/support/responses/release_number.json +0 -7
- data/test/support/responses/send_failure.json +0 -8
- data/test/support/responses/send_success.json +0 -8
- data/test/support/responses/test.pdf +0 -0
- data/test/support/responses/test_receive.json +0 -4
- data/test/test_helper.rb +0 -57
- data/test/test_phaxio.rb +0 -128
data/lib/phaxio/client.rb
CHANGED
@@ -1,502 +1,135 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
module Client
|
10
|
-
DIGEST = OpenSSL::Digest.new('sha1')
|
11
|
-
|
12
|
-
# Public: Send a fax.
|
13
|
-
#
|
14
|
-
# options - The Hash options used to refine the selection (default: {}):
|
15
|
-
# :to - The Phone Number (i.e. [country
|
16
|
-
# code][number] or just a 10 digit
|
17
|
-
# number in the US or Canada). Put
|
18
|
-
# square brackets after parameter
|
19
|
-
# name to send to multiple
|
20
|
-
# recipients (e.g. to[]) (required).
|
21
|
-
# :filename - A Ruby File of a document to fax
|
22
|
-
# (supported file types: https://www.phaxio.com/faq#faq10)
|
23
|
-
# :string_data - A String of html, plain text, or a
|
24
|
-
# URL. If additional files are
|
25
|
-
# specified as well, this data will
|
26
|
-
# be included first in the fax
|
27
|
-
# (optional).
|
28
|
-
# :string_data_type - An enum of the type of the string
|
29
|
-
# data that can be 'html', 'url', or
|
30
|
-
# 'text'. If not specified, default
|
31
|
-
# is 'text'. See string data
|
32
|
-
# rendering for more info (optional).
|
33
|
-
# :batch - The bool for running in batching
|
34
|
-
# mode. If present and true, fax will
|
35
|
-
# be sent in batching mode. Requires
|
36
|
-
# batch_delay to be specified
|
37
|
-
# (optional).
|
38
|
-
# :batch_delay - The int the of amount of time, in
|
39
|
-
# seconds, before the batch is fired.
|
40
|
-
# Must be specified if batch=true.
|
41
|
-
# Maximum delay is 3600 (1 hour)
|
42
|
-
# (optional).
|
43
|
-
# :batch_collision_avoidance - The bool for collision avoidance
|
44
|
-
# with batches. If true when
|
45
|
-
# batch=true, fax will be blocked
|
46
|
-
# until the receiving machine is no
|
47
|
-
# longer busy (optional).
|
48
|
-
# :callback_url - The String url for the callback.
|
49
|
-
# Overrides the globally set one
|
50
|
-
# (optional).
|
51
|
-
# :cancel_timeout - An int of the number of minutes
|
52
|
-
# after which the fax will be
|
53
|
-
# canceled if it hasn't yet
|
54
|
-
# completed. Must be between 1 and 60
|
55
|
-
# (optional).
|
56
|
-
#
|
57
|
-
# Examples
|
58
|
-
#
|
59
|
-
# Phaxio.send_fax(to: "0123456789", filename: File.new("docToSend.pdf"))
|
60
|
-
#
|
61
|
-
# Returns a HTTParty::Response object containing a success bool,
|
62
|
-
# a String message, and an in faxID.
|
63
|
-
def send_fax(options)
|
64
|
-
send_post("/send", options)
|
65
|
-
end
|
66
|
-
|
67
|
-
# Public: Resend a fax.
|
68
|
-
#
|
69
|
-
# options - The Hash options used to refine the selection (default: {}):
|
70
|
-
# :id - The int id of the fax you want to resend (required).
|
71
|
-
#
|
72
|
-
# Examples
|
73
|
-
#
|
74
|
-
# Phaxio.resend_fax(id: "123456")
|
75
|
-
#
|
76
|
-
# Returns a HTTParty::Response object containing a success bool,
|
77
|
-
# a message string, and data containing the fax ID int.
|
78
|
-
def resend_fax(options)
|
79
|
-
send_post("/resendFax", options)
|
80
|
-
end
|
81
|
-
|
82
|
-
# Public: Test receiving a fax.
|
83
|
-
#
|
84
|
-
# options - The Hash options used to refine the selection (default: {}):
|
85
|
-
# from_number - The Phone Number of the simulated sender
|
86
|
-
# (optional).
|
87
|
-
# to_number - The Phone Number receiving the fax (optional).
|
88
|
-
# filename - A String containing the name of the PDF that has
|
89
|
-
# a PhaxCode and is the file you want to simulate
|
90
|
-
# sending (required).
|
91
|
-
#
|
92
|
-
# Examples
|
93
|
-
#
|
94
|
-
# Phaxio.test_receive(filename: "test_file.pdf")
|
95
|
-
#
|
96
|
-
# Returns a HTTParty::Response object containing a success bool
|
97
|
-
# and a String message.
|
98
|
-
def test_receive(options)
|
99
|
-
send_post("/testReceive", options)
|
100
|
-
end
|
101
|
-
|
102
|
-
# Public: Provision a phone number that you can use to receive faxes in
|
103
|
-
# your Phaxio account.
|
104
|
-
#
|
105
|
-
# options - The Hash options used to refine the selection (default: {}):
|
106
|
-
# area_code - The integer area code of the number you'd like
|
107
|
-
# to provision (required).
|
108
|
-
# callback_url - A callback URL that Phaxio will post to when a
|
109
|
-
# fax is received by this number. This will
|
110
|
-
# override the global receive callback URL, if you
|
111
|
-
# have one set (optional).
|
112
|
-
#
|
113
|
-
# Examples
|
114
|
-
#
|
115
|
-
# Phaxio.provision_number(area_code: 802)
|
116
|
-
#
|
117
|
-
# Returns a HTTParty::Response object containing a success bool, a string
|
118
|
-
# message, and data containing the phone number, city, state, cost,
|
119
|
-
# last_billed_at, and the date the number was provisioned at.
|
120
|
-
def provision_number(options)
|
121
|
-
send_post("/provisionNumber", options)
|
122
|
-
end
|
123
|
-
|
124
|
-
# Public: Release a phone number that you no longer need. Once a phone
|
125
|
-
# number is released you will no longer be charged for it.
|
126
|
-
#
|
127
|
-
# options - The Hash options used to refine the selection (default: {}):
|
128
|
-
# number - The String of the phone number you want to release
|
129
|
-
# (required).
|
130
|
-
#
|
131
|
-
# Examples
|
132
|
-
#
|
133
|
-
# Phaxio.release_number(number: "8021112222")
|
134
|
-
#
|
135
|
-
# Returns a HTTParty::Response object containing a success bool and a
|
136
|
-
# string message.
|
137
|
-
def release_number(options)
|
138
|
-
send_post("/releaseNumber", options)
|
139
|
-
end
|
1
|
+
# @!macro [new] paging
|
2
|
+
# @note
|
3
|
+
# This action accepts paging parameters:
|
4
|
+
# - *per_page* [Integer] - The maximum number of results to return per
|
5
|
+
# call (i.e. "page"). Max 1000.
|
6
|
+
# - *page* [Integer] - The page number to return for the request. 1-based.
|
140
7
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
# s - Small JPG format thumbnail of the fax, 129 x 167 px.
|
172
|
-
# l - Large JPG format thumbnail of the fax, 300 x 388 px.
|
173
|
-
# p - PDF version of the fax (default).
|
174
|
-
#
|
175
|
-
# Examples
|
176
|
-
#
|
177
|
-
# Phaxio.get_fax_file(id: 1234, type: p)
|
178
|
-
# Phaxio.get_fax_file(id: 3254, type: l)
|
179
|
-
#
|
180
|
-
# Returns the fax as the type specified in the call, defaults to PDF.
|
181
|
-
def get_fax_file(options)
|
182
|
-
send_post("/faxFile", options)
|
183
|
-
end
|
184
|
-
|
185
|
-
# Public: List faxes within the specified time range.
|
186
|
-
#
|
187
|
-
# options - The Hash options used to refine the selection (default: {}):
|
188
|
-
# start - The Unix Timestamp for the beginning of the range
|
189
|
-
# (required).
|
190
|
-
# end - The Unix Timestamp for the end of the range (required).
|
191
|
-
#
|
192
|
-
# Examples
|
193
|
-
#
|
194
|
-
# Phaxio.list_faxes(start: 1293861600, end: 1294034400)
|
195
|
-
#
|
196
|
-
# Returns a HTTParty::Response object containing a success bool, a string
|
197
|
-
# message, paging information, and the fax data.
|
198
|
-
def list_faxes(options)
|
199
|
-
send_post("/faxList", options)
|
200
|
-
end
|
201
|
-
|
202
|
-
# Public: Get the status of a specific fax.
|
203
|
-
#
|
204
|
-
# options - The Hash options used to refine the selection (default: {}):
|
205
|
-
# id - The int id of the fax you want to get the status of
|
206
|
-
# (required).
|
207
|
-
#
|
208
|
-
# Examples
|
209
|
-
#
|
210
|
-
# Phaxio.get_fax_status(id: "123456")
|
211
|
-
#
|
212
|
-
# Returns a HTTParty::Response object containing a success bool,
|
213
|
-
# a String message, and the data of the fax.
|
214
|
-
def get_fax_status(options)
|
215
|
-
if options[:id].nil?
|
216
|
-
raise StandardError, "You must include a fax id."
|
8
|
+
module Phaxio
|
9
|
+
# @api private
|
10
|
+
|
11
|
+
class Client
|
12
|
+
class << self
|
13
|
+
# Makes a request to the Phaxio API.
|
14
|
+
#
|
15
|
+
# @param method [Symbol, String]
|
16
|
+
# The HTTP method for the request. Currently only `:get`, `:post`, and `:delete` are
|
17
|
+
# supported.
|
18
|
+
# @param endpoint [String]
|
19
|
+
# The endpoint for the API action, relative to `Phaxio::Config.api_endpoint`.
|
20
|
+
# @param params [Hash]
|
21
|
+
# Any parameters to be sent with the request.
|
22
|
+
#
|
23
|
+
# @return [Object]
|
24
|
+
# The `"data"` attribute of the deserialized JSON response. Varies based on the API action.
|
25
|
+
def request method, endpoint, params = {}
|
26
|
+
params = api_params params
|
27
|
+
begin
|
28
|
+
response = case method.to_s
|
29
|
+
when 'post' then post(endpoint, params)
|
30
|
+
when 'get' then get(endpoint, params)
|
31
|
+
when 'delete' then delete(endpoint, params)
|
32
|
+
else raise ArgumentError, "HTTP method `#{method}` is not supported."
|
33
|
+
end
|
34
|
+
handle_response response
|
35
|
+
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError => error
|
36
|
+
raise Error::ApiConnectionError, "Error communicating with Phaxio: #{error}"
|
37
|
+
end
|
217
38
|
end
|
218
39
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
#
|
227
|
-
# Examples
|
228
|
-
#
|
229
|
-
# Phaxio.cancel_fax(id: "123456")
|
230
|
-
#
|
231
|
-
# Returns a HTTParty::Response object containing a success bool
|
232
|
-
# and a String message.
|
233
|
-
def cancel_fax(options)
|
234
|
-
send_post("/faxCancel", options)
|
235
|
-
end
|
236
|
-
|
237
|
-
# Public: Delete a specific fax.
|
238
|
-
#
|
239
|
-
# options - The hash options used to refine the selection (defaults: {}):
|
240
|
-
# :id - The int ID of the fax you want to cancel
|
241
|
-
# (required).
|
242
|
-
# :files_only - The bool used to determine whether only the files
|
243
|
-
# are deleted. If not specified, default is false
|
244
|
-
# (optional).
|
245
|
-
#
|
246
|
-
# Examples
|
247
|
-
#
|
248
|
-
# Phaxio.delete_fax(id: 1234, files_only: true)
|
249
|
-
#
|
250
|
-
# Returns a HTTParty::Response object with success bool and message string.
|
251
|
-
def delete_fax(options)
|
252
|
-
send_post("/deleteFax", options)
|
253
|
-
end
|
254
|
-
|
255
|
-
# Public: Get the status of Client's account.
|
256
|
-
#
|
257
|
-
# Examples
|
258
|
-
#
|
259
|
-
# Phaxio.get_account_status
|
260
|
-
#
|
261
|
-
# Returns a HTTParty::Response object with success, message, and data
|
262
|
-
# (containing faxes_sent_this_month, faxes_sent_today, and balance).
|
263
|
-
def get_account_status
|
264
|
-
send_post("/accountStatus", {})
|
265
|
-
end
|
266
|
-
|
267
|
-
# Public: Attach a PhaxCode to a PDF you provide.
|
268
|
-
#
|
269
|
-
# options - Type: hash. Options used to refine the action (default: {}):
|
270
|
-
# x - Type: float. The x-coordinate (in PDF points*)
|
271
|
-
# where the PhaxCode should be drawn. x=0 is at the
|
272
|
-
# left-most point on the page. (required)
|
273
|
-
# y - Type: float. The y-coordinate (in PDF points*)
|
274
|
-
# where the PhaxCode should be drawn. Y=0 is the
|
275
|
-
# bottom-most point on the page. (required)
|
276
|
-
# filename - A Ruby File in PDF format (required)
|
277
|
-
# metadata - Type: string. Custom metadata to be associated
|
278
|
-
# with the created barcode. If not present, the
|
279
|
-
# basic PhaxCode for your account will be used.
|
280
|
-
# page_number - Type: integer. The page where the PhaxCode should
|
281
|
-
# be drawn. 1-based.
|
282
|
-
# *PDF points definition: A "point" is 1/72 of an inch. An
|
283
|
-
# 8.5"x11" document is therefore 612 pt x 792 pt.
|
284
|
-
#
|
285
|
-
# Examples
|
286
|
-
#
|
287
|
-
# Phaxio.attach_phaxcode_to_pdf(
|
288
|
-
# x: "0", y: "100", filename: File.new("input.pdf")
|
289
|
-
# )
|
290
|
-
#
|
291
|
-
# Response: A PDF file containing a PhaxCode at the location specified.
|
292
|
-
def attach_phaxcode_to_pdf(options)
|
293
|
-
if options[:filename].nil?
|
294
|
-
raise StandardError, 'You must include a PDF file.'
|
40
|
+
# @return [Faraday::Connection] A new Faraday connection to `Phaxio::Config.api_endpoint`.
|
41
|
+
def conn
|
42
|
+
Faraday.new(Phaxio.api_endpoint) do |conn|
|
43
|
+
conn.request :multipart
|
44
|
+
conn.request :url_encoded
|
45
|
+
conn.adapter :net_http
|
46
|
+
end
|
295
47
|
end
|
296
48
|
|
297
|
-
|
298
|
-
|
49
|
+
private
|
50
|
+
|
51
|
+
def handle_response response
|
52
|
+
content_type = response.headers[:content_type]
|
53
|
+
|
54
|
+
if content_type.start_with? 'application/json'
|
55
|
+
body = JSON.parse(response.body).with_indifferent_access
|
56
|
+
else
|
57
|
+
extension = MimeTypeHelper.extension_for_mimetype content_type
|
58
|
+
filename = File.join(
|
59
|
+
Dir.tmpdir,
|
60
|
+
Dir::Tmpname.make_tmpname('phaxio-', "download.#{extension}")
|
61
|
+
)
|
62
|
+
File.open(filename, 'wb') { |file| file.write response.body }
|
63
|
+
body = {'success' => response.success?, 'data' => File.open(filename, 'rb')}
|
64
|
+
end
|
65
|
+
|
66
|
+
if response.success?
|
67
|
+
raise(Error::GeneralError, body['message']) unless body['success']
|
68
|
+
|
69
|
+
# Check if this is a response with paging. If so, we want to return that along with the
|
70
|
+
# data.
|
71
|
+
if body.key? 'paging'
|
72
|
+
{'data' => body['data'], 'paging' => body['paging']}
|
73
|
+
else
|
74
|
+
body['data']
|
75
|
+
end
|
76
|
+
else
|
77
|
+
status = response.status
|
78
|
+
# TODO: Handle blank message
|
79
|
+
message = body['message']
|
80
|
+
|
81
|
+
case status
|
82
|
+
when 401
|
83
|
+
raise Error::AuthenticationError, "#{status}: #{message}"
|
84
|
+
when 404
|
85
|
+
raise Error::NotFoundError, "#{status}: #{message}"
|
86
|
+
when 422
|
87
|
+
raise Error::InvalidRequestError, "#{status}: #{message}"
|
88
|
+
when 429
|
89
|
+
raise Error::RateLimitExceededError, "#{status}: #{message}"
|
90
|
+
else
|
91
|
+
raise Error::GeneralError, "#{status}: #{message}"
|
92
|
+
end
|
93
|
+
end
|
299
94
|
end
|
300
95
|
|
301
|
-
|
302
|
-
|
96
|
+
def post endpoint, params = {}
|
97
|
+
# Handle file params
|
98
|
+
params.each do |k, v|
|
99
|
+
next unless k.to_s == 'file'
|
100
|
+
mime_type = MimeTypeHelper.mimetype_for_file v.path
|
101
|
+
params[k] = Faraday::UploadIO.new v, mime_type
|
102
|
+
end
|
303
103
|
|
304
|
-
|
305
|
-
#
|
306
|
-
# options - Type: hash. Options used to refine the action (default: {}):
|
307
|
-
# metadata - Type: string. Custom metadata to be associated with
|
308
|
-
# this barcode. If not present, the basic PhaxCode for
|
309
|
-
# your account will be used. (optional)
|
310
|
-
# redirect - Type: boolean. If present and true, the PhaxCode
|
311
|
-
# barcode image will be dumped in the response.
|
312
|
-
# (optional)
|
313
|
-
#
|
314
|
-
# Example:
|
315
|
-
# Phaxio.create_phaxcode(metadata: "sale_id=44")
|
316
|
-
#
|
317
|
-
# Response: If the redirect parameter is not provided, a JSON object with
|
318
|
-
# success, message, and data attributes is returned. The data
|
319
|
-
# attribute contains a url where the PhaxCode barcode image can be
|
320
|
-
# accessed. Otherwise, the image data is dumped in the response.
|
321
|
-
def create_phaxcode(options = {})
|
322
|
-
send_post('/createPhaxCode', options)
|
323
|
-
end
|
324
|
-
|
325
|
-
# Public: Get a Hosted Document with PhaxCode included
|
326
|
-
#
|
327
|
-
# Note: You will have to set up the hosted document with Phaxio (along with
|
328
|
-
# the relevant PhaxCode) before calling this method.
|
329
|
-
#
|
330
|
-
# options - Type: hash. Options used to refine the action (default: {}):
|
331
|
-
# name - Type: string. The name of a hosted document.
|
332
|
-
# (required)
|
333
|
-
# metadata - Type: string. Custom metadata to be associated with
|
334
|
-
# the PhaxCode that will be attached to the hosted
|
335
|
-
# document. If not present, the basic PhaxCode for your
|
336
|
-
# account will be used.
|
337
|
-
# (optional)
|
338
|
-
#
|
339
|
-
# Example:
|
340
|
-
# Phaxio.get_hosted_document(name:"business_fax")
|
341
|
-
#
|
342
|
-
# Response: A PDF copy of the hosted document with a PhaxCode included at
|
343
|
-
# the pre-specified location.
|
344
|
-
def get_hosted_document(options)
|
345
|
-
if options[:name].nil?
|
346
|
-
raise StandardError, 'You must include the name of the hosted document.'
|
104
|
+
conn.post endpoint, params
|
347
105
|
end
|
348
106
|
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
# Public: Get a list of supported countries for sending faxes
|
353
|
-
#
|
354
|
-
# Note: This method doesn't require API keys and is included for the sake of
|
355
|
-
# completion.
|
356
|
-
#
|
357
|
-
# Example:
|
358
|
-
# Phaxio.supported_countries
|
359
|
-
#
|
360
|
-
# Response: A JSON object with success, message, and data attributes. The
|
361
|
-
# data attribute contains a hash, where the key contains the name
|
362
|
-
# of the country, and the value is a hash of attributes for the
|
363
|
-
# country (currently only pricing information).
|
364
|
-
#
|
365
|
-
# Example Response:
|
366
|
-
# {
|
367
|
-
# "success": true,
|
368
|
-
# "message": "Data contains supported countries.",
|
369
|
-
# "data": {
|
370
|
-
# "United States": {
|
371
|
-
# "price_per_page": 7
|
372
|
-
# },
|
373
|
-
# "Canada": {
|
374
|
-
# "price_per_page": 7
|
375
|
-
# },
|
376
|
-
# "United Kingdom": {
|
377
|
-
# "price_per_page": 10
|
378
|
-
# },
|
379
|
-
# ...
|
380
|
-
# }
|
381
|
-
# }
|
382
|
-
def supported_countries
|
383
|
-
post('/supportedCountries')
|
384
|
-
end
|
385
|
-
|
386
|
-
# Public: List area codes available for purchasing numbers
|
387
|
-
#
|
388
|
-
# Note: This method doesn't require API keys and is included for the sake of
|
389
|
-
# completion.
|
390
|
-
#
|
391
|
-
# options - Type: hash. Options used to refine the query (default: {}):
|
392
|
-
# is_toll_free - Type: boolean. Will only return toll free area
|
393
|
-
# codes. (optional)
|
394
|
-
# state - Type: string. A two character state or province
|
395
|
-
# abbreviation (e.g. IL or YT). Will only return
|
396
|
-
# area codes for this state. (optional)
|
397
|
-
#
|
398
|
-
# Response: A JSON object with success, message, and data attributes. The
|
399
|
-
# data attribute contains a map of area codes to city and state.
|
400
|
-
#
|
401
|
-
# Example response:
|
402
|
-
# {
|
403
|
-
# "success": true,
|
404
|
-
# "message": "295 area codes available.",
|
405
|
-
# "data": {
|
406
|
-
# "201": {
|
407
|
-
# "city": "Bayonne, Jersey City, Union City",
|
408
|
-
# "state": "New Jersey"
|
409
|
-
# },
|
410
|
-
# "202": {
|
411
|
-
# "city": "Washington",
|
412
|
-
# "state": "District Of Columbia"
|
413
|
-
# },
|
414
|
-
# ... a lot more area codes here...
|
415
|
-
# }
|
416
|
-
# }
|
417
|
-
def area_codes(options = {})
|
418
|
-
post('/areaCodes', options)
|
419
|
-
end
|
420
|
-
|
421
|
-
def send_post(path, options)
|
422
|
-
post(
|
423
|
-
path, query: options.merge!(api_key: api_key, api_secret: api_secret)
|
424
|
-
)
|
425
|
-
end
|
426
|
-
|
427
|
-
# Public: Check the signature of the signed request.
|
428
|
-
#
|
429
|
-
# signature - Type: string. The X-Phaxio-Signature HTTP header value.
|
430
|
-
# url - Type: string. The full URL that was called by Phaxio,
|
431
|
-
# including the query. (required)
|
432
|
-
# params - Type: hash. The POSTed form data (required)
|
433
|
-
# files - Type: array. Submitted files (required - "received" fax
|
434
|
-
# callback only)
|
435
|
-
#
|
436
|
-
# Returns true if the signature matches the signed request, otherwise false
|
437
|
-
def valid_callback_signature?(signature, url, params, files = [])
|
438
|
-
check_signature = generate_check_signature(url, params, files)
|
439
|
-
check_signature == signature
|
440
|
-
end
|
441
|
-
|
442
|
-
# Public: Generate a signature using the request data and callback token
|
443
|
-
#
|
444
|
-
# url - Type: string. The full URL that was called by Phaxio,
|
445
|
-
# including the query. (required)
|
446
|
-
# params - Type: hash. The POSTed form data (required)
|
447
|
-
# files - Type: array. Submitted files (required - "received" fax
|
448
|
-
# callback only)
|
449
|
-
#
|
450
|
-
# Retuns a signature based on the request data and configured callback
|
451
|
-
# token, which can then be compared with the request signature.
|
452
|
-
def generate_check_signature(url, params, files = [])
|
453
|
-
params_string = generate_params_string(params)
|
454
|
-
file_string = generate_files_string(files)
|
455
|
-
callback_data = "#{url}#{params_string}#{file_string}"
|
456
|
-
OpenSSL::HMAC.hexdigest(DIGEST, callback_token, callback_data)
|
457
|
-
end
|
107
|
+
def get endpoint, params = {}
|
108
|
+
conn.get endpoint, params
|
109
|
+
end
|
458
110
|
|
459
|
-
|
111
|
+
def delete endpoint, params = {}
|
112
|
+
conn.delete endpoint, params
|
113
|
+
end
|
460
114
|
|
461
|
-
|
462
|
-
|
463
|
-
params_strings = sorted_params.map { |key, value| "#{key}#{value}" }
|
464
|
-
params_strings.join
|
465
|
-
end
|
115
|
+
def api_params params
|
116
|
+
params = default_params.merge params
|
466
117
|
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
end
|
118
|
+
# Convert times to ISO 8601
|
119
|
+
params.each do |k, v|
|
120
|
+
next unless v.kind_of?(Time) || v.kind_of?(Date)
|
121
|
+
params[k] = v.to_datetime.iso8601
|
122
|
+
end
|
473
123
|
|
474
|
-
|
475
|
-
|
476
|
-
end
|
124
|
+
params
|
125
|
+
end
|
477
126
|
|
478
|
-
|
479
|
-
|
127
|
+
def default_params
|
128
|
+
{
|
129
|
+
api_key: Phaxio.api_key,
|
130
|
+
api_secret: Phaxio.api_secret
|
131
|
+
}
|
132
|
+
end
|
480
133
|
end
|
481
134
|
end
|
482
|
-
|
483
|
-
# Public: Configure Phaxio with your api_key, api_secret, and the callback
|
484
|
-
# token provided in your Phaxio account (to verify that requests are
|
485
|
-
# coming from Phaxio).
|
486
|
-
#
|
487
|
-
# Examples
|
488
|
-
#
|
489
|
-
# Phaxio.config do |config|
|
490
|
-
# config.api_key = '12345678910'
|
491
|
-
# config.api_secret = '10987654321'
|
492
|
-
# config.callback_token = '32935829'
|
493
|
-
# end
|
494
|
-
#
|
495
|
-
# Returns nothing.
|
496
|
-
def self.config
|
497
|
-
yield(self)
|
498
|
-
end
|
499
|
-
|
500
|
-
extend Client
|
501
|
-
extend Config
|
502
135
|
end
|