phaxio 0.4.0 → 2.1.0.pre
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 +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +11 -0
- data/Gemfile +11 -5
- data/README.md +296 -40
- data/Rakefile +7 -9
- data/lib/phaxio.rb +48 -3
- data/lib/phaxio/client.rb +151 -241
- 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/ata.rb +219 -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 +10 -8
- data/spec/client_spec.rb +164 -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 +248 -0
- data/spec/resources/phax_code_spec.rb +111 -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/files/test.txt +1 -0
- data/spec/support/vcr.rb +8 -0
- data/spec/support/vcr_cassettes/resources/account/status.yml +45 -0
- data/spec/support/vcr_cassettes/resources/fax/cancel.yml +47 -0
- data/spec/support/vcr_cassettes/resources/fax/create.yml +50 -0
- data/spec/support/vcr_cassettes/resources/fax/create_for_cancel.yml +50 -0
- data/spec/support/vcr_cassettes/resources/fax/create_for_delete.yml +50 -0
- data/spec/support/vcr_cassettes/resources/fax/create_for_delete_file.yml +50 -0
- data/spec/support/vcr_cassettes/resources/fax/create_for_download_file.yml +50 -0
- data/spec/support/vcr_cassettes/resources/fax/create_for_get.yml +50 -0
- data/spec/support/vcr_cassettes/resources/fax/create_for_reference.yml +50 -0
- data/spec/support/vcr_cassettes/resources/fax/create_for_resend.yml +50 -0
- data/spec/support/vcr_cassettes/resources/fax/delete.yml +45 -0
- data/spec/support/vcr_cassettes/resources/fax/delete_file.yml +45 -0
- data/spec/support/vcr_cassettes/resources/fax/file.yml +50 -0
- data/spec/support/vcr_cassettes/resources/fax/get.yml +45 -0
- data/spec/support/vcr_cassettes/resources/fax/list.yml +51 -0
- data/spec/support/vcr_cassettes/resources/fax/reference.yml +45 -0
- data/spec/support/vcr_cassettes/resources/fax/resend.yml +47 -0
- data/spec/support/vcr_cassettes/resources/fax/test_receive.yml +51 -0
- data/spec/support/vcr_cassettes/resources/phax_code/create.yml +47 -0
- data/spec/support/vcr_cassettes/resources/phax_code/create_png.yml +52 -0
- data/spec/support/vcr_cassettes/resources/phax_code/get.yml +45 -0
- data/spec/support/vcr_cassettes/resources/phax_code/get_id.yml +46 -0
- data/spec/support/vcr_cassettes/resources/phax_code/get_id_png.yml +50 -0
- data/spec/support/vcr_cassettes/resources/phax_code/get_png.yml +50 -0
- data/spec/support/vcr_cassettes/resources/phone_number/create.yml +48 -0
- data/spec/support/vcr_cassettes/resources/phone_number/get.yml +46 -0
- data/spec/support/vcr_cassettes/resources/phone_number/list.yml +60 -0
- data/spec/support/vcr_cassettes/resources/phone_number/release.yml +45 -0
- data/spec/support/vcr_cassettes/resources/public/area_codes/list.yml +79 -0
- data/spec/support/vcr_cassettes/resources/public/country/list.yml +55 -0
- metadata +103 -73
- 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 -53
- data/test/test_phaxio.rb +0 -72
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
module Phaxio
|
|
2
|
+
module Resources
|
|
3
|
+
# Provides functionality for viewing and managing phone numbers.
|
|
4
|
+
class PhoneNumber < Resource
|
|
5
|
+
PHONE_NUMBERS_PATH = 'phone_numbers'.freeze
|
|
6
|
+
private_constant :PHONE_NUMBERS_PATH
|
|
7
|
+
|
|
8
|
+
# @return [String] The phone number itself, in E.164 format.
|
|
9
|
+
# @!attribute phone_number
|
|
10
|
+
|
|
11
|
+
# @return [String] The city associated with the phone number.
|
|
12
|
+
# @!attribute city
|
|
13
|
+
|
|
14
|
+
# @return [String] The state associated with the phone number.
|
|
15
|
+
# @!attribute state
|
|
16
|
+
|
|
17
|
+
# @return [String] The country associated with the phone number.
|
|
18
|
+
# @!attribute country
|
|
19
|
+
|
|
20
|
+
# @return [Integer] The cost of the phone number in cents.
|
|
21
|
+
# @!attribute cost
|
|
22
|
+
|
|
23
|
+
# @return [String] The callback URL associated with the phone number.
|
|
24
|
+
# @!attribute callback_url
|
|
25
|
+
|
|
26
|
+
has_normal_attributes %w(phone_number city state country cost callback_url)
|
|
27
|
+
|
|
28
|
+
# @return [Time] The time at which the phone number was provisioned.
|
|
29
|
+
# @!attribute provisioned_at
|
|
30
|
+
|
|
31
|
+
# @return [Time] The last time the phone number was billed.
|
|
32
|
+
# @!attribute last_billed_at
|
|
33
|
+
|
|
34
|
+
has_time_attributes %w(provisioned_at last_billed_at)
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
class << self
|
|
39
|
+
# Provision a new phone number.
|
|
40
|
+
# @param params [Hash]
|
|
41
|
+
# A hash of parameters to send to Phaxio:
|
|
42
|
+
# - *country_code* [Integer] - The country code (E.164) of the number you'd like to provision.
|
|
43
|
+
# - *area_code* [Integer] - The area code of the number you'd like to provision.
|
|
44
|
+
# - *callback_url* [String] - A callback URL that we'll post to when a fax is received by this number. This will override the global receive callback URL, if you have one specified.
|
|
45
|
+
# @return [Phaxio::Resources::PhoneNumber] The newly provisioned number.
|
|
46
|
+
# @raise [Phaxio::Error::PhaxioError]
|
|
47
|
+
# @see https://www.phaxio.com/docs/api/v2/phone_numbers/provision
|
|
48
|
+
def create params = {}
|
|
49
|
+
response = Client.request :post, phone_numbers_endpoint, params
|
|
50
|
+
response_record response
|
|
51
|
+
end
|
|
52
|
+
alias :provision :create
|
|
53
|
+
|
|
54
|
+
# Get information about a specific phone number.
|
|
55
|
+
# @param phone_number [String]
|
|
56
|
+
# The phone number itself, in E.164 format, which you want to get information about.
|
|
57
|
+
# @param params [Hash]
|
|
58
|
+
# A hash of parameters to send to Phaxio. This action has no unique parameters.
|
|
59
|
+
# @return [Phaxio::Resources::PhoneNumber] The requested number.
|
|
60
|
+
# @raise [Phaxio::Error::PhaxioError]
|
|
61
|
+
# @see https://www.phaxio.com/docs/api/v2/phone_numbers/get_number
|
|
62
|
+
def get phone_number, params = {}
|
|
63
|
+
response = Client.request :get, phone_number_endpoint(phone_number), params
|
|
64
|
+
response_record response
|
|
65
|
+
end
|
|
66
|
+
alias :find :get
|
|
67
|
+
alias :retrieve :get
|
|
68
|
+
|
|
69
|
+
# @macro paging
|
|
70
|
+
# Get a list of phone numbers that you currently own on Phaxio.
|
|
71
|
+
# @param params [Hash]
|
|
72
|
+
# A hash of parameters to send to Phaxio.
|
|
73
|
+
# - *country_code* [Integer] - An E.164 country code that you'd like to filter by.
|
|
74
|
+
# - *area_code* [Integer] - An area code that you'd like to filter by. If an area code is
|
|
75
|
+
# specified, then *country_code* is required.
|
|
76
|
+
# @return [Phaxio::Resource::Collection<Phaxio::Resources::PhoneNumber>]
|
|
77
|
+
# A collection of phone numbers.
|
|
78
|
+
# @raise [Phaxio::Error::PhaxioError]
|
|
79
|
+
# @see https://www.phaxio.com/docs/api/v2/phone_numbers/list
|
|
80
|
+
def list params = {}
|
|
81
|
+
response = Client.request :get, phone_numbers_endpoint, params
|
|
82
|
+
response_collection response
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Release a phone number that you no longer need. Once a phone number is released you will
|
|
86
|
+
# no longer be charged for it.
|
|
87
|
+
# @param phone_number [String]
|
|
88
|
+
# The phone number itself, in E.164 format, which you want to release.
|
|
89
|
+
# @param params [Hash]
|
|
90
|
+
# A hash of parameters to send to Phaxio. This action has no unique parameters.
|
|
91
|
+
# @return true
|
|
92
|
+
# @raise [Phaxio::Error::PhaxioError]
|
|
93
|
+
# @see https://www.phaxio.com/docs/api/v2/phone_numbers/release
|
|
94
|
+
def delete phone_number, params = {}
|
|
95
|
+
Client.request :delete, phone_number_endpoint(phone_number), params
|
|
96
|
+
true
|
|
97
|
+
end
|
|
98
|
+
alias :release :delete
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
def phone_numbers_endpoint
|
|
103
|
+
PHONE_NUMBERS_PATH
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def phone_number_endpoint phone_number
|
|
107
|
+
"#{phone_numbers_endpoint}/#{phone_number}"
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module Phaxio
|
|
2
|
+
module Resources
|
|
3
|
+
module Public
|
|
4
|
+
# Information about area codes for provisioning numbers.
|
|
5
|
+
#
|
|
6
|
+
# @see Phaxio::Resources::PhoneNumber.list_available_area_codes
|
|
7
|
+
class AreaCode < Resource
|
|
8
|
+
AVAILABLE_AREA_CODES_PATH = 'public/area_codes'.freeze
|
|
9
|
+
private_constant :AVAILABLE_AREA_CODES_PATH
|
|
10
|
+
|
|
11
|
+
# @return [Integer] The country code associated with this area code.
|
|
12
|
+
# @!attribute country_code
|
|
13
|
+
|
|
14
|
+
# @return [Integer] The actual area code.
|
|
15
|
+
# @!attribute area_code
|
|
16
|
+
|
|
17
|
+
# @return [String] The city associated with this area code.
|
|
18
|
+
# @!attribute city
|
|
19
|
+
|
|
20
|
+
# @return [String] The state associated with this area code.
|
|
21
|
+
# @!attribute state
|
|
22
|
+
|
|
23
|
+
# @return [String] The country associated with this area code.
|
|
24
|
+
# @!attribute country
|
|
25
|
+
|
|
26
|
+
# @return [true | false] Indicates whether this area code is toll free.
|
|
27
|
+
# @!attribute toll_free
|
|
28
|
+
|
|
29
|
+
has_normal_attributes %w[country_code area_code city state country toll_free]
|
|
30
|
+
|
|
31
|
+
class << self
|
|
32
|
+
# @macro paging
|
|
33
|
+
# Displays a list of area codes available for purchasing Phaxio numbers. This operation
|
|
34
|
+
# requires no authentication and can be used without passing an API key.
|
|
35
|
+
# @param params [Hash]
|
|
36
|
+
# A hash of parameters to send to Phaxio.
|
|
37
|
+
# - *toll_free* [true|false] - If set to *true*, only toll free area codes will be
|
|
38
|
+
# returned. If specified and set to *false*, only non-toll free area codes will be
|
|
39
|
+
# returned.
|
|
40
|
+
# - *country_code* [Integer] - An E.164 country code you'd like to filter by.
|
|
41
|
+
# - *country* [String] - A two character country abbreviation (ISO 3166; e.g. "US" or
|
|
42
|
+
# "CA") you'd like to filter by.
|
|
43
|
+
# - *state* [String] - A two character state or province abbreviation (ISO 3166; e.g.
|
|
44
|
+
# "IL" or "YT") you'd like to filter by. When using this parameter, *country_code* or
|
|
45
|
+
# *country* must also be provided.
|
|
46
|
+
# @return [Phaxio::Resource::Collection<Phaxio::Resources::AreaCode>] A collection of
|
|
47
|
+
# AreaCode objects.
|
|
48
|
+
# @raise [Phaxio::Error::PhaxioError]
|
|
49
|
+
# @see https://www.phaxio.com/docs/api/v2/public/list_area_codes
|
|
50
|
+
def list params = {}
|
|
51
|
+
response = Client.request :get, available_area_codes_endpoint, params
|
|
52
|
+
AreaCode.response_collection response
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def available_area_codes_endpoint
|
|
58
|
+
AVAILABLE_AREA_CODES_PATH
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module Phaxio
|
|
2
|
+
module Resources
|
|
3
|
+
module Public
|
|
4
|
+
class Country < Resource
|
|
5
|
+
SUPPORTED_COUNTRIES_PATH = 'public/countries'.freeze
|
|
6
|
+
private_constant :SUPPORTED_COUNTRIES_PATH
|
|
7
|
+
|
|
8
|
+
# @return [String] the name of the country.
|
|
9
|
+
# @!attribute name
|
|
10
|
+
|
|
11
|
+
# @return [String] the alpha2 representation of the country.
|
|
12
|
+
# @!attribute alpha2
|
|
13
|
+
|
|
14
|
+
# @return [Integer] the E.164 country code for the country.
|
|
15
|
+
# @!attribute country_code
|
|
16
|
+
|
|
17
|
+
# @return [Integer] the price per page for the country, in cents.
|
|
18
|
+
# @!attribute price_per_page
|
|
19
|
+
|
|
20
|
+
# @return [String] the level of send support provided for this country.
|
|
21
|
+
# @!attribute send_support
|
|
22
|
+
|
|
23
|
+
# @return [String] the level of receive support provided for this country.
|
|
24
|
+
# @!attribute receive_support
|
|
25
|
+
|
|
26
|
+
has_normal_attributes %w[
|
|
27
|
+
name alpha2 country_code price_per_page send_support receive_support
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
class << self
|
|
31
|
+
# @macro paging
|
|
32
|
+
# Get a list of supported countries for sending faxes.
|
|
33
|
+
# @param params [Hash]
|
|
34
|
+
# A hash of parameters to send to Phaxio. This action has no unique
|
|
35
|
+
# parameters.
|
|
36
|
+
# @return [Phaxio::Resource::Collection<Phaxio::Resources::Country>]
|
|
37
|
+
# A collection of supported countries.
|
|
38
|
+
# @raise Phaxio::Error::PhaxioError
|
|
39
|
+
# @see https://www.phaxio.com/docs/api/v2/public/list_countries
|
|
40
|
+
def list params = {}
|
|
41
|
+
response = Client.request :get, supported_countries_endpoint, params
|
|
42
|
+
Country.response_collection response
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def supported_countries_endpoint
|
|
48
|
+
SUPPORTED_COUNTRIES_PATH
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/phaxio/version.rb
CHANGED
data/phaxio.gemspec
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
require File.expand_path('../lib/phaxio/version', __FILE__)
|
|
3
3
|
|
|
4
4
|
Gem::Specification.new do |gem|
|
|
5
|
-
gem.authors = ["
|
|
6
|
-
gem.email = ["
|
|
7
|
-
gem.description = %q{
|
|
8
|
-
gem.summary = %q{
|
|
9
|
-
gem.homepage = "
|
|
5
|
+
gem.authors = ["Julien Negrotto"]
|
|
6
|
+
gem.email = ["julien@phaxio.com"]
|
|
7
|
+
gem.description = %q{Official ruby gem for interacting with Phaxio's API.}
|
|
8
|
+
gem.summary = %q{Official ruby gem for interacting with Phaxio's API.}
|
|
9
|
+
gem.homepage = "https://github.com/phaxio/phaxio-ruby"
|
|
10
10
|
|
|
11
11
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
12
12
|
gem.files = `git ls-files`.split("\n")
|
|
@@ -14,8 +14,10 @@ Gem::Specification.new do |gem|
|
|
|
14
14
|
gem.name = "phaxio"
|
|
15
15
|
gem.require_paths = ["lib"]
|
|
16
16
|
gem.version = Phaxio::VERSION
|
|
17
|
+
gem.licenses = ['MIT']
|
|
17
18
|
|
|
18
|
-
gem.
|
|
19
|
-
gem.
|
|
20
|
-
gem.
|
|
19
|
+
gem.required_ruby_version = '>= 2.0'
|
|
20
|
+
gem.add_dependency 'faraday', '>= 0.10', '< 2.0'
|
|
21
|
+
gem.add_dependency 'mime-types', '~> 3.0'
|
|
22
|
+
gem.add_dependency 'activesupport'
|
|
21
23
|
end
|
data/spec/client_spec.rb
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe Phaxio::Client do
|
|
4
|
+
subject(:client) { Phaxio::Client }
|
|
5
|
+
let(:test_connection) { instance_double Faraday::Connection }
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
allow(client).to receive(:conn) { test_connection }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def test_response status_code, message: 'This is a test.'
|
|
12
|
+
success = (200..299).include? status_code
|
|
13
|
+
instance_double(
|
|
14
|
+
Faraday::Response,
|
|
15
|
+
status: status_code,
|
|
16
|
+
success?: success,
|
|
17
|
+
headers: {content_type: 'application/json'},
|
|
18
|
+
body: %Q({"success": #{success}, "message": "#{message}", "data": {"foo": "bar"}})
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe 'making a request' do
|
|
23
|
+
it 'sends the request to Phaxio' do
|
|
24
|
+
endpoint = 'public/countries/'
|
|
25
|
+
params = {page: 1, per_page: 10}
|
|
26
|
+
expect(test_connection).to receive(:get) do |request_endpoint, request_params|
|
|
27
|
+
expect(request_endpoint).to eq(endpoint)
|
|
28
|
+
expect(request_params[:page]).to eq(params[:page])
|
|
29
|
+
expect(request_params[:per_page]).to eq(params[:per_page])
|
|
30
|
+
test_response 200
|
|
31
|
+
end
|
|
32
|
+
client.request :get, endpoint, params
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 'handles a single file param' do
|
|
36
|
+
file_path = File.expand_path File.join('..', 'support', 'files', 'test.pdf'), __FILE__
|
|
37
|
+
params = {to: '+12258888888', file: File.open(file_path)}
|
|
38
|
+
expect(test_connection).to receive(:post) do |request_endpoint, request_params|
|
|
39
|
+
expect(request_params[:file]).to be_a(Faraday::UploadIO)
|
|
40
|
+
expect(request_params[:file].original_filename).to eq(File.basename(file_path))
|
|
41
|
+
test_response 200
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
client.request :post, 'faxes', params
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it 'handles multiple file params' do
|
|
48
|
+
file_names = ['test.pdf', 'test.txt']
|
|
49
|
+
file_paths = file_names.map do |file_name|
|
|
50
|
+
File.expand_path File.join('..', 'support', 'files', file_name), __FILE__
|
|
51
|
+
end
|
|
52
|
+
params = {to: '+12258888888', file: file_paths.map { |file_path| File.open(file_path) }}
|
|
53
|
+
|
|
54
|
+
expect(test_connection).to receive(:post) do |request_endpoint, request_params|
|
|
55
|
+
expect(request_params[:file]).to be_a(Array)
|
|
56
|
+
correct_file_params = request_params[:file].all? do |file_param|
|
|
57
|
+
file_param.is_a? Faraday::UploadIO
|
|
58
|
+
end
|
|
59
|
+
expect(correct_file_params).to eq(true)
|
|
60
|
+
expect(request_params[:file].map(&:original_filename)).to eq(file_names)
|
|
61
|
+
test_response 200
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
client.request :post, 'faxes', params
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it 'uses the configured API key and secret by default' do
|
|
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)
|
|
71
|
+
test_response 200
|
|
72
|
+
end
|
|
73
|
+
client.request :get, 'test'
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it 'uses the api key specified in the params hash' do
|
|
77
|
+
custom_api_key = 'custom-api-key'
|
|
78
|
+
custom_api_secret = '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)
|
|
82
|
+
test_response 200
|
|
83
|
+
end
|
|
84
|
+
client.request :get, 'test', api_key: custom_api_key, api_secret: custom_api_secret
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it 'parses the response JSON and returns the data if the response indicates success' do
|
|
88
|
+
expect(test_connection).to receive(:get) { test_response 200 }
|
|
89
|
+
result = client.request :get, 'test'
|
|
90
|
+
expect(result).to eq({'foo' => 'bar'})
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it 'raises an authentication error if the response status is 401' do
|
|
94
|
+
expect(test_connection).to receive(:get) { test_response 401 }
|
|
95
|
+
expect {
|
|
96
|
+
client.request :get, 'test'
|
|
97
|
+
}.to raise_error(Phaxio::Error::AuthenticationError, '401: This is a test.')
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it 'raises a not found error if the response status is 404' do
|
|
101
|
+
expect(test_connection).to receive(:get) { test_response 404 }
|
|
102
|
+
expect do
|
|
103
|
+
client.request :get, 'test'
|
|
104
|
+
end.to raise_error(Phaxio::Error::NotFoundError, '404: This is a test.')
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it 'raises an invalid request error if the response status is 422' do
|
|
108
|
+
expect(test_connection).to receive(:get) { test_response 422 }
|
|
109
|
+
expect {
|
|
110
|
+
client.request :get, 'test'
|
|
111
|
+
}.to raise_error(Phaxio::Error::InvalidRequestError, '422: This is a test.')
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it 'raises a rate limit error if the response status is 429' do
|
|
115
|
+
expect(test_connection).to receive(:get) { test_response 429 }
|
|
116
|
+
expect {
|
|
117
|
+
client.request :get, 'test'
|
|
118
|
+
}.to raise_error(Phaxio::Error::RateLimitExceededError, '429: This is a test.')
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
it 'raises a general error for if the response status is 5XX' do
|
|
122
|
+
expect(test_connection).to receive(:get) { test_response 500 }
|
|
123
|
+
expect {
|
|
124
|
+
client.request :get, 'test'
|
|
125
|
+
}.to raise_error(Phaxio::Error::GeneralError, '500: This is a test.')
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
it 'raises a general error if the response status is some other value' do
|
|
129
|
+
expect(test_connection).to receive(:get) { test_response 418 }
|
|
130
|
+
expect {
|
|
131
|
+
client.request :get, 'test'
|
|
132
|
+
}.to raise_error(Phaxio::Error::GeneralError, '418: This is a test.')
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it 'raises an API connection error if a networking issue occurs' do
|
|
136
|
+
expect(test_connection).to receive(:get) { raise Faraday::ConnectionFailed, 'This is a test.' }
|
|
137
|
+
expect {
|
|
138
|
+
client.request :get, 'test'
|
|
139
|
+
}.to raise_error(Phaxio::Error::ApiConnectionError, 'Error communicating with Phaxio: This is a test.')
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it 'allows unknown errors to bubble up' do
|
|
143
|
+
expect(test_connection).to receive(:get) { raise RuntimeError, 'This is a test.' }
|
|
144
|
+
expect {
|
|
145
|
+
client.request :get, 'test'
|
|
146
|
+
}.to raise_error(RuntimeError, 'This is a test.')
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
it 'raises a general error if the status indicates success but the body indicates failure' do
|
|
150
|
+
expect(test_connection).to receive(:get) do
|
|
151
|
+
instance_double(
|
|
152
|
+
Faraday::Response,
|
|
153
|
+
status: 200,
|
|
154
|
+
success?: true,
|
|
155
|
+
headers: {content_type: 'application/json'},
|
|
156
|
+
body: '{"success": false, "message": "Something bad happened.", "data": {"foo": "bar"}}'
|
|
157
|
+
)
|
|
158
|
+
end
|
|
159
|
+
expect {
|
|
160
|
+
client.request :get, 'test'
|
|
161
|
+
}.to raise_error(Phaxio::Error::GeneralError, "Something bad happened.")
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Phaxio::MimeTypeHelper do
|
|
4
|
+
it 'gets the mimetype for a filename' do
|
|
5
|
+
expect(subject.mimetype_for_file('test.pdf')).to eq('application/pdf')
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it 'gets the extension for a mimetype' do
|
|
9
|
+
expect(subject.extension_for_mimetype('application/pdf')).to eq('pdf')
|
|
10
|
+
end
|
|
11
|
+
end
|