Skip to content

Commit f57fe11

Browse files
authored
Merge pull request #22 from phaxio/v2-1-pre
Bump to v2.1 and add ATA management actions
2 parents 4af3e14 + 483d9c3 commit f57fe11

40 files changed

Lines changed: 1219 additions & 1080 deletions

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ group :development do
1010
gem 'webmock'
1111
gem 'pry'
1212
gem 'yard'
13+
gem 'dotenv', require: false
1314
end

lib/phaxio.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'base64'
12
require 'json'
23
require 'tempfile'
34
require 'openssl'
@@ -16,7 +17,7 @@
1617
require file
1718
end
1819

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

lib/phaxio/client.rb

Lines changed: 31 additions & 10 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

@@ -109,20 +114,35 @@ def post endpoint, params = {}
109114
params[k] = file_param
110115
end
111116

112-
conn.post endpoint, params
117+
conn.post endpoint, params, api_headers(params)
118+
end
119+
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, api_headers(params)
113135
end
114136

115137
def get endpoint, params = {}
116-
conn.get endpoint, params
138+
conn.get endpoint, params, api_headers(params)
117139
end
118140

119141
def delete endpoint, params = {}
120-
conn.delete endpoint, params
142+
conn.delete endpoint, params, api_headers(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,11 +152,12 @@ 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-
}
155+
def api_headers params
156+
api_key = params[:api_key] || Phaxio.api_key
157+
api_secret = params[:api_secret] || Phaxio.api_secret
158+
return unless api_key && api_secret
159+
auth = Base64.strict_encode64("#{api_key}:#{api_secret}")
160+
{'Authorization' => "Basic #{auth}"}
140161
end
141162

142163
def file_to_param file

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

spec/client_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ def test_response status_code, message: 'This is a test.'
6565
end
6666

6767
it 'uses the configured API key and secret by default' do
68-
expect(test_connection).to receive(:get) do |_endpoint, request_params|
69-
expect(request_params[:api_key]).to eq(Phaxio.api_key)
70-
expect(request_params[:api_secret]).to eq(Phaxio.api_secret)
68+
expect(test_connection).to receive(:get) do |_endpoint, _request_params, request_headers|
69+
auth_header = "Basic #{Base64.strict_encode64("#{Phaxio.api_key}:#{Phaxio.api_secret}")}"
70+
expect(request_headers['Authorization']).to eq(auth_header)
7171
test_response 200
7272
end
7373
client.request :get, 'test'
@@ -76,9 +76,9 @@ def test_response status_code, message: 'This is a test.'
7676
it 'uses the api key specified in the params hash' do
7777
custom_api_key = 'custom-api-key'
7878
custom_api_secret = 'custom-api-secret'
79-
expect(test_connection).to receive(:get) do |_endpoint, request_params|
80-
expect(request_params[:api_key]).to eq(custom_api_key)
81-
expect(request_params[:api_secret]).to eq(custom_api_secret)
79+
expect(test_connection).to receive(:get) do |_endpoint, _request_params, request_headers|
80+
auth_header = "Basic #{Base64.strict_encode64('custom-api-key:custom-api-secret')}"
81+
expect(request_headers['Authorization']).to eq(auth_header)
8282
test_response 200
8383
end
8484
client.request :get, 'test', api_key: custom_api_key, api_secret: custom_api_secret

0 commit comments

Comments
 (0)