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,41 @@
1
+ module Phaxio
2
+ module Resources
3
+ class FaxRecipient < Resource
4
+ # @return [String] The phone number of the recipient in E.164 format.
5
+ # @!attribute phone_number
6
+
7
+ # @return [String] The status of the recipient.
8
+ # @!attribute status
9
+
10
+ # @return [Integer] The number of retries for this recipient.
11
+ # @!attribute retry_count
12
+
13
+ # @return [Integer] The bitrate in bits/second that the fax was transmitted at.
14
+ # @!attribute bitrate
15
+
16
+ # @return [Integer]
17
+ # The horizontal resolution that the fax was transmitted at in pixels per meter.
18
+ # @!attribute resolution
19
+
20
+ # @return [String]
21
+ # One of the Phaxio Error types. Will give you a general idea of what went wrong for this
22
+ # recipient.
23
+ # @!attribute error_type
24
+
25
+ # @return [String] A more detailed description of what went wrong for this receipient.
26
+ # @!attribute error_message
27
+
28
+ # @return [Integer] A numeric error code that corresponds to the error message, if any.
29
+ # @!attribute error_id
30
+
31
+ has_normal_attributes %w[
32
+ phone_number status retry_count bitrate resolution error_type error_message error_id
33
+ ]
34
+
35
+ # @return [Time] The time the job was completed for this recipient.
36
+ # @!attribute completed_at
37
+
38
+ has_time_attributes %w[completed_at]
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,89 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Provides functionality for viewing and managing PhaxCodes.
4
+ class PhaxCode < Resource
5
+ PHAX_CODES_PATH = 'phax_codes'.freeze
6
+ DEFAULT_PHAX_CODE_PATH = 'phax_code'.freeze
7
+ private_constant :PHAX_CODES_PATH, :DEFAULT_PHAX_CODE_PATH
8
+
9
+ # @return [String] The identifier for the PhaxCode.
10
+ # @!attribute identifier
11
+
12
+ # @return [String] The metadata associated with the PhaxCode.
13
+ # @!attribute metadata
14
+
15
+ has_normal_attributes %w[identifier metadata]
16
+
17
+ # @return [Time] The time that the PhaxCode was created.
18
+ # @!attribute created_at
19
+
20
+ has_time_attributes %w[created_at]
21
+
22
+ class << self
23
+ # Create a PhaxCode.
24
+ # @param params [Hash]
25
+ # A hash of parameters to send to Phaxio.
26
+ # - *metadata* [String] - Metadata to be associated with the PhaxCode.
27
+ # - *type* [String] - If set to "png", this method will return a PNG
28
+ # file instead of a PhaxCode object.
29
+ # @return [Phaxio::Resources::PhaxCode | File] The created PhaxCode
30
+ # @raise [Phaxio::Error::PhaxioError]
31
+ # @see https://www.phaxio.com/docs/api/v2/phaxcodes/create_phax_code
32
+ def create params = {}
33
+ endpoint = case (params[:type] || params['type']).to_s
34
+ when 'png' then phax_codes_endpoint_png
35
+ else phax_codes_endpoint
36
+ end
37
+ result = Client.request :post, endpoint, params
38
+ return result if result.is_a? File
39
+ response_record result
40
+ end
41
+
42
+ # Retrieve a PhaxCode.
43
+ # @param params [Hash]
44
+ # A hash of parameters to send to Phaxio.
45
+ # - *identifier* [String] - The identifier for the PhaxCode you want to get. If blank, the
46
+ # default PhaxCode will be returned.
47
+ # - *type* [String] - If set to "png", this method will return a PNG file instead of a
48
+ # PhaxCode object.
49
+ # @return [Phaxio::Resources::PhaxCode | File]
50
+ # @raise [Phaxio::Error::PhaxioError]
51
+ # @see https://www.phaxio.com/docs/api/v2/phaxcodes/retrieve_phax_code
52
+ def get params = {}
53
+ identifier = params[:identifier] || params['identifier']
54
+ endpoint = case (identifier)
55
+ when nil then default_phax_code_path
56
+ else phax_code_endpoint(identifier)
57
+ end
58
+ endpoint = case (params[:type] || params['type']).to_s
59
+ when 'png' then "#{endpoint}.png"
60
+ else endpoint
61
+ end
62
+ result = Client.request :get, endpoint, {}
63
+ return result if result.is_a? File
64
+ response_record result
65
+ end
66
+ alias :find :get
67
+ alias :retrieve :get
68
+
69
+ private
70
+
71
+ def phax_codes_endpoint
72
+ PHAX_CODES_PATH
73
+ end
74
+
75
+ def phax_codes_endpoint_png
76
+ "#{phax_codes_endpoint}.png"
77
+ end
78
+
79
+ def phax_code_endpoint(identifier)
80
+ "#{phax_codes_endpoint}/#{identifier}"
81
+ end
82
+
83
+ def default_phax_code_path
84
+ DEFAULT_PHAX_CODE_PATH
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -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,8 @@
1
+ module Phaxio
2
+ module Resources
3
+ module Public
4
+ end
5
+ end
6
+ end
7
+
8
+ Dir[File.expand_path(File.join('..', 'public', '*.rb'), __FILE__)].each { |file| require file }
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Phaxio
2
- VERSION = "0.5.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -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 = ["Josh Nankin"]
6
- gem.email = ["support@phaxio.com"]
7
- gem.description = %q{Ruby wrapper for the Phaxio API}
8
- gem.summary = %q{Ruby wrapper for the Phaxio API}
9
- gem.homepage = "http://github.com/phaxio/phaxio-ruby"
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")
@@ -15,11 +15,8 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Phaxio::VERSION
17
17
 
18
- gem.add_runtime_dependency "httmultiparty"
19
-
20
- gem.add_development_dependency 'bundler'
21
- gem.add_development_dependency 'fakeweb', '~> 1.3'
22
- gem.add_development_dependency 'rake', '~> 10'
23
- gem.add_development_dependency 'minitest', '~> 5'
24
- gem.add_development_dependency 'mocha', '~> 1.1'
18
+ gem.required_ruby_version = '>= 2.0'
19
+ gem.add_dependency 'faraday', '~> 0.10'
20
+ gem.add_dependency 'mime-types', '~> 3.0'
21
+ gem.add_dependency 'activesupport'
25
22
  end
@@ -0,0 +1,132 @@
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 'uses the configured API key and secret by default' do
36
+ expect(test_connection).to receive(:get) do |_endpoint, request_params|
37
+ expect(request_params[:api_key]).to eq(Phaxio.api_key)
38
+ expect(request_params[:api_secret]).to eq(Phaxio.api_secret)
39
+ test_response 200
40
+ end
41
+ client.request :get, 'test'
42
+ end
43
+
44
+ it 'uses the api key specified in the params hash' do
45
+ custom_api_key = 'custom-api-key'
46
+ custom_api_secret = 'custom-api-secret'
47
+ expect(test_connection).to receive(:get) do |_endpoint, request_params|
48
+ expect(request_params[:api_key]).to eq(custom_api_key)
49
+ expect(request_params[:api_secret]).to eq(custom_api_secret)
50
+ test_response 200
51
+ end
52
+ client.request :get, 'test', api_key: custom_api_key, api_secret: custom_api_secret
53
+ end
54
+
55
+ it 'parses the response JSON and returns the data if the response indicates success' do
56
+ expect(test_connection).to receive(:get) { test_response 200 }
57
+ result = client.request :get, 'test'
58
+ expect(result).to eq({'foo' => 'bar'})
59
+ end
60
+
61
+ it 'raises an authentication error if the response status is 401' do
62
+ expect(test_connection).to receive(:get) { test_response 401 }
63
+ expect {
64
+ client.request :get, 'test'
65
+ }.to raise_error(Phaxio::Error::AuthenticationError, '401: This is a test.')
66
+ end
67
+
68
+ it 'raises a not found error if the response status is 404' do
69
+ expect(test_connection).to receive(:get) { test_response 404 }
70
+ expect do
71
+ client.request :get, 'test'
72
+ end.to raise_error(Phaxio::Error::NotFoundError, '404: This is a test.')
73
+ end
74
+
75
+ it 'raises an invalid request error if the response status is 422' do
76
+ expect(test_connection).to receive(:get) { test_response 422 }
77
+ expect {
78
+ client.request :get, 'test'
79
+ }.to raise_error(Phaxio::Error::InvalidRequestError, '422: This is a test.')
80
+ end
81
+
82
+ it 'raises a rate limit error if the response status is 429' do
83
+ expect(test_connection).to receive(:get) { test_response 429 }
84
+ expect {
85
+ client.request :get, 'test'
86
+ }.to raise_error(Phaxio::Error::RateLimitExceededError, '429: This is a test.')
87
+ end
88
+
89
+ it 'raises a general error for if the response status is 5XX' do
90
+ expect(test_connection).to receive(:get) { test_response 500 }
91
+ expect {
92
+ client.request :get, 'test'
93
+ }.to raise_error(Phaxio::Error::GeneralError, '500: This is a test.')
94
+ end
95
+
96
+ it 'raises a general error if the response status is some other value' do
97
+ expect(test_connection).to receive(:get) { test_response 418 }
98
+ expect {
99
+ client.request :get, 'test'
100
+ }.to raise_error(Phaxio::Error::GeneralError, '418: This is a test.')
101
+ end
102
+
103
+ it 'raises an API connection error if a networking issue occurs' do
104
+ expect(test_connection).to receive(:get) { raise Faraday::ConnectionFailed, 'This is a test.' }
105
+ expect {
106
+ client.request :get, 'test'
107
+ }.to raise_error(Phaxio::Error::ApiConnectionError, 'Error communicating with Phaxio: This is a test.')
108
+ end
109
+
110
+ it 'allows unknown errors to bubble up' do
111
+ expect(test_connection).to receive(:get) { raise RuntimeError, 'This is a test.' }
112
+ expect {
113
+ client.request :get, 'test'
114
+ }.to raise_error(RuntimeError, 'This is a test.')
115
+ end
116
+
117
+ it 'raises a general error if the status indicates success but the body indicates failure' do
118
+ expect(test_connection).to receive(:get) do
119
+ instance_double(
120
+ Faraday::Response,
121
+ status: 200,
122
+ success?: true,
123
+ headers: {content_type: 'application/json'},
124
+ body: '{"success": false, "message": "Something bad happened.", "data": {"foo": "bar"}}'
125
+ )
126
+ end
127
+ expect {
128
+ client.request :get, 'test'
129
+ }.to raise_error(Phaxio::Error::GeneralError, "Something bad happened.")
130
+ end
131
+ end
132
+ end