fat_zebra 3.0.0 → 3.0.1

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.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -1
  3. data/fat_zebra.gemspec +4 -4
  4. data/lib/fat_zebra/api_helper.rb +95 -0
  5. data/lib/fat_zebra/api_operation/find.rb +18 -11
  6. data/lib/fat_zebra/api_operation/save.rb +2 -2
  7. data/lib/fat_zebra/api_operation/search.rb +19 -12
  8. data/lib/fat_zebra/api_operation/void.rb +1 -1
  9. data/lib/fat_zebra/api_resource.rb +7 -73
  10. data/lib/fat_zebra/bank_account.rb +1 -2
  11. data/lib/fat_zebra/batch.rb +5 -3
  12. data/lib/fat_zebra/card.rb +5 -4
  13. data/lib/fat_zebra/config.rb +31 -18
  14. data/lib/fat_zebra/customer.rb +2 -3
  15. data/lib/fat_zebra/direct_credit.rb +2 -3
  16. data/lib/fat_zebra/direct_debit.rb +2 -3
  17. data/lib/fat_zebra/errors.rb +22 -4
  18. data/lib/fat_zebra/fat_zebra_object.rb +4 -115
  19. data/lib/fat_zebra/object_helper.rb +118 -0
  20. data/lib/fat_zebra/payment_plan.rb +1 -2
  21. data/lib/fat_zebra/purchase.rb +3 -6
  22. data/lib/fat_zebra/refund.rb +2 -4
  23. data/lib/fat_zebra/request.rb +12 -8
  24. data/lib/fat_zebra/util.rb +17 -1
  25. data/lib/fat_zebra/version.rb +1 -1
  26. data/lib/fat_zebra/web_hook.rb +1 -2
  27. data/lib/fat_zebra.rb +2 -0
  28. data/spec/cassettes/FatZebra_BankAccount/_create/1_1_3.yml +55 -0
  29. data/spec/cassettes/FatZebra_Batch/_create/with_file/1_1_1_3.yml +65 -0
  30. data/spec/cassettes/FatZebra_Batch/_create/with_path/1_1_2_3.yml +65 -0
  31. data/spec/cassettes/FatZebra_Batch/_find/1_2_3.yml +118 -0
  32. data/spec/cassettes/FatZebra_Card/_create/1_1_7.yml +55 -0
  33. data/spec/cassettes/FatZebra_Card/_update/1_2_4.yml +105 -0
  34. data/spec/cassettes/FatZebra_Card/_update/validations/1_2_5_1.yml +55 -0
  35. data/spec/cassettes/FatZebra_Customer/_create/1_1_7.yml +57 -0
  36. data/spec/cassettes/FatZebra_Customer/_delete/1_5_2.yml +107 -0
  37. data/spec/cassettes/FatZebra_Customer/_find/1_3_3.yml +111 -0
  38. data/spec/cassettes/FatZebra_Customer/_update/1_4_3.yml +108 -0
  39. data/spec/cassettes/FatZebra_DirectCredit/_create/1_1_5.yml +54 -0
  40. data/spec/cassettes/FatZebra_DirectCredit/_delete/1_4_2.yml +103 -0
  41. data/spec/cassettes/FatZebra_DirectCredit/_find/1_2_3.yml +107 -0
  42. data/spec/cassettes/FatZebra_DirectDebit/_create/1_1_5.yml +54 -0
  43. data/spec/cassettes/FatZebra_DirectDebit/_delete/1_4_2.yml +103 -0
  44. data/spec/cassettes/FatZebra_DirectDebit/_find/1_2_3.yml +107 -0
  45. data/spec/cassettes/FatZebra_PaymentPlan/_create/1_1_4.yml +114 -0
  46. data/spec/cassettes/FatZebra_PaymentPlan/_delete/1_5_2.yml +164 -0
  47. data/spec/cassettes/FatZebra_PaymentPlan/_find/1_2_4.yml +173 -0
  48. data/spec/cassettes/FatZebra_PaymentPlan/_update/1_3_4.yml +164 -0
  49. data/spec/cassettes/FatZebra_Purchase/_capture/1_5_3.yml +107 -0
  50. data/spec/cassettes/FatZebra_Purchase/_capture/1_6_3.yml +107 -0
  51. data/spec/cassettes/FatZebra_Purchase/_create/1_1_13.yml +55 -0
  52. data/spec/cassettes/FatZebra_Purchase/_create/bad_request/1_1_14_1.yml +57 -0
  53. data/spec/cassettes/FatZebra_Purchase/_create/bad_request/1_1_14_2.yml +57 -0
  54. data/spec/cassettes/FatZebra_Purchase/_find/1_2_4.yml +109 -0
  55. data/spec/cassettes/FatZebra_Purchase/_find/1_3_1.yml +109 -0
  56. data/spec/cassettes/FatZebra_Purchase/_find/1_3_2.yml +109 -0
  57. data/spec/cassettes/FatZebra_Purchase/_find/1_3_3.yml +109 -0
  58. data/spec/cassettes/FatZebra_Purchase/_find/1_3_4.yml +109 -0
  59. data/spec/cassettes/FatZebra_Purchase/_refund/1_4_3.yml +107 -0
  60. data/spec/cassettes/FatZebra_Purchase/_refund/1_5_3.yml +107 -0
  61. data/spec/cassettes/FatZebra_Purchase/_search/1_3_4.yml +170 -0
  62. data/spec/cassettes/FatZebra_Purchase/_search/1_4_1.yml +170 -0
  63. data/spec/cassettes/FatZebra_Purchase/_search/1_4_2.yml +170 -0
  64. data/spec/cassettes/FatZebra_Purchase/_search/1_4_3.yml +170 -0
  65. data/spec/cassettes/FatZebra_Purchase/_search/1_4_4.yml +170 -0
  66. data/spec/cassettes/FatZebra_Purchase/_settlement/1_8_4.yml +842 -0
  67. data/spec/cassettes/FatZebra_Purchase/_settlement/1_9_1.yml +828 -0
  68. data/spec/cassettes/FatZebra_Purchase/_settlement/1_9_2.yml +830 -0
  69. data/spec/cassettes/FatZebra_Purchase/_settlement/1_9_3.yml +832 -0
  70. data/spec/cassettes/FatZebra_Purchase/_settlement/1_9_4.yml +834 -0
  71. data/spec/cassettes/FatZebra_Purchase/_void/1_6_4.yml +107 -0
  72. data/spec/cassettes/FatZebra_Purchase/_void/1_7_4.yml +107 -0
  73. data/spec/cassettes/FatZebra_Purchase/_void/1_8_1.yml +107 -0
  74. data/spec/cassettes/FatZebra_Purchase/_void/1_8_2.yml +107 -0
  75. data/spec/cassettes/FatZebra_Purchase/_void/1_8_3.yml +107 -0
  76. data/spec/cassettes/FatZebra_Purchase/_void/1_8_4.yml +107 -0
  77. data/spec/cassettes/FatZebra_Refund/_create/1_1_10.yml +107 -0
  78. data/spec/cassettes/FatZebra_Refund/_find/1_2_3.yml +161 -0
  79. data/spec/cassettes/FatZebra_Refund/_void/1_4_4.yml +159 -0
  80. data/spec/cassettes/FatZebra_WebHook/_create/1_1_4.yml +55 -0
  81. data/spec/cassettes/FatZebra_WebHook/_delete/1_4_2.yml +105 -0
  82. data/spec/cassettes/FatZebra_WebHook/_update/1_3_3.yml +106 -0
  83. data/spec/lib/fat_zebra/bank_account_spec.rb +2 -2
  84. data/spec/lib/fat_zebra/batch_spec.rb +4 -4
  85. data/spec/lib/fat_zebra/card_spec.rb +2 -2
  86. data/spec/lib/fat_zebra/config_spec.rb +22 -4
  87. data/spec/lib/fat_zebra/customer_spec.rb +5 -5
  88. data/spec/lib/fat_zebra/direct_credit_spec.rb +4 -4
  89. data/spec/lib/fat_zebra/direct_debit_spec.rb +4 -4
  90. data/spec/lib/fat_zebra/fat_zebra_object_spec.rb +3 -2
  91. data/spec/lib/fat_zebra/payment_plan_spec.rb +4 -4
  92. data/spec/lib/fat_zebra/purchase_spec.rb +16 -3
  93. data/spec/lib/fat_zebra/refund_spec.rb +5 -2
  94. data/spec/lib/fat_zebra/util_spec.rb +5 -0
  95. data/spec/lib/fat_zebra/web_hook_spec.rb +4 -4
  96. data/spec/spec_helper.rb +1 -1
  97. metadata +76 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a2d528ebf041ec5f73b61570237e3547dc3d9d45
4
- data.tar.gz: c8b2990d36c46054972c47fe5272f21002e648b6
3
+ metadata.gz: 0ac27cea9c7baf7c382f97bb6eadeede98ebdd03
4
+ data.tar.gz: 31a7ebdd9646a45834a478ae3b8d2cbb142232fa
5
5
  SHA512:
6
- metadata.gz: 92da2daddc57c3250bc439cb7a29553de9fb0db58536ff01e34fe77cc22604ac26f129075eae2b6b40af0fa5a3c53b541c9c89f99b8d558808bcce07b65fa1a4
7
- data.tar.gz: 590f8fc2a63c58f2e1004bbddb1ebadccebf81f25c0e1d94336c2ccf62b2522cb5f35f5973e575ac28262fe39c89d25baac574d4817b767de2cc95348b160f96
6
+ metadata.gz: c8e625b2825444e7cc694e751d3ec572ed17a2560255fe096882089033192c7306ed6e8a46dbf2fc20c57469b0011df4cde81b05c2725e6727326ec74dbf670c
7
+ data.tar.gz: 7fd6085163d5071a61e61a934b5c2c6a1d5d59146799e3759341549d5eb246075f8eee37ccdc25d2a7aa13e92cc20c19c2003dc7c4ae9495ca7d9578224f5dce
data/.rubocop.yml CHANGED
@@ -13,7 +13,7 @@ Style/NumericPredicate:
13
13
  Metrics/AbcSize:
14
14
  # The ABC size is a calculated magnitude, so this number can be a Fixnum or
15
15
  # a Float.
16
- Max: 25
16
+ Max: 27
17
17
  Metrics/LineLength:
18
18
  Max: 200
19
19
  Metrics/ClassLength:
@@ -25,6 +25,8 @@ Metrics/MethodLength:
25
25
  Metrics/ModuleLength:
26
26
  CountComments: false
27
27
  Max: 200
28
+ Metrics/CyclomaticComplexity:
29
+ Max: 7
28
30
 
29
31
  Documentation:
30
32
  Enabled: false
data/fat_zebra.gemspec CHANGED
@@ -18,10 +18,10 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {spec}/*`.split("\n")
19
19
  s.require_paths = %w[lib]
20
20
 
21
- s.add_development_dependency 'rspec'
21
+ s.add_development_dependency 'rspec', '~> 3.6'
22
22
  s.add_development_dependency 'rake'
23
23
  s.add_development_dependency 'pry'
24
- s.add_development_dependency 'vcr'
25
- s.add_development_dependency 'webmock'
26
- s.add_development_dependency 'rubocop'
24
+ s.add_development_dependency 'vcr', '~> 3.0'
25
+ s.add_development_dependency 'webmock', '~> 3.0'
26
+ s.add_development_dependency 'rubocop', '~> 0.49'
27
27
  end
@@ -0,0 +1,95 @@
1
+ module FatZebra
2
+ ##
3
+ # == FatZebra \Helper
4
+ #
5
+ # Help for the api resource
6
+ module APIHelper
7
+
8
+ module ClassMethods
9
+
10
+ def base_path
11
+ ''
12
+ end
13
+
14
+ ##
15
+ # @return [String] resource path
16
+ def resource_path(path = nil)
17
+ "/#{base_path}#{path || @resource_name || CGI.escape(resource_name)}"
18
+ end
19
+
20
+ ##
21
+ # @return [String] resource name
22
+ def resource_name
23
+ Util.underscore("#{class_name}s")
24
+ end
25
+
26
+ ##
27
+ # @return [String] the class name
28
+ def class_name
29
+ name.split('::')[-1]
30
+ end
31
+
32
+ ##
33
+ # Send a request to the API
34
+ def request
35
+ raise 'need to be implemented'
36
+ end
37
+
38
+ def build_endpoint_url(base_url, path, params = nil, options = {})
39
+ host, port = base_url.split(':')
40
+ port = port.to_i if port
41
+
42
+ url_params = Util.compact(
43
+ host: host,
44
+ path: path,
45
+ port: port,
46
+ query: params
47
+ )
48
+
49
+ return URI::HTTPS.build(url_params) if options[:http_secure]
50
+ URI::HTTP.build(url_params)
51
+ end
52
+
53
+ def default_headers
54
+ {
55
+ headers: {
56
+ accept: 'application/json',
57
+ content_type: 'application/json'
58
+ }
59
+ }
60
+ end
61
+
62
+ end
63
+
64
+ ##
65
+ # @return [String] resource name
66
+ def resource_name
67
+ self.class.resource_name
68
+ end
69
+
70
+ ##
71
+ # @return [String] resource path
72
+ def resource_path(path = nil)
73
+ self.class.resource_path(path)
74
+ end
75
+
76
+ ##
77
+ # Send a request to the API
78
+ #
79
+ # @param [Symbol] method for the request
80
+ # @param [String] endpoint for the request
81
+ # @param [Hash] options
82
+ # @param [Hash] Payload
83
+ #
84
+ # @return [Hash] response
85
+ # @raise [FatZebra::RequestError] if the request is invalid
86
+ def request(method, resource_path, payload = {}, options = {})
87
+ self.class.request(method, resource_path, payload, options)
88
+ end
89
+
90
+ def self.included(base)
91
+ base.extend(ClassMethods)
92
+ end
93
+
94
+ end
95
+ end
@@ -4,18 +4,25 @@ module FatZebra
4
4
  # == Find a resource for the API
5
5
  #
6
6
  module Find
7
+ module ClassMethods
7
8
 
8
- ##
9
- # Find an API Resource
10
- #
11
- # @param [String] id for the request
12
- # @param [Hash] params for the request
13
- # @param [Hash] Additional options for the request
14
- #
15
- # @return [FatZebra::Object] response from the API
16
- def find(id, params = {}, options = {})
17
- response = request(:get, "#{resource_path}/#{id}", params, options)
18
- initialize_from(response)
9
+ ##
10
+ # Find an API Resource
11
+ #
12
+ # @param [String] id for the request
13
+ # @param [Hash] params for the request
14
+ # @param [Hash] Additional options for the request
15
+ #
16
+ # @return [FatZebra::Object] response from the API
17
+ def find(id, params = {}, options = {})
18
+ response = request(:get, "#{resource_path}/#{id}", params, options)
19
+ initialize_from(response)
20
+ end
21
+
22
+ end
23
+
24
+ def self.included(base)
25
+ base.extend(ClassMethods)
19
26
  end
20
27
 
21
28
  end
@@ -14,7 +14,7 @@ module FatZebra
14
14
  #
15
15
  # @return [FatZebra::Object] response from the API
16
16
  def create(params = {}, options = {})
17
- valid!(params, :create)
17
+ valid!(params, :create) if respond_to?(:valid!)
18
18
 
19
19
  response = request(:post, resource_path, params, options)
20
20
  initialize_from(response)
@@ -29,7 +29,7 @@ module FatZebra
29
29
  #
30
30
  # @return [FatZebra::Object] response from the API
31
31
  def update(id, params = {}, options = {})
32
- valid!(params, :update)
32
+ valid!(params, :update) if respond_to?(:valid!)
33
33
 
34
34
  response = request(:put, "#{resource_path}/#{id}", params, options)
35
35
  initialize_from(response)
@@ -12,19 +12,26 @@ module FatZebra
12
12
  limit: 10
13
13
  }.freeze
14
14
 
15
- ##
16
- # Search for API Resources
17
- #
18
- # @param [Hash] params for the request
19
- # @param [Hash] Additional options for the request
20
- #
21
- # @return [FatZebra::Object] response from the API
22
- def search(params = {}, options = {})
23
- params = DEFAULT_PARAMS.merge(params)
24
- params = Util.format_dates_in_hash(params)
15
+ module ClassMethods
16
+
17
+ ##
18
+ # Search for API Resources
19
+ #
20
+ # @param [Hash] params for the request
21
+ # @param [Hash] Additional options for the request
22
+ #
23
+ # @return [FatZebra::Object] response from the API
24
+ def search(params = {}, options = {})
25
+ params = DEFAULT_PARAMS.merge(params)
26
+
27
+ response = request(:get, resource_path, params, options)
28
+ initialize_from(response)
29
+ end
30
+
31
+ end
25
32
 
26
- response = request(:get, resource_path, params, options)
27
- initialize_from(response)
33
+ def self.included(base)
34
+ base.extend(ClassMethods)
28
35
  end
29
36
 
30
37
  end
@@ -15,7 +15,7 @@ module FatZebra
15
15
  #
16
16
  # @return [FatZebra::Object] response from the API
17
17
  def void(id, params = {}, options = {})
18
- valid!(params, :void)
18
+ valid!(params, :void) if respond_to?(:valid!)
19
19
 
20
20
  params = {
21
21
  id: id
@@ -4,51 +4,12 @@ module FatZebra
4
4
  #
5
5
  # Define the API requests methods
6
6
  class APIResource < FatZebraObject
7
-
8
- ##
9
- # @return [String] resource name
10
- def resource_name
11
- self.class.resource_name
12
- end
13
-
14
- ##
15
- # @return [String] resource path
16
- def resource_path(path = nil)
17
- self.class.resource_path(path)
18
- end
19
-
20
- ##
21
- # Send a request to the API
22
- #
23
- # @param [Symbol] method for the request
24
- # @param [String] endpoint for the request
25
- # @param [Hash] options
26
- # @param [Hash] Payload
27
- #
28
- # @return [Hash] response
29
- # @raise [FatZebra::RequestError] if the request is invalid
30
- def request(method, resource_path, payload = {}, options = {})
31
- self.class.request(method, resource_path, payload, options)
32
- end
7
+ include APIHelper
33
8
 
34
9
  class << self
35
10
 
36
- ##
37
- # @return [String] resource path
38
- def resource_path(path = nil)
39
- "/#{configurations.api_version}/#{path || @resource_name || CGI.escape(resource_name)}"
40
- end
41
-
42
- ##
43
- # @return [String] resource name
44
- def resource_name
45
- Util.underscore("#{class_name}s")
46
- end
47
-
48
- ##
49
- # @return [String] the class name
50
- def class_name
51
- name.split('::')[-1]
11
+ def base_path
12
+ "#{configurations.api_version}/"
52
13
  end
53
14
 
54
15
  ##
@@ -63,13 +24,9 @@ module FatZebra
63
24
  # @raise [FatZebra::RequestError] if the request is invalid
64
25
  def request(method, path, payload = {}, options = {})
65
26
  payload[:test] = true if configurations.test_mode
66
-
67
- if method == :get
68
- url_params = !payload.empty? && Util.encode_parameters(payload)
69
- payload = nil
70
- end
71
-
72
- uri = build_endpoint_url(path, url_params)
27
+ payload = Util.format_dates_in_hash(payload)
28
+ url_params = Util.encode_parameters(payload) if method == :get
29
+ uri = build_endpoint_url(configurations.gateway, path, url_params, http_secure: configurations.http_secure)
73
30
 
74
31
  request_options = Util.compact(
75
32
  method: method,
@@ -77,7 +34,7 @@ module FatZebra
77
34
  payload: payload,
78
35
  proxy: configurations.proxy,
79
36
  use_ssl: configurations.http_secure
80
- ).merge(authentication).merge(headers).merge(options)
37
+ ).merge(authentication).merge(default_headers).merge(configurations.global_options).merge(options)
81
38
 
82
39
  request = Request.execute(request_options)
83
40
 
@@ -86,20 +43,6 @@ module FatZebra
86
43
 
87
44
  private
88
45
 
89
- def build_endpoint_url(path, params = nil)
90
- host, port = configurations.gateway.split(':')
91
-
92
- url_params = Util.compact(
93
- host: host,
94
- path: path,
95
- port: port,
96
- query: params
97
- )
98
-
99
- return URI::HTTPS.build(url_params) if configurations.http_secure
100
- URI::HTTP.build(url_params)
101
- end
102
-
103
46
  def ssl_options
104
47
  return {} unless configurations.http_secure
105
48
  {
@@ -117,15 +60,6 @@ module FatZebra
117
60
  }
118
61
  end
119
62
 
120
- def headers
121
- {
122
- headers: {
123
- accept: 'application/json',
124
- content_type: 'application/json'
125
- }
126
- }
127
- end
128
-
129
63
  def configurations
130
64
  FatZebra.configurations
131
65
  end
@@ -9,8 +9,7 @@ module FatZebra
9
9
  #
10
10
  class BankAccount < APIResource
11
11
 
12
- extend FatZebra::APIOperation::Search
13
-
12
+ include FatZebra::APIOperation::Search
14
13
  include FatZebra::APIOperation::Save
15
14
 
16
15
  validates :account_name, required: true, on: :create
@@ -12,9 +12,8 @@ module FatZebra
12
12
  class Batch < APIResource
13
13
  @resource_name = 'batches'
14
14
 
15
- extend FatZebra::APIOperation::Search
16
- extend FatZebra::APIOperation::Find
17
-
15
+ include FatZebra::APIOperation::Search
16
+ include FatZebra::APIOperation::Find
18
17
  include FatZebra::APIOperation::Save
19
18
  include FatZebra::APIOperation::Delete
20
19
 
@@ -50,6 +49,9 @@ module FatZebra
50
49
  # @return [String] formated as CSV
51
50
  def result(params = {}, options = {})
52
51
  request(:get, "#{resource_path}/#{id}/result.csv", params, options)
52
+ rescue FatZebra::RequestError => error
53
+ return error.http_body if error.http_status == 422
54
+ raise
53
55
  end
54
56
 
55
57
  end
@@ -11,10 +11,11 @@ module FatZebra
11
11
 
12
12
  include FatZebra::APIOperation::Save
13
13
 
14
- validates :card_holder, required: true, on: :create
15
- validates :card_number, required: true, on: :create
16
- validates :card_expiry, required: true, on: :create
17
- validates :cvv, required: true, on: :create
14
+ validates :card_holder, required: { unless: %i[wallet] }, on: :create
15
+ validates :card_number, required: { unless: %i[wallet] }, on: :create
16
+ validates :card_expiry, required: { unless: %i[wallet] }, on: :create
17
+
18
+ validates :wallet, required: { unless: %i[card_number] }, on: :create
18
19
 
19
20
  validates :card_expiry, required: true, on: :update
20
21
 
@@ -5,6 +5,11 @@ module FatZebra
5
5
  # Represent the FatZebra configuration for the API
6
6
  class Config
7
7
 
8
+ GATEWAY_URLS = {
9
+ production: 'gateway.fatzebra.com.au',
10
+ sandbox: 'gateway.sandbox.fatzebra.com.au'
11
+ }.freeze
12
+
8
13
  ##
9
14
  # @return [String] Username
10
15
  attr_accessor :username
@@ -17,14 +22,6 @@ module FatZebra
17
22
  # @return [String] Test mode
18
23
  attr_accessor :test_mode
19
24
 
20
- ##
21
- # @return [String] Sandbox
22
- attr_accessor :sandbox
23
-
24
- ##
25
- # @return [String] Options
26
- attr_accessor :options
27
-
28
25
  ##
29
26
  # @return [String] Gateway url
30
27
  attr_accessor :gateway
@@ -41,27 +38,36 @@ module FatZebra
41
38
  # @return [String] api version
42
39
  attr_accessor :api_version
43
40
 
41
+ ##
42
+ # @return [String] global request options
43
+ attr_accessor :global_options
44
+
44
45
  ##
45
46
  # @param [Hash{Symbol=>Object}]
46
47
  # Initialize and validate the configuration
47
48
  def initialize(options = {})
48
- self.token = options[:token]
49
- self.username = options[:username]
50
- self.gateway = options[:gateway] || 'gateway.fatzebra.com.au'
51
- self.test_mode = options[:test_mode] || false
52
- self.http_secure = options[:http_secure] || true
53
- self.api_version = options[:api_version] || 'v1.0'
54
- self.sandbox = options[:sandbox]
55
- self.options = options[:options]
56
- self.proxy = options[:proxy]
49
+ self.token = options[:token]
50
+ self.username = options[:username]
51
+ self.gateway = options[:gateway] || :production
52
+ self.test_mode = options[:test_mode] || false
53
+ self.http_secure = options[:http_secure] || true
54
+ self.api_version = options[:api_version] || 'v1.0'
55
+ self.proxy = options[:proxy]
56
+ self.global_options = options[:global_options] || {}
57
+
58
+ valid! unless options.empty?
57
59
  end
58
60
 
59
61
  ##
60
62
  # validate the configuration
61
63
  # Raise when configuration is not valid
64
+ # Remove "http://" or "https://" from urls
65
+ #
62
66
  # @return [Boolean] true
63
67
  def valid!
64
- %i[username token].each do |field|
68
+ format!
69
+
70
+ %i[username token gateway api_version].each do |field|
65
71
  validate_presence(field)
66
72
  end
67
73
 
@@ -70,6 +76,13 @@ module FatZebra
70
76
 
71
77
  private
72
78
 
79
+ def format!
80
+ self.gateway = GATEWAY_URLS[gateway] if gateway.is_a?(Symbol)
81
+
82
+ self.gateway = Util.cleanup_host(gateway)
83
+ self.proxy = Util.cleanup_host(proxy) if proxy
84
+ end
85
+
73
86
  def validate_presence(field)
74
87
  raise FatZebra::ConfigurationError, "#{field} can't be blank" if send(field).nil? || send(field).empty?
75
88
  end
@@ -11,9 +11,8 @@ module FatZebra
11
11
  #
12
12
  class Customer < APIResource
13
13
 
14
- extend FatZebra::APIOperation::Search
15
- extend FatZebra::APIOperation::Find
16
-
14
+ include FatZebra::APIOperation::Search
15
+ include FatZebra::APIOperation::Find
17
16
  include FatZebra::APIOperation::Save
18
17
  include FatZebra::APIOperation::Delete
19
18
 
@@ -11,9 +11,8 @@ module FatZebra
11
11
  #
12
12
  class DirectCredit < APIResource
13
13
 
14
- extend FatZebra::APIOperation::Find
15
- extend FatZebra::APIOperation::Search
16
-
14
+ include FatZebra::APIOperation::Find
15
+ include FatZebra::APIOperation::Search
17
16
  include FatZebra::APIOperation::Save
18
17
  include FatZebra::APIOperation::Delete
19
18
 
@@ -11,9 +11,8 @@ module FatZebra
11
11
  #
12
12
  class DirectDebit < APIResource
13
13
 
14
- extend FatZebra::APIOperation::Find
15
- extend FatZebra::APIOperation::Search
16
-
14
+ include FatZebra::APIOperation::Find
15
+ include FatZebra::APIOperation::Search
17
16
  include FatZebra::APIOperation::Save
18
17
  include FatZebra::APIOperation::Delete
19
18
 
@@ -4,10 +4,6 @@ module FatZebra
4
4
  # Configuration Error
5
5
  class ConfigurationError < StandardError; end
6
6
 
7
- ##
8
- # Request to API Error
9
- class RequestError < StandardError; end
10
-
11
7
  ##
12
8
  # Validations for the request
13
9
  class RequestValidationError < StandardError; end
@@ -16,4 +12,26 @@ module FatZebra
16
12
  # Method unknown for the request
17
13
  class UnknownRequestMethod < StandardError; end
18
14
 
15
+ ##
16
+ # Request to API Error
17
+ class RequestError < StandardError
18
+
19
+ attr_reader :http_body
20
+ attr_reader :http_status
21
+ attr_reader :http_status_type
22
+ attr_reader :http_message
23
+
24
+ def initialize(response, message: nil)
25
+ @http_status = response.code.to_i
26
+ @http_status_type = response.code_type
27
+ @http_body = response.body
28
+ @http_message = message || response.message
29
+ end
30
+
31
+ def to_s
32
+ "Status #{http_status}: #{http_message}"
33
+ end
34
+
35
+ end
36
+
19
37
  end