phaxio 0.5.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,31 @@
1
+ module Phaxio
2
+ class Config
3
+ DEFAULT_API_ENDPOINT = 'https://api.phaxio.com/v2/'.freeze
4
+
5
+ class << self
6
+ # Your Phaxio API key. This will be used for all interactions with the Phaxio API.
7
+ #
8
+ # To find your API key, visit https://console.phaxio.com/api_credentials
9
+ attr_accessor :api_key
10
+
11
+ # Your Phaxio API secret. This will be used for all interactions with the Phaxio API.
12
+ #
13
+ # To find your API secret, visit https://console.phaxio.com/api_credentials
14
+ attr_accessor :api_secret
15
+
16
+ # Your Phaxio callback token. This will be used to verify that callback requests are coming
17
+ # from Phaxio.
18
+ #
19
+ # To find your callback token, visit https://console.phaxio.com/user/callbacks/edit
20
+ attr_accessor :callback_token
21
+
22
+ # The Phaxio API endpoint. Users generally shouldn't need to change it.
23
+ # Defaults to https://api.phaxio.com/v2/
24
+ attr_writer :api_endpoint
25
+
26
+ def api_endpoint
27
+ @api_endpoint || DEFAULT_API_ENDPOINT
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ module Phaxio
2
+ module Error
3
+ PhaxioError = Class.new StandardError
4
+ %w[
5
+ AuthenticationError
6
+ NotFoundError
7
+ InvalidRequestError
8
+ RateLimitExceededError
9
+ GeneralError
10
+ ApiConnectionError
11
+ ].each { |error_klass_name| const_set error_klass_name, Class.new(PhaxioError) }
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module Phaxio
2
+ # @api private
3
+ module MimeTypeHelper
4
+ class << self
5
+ def extension_for_mimetype mimetype
6
+ MIME::Types[mimetype].first.extensions.first
7
+ end
8
+
9
+ def mimetype_for_file file_path
10
+ MIME::Types.of(file_path).first.content_type
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,168 @@
1
+ module Phaxio
2
+ # The base class for API resources, such as `Fax` and `PhoneNumber`.
3
+ #
4
+ # This class is considered an implementation detail, and shouldn't be directly relied upon by
5
+ # users.
6
+ #
7
+ # The only exception is that this class will continue to be the base class for all Phaxio
8
+ # resources, so checking whether a fax instance is a kind of Phaxio::Resource will always return
9
+ # true.
10
+ class Resource
11
+ private
12
+
13
+ # The raw response data
14
+ attr_accessor :raw_data
15
+
16
+ # Populates the instance's attributes based on the `raw_data`.
17
+ def populate_attributes
18
+ self.class.normal_attribute_list.each do |normal_attribute|
19
+ self.public_send "#{normal_attribute}=", raw_data[normal_attribute]
20
+ end
21
+
22
+ self.class.time_attribute_list.each do |time_attribute|
23
+ time = raw_data[time_attribute]
24
+ time = Time.parse(time) if !time.nil?
25
+ self.public_send "#{time_attribute}=", time
26
+ end
27
+
28
+ self.class.collection_attribute_mappings.each do |collection_attribute, klass|
29
+ collection = raw_data[collection_attribute] || []
30
+ collection = {'data' => collection}
31
+ collection = klass.response_collection(collection)
32
+ self.public_send "#{collection_attribute}=", collection
33
+ end
34
+ end
35
+
36
+ # @see Phaxio::Resource.response_record
37
+ def initialize raw_data
38
+ self.raw_data = raw_data
39
+ populate_attributes
40
+ end
41
+
42
+ class << self
43
+ # @api private
44
+ # Returns a new instance of the resource for this data.
45
+ # @param raw_data [Hash] The raw response data from Phaxio.
46
+ # @return [Phaxio::Resource] The resource instance.
47
+ def response_record raw_data
48
+ new raw_data
49
+ end
50
+
51
+ # @api private
52
+ # Returns a new collection of resource instances for this data.
53
+ # @param raw_data [Array] The raw response data from Phaxio.
54
+ # @return [Phaxio::Resource::Collection] A collection of Phaxio::Resource instances.
55
+ def response_collection raw_data
56
+ Collection.new raw_data, self
57
+ end
58
+
59
+ # @api private
60
+ # Full list of resource-specific attributes.
61
+ attr_accessor :attribute_list
62
+
63
+ # @api private
64
+ # List of resource-specific attributes that don't require additional processing during
65
+ # instance data population.
66
+ attr_accessor :normal_attribute_list
67
+
68
+ # @api private
69
+ # List of resource-specific attributes that must be parsed into a Time object during instance
70
+ # data population.
71
+ attr_accessor :time_attribute_list
72
+
73
+ # @api private
74
+ # Mapping of resource-specific attributes that must be parsed into a resource collection.
75
+ attr_accessor :collection_attribute_mappings
76
+
77
+ private :new
78
+
79
+ private
80
+
81
+ # Creates accessors for the given normal attributes and adds them to the class's internal
82
+ # attribute lists.
83
+ # @param attribute_list [Array]
84
+ # A list of attributes as strings or symbols.
85
+ # @see Phaxio::Resource.normal_attribute_list
86
+ def has_normal_attributes attribute_list
87
+ attribute_list = attribute_list.map { |attribute_name| attribute_name.to_s.freeze }
88
+ attr_accessor *attribute_list
89
+ self.attribute_list += attribute_list
90
+ self.normal_attribute_list += attribute_list
91
+ end
92
+
93
+ # Creates accessors for the given time attributes and adds them to the class's internal
94
+ # attribute lists.
95
+ # @param attribute_list [Array]
96
+ # A list of attributes as strings or symbols.
97
+ # @see Phaxio::Resource.time_attribute_list
98
+ def has_time_attributes attribute_list
99
+ attribute_list = attribute_list.map { |attribute_name| attribute_name.to_s.freeze }
100
+ attr_accessor *attribute_list
101
+ self.attribute_list += attribute_list
102
+ self.time_attribute_list += attribute_list
103
+ end
104
+
105
+ # Creates accessors for the given collection attributes and adds them to the class's internal
106
+ # attribute lists.
107
+ # @param attribute_hash [Hash<String, Symbol => Phaxio::Resource>]
108
+ # A hash which has keys corresponding to the attribute name on this resource, and values
109
+ # corresponding to the resource class for the collection's items.
110
+ # @see Phaxio::Resource.collection_attribute_mappings
111
+ def has_collection_attributes attribute_hash
112
+ # Array#to_h doesn't exist in 2.0.0, hence the inject here.
113
+ attribute_hash = attribute_hash
114
+ .map { |k, v| [ k.to_s.freeze, v ] }
115
+ .inject({}) { |memo, obj| memo.tap { |memo| memo[obj.first] = obj.last } }
116
+ attr_accessor *attribute_hash.keys
117
+ self.attribute_list += attribute_hash.keys
118
+ self.collection_attribute_mappings = self.collection_attribute_mappings.merge(attribute_hash)
119
+ end
120
+
121
+ # Use the inherited hook to dynamically set each subclass's attribute lists to empty arrays
122
+ # upon creation.
123
+ def inherited subclass
124
+ subclass.attribute_list = []
125
+ subclass.normal_attribute_list = []
126
+ subclass.time_attribute_list = []
127
+ subclass.collection_attribute_mappings = {}
128
+ end
129
+ end
130
+
131
+ class Collection
132
+ include Enumerable
133
+
134
+ # The raw response data
135
+ attr_accessor :raw_data, :collection, :total, :per_page, :page
136
+
137
+ # Returns a new collection of resource instances for this data. Generally this is not called
138
+ # directly.
139
+ #
140
+ # @see Phaxio::Resource.response_collection
141
+ def initialize response_data, resource
142
+ if response_data.key? 'paging'
143
+ self.total = response_data['paging']['total']
144
+ self.per_page = response_data['paging']['per_page']
145
+ self.page = response_data['paging']['page']
146
+ end
147
+ self.raw_data = response_data['data']
148
+ self.collection = raw_data.map { |record_data| resource.response_record record_data }
149
+ end
150
+
151
+ def [] idx
152
+ collection[idx]
153
+ end
154
+
155
+ def each(&block)
156
+ collection.each(&block)
157
+ end
158
+
159
+ def length
160
+ collection.length
161
+ end
162
+
163
+ def size
164
+ length
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,7 @@
1
+ module Phaxio
2
+ # This module contains all Phaxio Resource classes. These have already been included into the
3
+ # Phaxio namespace, so you can write Phaxio::Fax instead of Phaxio::Resources::Fax. If that's
4
+ # still too verbose, you can `include Phaxio::Resources` to pull only these in.
5
+ module Resources
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Information about your Phaxio account.
4
+ class Account < Resource
5
+ ACCOUNT_PATH = 'account'
6
+ private_constant :ACCOUNT_PATH
7
+
8
+ # @return [Integer] Your current account funds balance in cents.
9
+ # @!attribute balance
10
+
11
+ # @return [Hash<String: Integer>] A hash of the number of faxes sent and received today.
12
+ # @!attribute faxes_today
13
+
14
+ # @return [Hash<String: Integer>] A hash of the number of faxes sent and received this month.
15
+ # @!attribute faxes_this_month
16
+ has_normal_attributes %w[balance faxes_today faxes_this_month]
17
+
18
+ class << self
19
+ # Get information about your Phaxio account, including your balance, number of faxes sent
20
+ # today, and number of faxes sent this week.
21
+ # @param params [Hash]
22
+ # Any parameters to send to Phaxio. This action does not have any unique parameters.
23
+ # @return [Phaxio::Resources::Acount] Your account information.
24
+ # @raise [Phaxio::Error::PhaxioError]
25
+ # @see https://www.phaxio.com/docs/api/v2/account/status
26
+ def get params = {}
27
+ response = Client.request :get, account_status_endpoint, params
28
+ response_record response
29
+ end
30
+ alias :status :get
31
+ alias :retrieve :get
32
+
33
+ private
34
+
35
+ def account_status_endpoint
36
+ "#{ACCOUNT_PATH}/status"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,65 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Provides utilities for working with callbacks.
4
+ # @see https://www.phaxio.com/docs/api/v2/faxes/send_callback
5
+ # @see https://www.phaxio.com/docs/api/v2/faxes/receive_callback
6
+ class Callback
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 callback URL used in this request.
17
+ # @param params [Hash]
18
+ # The parameters received with the callback, excluding files.
19
+ # @param files [Array<File>]
20
+ # The files received with the callback, 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
+ callback_data = "#{url}#{params_string}#{files_string}"
35
+ OpenSSL::HMAC.hexdigest(DIGEST, callback_token, callback_data)
36
+ end
37
+
38
+ def callback_token
39
+ Phaxio.callback_token or raise(Error::PhaxioError, 'No callback 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
@@ -0,0 +1,310 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Provides functionality for viewing and managing faxes.
4
+ class Fax < Resource
5
+ FAXES_PATH = 'faxes'.freeze
6
+ private_constant :FAXES_PATH
7
+
8
+ # @return [Integer] the ID associated with this fax.
9
+ # @!attribute id
10
+
11
+ # @return ["sent" | "received"] the direction of the fax.
12
+ # @!attribute direction
13
+
14
+ # @return [Integer] the number of pages in the fax.
15
+ # @!attribute num_pages
16
+
17
+ # @return [Integer] the cost of the fax in cents.
18
+ # @!attribute cost
19
+
20
+ # @return [String] the status of the fax.
21
+ # @!attribute status
22
+
23
+ # @return [true | false]
24
+ # an indication of whether or not this is a test fax.
25
+ # @!attribute is_test
26
+
27
+ # @return [String]
28
+ # for sent faxes only, the number set as the Caller ID when sending the
29
+ # fax.
30
+ # @!attribute caller_id
31
+
32
+ # @return [String]
33
+ # for received faxes only, the sender's E.164 phone number.
34
+ # @!attribute from_number
35
+
36
+ # @return [String]
37
+ # for received faxes only, the Phaxio phone number that was used to
38
+ # receive the call.
39
+ # @!attribute to_number
40
+
41
+ # @return [String]
42
+ # one of the Phaxio error types. Will give you a general idea of what
43
+ # went wrong for a failed fax.
44
+ # @!attribute error_type
45
+
46
+ # @return [String]
47
+ # a more detailed description of what went wrong for a failed fax.
48
+ # @!attribute error_message
49
+
50
+ # @return [Integer]
51
+ # a numeric error code that corresponds to the error message, if any.
52
+ # @!attribute error_id
53
+
54
+ # @return [Hash]
55
+ # a hash of tag name and value pairs. If a fax was sent with tag
56
+ # metadata, it will appear here.
57
+ # @!attribute tags
58
+ has_normal_attributes %w[
59
+ id direction num_pages cost status is_test caller_id from_number
60
+ to_number error_type error_message error_id tags
61
+ ]
62
+
63
+ # @return [Time]
64
+ # the time the fax was created.
65
+ # @!attribute created_at
66
+
67
+ # @return [Time]
68
+ # the time the fax was completed.
69
+ # @!attribute completed_at
70
+
71
+ has_time_attributes %w[created_at completed_at]
72
+
73
+ # @return [Phaxio::Resource::Collection<Phaxio::Resources::FaxRecipient>]
74
+ # a collection of this fax's recipients.
75
+ # @!attribute recipients
76
+
77
+ has_collection_attributes({recipients: FaxRecipient})
78
+
79
+ # A reference to a fax. This is returned by certain actions which don't
80
+ # return the full fax.
81
+ class Reference
82
+ # @return [Integer]
83
+ # The ID of the fax being referenced.
84
+ attr_accessor :id
85
+
86
+ # Gets the referenced fax.
87
+ # @return [Phaxio::Resource::Fax]
88
+ # The referenced Fax.
89
+ def get
90
+ Fax.get self
91
+ end
92
+ alias :retrieve :get
93
+ alias :find :get
94
+
95
+ def to_i
96
+ id
97
+ end
98
+
99
+ private
100
+
101
+ def initialize id
102
+ self.id = id
103
+ end
104
+ end
105
+
106
+ class << self
107
+ # @macro paging
108
+ # List faxes in date range.
109
+ # @param params [Hash]
110
+ # Any parameters to send to Phaxio.
111
+ # - *direction* [String] - Either "sent" or "received". Limits results
112
+ # to faxes with the specified direction.
113
+ # - *status* [String] - Limits results to faxes with the specified
114
+ # status.
115
+ # - *phone_number* [String] - A phone number in E.164 format that you
116
+ # want to use to filter results. The phone number must be an exact
117
+ # match, not a number fragment.
118
+ # - *tag* [Hash<String: String>] - A tag name and value that you want
119
+ # to use to filter results.
120
+ # @return [Phaxio::Resource::Collection<Phaxio::Resources::Fax>]
121
+ # The collection of faxes matching your request.
122
+ # @raise [Phaxio::Error::PhaxioError]
123
+ # @see https://www.phaxio.com/docs/api/v2/faxes/list_faxes
124
+ def list params = {}
125
+ response = Client.request :get, faxes_endpoint, params
126
+ response_collection response
127
+ end
128
+
129
+ # Create and send a fax.
130
+ # @param params [Hash]
131
+ # Any parameters to send to Phaxio. At least one *to* number is
132
+ # required, as well as at least one *file* or *content_url*.
133
+ # - *to* [String | Array<String>] - One or more phone numbers in E.164
134
+ # format where this fax should be sent.
135
+ # - *file* [File | Array<File>] - The fax file(s) to be sent.
136
+ # - *content_url* [String, Array<String>] - URL(s) to be rendered and
137
+ # sent as the fax content. If the *file* param is included as well,
138
+ # URL content will come first in the transmitted files.
139
+ # - *header_text* [String] - Text that will be displayed at the top of
140
+ # each page of the fax. 50-character maximum. Defaults to "-".
141
+ # - *batch_delay* [Integer] - Enabled batching and specifies the
142
+ # amount of time, in seconds, before the batch is fired. Max is 3600
143
+ # (one hour).
144
+ # - *batch_collision_avoidance* [true | false] - When *batch_delay* is
145
+ # set, the fax will be blocked until the receiving machine is no
146
+ # longer busy.
147
+ # - *callback_url* [String] - You can specify a callback URL that will
148
+ # override one set globally in your account.
149
+ # - *cancel_timeout* [Integer] - Number of minutes after which the fax
150
+ # will be canceled if it hasn't yet completed. Must be between 3 and
151
+ # \60. Additionally, for faxes with *batch_delay* set, it must be at
152
+ # least 3 minutes after the *batch_delay*. If not, it will be
153
+ # automatically extended when batching.
154
+ # - *tag* [Hash<String: Object>] - A tag that contains metadata
155
+ # relevant to your application. For example, you may wish to tag a
156
+ # fax with an order id in your application. You could pass Phaxio
157
+ # the following parameter: +tag: {order_id: 1234}+. You may specify
158
+ # up to 10 tags.
159
+ # @return [Phaxio::Resources::Fax]
160
+ # The created fax.
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
164
+ def create params = {}
165
+ response = Client.request :post, faxes_endpoint, params
166
+ response_reference response
167
+ end
168
+ alias :send :create
169
+
170
+ # Get fax info.
171
+ # @param id [Integer]
172
+ # The ID of the fax to retrieve information about.
173
+ # @param params [Hash]
174
+ # A hash of parameters to send to Phaxio. This action takes no unique parameters.
175
+ # @return [Phaxio::Resource::Fax] The requested fax.
176
+ # @raise [Phaxio::Error::PhaxioError]
177
+ # @see https://www.phaxio.com/docs/api/v2/faxes/get_fax
178
+ def get id, params = {}
179
+ response = Client.request :get, fax_endpoint(id.to_i), params
180
+ response_record response
181
+ end
182
+ alias :retrieve :get
183
+ alias :find :get
184
+
185
+ # Cancel a fax.
186
+ # @param id [Integer]
187
+ # The ID of the fax to cancel.
188
+ # @param params [Hash]
189
+ # A hash of parameters to send to Phaxio. This action takes no unique
190
+ # parameters.
191
+ # @return [Phaxio::Resources::Fax::Reference]
192
+ # A reference to the canceled fax.
193
+ # @raise [Phaxio::Error::PhaxioError]
194
+ # @see https://www.phaxio.com/docs/api/v2/faxes/cancel
195
+ def cancel id, params = {}
196
+ response = Client.request :post, cancel_fax_endpoint(id), params
197
+ response_reference response
198
+ end
199
+
200
+ # Resend a fax.
201
+ # @param id [Integer]
202
+ # The ID of the fax to resend.
203
+ # @param params [Hash]
204
+ # A hash of parameters to send to Phaxio.
205
+ # - *callback_url* [String] - This parameter may be used to set a
206
+ # different callback URL for the new fax.
207
+ # @return [Phaxio::Resources::Fax::Reference]
208
+ # A reference to the resent fax.
209
+ # @raise [Phaxio::Error::PhaxioError]
210
+ # @see https://www.phaxio.com/docs/api/v2/faxes/resend
211
+ def resend id, params = {}
212
+ response = Client.request :post, resend_fax_endpoint(id), params
213
+ response_reference response
214
+ end
215
+
216
+ # Delete a fax. May only be used with test API credentials.
217
+ # @param id [Integer]
218
+ # The ID of the fax to delete.
219
+ # @param params [Hash]
220
+ # A hash of parameters to send to Phaxio. This action takes no unique
221
+ # parameters.
222
+ # @return [true]
223
+ # @raise [Phaxio::Error::PhaxioError]
224
+ # @see https://www.phaxio.com/docs/api/v2/faxes/delete_fax
225
+ def delete id, params = {}
226
+ Client.request :delete, fax_endpoint(id), params
227
+ true
228
+ end
229
+
230
+ # Delete fax files.
231
+ # @param id [Integer]
232
+ # The ID of the fax for which you want to delete files.
233
+ # @param params [Hash]
234
+ # A hash of parameters to send to Phaxio. This action takes no unique
235
+ # parameters.
236
+ # @return [true]
237
+ # @raise [Phaxio::Error::PhaxioError]
238
+ # @see https://www.phaxio.com/docs/api/v2/faxes/delete_fax_file
239
+ def delete_file id, params = {}
240
+ Client.request :delete, fax_file_endpoint(id), params
241
+ true
242
+ end
243
+
244
+ # Get fax content file or thumbnail.
245
+ # @param id [Integer]
246
+ # The ID of the fax for which you want to get a file.
247
+ # @param params [Hash]
248
+ # A hash of parameters to send to Phaxio.
249
+ # - *thumbnail* ["s" | "l"] - If set to +"s"+ (small) or +"l"+
250
+ # (large), a thumbnail of the requested size will be returned.
251
+ # If unset, returns a PDF of the fax image.
252
+ # @return [File]
253
+ # The requested fax file.
254
+ # @raise [Phaxio::Error::PhaxioError]
255
+ # @see https://www.phaxio.com/docs/api/v2/faxes/get_fax_file
256
+ def file id, params = {}
257
+ Client.request :get, fax_file_endpoint(id), params
258
+ end
259
+
260
+ # Test receiving a fax. May only be used with test API credentials.
261
+ # @param params [Hash]
262
+ # A hash of parameters to send to Phaxio.
263
+ # - *file* [File] - A PDF file to simulate receiving.
264
+ # - *from_number* [String] - The phone number of the simulated sender
265
+ # in E.164 format. Default is the public Phaxio phone number.
266
+ # - *to_number* [String] - The phone number, in E.164 format, that is
267
+ # receiving the fax. Specifically, a Phaxio phone number in your
268
+ # account that is "receiving" the fax, or the public Phaxio phone
269
+ # number. Default is the public Phaxio phone number.
270
+ # @return [true]
271
+ # @raise [Phaxio::Error::PhaxioError]
272
+ # @see https://www.phaxio.com/docs/api/v2/faxes/test_receive
273
+ def test_receive params = {}
274
+ Client.request :post, faxes_endpoint, test_receive_params(params)
275
+ true
276
+ end
277
+
278
+ private
279
+
280
+ def response_reference response
281
+ Reference.new response['id']
282
+ end
283
+
284
+ def faxes_endpoint
285
+ FAXES_PATH
286
+ end
287
+
288
+ def fax_endpoint id
289
+ "#{FAXES_PATH}/#{id}"
290
+ end
291
+
292
+ def fax_file_endpoint id
293
+ "#{fax_endpoint(id)}/file"
294
+ end
295
+
296
+ def cancel_fax_endpoint id
297
+ "#{fax_endpoint(id)}/cancel"
298
+ end
299
+
300
+ def resend_fax_endpoint id
301
+ "#{fax_endpoint(id)}/resend"
302
+ end
303
+
304
+ def test_receive_params params
305
+ {direction: 'received'}.merge(params)
306
+ end
307
+ end
308
+ end
309
+ end
310
+ end