windcave_rest 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,234 @@
1
+ require 'uri'
2
+ require 'logger'
3
+
4
+ module WindcaveRest
5
+ class Configuration
6
+ # Defines url scheme
7
+ attr_accessor :scheme
8
+
9
+ # Defines url host
10
+ attr_accessor :host
11
+
12
+ # Defines url base path
13
+ attr_accessor :base_path
14
+
15
+ # Supported environments
16
+ attr_accessor :supported_environments
17
+
18
+ # Current environment
19
+ attr_accessor :environment
20
+
21
+ # Defines API keys used with API Key authentications.
22
+ #
23
+ # @return [Hash] key: parameter name, value: parameter value (API key)
24
+ #
25
+ # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
26
+ # config.api_key['api_key'] = 'xxx'
27
+ attr_accessor :api_key
28
+
29
+ # Defines API key prefixes used with API Key authentications.
30
+ #
31
+ # @return [Hash] key: parameter name, value: API key prefix
32
+ #
33
+ # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
34
+ # config.api_key_prefix['api_key'] = 'Token'
35
+ attr_accessor :api_key_prefix
36
+
37
+ # Defines the username used with HTTP basic authentication.
38
+ #
39
+ # @return [String]
40
+ attr_accessor :username
41
+
42
+ # Defines the password used with HTTP basic authentication.
43
+ #
44
+ # @return [String]
45
+ attr_accessor :password
46
+
47
+ # Defines the access token (Bearer) used with OAuth2.
48
+ attr_accessor :access_token
49
+
50
+ # Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
51
+ # details will be logged with `logger.debug` (see the `logger` attribute).
52
+ # Default to false.
53
+ #
54
+ # @return [true, false]
55
+ attr_accessor :debugging
56
+
57
+ # Defines the logger used for debugging.
58
+ # Default to `Rails.logger` (when in Rails) or logging to STDOUT.
59
+ #
60
+ # @return [#debug]
61
+ attr_accessor :logger
62
+
63
+ # Defines the temporary folder to store downloaded files
64
+ # (for API endpoints that have file response).
65
+ # Default to use `Tempfile`.
66
+ #
67
+ # @return [String]
68
+ attr_accessor :temp_folder_path
69
+
70
+ # The time limit for HTTP request in seconds.
71
+ # Default to 0 (never times out).
72
+ attr_accessor :timeout
73
+
74
+ # Number of retries allowed if the first one fails.
75
+ # Default to 0 (no retries).
76
+ attr_accessor :num_retries
77
+
78
+ # Retry interval.
79
+ # Default to 0 (retry immediately).
80
+ attr_accessor :retry_interval
81
+
82
+
83
+ ### TLS/SSL setting
84
+ # Set this to false to skip verifying SSL certificate when calling API from https server.
85
+ # Default to true.
86
+ #
87
+ # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
88
+ #
89
+ # @return [true, false]
90
+ attr_accessor :verify_ssl
91
+
92
+ ### TLS/SSL setting
93
+ # Set this to false to skip verifying SSL host name
94
+ # Default to true.
95
+ #
96
+ # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
97
+ #
98
+ # @return [true, false]
99
+ attr_accessor :verify_ssl_host
100
+
101
+ ### TLS/SSL setting
102
+ # Set this to customize the certificate file to verify the peer.
103
+ #
104
+ # @return [String] the path to the certificate file
105
+ #
106
+ # @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
107
+ # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
108
+ attr_accessor :ssl_ca_cert
109
+
110
+ ### TLS/SSL setting
111
+ # Client certificate file (for client certificate)
112
+ attr_accessor :cert_file
113
+
114
+ ### TLS/SSL setting
115
+ # Client private key file (for client certificate)
116
+ attr_accessor :key_file
117
+
118
+ # Set this to customize parameters encoding of array parameter with multi collectionFormat.
119
+ # Default to nil.
120
+ #
121
+ # @see The params_encoding option of Ethon. Related source code:
122
+ # https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L96
123
+ attr_accessor :params_encoding
124
+
125
+ attr_accessor :inject_format
126
+
127
+ attr_accessor :force_ending_format
128
+
129
+ attr_accessor :api_version
130
+
131
+ attr_accessor :platform
132
+
133
+ attr_accessor :user_agent
134
+
135
+ def initialize
136
+ @scheme = 'https'
137
+ @host = 'uat.windcave.com'
138
+ @supported_environments = { :test => "uat.windcave.com", :live => "sec.windcave.com" }
139
+ @environment = "test"
140
+ @base_path = '/api/v1'
141
+ @api_key = {}
142
+ @api_key_prefix = {}
143
+ @timeout = 0
144
+ @verify_ssl = true
145
+ @verify_ssl_host = true
146
+ @params_encoding = nil
147
+ @cert_file = nil
148
+ @key_file = nil
149
+ @debugging = false
150
+ @inject_format = false
151
+ @force_ending_format = false
152
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
153
+ @api_version = '1'
154
+ @retry_interval = 0
155
+ @num_retries = 3
156
+ @user_agent = "windcave_rest/#{VERSION}/ruby"
157
+
158
+ yield(self) if block_given?
159
+ end
160
+
161
+ # The default Configuration object.
162
+ def self.default
163
+ @@default ||= Configuration.new
164
+ end
165
+
166
+ def configure
167
+ yield(self) if block_given?
168
+ end
169
+
170
+ def scheme=(scheme)
171
+ # remove :// from scheme
172
+ @scheme = scheme.sub(/:\/\//, '')
173
+ end
174
+
175
+ def environment=(environment)
176
+ if @supported_environments[environment.to_sym]
177
+ @environment = environment
178
+ @host = @supported_environments[environment.to_sym]
179
+ end
180
+ end
181
+
182
+ def host=(host)
183
+ # remove http(s):// and anything after a slash
184
+ @host = host.sub(/https?:\/\//, '').split('/').first
185
+ end
186
+
187
+ def base_path=(base_path)
188
+ # Add leading and trailing slashes to base_path
189
+ @base_path = "/#{base_path}".gsub(/\/+/, '/')
190
+ @base_path = "" if @base_path == "/"
191
+ end
192
+
193
+ def base_url
194
+ url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
195
+ URI.encode(url)
196
+ end
197
+
198
+ # Gets API key (with prefix if set).
199
+ # @param [String] param_name the parameter name of API key auth
200
+ def api_key_with_prefix(param_name)
201
+ if @api_key_prefix[param_name]
202
+ "#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
203
+ else
204
+ @api_key[param_name]
205
+ end
206
+ end
207
+
208
+ # Gets Basic Auth token string
209
+ def basic_auth_token
210
+ 'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
211
+ end
212
+
213
+ def user_agent=(user_agent)
214
+ @user_agent = user_agent
215
+ end
216
+
217
+ def platform=(platform)
218
+ @platform = platform
219
+ end
220
+
221
+ # Returns Auth Settings hash for api client.
222
+ def auth_settings
223
+ {
224
+ 'Authorization' =>
225
+ {
226
+ type: 'api_key',
227
+ in: 'header',
228
+ key: 'Authorization',
229
+ value: api_key_with_prefix('Authorization')
230
+ },
231
+ }
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,24 @@
1
+ module WindcaveRest
2
+ class Address < WindcaveRest::Base
3
+ api_attribute :name
4
+
5
+ # (required)
6
+ api_attribute :address1
7
+
8
+ api_attribute :address2
9
+
10
+ api_attribute :address3
11
+
12
+ api_attribute :city
13
+
14
+ # ISO 3166-1 alpha-2
15
+ api_attribute :country_code
16
+
17
+ # state of country, second part of ISO 3166-2 code
18
+ api_attribute :state
19
+
20
+ api_attribute :postal_code
21
+
22
+ api_attribute :phone_number
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ module WindcaveRest
2
+ class Avs < WindcaveRest::Base
3
+
4
+ # (required) post code of the card holders registered billing address
5
+ api_attribute :post_code
6
+
7
+ # (required) street address of the card holders registered billing address
8
+ api_attribute :street_address
9
+
10
+ # (required) instruction on how to process avs response. 0, 1, 2, 3, 4
11
+ api_attribute :avs_action, :'Integer'
12
+
13
+ # (required) avs action as a string
14
+ api_attribute :avs_action_name
15
+
16
+ # (required) outcome code, single Upper case character as per https://en.wikipedia.org/wiki/Address_Verification_System
17
+ api_attribute :avs_result_code
18
+
19
+ # (required) outcome description
20
+ api_attribute :avs_result_description
21
+ end
22
+ end
@@ -0,0 +1,152 @@
1
+ module WindcaveRest
2
+ class Base
3
+ # Initializes the object
4
+ # @param [Hash] attributes Model attributes in the form of hash
5
+ def initialize(attributes = {})
6
+ return unless attributes.is_a?(Hash)
7
+
8
+ # convert string to symbol for hash key
9
+ attributes = attributes.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
10
+
11
+ self.class.windcave_types.keys.each do |attrib|
12
+ if attributes.has_key?(attrib)
13
+ self.send("#{attrib.to_s}=", attributes[attrib])
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.api_attribute(attribute, datatype = :'String', api_format = :"#{attribute.to_s.camel_case_lower}")
19
+ self.__send__(:attr_accessor, attribute)
20
+
21
+ @attribute_map ||= {}
22
+ @windcave_types ||= {}
23
+
24
+ @attribute_map[attribute] = api_format
25
+ @windcave_types[attribute] = datatype
26
+ end
27
+
28
+ def self.attribute_map
29
+ @attribute_map
30
+ end
31
+
32
+ def self.windcave_types
33
+ @windcave_types
34
+ end
35
+
36
+ # Builds the object from hash
37
+ # @param [Hash] attributes Model attributes in the form of hash
38
+ # @return [Object] Returns the model itself
39
+ def build_from_hash(attributes)
40
+ return nil unless attributes.is_a?(Hash)
41
+ self.class.windcave_types.each_pair do |key, type|
42
+ if type =~ /\AArray<(.*)>/i
43
+ # check to ensure the input is an array given that the the attribute
44
+ # is documented as an array but the input is not
45
+ if attributes[self.class.attribute_map[key]].is_a?(Array)
46
+ self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
47
+ end
48
+ elsif !attributes[self.class.attribute_map[key]].nil?
49
+ self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
50
+ end # or else data not found in attributes(hash), not an issue as the data can be optional
51
+ end
52
+
53
+ self
54
+ end
55
+
56
+ # Deserializes the data based on type
57
+ # @param string type Data type
58
+ # @param string value Value to be deserialized
59
+ # @return [Object] Deserialized data
60
+ def _deserialize(type, value)
61
+ case type.to_sym
62
+ when :DateTime
63
+ DateTime.parse(value)
64
+ when :Date
65
+ Date.parse(value)
66
+ when :String
67
+ value.to_s
68
+ when :Integer
69
+ value.to_i
70
+ when :Float
71
+ value.to_f
72
+ when :BOOLEAN
73
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
74
+ true
75
+ else
76
+ false
77
+ end
78
+ when :Object
79
+ # generic object (usually a Hash), return directly
80
+ value
81
+ when /\AArray<(?<inner_type>.+)>\z/
82
+ inner_type = Regexp.last_match[:inner_type]
83
+ value.map { |v| _deserialize(inner_type, v) }
84
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
85
+ k_type = Regexp.last_match[:k_type]
86
+ v_type = Regexp.last_match[:v_type]
87
+ {}.tap do |hash|
88
+ value.each do |k, v|
89
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
90
+ end
91
+ end
92
+ else # model
93
+ temp_model = WindcaveRest.const_get(type).new
94
+ temp_model.build_from_hash(value)
95
+ end
96
+ end
97
+
98
+ # Returns the string representation of the object
99
+ # @return [String] String presentation of the object
100
+ def to_s
101
+ to_hash.to_s
102
+ end
103
+
104
+ # to_body is an alias to to_hash (backward compatibility)
105
+ # @return [Hash] Returns the object in the form of hash
106
+ def to_body
107
+ to_hash
108
+ end
109
+
110
+ # Returns the object in the form of hash
111
+ # @return [Hash] Returns the object in the form of hash
112
+ def to_hash
113
+ hash = {}
114
+ self.class.attribute_map.each_pair do |attr, param|
115
+ value = self.send(attr)
116
+ next if value.nil?
117
+ hash[param] = _to_hash(value)
118
+ end
119
+ hash
120
+ end
121
+
122
+ # Outputs non-array value in the form of hash
123
+ # For object, use to_hash. Otherwise, just return the value
124
+ # @param [Object] value Any valid value
125
+ # @return [Hash] Returns the value in the form of hash
126
+ def _to_hash(value)
127
+ if value.is_a?(Array)
128
+ value.compact.map{ |v| _to_hash(v) }
129
+ elsif value.is_a?(Hash)
130
+ {}.tap do |hash|
131
+ value.each { |k, v| hash[k] = _to_hash(v) }
132
+ end
133
+ elsif value.respond_to? :to_hash
134
+ value.to_hash
135
+ else
136
+ value
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ # to convert from snake case to camel case in String
143
+ class String
144
+ def camel_case
145
+ return self if self !~ /_/ && self =~ /[A-Z]+.*/
146
+ split('_').map{|e| e.capitalize}.join
147
+ end
148
+
149
+ def camel_case_lower
150
+ self.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
151
+ end
152
+ end
@@ -0,0 +1,6 @@
1
+ module WindcaveRest
2
+ class Browser < WindcaveRest::Base
3
+ api_attribute :ip_address
4
+ api_attribute :user_agent
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ module WindcaveRest
2
+ class CallbackUrls < WindcaveRest::Base
3
+ # populate with the url you want the cardholder to be redirected back to on processing an approved transaction.
4
+ api_attribute :approved
5
+
6
+ # populate with the url you want the cardholder to be redirected back to on processing a final declined transaction.
7
+ api_attribute :declined
8
+
9
+ # populate with the url you want the cardholder to be redirected back to on a cancel payment occurring.
10
+ api_attribute :cancelled
11
+ end
12
+ end