Skip to content

Commit cfc2ae0

Browse files
author
Julien Negrotto
committed
Bump to v2.1 and add ATA management actions
Need to update to 2.1 for these to be available without manually setting the api url.
1 parent 29487f3 commit cfc2ae0

5 files changed

Lines changed: 244 additions & 12 deletions

File tree

lib/phaxio.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require file
1717
end
1818

19-
%w[fax_recipient fax account callback phax_code phone_number public].each do |filename|
19+
%w[fax_recipient fax account callback phax_code phone_number public ata].each do |filename|
2020
require File.expand_path(File.join('..', 'phaxio', 'resources', filename), __FILE__)
2121
end
2222

lib/phaxio/client.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def request method, endpoint, params = {}
2727
begin
2828
response = case method.to_s
2929
when 'post' then post(endpoint, params)
30+
when 'patch' then patch(endpoint, params)
3031
when 'get' then get(endpoint, params)
3132
when 'delete' then delete(endpoint, params)
3233
else raise ArgumentError, "HTTP method `#{method}` is not supported."
@@ -43,6 +44,10 @@ def conn
4344
conn.request :multipart
4445
conn.request :url_encoded
4546
conn.adapter :net_http
47+
48+
if Phaxio.api_key && Phaxio.api_secret
49+
conn.basic_auth Phaxio.api_key, Phaxio.api_secret
50+
end
4651
end
4752
end
4853

@@ -112,6 +117,23 @@ def post endpoint, params = {}
112117
conn.post endpoint, params
113118
end
114119

120+
def patch endpoint, params = {}
121+
# Handle file params
122+
params.each do |k, v|
123+
next unless k.to_s == 'file'
124+
125+
if v.is_a? Array
126+
file_param = v.map { |file| file_to_param file }
127+
else
128+
file_param = file_to_param v
129+
end
130+
131+
params[k] = file_param
132+
end
133+
134+
conn.patch endpoint, params
135+
end
136+
115137
def get endpoint, params = {}
116138
conn.get endpoint, params
117139
end
@@ -121,8 +143,6 @@ def delete endpoint, params = {}
121143
end
122144

123145
def api_params params
124-
params = default_params.merge params
125-
126146
# Convert times to ISO 8601
127147
params.each do |k, v|
128148
next unless v.kind_of?(Time) || v.kind_of?(Date)
@@ -132,13 +152,6 @@ def api_params params
132152
params
133153
end
134154

135-
def default_params
136-
{
137-
api_key: Phaxio.api_key,
138-
api_secret: Phaxio.api_secret
139-
}
140-
end
141-
142155
def file_to_param file
143156
mime_type = MimeTypeHelper.mimetype_for_file file.path
144157
Faraday::UploadIO.new file, mime_type

lib/phaxio/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Phaxio
22
class Config
3-
DEFAULT_API_ENDPOINT = 'https://api.phaxio.com/v2/'.freeze
3+
DEFAULT_API_ENDPOINT = 'https://api.phaxio.com/v2.1/'.freeze
44

55
class << self
66
# Your Phaxio API key. This will be used for all interactions with the Phaxio API.

lib/phaxio/resources/ata.rb

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/phaxio/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Phaxio
2-
VERSION = "2.0.1"
2+
VERSION = "2.1.0.pre"
33
end

0 commit comments

Comments
 (0)