fat_zebra_multi 3.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.rspec +0 -0
  4. data/.rubocop.yml +44 -0
  5. data/.travis.yml +10 -0
  6. data/Gemfile +4 -0
  7. data/README.md +104 -0
  8. data/Rakefile +11 -0
  9. data/fat_zebra_multi.gemspec +27 -0
  10. data/lib/fat_zebra.rb +65 -0
  11. data/lib/fat_zebra/api_helper.rb +91 -0
  12. data/lib/fat_zebra/api_operation/delete.rb +38 -0
  13. data/lib/fat_zebra/api_operation/find.rb +30 -0
  14. data/lib/fat_zebra/api_operation/save.rb +67 -0
  15. data/lib/fat_zebra/api_operation/search.rb +39 -0
  16. data/lib/fat_zebra/api_operation/void.rb +52 -0
  17. data/lib/fat_zebra/api_resource.rb +88 -0
  18. data/lib/fat_zebra/bank_account.rb +20 -0
  19. data/lib/fat_zebra/batch.rb +60 -0
  20. data/lib/fat_zebra/card.rb +23 -0
  21. data/lib/fat_zebra/config.rb +90 -0
  22. data/lib/fat_zebra/customer.rb +26 -0
  23. data/lib/fat_zebra/direct_credit.rb +26 -0
  24. data/lib/fat_zebra/direct_debit.rb +26 -0
  25. data/lib/fat_zebra/errors.rb +37 -0
  26. data/lib/fat_zebra/fat_zebra_object.rb +68 -0
  27. data/lib/fat_zebra/information.rb +29 -0
  28. data/lib/fat_zebra/object_helper.rb +119 -0
  29. data/lib/fat_zebra/payment_plan.rb +61 -0
  30. data/lib/fat_zebra/purchase.rb +96 -0
  31. data/lib/fat_zebra/refund.rb +27 -0
  32. data/lib/fat_zebra/request.rb +174 -0
  33. data/lib/fat_zebra/request/multipart/epilogue.rb +23 -0
  34. data/lib/fat_zebra/request/multipart/file_io.rb +40 -0
  35. data/lib/fat_zebra/request/multipart/param.rb +37 -0
  36. data/lib/fat_zebra/request/multipart/part.rb +28 -0
  37. data/lib/fat_zebra/request/multipart/stream.rb +57 -0
  38. data/lib/fat_zebra/util.rb +71 -0
  39. data/lib/fat_zebra/validation.rb +67 -0
  40. data/lib/fat_zebra/version.rb +3 -0
  41. data/lib/fat_zebra/web_hook.rb +22 -0
  42. data/spec/cassettes/FatZebra_BankAccount/_create/1_1_1.yml +53 -0
  43. data/spec/cassettes/FatZebra_BankAccount/_create/1_1_2.yml +53 -0
  44. data/spec/cassettes/FatZebra_BankAccount/_search/1_2_1.yml +164 -0
  45. data/spec/cassettes/FatZebra_BankAccount/_search/1_2_2.yml +164 -0
  46. data/spec/cassettes/FatZebra_BankAccount/_search/1_2_3.yml +164 -0
  47. data/spec/cassettes/FatZebra_Batch/_create/with_file/1_1_1_1.yml +61 -0
  48. data/spec/cassettes/FatZebra_Batch/_create/with_file/1_1_1_2.yml +61 -0
  49. data/spec/cassettes/FatZebra_Batch/_create/with_path/1_1_2_1.yml +61 -0
  50. data/spec/cassettes/FatZebra_Batch/_create/with_path/1_1_2_2.yml +61 -0
  51. data/spec/cassettes/FatZebra_Batch/_find/1_2_1.yml +112 -0
  52. data/spec/cassettes/FatZebra_Batch/_find/1_2_2.yml +112 -0
  53. data/spec/cassettes/FatZebra_Batch/_result/1_4_1.yml +163 -0
  54. data/spec/cassettes/FatZebra_Batch/_search/1_3_1.yml +54 -0
  55. data/spec/cassettes/FatZebra_Batch/_search/1_3_2.yml +54 -0
  56. data/spec/cassettes/FatZebra_Batch/_search/1_3_3.yml +54 -0
  57. data/spec/cassettes/FatZebra_Card/_create/1_1_1.yml +54 -0
  58. data/spec/cassettes/FatZebra_Card/_create/1_1_2.yml +54 -0
  59. data/spec/cassettes/FatZebra_Card/_create/1_1_3.yml +54 -0
  60. data/spec/cassettes/FatZebra_Card/_create/1_1_4.yml +54 -0
  61. data/spec/cassettes/FatZebra_Card/_create/1_1_5.yml +54 -0
  62. data/spec/cassettes/FatZebra_Card/_create/1_1_6.yml +54 -0
  63. data/spec/cassettes/FatZebra_Card/_update/1_2_1.yml +105 -0
  64. data/spec/cassettes/FatZebra_Card/_update/1_2_2.yml +105 -0
  65. data/spec/cassettes/FatZebra_Card/_update/1_2_3.yml +105 -0
  66. data/spec/cassettes/FatZebra_Card/_update/validations/1_2_4_1.yml +54 -0
  67. data/spec/cassettes/FatZebra_Customer/_create/1_1_1.yml +55 -0
  68. data/spec/cassettes/FatZebra_Customer/_create/1_1_2.yml +55 -0
  69. data/spec/cassettes/FatZebra_Customer/_create/1_1_3.yml +55 -0
  70. data/spec/cassettes/FatZebra_Customer/_create/1_1_4.yml +55 -0
  71. data/spec/cassettes/FatZebra_Customer/_create/1_1_5.yml +55 -0
  72. data/spec/cassettes/FatZebra_Customer/_create/1_1_6.yml +55 -0
  73. data/spec/cassettes/FatZebra_Customer/_delete/1_5_1.yml +103 -0
  74. data/spec/cassettes/FatZebra_Customer/_find/1_3_1.yml +107 -0
  75. data/spec/cassettes/FatZebra_Customer/_find/1_3_2.yml +107 -0
  76. data/spec/cassettes/FatZebra_Customer/_search/1_2_1.yml +60 -0
  77. data/spec/cassettes/FatZebra_Customer/_search/1_2_2.yml +60 -0
  78. data/spec/cassettes/FatZebra_Customer/_search/1_2_3.yml +60 -0
  79. data/spec/cassettes/FatZebra_Customer/_update/1_4_1.yml +106 -0
  80. data/spec/cassettes/FatZebra_Customer/_update/1_4_2.yml +106 -0
  81. data/spec/cassettes/FatZebra_DirectCredit/_create/1_1_1.yml +52 -0
  82. data/spec/cassettes/FatZebra_DirectCredit/_create/1_1_2.yml +52 -0
  83. data/spec/cassettes/FatZebra_DirectCredit/_create/1_1_3.yml +52 -0
  84. data/spec/cassettes/FatZebra_DirectCredit/_create/1_1_4.yml +52 -0
  85. data/spec/cassettes/FatZebra_DirectCredit/_create/validations/valid/1_1_5_1_1.yml +52 -0
  86. data/spec/cassettes/FatZebra_DirectCredit/_delete/1_4_1.yml +99 -0
  87. data/spec/cassettes/FatZebra_DirectCredit/_find/1_2_1.yml +103 -0
  88. data/spec/cassettes/FatZebra_DirectCredit/_find/1_2_2.yml +103 -0
  89. data/spec/cassettes/FatZebra_DirectCredit/_find/1_2_3.yml +103 -0
  90. data/spec/cassettes/FatZebra_DirectCredit/_search/1_3_1.yml +152 -0
  91. data/spec/cassettes/FatZebra_DirectCredit/_search/1_3_2.yml +152 -0
  92. data/spec/cassettes/FatZebra_DirectDebit/_create/1_1_1.yml +52 -0
  93. data/spec/cassettes/FatZebra_DirectDebit/_create/1_1_2.yml +52 -0
  94. data/spec/cassettes/FatZebra_DirectDebit/_create/1_1_3.yml +52 -0
  95. data/spec/cassettes/FatZebra_DirectDebit/_create/1_1_4.yml +52 -0
  96. data/spec/cassettes/FatZebra_DirectDebit/_create/validations/valid/1_1_5_1_1.yml +52 -0
  97. data/spec/cassettes/FatZebra_DirectDebit/_delete/1_4_1.yml +99 -0
  98. data/spec/cassettes/FatZebra_DirectDebit/_find/1_2_1.yml +103 -0
  99. data/spec/cassettes/FatZebra_DirectDebit/_find/1_2_2.yml +103 -0
  100. data/spec/cassettes/FatZebra_DirectDebit/_find/1_2_3.yml +103 -0
  101. data/spec/cassettes/FatZebra_DirectDebit/_search/1_3_1.yml +152 -0
  102. data/spec/cassettes/FatZebra_DirectDebit/_search/1_3_2.yml +152 -0
  103. data/spec/cassettes/FatZebra_Information/_ping/1_1_1.yml +54 -0
  104. data/spec/cassettes/FatZebra_Information/_ping/1_1_2.yml +54 -0
  105. data/spec/cassettes/FatZebra_PaymentPlan/_active_/1_8_1.yml +222 -0
  106. data/spec/cassettes/FatZebra_PaymentPlan/_active_/1_8_2.yml +222 -0
  107. data/spec/cassettes/FatZebra_PaymentPlan/_create/1_1_1.yml +110 -0
  108. data/spec/cassettes/FatZebra_PaymentPlan/_create/1_1_2.yml +110 -0
  109. data/spec/cassettes/FatZebra_PaymentPlan/_create/1_1_3.yml +110 -0
  110. data/spec/cassettes/FatZebra_PaymentPlan/_delete/1_5_1.yml +158 -0
  111. data/spec/cassettes/FatZebra_PaymentPlan/_destroy/1_6_1.yml +158 -0
  112. data/spec/cassettes/FatZebra_PaymentPlan/_find/1_2_1.yml +167 -0
  113. data/spec/cassettes/FatZebra_PaymentPlan/_find/1_2_2.yml +167 -0
  114. data/spec/cassettes/FatZebra_PaymentPlan/_find/1_2_3.yml +167 -0
  115. data/spec/cassettes/FatZebra_PaymentPlan/_suspend_/1_7_1.yml +166 -0
  116. data/spec/cassettes/FatZebra_PaymentPlan/_suspend_/1_7_2.yml +166 -0
  117. data/spec/cassettes/FatZebra_PaymentPlan/_update/1_3_1.yml +160 -0
  118. data/spec/cassettes/FatZebra_PaymentPlan/_update/1_3_2.yml +160 -0
  119. data/spec/cassettes/FatZebra_PaymentPlan/_update/1_3_3.yml +160 -0
  120. data/spec/cassettes/FatZebra_PaymentPlan/_update/1_4_1.yml +166 -0
  121. data/spec/cassettes/FatZebra_PaymentPlan/_update/1_4_2.yml +166 -0
  122. data/spec/cassettes/FatZebra_Purchase/_capture/1_5_1.yml +115 -0
  123. data/spec/cassettes/FatZebra_Purchase/_capture/1_5_2.yml +115 -0
  124. data/spec/cassettes/FatZebra_Purchase/_capture/1_5_3.yml +115 -0
  125. data/spec/cassettes/FatZebra_Purchase/_create/1_1_1.yml +53 -0
  126. data/spec/cassettes/FatZebra_Purchase/_create/1_1_10.yml +53 -0
  127. data/spec/cassettes/FatZebra_Purchase/_create/1_1_11.yml +53 -0
  128. data/spec/cassettes/FatZebra_Purchase/_create/1_1_12.yml +53 -0
  129. data/spec/cassettes/FatZebra_Purchase/_create/1_1_13.yml +53 -0
  130. data/spec/cassettes/FatZebra_Purchase/_create/1_1_2.yml +53 -0
  131. data/spec/cassettes/FatZebra_Purchase/_create/1_1_3.yml +53 -0
  132. data/spec/cassettes/FatZebra_Purchase/_create/1_1_4.yml +53 -0
  133. data/spec/cassettes/FatZebra_Purchase/_create/1_1_5.yml +53 -0
  134. data/spec/cassettes/FatZebra_Purchase/_create/1_1_6.yml +53 -0
  135. data/spec/cassettes/FatZebra_Purchase/_create/1_1_7.yml +53 -0
  136. data/spec/cassettes/FatZebra_Purchase/_create/1_1_8.yml +53 -0
  137. data/spec/cassettes/FatZebra_Purchase/_create/1_1_9.yml +53 -0
  138. data/spec/cassettes/FatZebra_Purchase/_create/bad_request/1_1_14_1.yml +55 -0
  139. data/spec/cassettes/FatZebra_Purchase/_create/bad_request/1_1_14_2.yml +55 -0
  140. data/spec/cassettes/FatZebra_Purchase/_find/1_2_1.yml +117 -0
  141. data/spec/cassettes/FatZebra_Purchase/_find/1_2_2.yml +117 -0
  142. data/spec/cassettes/FatZebra_Purchase/_find/1_2_3.yml +117 -0
  143. data/spec/cassettes/FatZebra_Purchase/_find/1_2_4.yml +117 -0
  144. data/spec/cassettes/FatZebra_Purchase/_refund/1_4_1.yml +115 -0
  145. data/spec/cassettes/FatZebra_Purchase/_refund/1_4_2.yml +115 -0
  146. data/spec/cassettes/FatZebra_Purchase/_refund/1_4_3.yml +115 -0
  147. data/spec/cassettes/FatZebra_Purchase/_search/1_3_1.yml +164 -0
  148. data/spec/cassettes/FatZebra_Purchase/_search/1_3_2.yml +164 -0
  149. data/spec/cassettes/FatZebra_Purchase/_search/1_3_3.yml +164 -0
  150. data/spec/cassettes/FatZebra_Purchase/_settlement/1_8_1.yml +652 -0
  151. data/spec/cassettes/FatZebra_Purchase/_settlement/1_8_2.yml +652 -0
  152. data/spec/cassettes/FatZebra_Purchase/_settlement/1_8_3.yml +652 -0
  153. data/spec/cassettes/FatZebra_Purchase/_void/1_6_1.yml +103 -0
  154. data/spec/cassettes/FatZebra_Purchase/_void/1_6_2.yml +103 -0
  155. data/spec/cassettes/FatZebra_Purchase/_void/1_6_3.yml +103 -0
  156. data/spec/cassettes/FatZebra_Purchase/_void/1_6_4.yml +103 -0
  157. data/spec/cassettes/FatZebra_Purchase/_void/1_7_1.yml +103 -0
  158. data/spec/cassettes/FatZebra_Purchase/_void/1_7_2.yml +103 -0
  159. data/spec/cassettes/FatZebra_Purchase/_void/1_7_3.yml +103 -0
  160. data/spec/cassettes/FatZebra_Purchase/_void/1_7_4.yml +103 -0
  161. data/spec/cassettes/FatZebra_Refund/_create/1_1_1.yml +103 -0
  162. data/spec/cassettes/FatZebra_Refund/_create/1_1_10.yml +103 -0
  163. data/spec/cassettes/FatZebra_Refund/_create/1_1_2.yml +103 -0
  164. data/spec/cassettes/FatZebra_Refund/_create/1_1_3.yml +103 -0
  165. data/spec/cassettes/FatZebra_Refund/_create/1_1_4.yml +103 -0
  166. data/spec/cassettes/FatZebra_Refund/_create/1_1_5.yml +103 -0
  167. data/spec/cassettes/FatZebra_Refund/_create/1_1_6.yml +103 -0
  168. data/spec/cassettes/FatZebra_Refund/_create/1_1_7.yml +103 -0
  169. data/spec/cassettes/FatZebra_Refund/_create/1_1_8.yml +103 -0
  170. data/spec/cassettes/FatZebra_Refund/_create/1_1_9.yml +103 -0
  171. data/spec/cassettes/FatZebra_Refund/_find/1_2_1.yml +155 -0
  172. data/spec/cassettes/FatZebra_Refund/_find/1_2_2.yml +155 -0
  173. data/spec/cassettes/FatZebra_Refund/_find/1_2_3.yml +155 -0
  174. data/spec/cassettes/FatZebra_Refund/_search/responds_with_the_newly-created_refunds.yml +214 -0
  175. data/spec/cassettes/FatZebra_Refund/_void/1_4_1.yml +153 -0
  176. data/spec/cassettes/FatZebra_Refund/_void/1_4_2.yml +153 -0
  177. data/spec/cassettes/FatZebra_Refund/_void/1_4_3.yml +153 -0
  178. data/spec/cassettes/FatZebra_Refund/_void/1_4_4.yml +153 -0
  179. data/spec/cassettes/FatZebra_WebHook/_create/1_1_1.yml +53 -0
  180. data/spec/cassettes/FatZebra_WebHook/_create/1_1_2.yml +53 -0
  181. data/spec/cassettes/FatZebra_WebHook/_create/1_1_3.yml +53 -0
  182. data/spec/cassettes/FatZebra_WebHook/_delete/1_4_1.yml +101 -0
  183. data/spec/cassettes/FatZebra_WebHook/_search/1_2_1.yml +298 -0
  184. data/spec/cassettes/FatZebra_WebHook/_search/1_2_2.yml +300 -0
  185. data/spec/cassettes/FatZebra_WebHook/_update/1_3_1.yml +104 -0
  186. data/spec/cassettes/FatZebra_WebHook/_update/1_3_2.yml +104 -0
  187. data/spec/cassettes/Multiple_accounts/The_basics/1_1_5.yml +59 -0
  188. data/spec/cassettes/Multiple_accounts/The_basics/1_1_6.yml +59 -0
  189. data/spec/fixtures/batch_test.csv +4 -0
  190. data/spec/lib/fat_zebra/api_resource_spec.rb +25 -0
  191. data/spec/lib/fat_zebra/bank_account_spec.rb +34 -0
  192. data/spec/lib/fat_zebra/batch_spec.rb +59 -0
  193. data/spec/lib/fat_zebra/card_spec.rb +41 -0
  194. data/spec/lib/fat_zebra/config_spec.rb +69 -0
  195. data/spec/lib/fat_zebra/customer_spec.rb +53 -0
  196. data/spec/lib/fat_zebra/direct_credit_spec.rb +74 -0
  197. data/spec/lib/fat_zebra/direct_debit_spec.rb +74 -0
  198. data/spec/lib/fat_zebra/fat_zebra_object_spec.rb +117 -0
  199. data/spec/lib/fat_zebra/information_spec.rb +13 -0
  200. data/spec/lib/fat_zebra/multiple_accounts_spec.rb +69 -0
  201. data/spec/lib/fat_zebra/payment_plan_spec.rb +94 -0
  202. data/spec/lib/fat_zebra/purchase_spec.rb +105 -0
  203. data/spec/lib/fat_zebra/refund_spec.rb +95 -0
  204. data/spec/lib/fat_zebra/request_spec.rb +59 -0
  205. data/spec/lib/fat_zebra/util_spec.rb +43 -0
  206. data/spec/lib/fat_zebra/validation_spec.rb +22 -0
  207. data/spec/lib/fat_zebra/web_hook_spec.rb +50 -0
  208. data/spec/spec_helper.rb +68 -0
  209. data/spec/support/payloads.rb +43 -0
  210. data/vendor/cacert.pem +3390 -0
  211. metadata +338 -0
@@ -0,0 +1,27 @@
1
+ module FatZebra
2
+ ##
3
+ # == FatZebra \Refund
4
+ #
5
+ # Manage refund for the API
6
+ #
7
+ # * search
8
+ # * save
9
+ # * find
10
+ # * void
11
+ #
12
+ class Refund < APIResource
13
+
14
+ include FatZebra::APIOperation::Find
15
+ include FatZebra::APIOperation::Search
16
+ include FatZebra::APIOperation::Save
17
+ include FatZebra::APIOperation::Void
18
+
19
+ validates :amount, required: true, on: :create
20
+ validates :reference, required: true, on: :create
21
+
22
+ def declined?
23
+ refunded == 'Declined'
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,174 @@
1
+ require 'fat_zebra/request/multipart/part'
2
+ require 'fat_zebra/request/multipart/stream'
3
+ require 'fat_zebra/request/multipart/epilogue'
4
+ require 'fat_zebra/request/multipart/file_io'
5
+ require 'fat_zebra/request/multipart/param'
6
+
7
+ module FatZebra
8
+ ##
9
+ # == FatZebra \Request
10
+ #
11
+ # Build request
12
+ class Request
13
+ class << self
14
+
15
+ def execute(params = {})
16
+ klass = new(params)
17
+
18
+ case params[:method]
19
+ when :post
20
+ klass.post
21
+ when :put
22
+ klass.put
23
+ when :delete
24
+ klass.delete
25
+ when :get
26
+ klass.get
27
+ else
28
+ raise FatZebra::UnknownRequestMethod, "#{params[:method]} hasn't been implemented"
29
+ end
30
+ end
31
+
32
+ def post(params = {})
33
+ new(params).post
34
+ end
35
+
36
+ def put(params = {})
37
+ new(params).put
38
+ end
39
+
40
+ def get(params = {})
41
+ new(params).get
42
+ end
43
+
44
+ def delete(params = {})
45
+ new(params).delete
46
+ end
47
+ end
48
+
49
+ attr_reader :params, :http, :request
50
+
51
+ def initialize(params = {})
52
+ @params = params
53
+ @http = Net::HTTP.new(uri.host,
54
+ uri.port,
55
+ proxy_host,
56
+ uri_proxy.port)
57
+
58
+ setup_ssl if params[:use_ssl]
59
+ end
60
+
61
+ def post
62
+ @request = Net::HTTP::Post.new(uri.path)
63
+
64
+ setup_auth_basic if params[:basic_auth]
65
+
66
+ if params[:payload] && params[:payload][:multipart]
67
+ build_multipart_post
68
+ else
69
+ set_header
70
+ request.body = params[:payload].to_json
71
+ end
72
+
73
+ handle_request
74
+ end
75
+
76
+ def put
77
+ @request = Net::HTTP::Put.new(uri.path)
78
+
79
+ setup_auth_basic if params[:basic_auth]
80
+ set_header
81
+ request.body = params[:payload].to_json
82
+
83
+ handle_request
84
+ end
85
+
86
+ def get
87
+ @request = Net::HTTP::Get.new(uri)
88
+
89
+ setup_auth_basic if params[:basic_auth]
90
+ set_header
91
+
92
+ handle_request
93
+ end
94
+
95
+ def delete
96
+ @request = Net::HTTP::Delete.new(uri.path)
97
+
98
+ setup_auth_basic if params[:basic_auth]
99
+ request.body = params[:payload].to_json
100
+
101
+ handle_request
102
+ end
103
+
104
+ private
105
+
106
+ def handle_request
107
+ response = http.request(request)
108
+
109
+ case [response.code_type]
110
+ when [Net::HTTPOK], [Net::HTTPCreated], [Net::HTTPAccepted]
111
+ response
112
+ when [Net::HTTPUnauthorized]
113
+ raise RequestError.new(response, message: 'Unauthorized, please check your authentication username or token')
114
+ when [Net::HTTPNotFound]
115
+ raise RequestError.new(response, message: 'Requested Resource not found')
116
+ when [Net::HTTPRequestTimeOut]
117
+ raise RequestError.new(response, message: 'Verify the address for the service')
118
+ when [Net::HTTPInternalServerError]
119
+ raise RequestError.new(response, message: 'Server Error, please check with service')
120
+ when [Net::HTTPNotImplemented]
121
+ raise RequestError.new(response, message: 'Problem processing your request - please check your data')
122
+ else
123
+ raise RequestError, response
124
+ end
125
+ end
126
+
127
+ def build_multipart_post
128
+ parts = []
129
+ Util.hash_except(params[:payload], :multipart, :content_type).each do |(key, value)|
130
+ parts << (value.is_a?(File) ? Multipart::FileIO.new(params[:payload]) : Multipart::Param.new(key, value))
131
+ end
132
+ parts << Multipart::Epilogue.new
133
+
134
+ request.content_length = parts.inject(0) { |sum, part| sum + part.length }
135
+ request.content_type = 'multipart/form-data; boundary=' + parts.first.boundary
136
+ request.body_stream = Multipart::Stream.new(parts.map(&:to_io))
137
+ end
138
+
139
+ def setup_auth_basic
140
+ request.basic_auth(params[:basic_auth][:user], params[:basic_auth][:password])
141
+ end
142
+
143
+ def set_header
144
+ params[:headers].each do |(type, value)|
145
+ formated_type = type.to_s.split(/_/).map(&:capitalize).join('-')
146
+
147
+ request[formated_type] = value
148
+ end
149
+ end
150
+
151
+ def setup_ssl
152
+ http.use_ssl = true
153
+ http.ca_file = params[:ca_file]
154
+ http.verify_mode = params[:verify_mode]
155
+ end
156
+
157
+ def uri
158
+ @uri ||= URI(params[:url])
159
+ end
160
+
161
+ def uri_proxy
162
+ @uri_proxy ||=
163
+ if params[:proxy]
164
+ URI(params[:proxy])
165
+ else
166
+ OpenStruct.new
167
+ end
168
+ end
169
+
170
+ def proxy_host
171
+ uri_proxy.host || :ENV
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,23 @@
1
+ module FatZebra
2
+ ##
3
+ # == FatZebra \Request \Multipart \Epilogue
4
+ #
5
+ # Add the epilogue to the multipart
6
+ class Request
7
+ module Multipart
8
+ class Epilogue
9
+ include Part
10
+
11
+ def initialize
12
+ @io = StringIO.new(footer)
13
+ end
14
+
15
+ private
16
+
17
+ def footer
18
+ "--#{boundary}--#{LINE_BREAK}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ module FatZebra
2
+ ##
3
+ # == FatZebra \Request \Multipart \FileIO
4
+ #
5
+ # Add file to the multipart
6
+ class Request
7
+ module Multipart
8
+ class FileIO
9
+ include Part
10
+
11
+ attr_reader :length, :stream
12
+
13
+ def initialize(options)
14
+ @filename = options[:filename]
15
+ @file = options[:file]
16
+ @content_type = options[:content_type]
17
+ @options = options
18
+ @file_size = File.size(@file)
19
+ @footer = LINE_BREAK
20
+ @length = header.bytesize + @file_size + @footer.length
21
+ @io = Stream.new(StringIO.new(header), @file, StringIO.new(@footer))
22
+ end
23
+
24
+ def header
25
+ part = []
26
+ part << "--#{boundary}"
27
+ part << "Content-Disposition: form-data; name=\"#{@filename}\"; filename=\"#{@filename}\""
28
+ part << "Content-Length: #{@size}"
29
+ part << "Content-Type: #{@content_type}"
30
+ part << 'Content-Transfer-Encoding: binary'
31
+ part << LINE_BREAK
32
+
33
+ part.join(LINE_BREAK)
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ module FatZebra
2
+ ##
3
+ # == FatZebra \Request \Multipart \Param
4
+ #
5
+ # Add param to the multipart
6
+ class Request
7
+ module Multipart
8
+ class Param
9
+ include Part
10
+
11
+ def initialize(name, value)
12
+ @name = name
13
+ @value = value
14
+ @io = StringIO.new(part)
15
+ end
16
+
17
+ def length
18
+ part.bytesize
19
+ end
20
+
21
+ private
22
+
23
+ def part
24
+ part = []
25
+ part << "--#{boundary}"
26
+ part << "Content-Disposition: form-data; name=\"#{@name}\""
27
+ part << ''
28
+ part << @value
29
+
30
+ part.join(LINE_BREAK)
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,28 @@
1
+ module FatZebra
2
+ ##
3
+ # == FatZebra \Request \Multipart \Part
4
+ #
5
+ # Define one part
6
+ class Request
7
+ module Multipart
8
+ module Part
9
+
10
+ LINE_BREAK = "\r\n".freeze
11
+
12
+ def boundary
13
+ '----FatZebraMultipartPost'
14
+ end
15
+
16
+ def length
17
+ @io.length
18
+ end
19
+
20
+ def to_io
21
+ @io
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,57 @@
1
+ module FatZebra
2
+ ##
3
+ # == FatZebra \Request \Multipart \Stream
4
+ #
5
+ # Steam the multipart
6
+ class Request
7
+ module Multipart
8
+ class Stream
9
+
10
+ BINARY_ENCODING = 'BINARY'.freeze
11
+
12
+ def initialize(*ios)
13
+ @ios = ios.flatten
14
+ @index = 0
15
+ end
16
+
17
+ # rubocop:disable Metrics/CyclomaticComplexity
18
+ # rubocop:disable Metrics/PerceivedComplexity
19
+ def read(length = nil, outbuf = nil)
20
+ got_result = false
21
+ outbuf = outbuf ? outbuf.replace('') : ''
22
+
23
+ while current_io
24
+ result = current_io.read(length)
25
+
26
+ if result
27
+ got_result ||= !result.nil?
28
+ result.force_encoding(BINARY_ENCODING) if result.respond_to?(:force_encoding)
29
+ outbuf << result
30
+
31
+ length -= result.length if length
32
+ break if length == 0
33
+ end
34
+
35
+ next_io
36
+ end
37
+
38
+ !got_result && length ? nil : outbuf
39
+ end
40
+ # rubocop:enable Metrics/CyclomaticComplexity
41
+ # rubocop:enable Metrics/PerceivedComplexity
42
+
43
+ private
44
+
45
+ def current_io
46
+ @ios[@index]
47
+ end
48
+
49
+ def next_io
50
+ @index += 1
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,71 @@
1
+ module FatZebra
2
+ module Util
3
+ class << self
4
+
5
+ DATE_FORMAT = '%Y/%m/%d'.freeze
6
+ REGEXP_HTTP = %r{http[s]?\:\/\/}
7
+
8
+ def cleanup_host(uri)
9
+ uri.gsub(REGEXP_HTTP, '')
10
+ end
11
+
12
+ ##
13
+ # Remove nil value from hash
14
+ #
15
+ # @return [Hash]
16
+ def compact(hash)
17
+ # Can use Hash#compact when Ruby 2.3 compatability is dropped
18
+ hash.reject { |_, value| value.nil? }
19
+ end
20
+
21
+ ##
22
+ # Convert string to camelize
23
+ #
24
+ # @param [String]
25
+ #
26
+ # @example
27
+ # camelize('my_model') => 'MyModel'
28
+ def camelize(string)
29
+ string.split('_').map(&:capitalize).join
30
+ end
31
+
32
+ ##
33
+ # Convert string to underscore
34
+ #
35
+ # @param [String]
36
+ #
37
+ # @example
38
+ # underscore('MyModel') => 'my_model'
39
+ def underscore(string)
40
+ string
41
+ .gsub(/::/, '/')
42
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
43
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
44
+ .tr('-', '_')
45
+ .downcase
46
+ end
47
+
48
+ def hash_except(hash, *keys)
49
+ copy = hash.dup
50
+ keys.each { |key| copy.delete(key) }
51
+ copy
52
+ end
53
+
54
+ ##
55
+ # Format Date attribute in the params
56
+ #
57
+ # @param [Hash] params
58
+ #
59
+ # @return [Hash] date formated params
60
+ def format_dates_in_hash(hash)
61
+ # Can use Hash#transform_values! when Ruby 2.3 compatablility is dropped
62
+ hash.each do |(key, value)|
63
+ hash[key] = value.strftime DATE_FORMAT if value.respond_to? :strftime
64
+ end
65
+
66
+ hash
67
+ end
68
+
69
+ end
70
+ end
71
+ end