phaxio 0.4.0 → 2.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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,219 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Provides functionality for managing ATAs.
4
+ class Ata < Resource
5
+ ATAS_PATH = 'atas'.freeze
6
+ private_constant :ATAS_PATH
7
+
8
+ # @return [Integer] the ID of the ATA.
9
+ # @!attribute id
10
+
11
+ # @return [String] the name of the ATA.
12
+ # @!attribute name
13
+
14
+ # @return [String] the description of the ATA.
15
+ # @!attribute description
16
+
17
+ # @return [String] The user phone number associated with the ATA.
18
+ # @!attribute user_phone_number
19
+
20
+ # @return [String] The domain for the ATA.
21
+ # @!attribute domain
22
+
23
+ # @return [String] The username for the ATA.
24
+ # @!attribute uername
25
+
26
+ # @return [String] The password for the ATA.
27
+ # @!attribute password
28
+
29
+ has_normal_attributes %w[
30
+ id name description user_phone_number domain username password
31
+ ]
32
+
33
+ # A reference to an ATA. This is returned by certain actions which don't
34
+ # return the full ATA.
35
+ class Reference
36
+ # @return [Integer]
37
+ # The ID of the referenced ATA.
38
+ attr_accessor :id
39
+
40
+ def to_i
41
+ id
42
+ end
43
+
44
+ private
45
+
46
+ def initialize id
47
+ self.id = id
48
+ end
49
+ end
50
+
51
+ # A reference to a phone number, returned by ATA phone number management
52
+ # actions.
53
+ class PhoneNumberReference
54
+ # @return [String]
55
+ # The phone number.
56
+ attr_accessor :phone_number
57
+
58
+ def to_s
59
+ phone_number
60
+ end
61
+
62
+ private
63
+
64
+ def initialize phone_number
65
+ self.phone_number = phone_number
66
+ end
67
+ end
68
+
69
+ class << self
70
+ # @macro paging
71
+ # List ATAs
72
+ # @param params[Hash]
73
+ # Any parameters to send to Phaxio.
74
+ # @return [Phaxio::Resource::Collection<Phaxio::Resources::Ata>]
75
+ # The collection of ATAs matching your request.
76
+ # @raise [Phaxio::Error::PhaxioError]
77
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/list
78
+ def list params = {}
79
+ response = Client.request :get, atas_endpoint, params
80
+ response_collection response
81
+ end
82
+
83
+ # Create an ATA
84
+ # @param params [Hash]
85
+ # Any parameters to send to Phaxio.
86
+ # - *name* [String] - A name used to identify the ATA.
87
+ # - *description* [String] - A longer description of the ATA.
88
+ # - *domain* [String] - A domain for the ATA.
89
+ # @return [Phaxio::Resources::Ata]
90
+ # The created ATA, including the generated username and password.
91
+ # @raise [Phaxio::Error::PhaxioError]
92
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/create
93
+ def create params = {}
94
+ response = Client.request :post, atas_endpoint, params
95
+ response_record response
96
+ end
97
+
98
+ # Get an ATA
99
+ # @param id [Integer]
100
+ # The ID of the ATA to retrieve information about.
101
+ # @param params [Hash]
102
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
103
+ # @return [Phaxio::Resources::Ata]
104
+ # The requested ATA.
105
+ # @raise [Phaxio::Error::PhaxioError]
106
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/get
107
+ def get id, params = {}
108
+ response = Client.request :get, ata_endpoint(id.to_i), params
109
+ response_record response
110
+ end
111
+ alias :retrieve :get
112
+ alias :find :get
113
+
114
+ # Update an ATA
115
+ # @param id [Integer]
116
+ # The ID of the ATA to update.
117
+ # @param params [Hash]
118
+ # Any parameters to send to Phaxio.
119
+ # - *name* [String] - A name used to identify the ATA.
120
+ # - *description* [String] - A longer description of the ATA.
121
+ # - *domain* [String] - A domain for the ATA.
122
+ # @return [Phaxio::Resources::Ata]
123
+ # The updated ATA.
124
+ # @raise [Phaxio::Error::PhaxioError]
125
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/update
126
+ def update id, params = {}
127
+ response = Client.request :patch, ata_endpoint(id.to_i), params
128
+ response_record response
129
+ end
130
+
131
+ # Regenerate credentials for an ATA
132
+ # @param id [Integer]
133
+ # The ID of the ATA for which credentials should be regenerated.
134
+ # @param params [Hash]
135
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
136
+ # @return [Phaxio::Resources::Ata]
137
+ # The ATA, including the new username and password.
138
+ # @raise Phaxio::Error::PhaxioError
139
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/regenerate_credentials
140
+ def regenerate_credentials id, params = {}
141
+ response = Client.request :patch, regenerate_credentials_endpoint(id.to_i), params
142
+ response_record response
143
+ end
144
+
145
+ # Delete an ATA
146
+ # @param id [Integer]
147
+ # The Id of the ATA to delete.
148
+ # @param params [Hash]
149
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
150
+ # @return [Phaxio::Resources::Ata::Reference]
151
+ # A reference to the deleted ATA.
152
+ # @raise [Phaxio::Error::PhaxioError]
153
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/delete
154
+ def delete id, params = {}
155
+ response = Client.request :delete, ata_endpoint(id.to_i), params
156
+ response_reference response
157
+ end
158
+
159
+ # Add a phone number
160
+ # @param id [Integer]
161
+ # The ID of the ATA to which you want to add a number.
162
+ # @param phone_number [String]
163
+ # The phone number to add to the ATA.
164
+ # @param params [Hash]
165
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
166
+ # @return [Phaxio::Resources::Ata::PhoneNumberReference]
167
+ # A reference to the added phone number.
168
+ # @raise [Phaxio::Error::PhaxioError]
169
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/add_phone_number
170
+ def add_phone_number id, phone_number, params = {}
171
+ response = Client.request :post, phone_number_endpoint(id, phone_number), params
172
+ response_phone_number_reference response
173
+ end
174
+
175
+ # Remove a phone number
176
+ # @param id [Integer]
177
+ # The ID of the ATA from which you want to remove the phone number.
178
+ # @param phone_number [String]
179
+ # The phone number you want to remove.
180
+ # @param params [Hash]
181
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
182
+ # @return [Phaxio::Resources::Ata::PhoneNumberReference]
183
+ # A reference to the removed phone number.
184
+ # @raise [Phaxio::Error::PhaxioError]
185
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/remove_phone_number
186
+ def remove_phone_number id, phone_number, params = {}
187
+ response = Client.request :delete, phone_number_endpoint(id, phone_number), params
188
+ response_phone_number_reference response
189
+ end
190
+
191
+ private
192
+
193
+ def response_reference response
194
+ Reference.new Integer(response['id'])
195
+ end
196
+
197
+ def response_phone_number_reference response
198
+ PhoneNumberReference.new(response['phone_number'])
199
+ end
200
+
201
+ def atas_endpoint
202
+ ATAS_PATH
203
+ end
204
+
205
+ def ata_endpoint id
206
+ "#{atas_endpoint}/#{id}"
207
+ end
208
+
209
+ def regenerate_credentials_endpoint id
210
+ "#{ata_endpoint(id)}/regenerate_credentials"
211
+ end
212
+
213
+ def phone_number_endpoint id, phone_number
214
+ "#{ata_endpoint(id)}/phone_numbers/#{phone_number}"
215
+ end
216
+ end
217
+ end
218
+ end
219
+ end