konduto-ruby 1.0.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d1bbfcf9c8de674af2c94ae36cb389533e433a8
4
- data.tar.gz: d54dc74d9e525e5c88a00f330bd1417c965d0bbd
3
+ metadata.gz: 2e6c731cc451c876728e72d78c83d3ad377594e8
4
+ data.tar.gz: e23494d79a6eb309cd9df9559ce6fa23b90568e1
5
5
  SHA512:
6
- metadata.gz: 023c74c910075008db964a84d25761a28ad905f5dacc88c417b94398e51959480ada84d53e878ac1c205b3b3cdcd7558e52f884f1fd5bd8e2229721e78ffe265
7
- data.tar.gz: 67ae56a33a059d4c53ae1e82c9ba0f60e512ca2d4ed89ac6c0e33ea8ce9479d736712de13f946952d1e02757a8fa51e3484be61843aa5a17c4c843218214dce2
6
+ metadata.gz: c91ec72c7626b8825d3253b53ffa492ae3b4871046a6f22746d8f9d5c2a1f473dab2f964e31d3b6b0b37676f900cc406c0dbc57cebb2bd429d96d1f738998b1b
7
+ data.tar.gz: a0dd0e4746672156586de32a7744e1342e10872f0736d4fed1712796ae936b5766af6f9f1b97bdbb1e7d4f78eeddfeed2e2cbc2c3317723390d4527211716c47
@@ -0,0 +1,236 @@
1
+ ## Hey there,
2
+
3
+ This document is a rubyist adaptation of the original README file made for the Java API version. You can get access to it by clicking [here](https://github.com/konduto/java-sdk/blob/master/README.md). **Konduto is a trademark, all rights reserved**. This project has no intention beyond helping ruby users to get easy access to Konduto's API through a simplified interface.
4
+
5
+ ## Intro
6
+
7
+ Welcome! This document will explain how to integrate with Konduto's anti-fraud service so you can begin to spot fraud on your e-commerce website.
8
+
9
+ Our service uses the visitor's behavior to analyze browsing patterns and detect fraud. You will need to add a **JavaScript** snippet to your website and tag your pages, so we can see your visitors, and call our **REST API** to send purchases, so we can analyze them.
10
+
11
+ This document refers to the **Ruby SDK** used for our API.
12
+
13
+ ## Requirements
14
+
15
+ * This API was tested with Ruby MRI version 2.
16
+
17
+ ## Installation
18
+
19
+ To get started add Konduto's gem as a dependency in your **Gemfile**:
20
+
21
+ gem 'konduto-ruby'
22
+
23
+ After that, run **bundle install**. If your are not using a Gemfile, run the following command
24
+
25
+ $ gem install konduto-ruby
26
+
27
+ ## Getting Started
28
+
29
+ When a customer makes a purchase you must send the order information to us so we can analyze it. We perform a real-time analysis and return you a **recommendation** of what to do next and a score, a numeric confidence level about that order.
30
+
31
+ While many of the parameters we accept are optional we recommend you send all you can, because every data point matters for the analysis. The **billing address** and **credit card information** are specially important, though we understand there are cases where you don't have that information.
32
+
33
+
34
+ ## Set your API key
35
+
36
+ You will need an API key to authenticate the requests. Luckily for you the examples below have been populated with a working key, so you can just copy and paste to see how it works.
37
+
38
+ ```ruby
39
+ # creates a Konduto instance, which is a class that communicates with our API by using HTTP methods.
40
+ konduto = KondutoRuby.new('T738D516F09CAB3A2C1EE') # T738D516F09CAB3A2C1EE is the API key
41
+ ```
42
+
43
+ ## Creating an order
44
+
45
+ `KondutoOrder` is a class that models the attributes and behavior of an order.
46
+
47
+ All entities involved in Konduto's analysis process (e.g customer, shopping cart, payment, etc.) inherit
48
+ from KondutoBase and you can get access to them by calling their name and passing a hash of attributes, as shown below.
49
+
50
+ ```ruby
51
+ order = KondutoOrder.new({
52
+ id: '123',
53
+ totalAmount: 123.4,
54
+ customer: customer # customer is an instance of KondutoCustomer
55
+ })
56
+ ```
57
+ One can also use the more conventional set-based approach as seen below.
58
+
59
+ ```ruby
60
+ order = KondutoOrder.new
61
+ order.id = "123"
62
+ order.total_amount = 123.4
63
+ order.customer = customer
64
+ ```
65
+
66
+ >
67
+ **NOTICE**: the order created above is really, really simple. The more detail you provide, more accurate Konduto's analysis will be.
68
+ >
69
+
70
+ ### Order parameters
71
+
72
+ Parameter | Description
73
+ --- | ---
74
+ id | _(required)_ Unique identifier for each order.
75
+ visitor | _(required)_ Visitor identifier obtained from our JavaScript snippet.
76
+ total_amount | _(required)_ Total order amount.
77
+ shipping_amount | _(optional)_ Shipping and handling amount.
78
+ tax_amount | _(optional)_ Taxes amount.
79
+ currency | _(optional)_ Currency code with 3 letters (ISO-4712).
80
+ installments | _(optional)_ Number of installments in the payment plan.
81
+ ip | _(optional)_ Customer's IPv4 address.
82
+ customer | _(required)_ Object containing the customer details.
83
+ payment | _(optional)_ Array containing the payment methods.
84
+ billing | _(optional)_ Object containing the billing information.
85
+ shipping | _(optional)_ Object containing the shipping information.
86
+ shopping_cart | _(optional)_ Array containing the items purchased.
87
+ analyze | _(optional)_ A boolean indicating if the order should be analyzed. Defaults to **true**.
88
+ first_message | _(optional)_ Time when the first message was exchanged between customer and seller.
89
+ messages_exchanged | _(optional)_ Number of messages exchanged between customer and seller.
90
+ purchased_at | _(optional)_ Time when the customer purchased from the seller.
91
+
92
+ #### Customer information
93
+
94
+ Parameter | Description
95
+ --- | ---
96
+ id | _(required)_ **Unique** identifier for each customer. Can be anything you like (counter, id, e-mail address) as long as it's consistent in future orders.
97
+ name | _(required)_ Customer's full name.
98
+ email | _(required)_ Customer's e-mail address
99
+ tax_id | _(optional)_ Customer's tax id.
100
+ phone1 | _(optional)_ Customer's primary phone number
101
+ phone 2 | _(optional)_ Customer's secondary phone number
102
+ new | _(optional)_ Boolean indicating if the customer is using a newly created account for this purchase.
103
+ vip | _(optional)_ Boolean indicating if the customer is a VIP or frequent buyer.
104
+ created_at | _(optional)_ Date when customer was created.
105
+
106
+
107
+ #### Payment information
108
+
109
+ Parameter | Description
110
+ --- | ---
111
+ status | _(required)_ The status of the transaction returned by the payment processor. Accepts `approved`, `declined` or `pending` if the payment wasn't been processed yet.
112
+ bin | _(optional)_ First six digits of the customer's credit card. Used to identify the type of card being sent.
113
+ last4 | _(optional)_ Four last digits of the customer's credit card number.
114
+ expiration_date | _(optional)_ Card's expiration date under MMYYYY format.
115
+
116
+
117
+ #### Billing address
118
+
119
+ Parameter | Description
120
+ --- | ---
121
+ name | _(optional)_ Cardholder's full name.
122
+ address1 | _(optional)_ Cardholder's billing address on file with the bank.
123
+ address2 | _(optional)_ Additional cardholder address information.
124
+ city | _(optional)_ Cardholder's city.
125
+ state | _(optional)_ Cardholder's state.
126
+ zip | _(optional)_ Cardholder's ZIP code.
127
+ country | _(optional)_ Cardholder's country code (ISO 3166-2)
128
+
129
+
130
+ #### Shipping address
131
+
132
+ Parameter | Description
133
+ --- | ---
134
+ name | _(optional)_ Recipient's full name.
135
+ address1 | _(optional)_ Recipient's shipping address.
136
+ address2 | _(optional)_ Additional recipient address information.
137
+ city | _(optional)_ Recipient's city.
138
+ state | _(optional)_ Recipient's state.
139
+ zip | _(optional)_ Recipient's ZIP code.
140
+ country | _(optional)_ Recipient's country code (ISO 3166-2)
141
+
142
+
143
+ #### Shopping cart
144
+
145
+ Parameter | Description
146
+ --- | ---
147
+ sku | _(optional)_ Product or service's SKU or inventory id.
148
+ product_code | _(optional)_ Product or service's UPC, barcode or secondary id.
149
+ category | _(optional)_ Category code for the item purchased. [See here](http://docs.konduto.com/#n-tables) for the list.
150
+ name | _(optional)_ Name of the product or service.
151
+ description | _(optional)_ Detailed description of the item.
152
+ unit_cost | _(optional)_ Cost of a single unit of this item.
153
+ quantity | _(optional)_ Number of units purchased.
154
+ discount | _(optional)_ Discounted amount for this item.
155
+ created_at | _(optional)_ Date when this item was created.
156
+
157
+ ### Seller
158
+
159
+ Parameter | Description
160
+ --- | ---
161
+ id | _(required)_ Seller's id
162
+ name | _(optional)_ Sellers's name
163
+ created_at | _(optional)_ Date when the seller was created
164
+
165
+
166
+ ## Sending an order for analysis.
167
+
168
+ After creating the order, sending it to Konduto's analysis is very simple.
169
+
170
+ ```ruby
171
+ if order.valid?
172
+ begin
173
+ konduto.analyze(order);
174
+ # A RuntimeError will be thrown if the response is anything other than 200 OK.
175
+ rescue RuntimeError => e
176
+ # Put any exception handling here.
177
+ p e.message
178
+ end
179
+ else
180
+ p order.errors.messages
181
+ end
182
+ ```
183
+
184
+ Notice that if the analysis fails, a **RuntimError** will be thrown. Handle it as you wish.
185
+
186
+ After the analysis, some order attributes will be filled. For example the recommendation.
187
+
188
+ ```ruby
189
+ # The command below should print something like "Konduto recommendation is to APPROVE".
190
+ p "Konduto recommendation is to: #{order.recommendation}";
191
+ ```
192
+
193
+ ## Querying an order from our servers.
194
+
195
+ In order to do that use the Konduto class in the following way:
196
+
197
+ ```ruby
198
+ order = konduto.get_order order_id # orderId is a String
199
+ ```
200
+
201
+ ## Updating an order status
202
+
203
+ ```ruby
204
+ # the order status will be set to newStatus if the request succeeds.
205
+ konduto.update_order_status(order, new_status, 'some comments');
206
+ ```
207
+
208
+ Parameter | Description
209
+ --- | ---
210
+ status | _(required)_ New status for this transaction. Either `approved`, `declined` or `fraud`, when you have identified a fraud or chargeback.
211
+ comments | _(required)_ Reason or comments about the status update.
212
+
213
+ ## Sending requests through a proxy
214
+ To send requests through a proxy just build a new Konduto instance and set the proxy host passing the proxy url as parameters of `proxy=` method. If the proxy requires username and password, just set then at the proxy url you'll pass to `proxy=` method.
215
+
216
+ ```ruby
217
+ konduto = KondutoRuby.new(API_KEY)
218
+ konduto.proxy = 'http://server-ip:port/'
219
+ # Or, using username and password
220
+ konduto.proxy = 'http://USERNAME:PASSWORD@proxy-server:port/'
221
+ # use Konduto's API as usual
222
+ konduto.get_order(ORDER_ID)
223
+ ```
224
+
225
+ ## Reference Tables
226
+
227
+ Please [click here](http://docs.konduto.com/#n-tables) for the Currency and Category reference tables.
228
+
229
+ ## Troubleshooting
230
+
231
+ If you experience problems sending orders for analysis, querying orders or updating order status, it might be a good idea
232
+ to call `konduto.debug()`. This will print out the API Key, the endpoint, ~~the request body and the response body~~.
233
+
234
+ ## Support
235
+
236
+ Feel free to end us a feedback via email or open an issue if you have some doubts or find out bugs. You can also contact konduto's [support team](mailto:support@konduto.com) if you have any questions or suggestions!
@@ -1,9 +1,11 @@
1
+ require 'json'
2
+ require 'konduto-ruby/konduto_base'
3
+ Dir[__dir__ + '/konduto-ruby/*.rb'].each { |file| require file }
4
+
1
5
  class KondutoRuby
2
- require 'json'
3
6
  require 'uri'
4
7
  require 'base64'
5
8
  require 'net/http'
6
- require 'konduto-ruby/konduto_order'
7
9
  attr_accessor :request_body, :response_body, :endpoint
8
10
  attr_reader :api_key
9
11
 
@@ -98,4 +100,8 @@ class KondutoRuby
98
100
  raise (JSON.parse(response.body)['message']).to_s
99
101
  end
100
102
  end
103
+
104
+ def debug
105
+ "API Key: #{@api_key}\nEndpoint: #{@endpoint}\n"
106
+ end
101
107
  end
@@ -0,0 +1,51 @@
1
+ module Konduto
2
+ module Associations
3
+
4
+ def has_one(model, options = {})
5
+ name = options[:alias] || model
6
+
7
+ self.send(:define_method, name) do
8
+ instance_variable_get("@#{name}")
9
+ end
10
+
11
+ self.send(:define_method, "#{name}=".to_sym) do |value|
12
+ klass = "Konduto#{model.to_s.gsub(/_/, ' ').split.map(&:capitalize).join('')}"
13
+
14
+ if value.class.to_s == klass
15
+ instance_variable_set("@#{name}", value)
16
+ elsif value.is_a? Hash
17
+ instance_variable_set("@#{name}", Object::const_get(klass).new(value))
18
+ else
19
+ raise ArgumentError, "Expected class #{klass}, got #{value.class} at #{self.class}"
20
+ end
21
+ end
22
+ end
23
+
24
+ def has_many(model, options = {})
25
+ name = options[:alias] || model
26
+
27
+ self.send(:define_method, name) do
28
+ instance_variable_set("@#{name}", []) if instance_variable_get("@#{name}").nil?
29
+
30
+ instance_variable_get("@#{name}")
31
+ end
32
+
33
+ self.send(:define_method, "#{name}=".to_sym) do |arr|
34
+ klass = "Konduto#{model.to_s.gsub(/_/, ' ').split.map(&:capitalize).join('')}"
35
+ temp_arr = []
36
+
37
+ arr.each do |value|
38
+ if value.class.to_s == klass
39
+ temp_arr << value
40
+ elsif value.is_a? Hash
41
+ temp_arr << Object::const_get(klass).new(value)
42
+ else
43
+ raise ArgumentError, "Expected class #{klass}, got #{value.class} at #{self.class}"
44
+ end
45
+ end
46
+
47
+ instance_variable_set("@#{name}", temp_arr)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ module Konduto
2
+ module Attributes
3
+ def attributes(*attributes)
4
+ attributes.each do |attribute|
5
+ if attribute.is_a? Hash
6
+ klass = attribute.values[0].to_s.gsub(/_/, ' ').split.map(&:capitalize).join('')
7
+ type = Object::const_get(klass)
8
+ name = attribute.keys[0]
9
+ else
10
+ name = attribute
11
+ end
12
+
13
+ self.send(:define_method, "#{name}=".to_sym) do |value|
14
+ unless type.nil?
15
+ if value.is_a?(String)
16
+ value = type.parse(value) if type == Date || type == DateTime
17
+ value = value.to_sym if type == Symbol
18
+ end
19
+ end
20
+
21
+ instance_variable_set("@#{name.to_s.gsub(/[?|!]$/, '')}", value)
22
+ end
23
+
24
+ self.send(:define_method, name) do
25
+ instance_variable_get("@#{name.to_s.gsub(/[?|!]$/, '')}")
26
+ end
27
+ end
28
+ end
29
+
30
+ alias :attribute :attributes
31
+ end
32
+ end
@@ -1,28 +1,3 @@
1
- class KondutoAddress
2
- attr_accessor :name, :address1, :address2, :city, :state, :country, :zip
3
-
4
- def initialize(*args)
5
- unless args[0].nil?
6
- args[0].each do |k,v|
7
- instance_variable_set("@#{k}", v) unless v.nil?
8
- end
9
- end
10
- end
11
-
12
- def to_hash
13
- hash = {
14
- name: self.name,
15
- address1: self.address1,
16
- address2: self.address2,
17
- city: self.city,
18
- state: self.state,
19
- country: self.country,
20
- zip: self.zip
21
- }
22
- KondutoUtils.remove_nil_keys_from_hash(hash)
23
- end
24
-
25
- def to_json
26
- self.to_hash.to_json
27
- end
1
+ class KondutoAddress < KondutoBase
2
+ attributes :name, :address1, :address2, :zip, :city, :state, :country
28
3
  end
@@ -0,0 +1,56 @@
1
+ require_relative 'associations/associations'
2
+ require_relative 'validations/validations'
3
+ require_relative 'attributes'
4
+
5
+ class KondutoBase
6
+ extend Konduto::Associations
7
+ extend Konduto::Attributes
8
+ include Konduto::Validations
9
+
10
+ def initialize(*args)
11
+ unless args[0].nil?
12
+ args[0].each do |key, value|
13
+ unless value.nil?
14
+ if respond_to? "#{key}=".to_sym
15
+ send("#{key}=", value)
16
+ elsif key == 'class'
17
+ send('klass=', value)
18
+ else
19
+ instance_variable_set("@#{key}", value)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def to_hash
27
+ Hash[
28
+ instance_variables.map do |name|
29
+ value = instance_variable_get(name)
30
+
31
+ if value.respond_to? :each
32
+ value = value.map {|v| v.to_hash }
33
+ elsif !value.instance_variables.empty?
34
+ value = value.to_hash
35
+ elsif value.is_a?(Date) || value.is_a?(Symbol)
36
+ value = value.to_s
37
+ end
38
+
39
+ [name == :@klass ? 'class' : name.to_s.gsub(/^@/, ''), value]
40
+ end
41
+ ]
42
+ end
43
+
44
+ def to_json
45
+ raise RuntimeError, 'Invalid object for serialization' unless self.valid?
46
+ self.to_hash.to_json
47
+ end
48
+
49
+ def ==(other)
50
+ self.instance_variables.each do |name|
51
+ return false unless self.instance_variable_get(name) == other.instance_variable_get(name)
52
+ end
53
+
54
+ true
55
+ end
56
+ end
@@ -0,0 +1,4 @@
1
+ class KondutoBusLeg < KondutoTravelLeg
2
+ attributes :origin_city, :destination_city
3
+ validates_presence_of :origin_city, :destination_city
4
+ end
@@ -1,31 +1,6 @@
1
- class KondutoCustomer
2
- attr_accessor :id, :name, :email, :dob, :tax_id, :phone1, :phone2, :created_at, :new, :vip
1
+ class KondutoCustomer < KondutoBase
2
+ attributes :id, :name, :email, :new?, :vip?, :phone1, :phone2, :tax_id
3
+ attribute created_at: :date
3
4
 
4
- def initialize(*args)
5
- unless args[0].nil?
6
- args[0].each do |k,v|
7
- instance_variable_set("@#{k}", v) unless v.nil?
8
- end
9
- end
10
- end
11
-
12
- def to_hash
13
- hash = {
14
- id: self.id,
15
- name: self.name,
16
- email: self.email,
17
- dob: self.dob,
18
- tax_id: self.tax_id,
19
- phone1: self.phone1,
20
- phone2: self.phone2,
21
- created_at: self.created_at,
22
- new: self.new,
23
- vip: self.vip
24
- }
25
- KondutoUtils.remove_nil_keys_from_hash(hash)
26
- end
27
-
28
- def to_json
29
- self.to_hash.to_json
30
- end
5
+ validates_presence_of :id, :name, :email
31
6
  end
@@ -1,31 +1,4 @@
1
- class KondutoDevice
2
- attr_accessor :user_id, :fingerprint, :platform, :browser, :language, :timezone, :cookie, :javascript, :flash, :ip
3
-
4
- def initialize(*args)
5
- unless args[0].nil?
6
- args[0].each do |k,v|
7
- instance_variable_set("@#{k}", v) unless v.nil?
8
- end
9
- end
10
- end
11
-
12
- def to_hash
13
- hash = {
14
- user_id: self.user_id,
15
- fingerprint: self.fingerprint,
16
- platform: self.platform,
17
- browser: self.browser,
18
- language: self.language,
19
- timezone: self.timezone,
20
- cookie: self.cookie,
21
- javascript: self.javascript,
22
- flash: self.flash,
23
- ip: self.ip
24
- }
25
- KondutoUtils.remove_nil_keys_from_hash(hash)
26
- end
27
-
28
- def to_json
29
- self.to_hash.to_json
30
- end
1
+ class KondutoDevice < KondutoBase
2
+ attributes :user_id, :fingerprint, :platform, :browser, :language, :timezone, \
3
+ :cookie, :javascript, :flash, :ip
31
4
  end
@@ -0,0 +1,8 @@
1
+ class KondutoFlightLeg < KondutoTravelLeg
2
+ attributes :origin_airport, :origin_city, :destination_airport, :destination_city
3
+ validates_presence_of :origin_airport, :destination_airport
4
+
5
+ validates(:origin_airport, :destination_airport) do |attr|
6
+ attr.match(/[a-zA-Z]{3}/)
7
+ end
8
+ end
@@ -1,24 +1,3 @@
1
- class KondutoGeolocation
2
- attr_accessor :city, :state, :country
3
-
4
- def initialize(*args)
5
- unless args[0].nil?
6
- args[0].each do |k,v|
7
- instance_variable_set("@#{k}", v) unless v.nil?
8
- end
9
- end
10
- end
11
-
12
- def to_hash
13
- hash = {
14
- city: self.city,
15
- state: self.state,
16
- country: self.country
17
- }
18
- KondutoUtils.remove_nil_keys_from_hash(hash)
19
- end
20
-
21
- def to_json
22
- self.to_hash.to_json
23
- end
1
+ class KondutoGeolocation < KondutoBase
2
+ attributes :city, :state, :country
24
3
  end
@@ -1,30 +1,4 @@
1
- class KondutoItem
2
- attr_accessor :sku, :category, :name, :description, :product_code, :unit_cost, :quantity, :discount, :created_at
3
-
4
- def initialize(*args)
5
- unless args[0].nil?
6
- args[0].each do |k,v|
7
- instance_variable_set("@#{k}", v) unless v.nil?
8
- end
9
- end
10
- end
11
-
12
- def to_hash
13
- hash = {
14
- sku: self.sku,
15
- category: self.category,
16
- name: self.name,
17
- description: self.description,
18
- product_code: self.product_code,
19
- unit_cost: self.unit_cost,
20
- quantity: self.quantity,
21
- discount: self.discount,
22
- created_at: self.created_at
23
- }
24
- KondutoUtils.remove_nil_keys_from_hash(hash)
25
- end
26
-
27
- def to_json
28
- self.to_hash.to_json
29
- end
1
+ class KondutoItem < KondutoBase
2
+ attributes :sku, :category, :name, :description, :product_code, :unit_cost, :quantity, :discount
3
+ attribute created_at: :date
30
4
  end
@@ -1,23 +1,3 @@
1
- class KondutoLoyalty
2
- attr_accessor :program, :category
3
-
4
- def initialize(*args)
5
- unless args[0].nil?
6
- args[0].each do |k,v|
7
- instance_variable_set("@#{k}", v) unless v.nil?
8
- end
9
- end
10
- end
11
-
12
- def to_hash
13
- hash = {
14
- program: self.program,
15
- category: self.category
16
- }
17
- KondutoUtils.remove_nil_keys_from_hash(hash)
18
- end
19
-
20
- def to_json
21
- self.to_hash.to_json
22
- end
1
+ class KondutoLoyalty < KondutoBase
2
+ attributes :program, :category
23
3
  end
@@ -0,0 +1,5 @@
1
+ class KondutoNavigationInfo < KondutoBase
2
+ attributes :session_time, :referrer, :time_site_1d, :new_accounts_1d, :password_resets_1d, :sales_declined_1d,\
3
+ :sessions_1d, :time_since_last_sale, :time_site_7d, :time_per_page_7d, :new_accounts_7d, \
4
+ :password_resets_7d, :checkout_count_7d, :sales_declined_7d, :sessions_7d
5
+ end
@@ -1,153 +1,23 @@
1
- class KondutoOrder
2
- require 'konduto-ruby/konduto_address'
3
- require 'konduto-ruby/konduto_customer'
4
- require 'konduto-ruby/konduto_device'
5
- require 'konduto-ruby/konduto_geolocation'
6
- require 'konduto-ruby/konduto_item'
7
- require 'konduto-ruby/konduto_loyalty'
8
- require 'konduto-ruby/konduto_navigation'
9
- require 'konduto-ruby/konduto_order_status'
10
- require 'konduto-ruby/konduto_passenger'
11
- require 'konduto-ruby/konduto_payment'
12
- require 'konduto-ruby/konduto_seller'
13
- require 'konduto-ruby/konduto_travel'
14
- require 'konduto-ruby/konduto_travel_info'
15
- require 'konduto-ruby/konduto_utils'
16
-
17
- attr_accessor :id, :visitor, :timestamp, :total_amount, :shipping_amount, :tax_amount, :customer, :currency, \
18
- :installments, :ip, :score, :shipping_address, :billing_address, :recommendation, :status, \
19
- :geolocation, :analyze, :messages_enchanged, :first_message, :purchased_at, :seller, :payment, \
20
- :shopping_cart, :device, :navigation, :travel
21
-
22
- def initialize(*args)
23
- params = args[0]
24
- self.shopping_cart = []
25
- self.payment = []
26
- if params.nil?
27
- self.shipping_address = KondutoAddress.new
28
- self.billing_address = KondutoAddress.new
29
- self.customer = KondutoCustomer.new
30
- self.seller = KondutoSeller.new
31
- self.travel = KondutoTravel.new
32
- self.device = KondutoDevice.new
33
- self.geolocation = KondutoGeolocation.new
34
- self.navigation = KondutoNavigation.new
35
- else
36
- params = KondutoUtils.deep_symbolize_keys params
37
- if params[shipping_address].nil?
38
- self.shipping_address = KondutoAddress.new
39
- else
40
- self.shipping_address = KondutoAddress.new params[:shipping_address]
41
- params.delete :shipping_address
42
- end
43
-
44
- if params[:billing_address].nil?
45
- self.billing_address = KondutoAddress.new
46
- else
47
- self.billing_address = KondutoAddress.new params[:billing_address]
48
- params.delete :billing_address
49
- end
50
-
51
- if params[:customer].nil?
52
- self.customer = KondutoCustomer.new
53
- else
54
- self.customer = KondutoCustomer.new params[:customer]
55
- params.delete :customer
56
- end
57
-
58
- if params[:seller].nil?
59
- self.seller = KondutoSeller.new
60
- else
61
- self.seller = KondutoSeller.new params[:seller]
62
- params.delete :seller
63
- end
64
-
65
- if params[:travel].nil?
66
- self.travel = KondutoTravel.new
67
- else
68
- self.travel = KondutoTravel.new params[:travel]
69
- params.delete :travel
70
- end
71
-
72
- if params[:device].nil?
73
- self.device = KondutoDevice.new
74
- else
75
- self.device = KondutoDevice.new params[:device]
76
- params.delete :device
77
- end
78
-
79
- if params[:geolocation].nil?
80
- self.geolocation = KondutoGeolocation.new
81
- else
82
- self.geolocation = KondutoGeolocation.new params[:geolocation]
83
- params.delete :geolocation
84
- end
85
-
86
- if params[:navigation].nil?
87
- self.navigation = KondutoNavigation.new
88
- else
89
- self.navigation = KondutoNavigation.new params[:navigation]
90
- params.delete :navigation
91
- end
92
-
93
- unless params[:payment].nil? || params[:payment].empty?
94
- params[:payment].each do |p|
95
- self.payment << KondutoPayment.new(p)
96
- end
97
- params.delete :payment
98
- end
99
-
100
- unless params[:shopping_cart].nil? || params[:shopping_cart].empty?
101
- params[:shopping_cart].each do |i|
102
- self.shopping_cart << KondutoItem.new(i)
103
- end
104
- params.delete :shopping_cart
105
- end
106
-
107
- params.each do |k,v|
108
- instance_variable_set("@#{k}", v) unless v.nil?
109
- end
110
- end
111
- end
112
-
113
- def add_item (product)
114
- self.shopping_cart << product
115
- end
116
-
117
- def add_payment (payment)
118
- self.payment << payment
119
- end
120
-
121
- def to_hash
122
- hash = {
123
- id:self.id,
124
- visitor:self.visitor,
125
- total_amount:self.total_amount,
126
- shipping_amount:self.shipping_amount,
127
- tax_amount:self.tax_amount,
128
- currency:self.currency,
129
- installments:self.installments,
130
- ip:self.ip,
131
- first_message:self.first_message,
132
- messages_exchanged:self.messages_enchanged,
133
- purchased_at:self.purchased_at,
134
- analyze:self.analyze,
135
- customer:self.customer.to_hash,
136
- payment: KondutoUtils.array_to_hash(self.payment),
137
- billing:self.billing_address.to_hash,
138
- shipping:self.shipping_address.to_hash,
139
- shopping_cart:KondutoUtils.array_to_hash(self.shopping_cart),
140
- travel:self.travel.to_hash,
141
- seller:self.seller.to_hash,
142
- device:self.device.to_hash,
143
- geolocation:self.geolocation.to_hash,
144
- navigation:self.navigation.to_hash
145
- }
146
- KondutoUtils.remove_nil_keys_from_hash(hash)
147
- end
148
-
149
- def to_json
150
- self.to_hash.to_json
151
- end
152
-
1
+ class KondutoOrder < KondutoBase
2
+ has_many :item, alias: :shopping_cart
3
+ has_many :payment
4
+
5
+ has_one :address, alias: :shipping
6
+ has_one :address, alias: :billing
7
+ has_one :customer
8
+ has_one :seller
9
+ has_one :travel
10
+ has_one :device
11
+ has_one :geolocation
12
+ has_one :navigation
13
+
14
+ attributes :id, :visitor, :timestamp, :total_amount, :tax_amount, :currency, :installments,
15
+ :ip, :score, :analyze, :messages_exchanged, :shipping_amount
16
+
17
+ attribute first_message: :date_time
18
+ attribute purchased_at: :date_time
19
+ attribute status: :symbol
20
+ attribute recommendation: :symbol
21
+
22
+ validates_presence_of :id, :total_amount, :customer
153
23
  end
@@ -1,26 +1,7 @@
1
- class KondutoOrderStatus
2
- attr_accessor :status, :comments
1
+ class KondutoOrderStatus < KondutoBase
2
+ attributes :status, :comments
3
3
 
4
4
  def self.allowed_status
5
5
  %w(APPROVED DECLINED NOT_AUTHORIZED CANCELLED FRAUD)
6
6
  end
7
- def initialize(*args)
8
- unless args[0].nil?
9
- args[0].each do |k,v|
10
- instance_variable_set("@#{k}", v) unless v.nil?
11
- end
12
- end
13
- end
14
-
15
- def to_hash
16
- hash = {
17
- status: self.status,
18
- comments: self.comments
19
- }
20
- KondutoUtils.remove_nil_keys_from_hash(hash)
21
- end
22
-
23
- def to_json
24
- self.to_hash.to_json
25
- end
26
7
  end
@@ -1,40 +1,12 @@
1
- class KondutoPassenger
2
- require 'konduto-ruby/konduto_loyalty'
1
+ class KondutoPassenger < KondutoBase
2
+ attributes :name, :document, :nationality, :frequent_traveler, :special_needs, \
3
+ :loyalty, :loyalty_program, :loyalty_category
4
+ attribute dob: :date
5
+ attribute document_type: :symbol
3
6
 
4
- attr_accessor :name, :document, :document_type, :dob, :nationality, :frequent_traveller, :special_needs, :loyalty
7
+ validates_presence_of :name, :document, :document_type, :nationality
5
8
 
6
- TYPE_DOCUMENT = [:passport, :id]
7
- def initialize(*args)
8
- if args[0].nil?
9
- self.loyalty = KondutoLoyalty.new
10
- else
11
- if args[0][:loyalty].nil?
12
- self.loyalty = KondutoLoyalty.new
13
- else
14
- self.loyalty = KondutoLoyalty.new args[0][:loyalty]
15
- args[0].delete :loyalty
16
- end
17
- args[0].each do |k,v|
18
- instance_variable_set("@#{k}", v) unless v.nil?
19
- end
20
- end
21
- end
22
-
23
- def to_hash
24
- hash = {
25
- name: self.name,
26
- document: self.document,
27
- document_type: self.document_type,
28
- dob: self.dob,
29
- nationality: self.nationality,
30
- frequent_traveller: self.frequent_traveller,
31
- special_needs: self.special_needs,
32
- loyalty: self.loyalty.to_hash
33
- }
34
- KondutoUtils.remove_nil_keys_from_hash(hash)
35
- end
36
-
37
- def to_json
38
- self.to_hash.to_json
9
+ validates(:nationality) do |attr|
10
+ attr.match(/[a-zA-Z]{2}/)
39
11
  end
40
12
  end
@@ -1,29 +1,8 @@
1
- class KondutoPayment
2
- attr_accessor :type, :status, :bin, :last4, :expiration_date
1
+ class KondutoPayment < KondutoBase
2
+ attributes :expiration_date, :bin, :last4
3
+ attribute type: :symbol
4
+ attribute status: :symbol
3
5
 
4
6
  TYPE_PAYMENT = [:credit, :boleto, :debit, :transfer, :voucher]
5
7
  TYPE_STATUS = [:approved, :declined, :pending]
6
-
7
- def initialize(*args)
8
- unless args[0].nil?
9
- args[0].each do |k,v|
10
- instance_variable_set("@#{k}", v) unless v.nil?
11
- end
12
- end
13
- end
14
-
15
- def to_hash
16
- hash = {
17
- type: self.type,
18
- status: self.status,
19
- bin: self.bin,
20
- last4: self.last4,
21
- expiration_date: self.expiration_date
22
- }
23
- KondutoUtils.remove_nil_keys_from_hash(hash)
24
- end
25
-
26
- def to_json
27
- self.to_hash.to_json
28
- end
29
8
  end
@@ -1,24 +1,4 @@
1
- class KondutoSeller
2
- attr_accessor :id, :name, :created_at
3
-
4
- def initialize(*args)
5
- unless args[0].nil?
6
- args[0].each do |k,v|
7
- instance_variable_set("@#{k}", v) unless v.nil?
8
- end
9
- end
10
- end
11
-
12
- def to_hash
13
- hash = {
14
- id: self.id,
15
- name: self.name,
16
- created_at: self.created_at
17
- }
18
- KondutoUtils.remove_nil_keys_from_hash(hash)
19
- end
20
-
21
- def to_json
22
- self.to_hash.to_json
23
- end
1
+ class KondutoSeller < KondutoBase
2
+ attributes :id, :name
3
+ attribute created_at: :date
24
4
  end
@@ -1,51 +1,9 @@
1
- class KondutoTravel
2
- require 'konduto-ruby/konduto_travel_info'
3
- require 'konduto-ruby/konduto_passenger'
4
- require 'konduto-ruby/konduto_utils'
5
- attr_accessor :type, :departure, :return, :passengers
6
- TYPE_TRAVEL = [:flight, :bus]
1
+ class KondutoTravel < KondutoBase
2
+ has_one :flight_leg, alias: :departure
3
+ has_one :flight_leg, alias: :return
4
+ has_many :passenger, alias: :passengers
7
5
 
8
- def initialize(*args)
9
- self.passengers = []
6
+ attribute type: :symbol
10
7
 
11
- if args[0].nil?
12
- self.departure = KondutoTravelInfo.new
13
- self.return = KondutoTravelInfo.new
14
- else
15
- if args[0][:departure].nil?
16
- self.departure = KondutoTravelInfo.new
17
- else
18
- self.departure = KondutoTravelInfo.new args[0][:departure]
19
- args[0].delete :departure
20
- end
21
- if args[0][:return].nil?
22
- self.return = KondutoTravelInfo.new
23
- else
24
- self.return = KondutoTravelInfo.new args[0][:return]
25
- args[0].delete :return
26
- end
27
-
28
- args[0].each do |k,v|
29
- instance_variable_set("@#{k}", v) unless v.nil?
30
- end
31
- end
32
- end
33
-
34
- def add_passenger (passenger)
35
- self.passengers << passenger
36
- end
37
-
38
- def to_hash
39
- hash = {
40
- type: self.type,
41
- departure: self.departure.to_hash,
42
- return: self.return.to_hash,
43
- passengers: KondutoUtils.array_to_hash(self.passengers)
44
- }
45
- KondutoUtils.remove_nil_keys_from_hash(hash)
46
- end
47
-
48
- def to_json
49
- self.to_hash.to_json
50
- end
8
+ validates_presence_of :type, :departure, :passengers
51
9
  end
@@ -1,27 +1,6 @@
1
- class KondutoTravelInfo
2
- attr_accessor :date, :number_of_connections, :class, :fare_basis
1
+ class KondutoTravelInfo < KondutoBase
2
+ attributes :number_of_connections, :class, :fare_basis
3
+ attribute date: :date
3
4
 
4
5
  TRAVEL_CLASS = [:economy, :business, :first]
5
-
6
- def initialize(*args)
7
- unless args[0].nil?
8
- args[0].each do |k,v|
9
- instance_variable_set("@#{k}", v) unless v.nil?
10
- end
11
- end
12
- end
13
-
14
- def to_hash
15
- hash = {
16
- date: self.date,
17
- number_of_connections: self.number_of_connections,
18
- class: self.class,
19
- fare_basis: self.fare_basis
20
- }
21
- KondutoUtils.remove_nil_keys_from_hash(hash)
22
- end
23
-
24
- def to_json
25
- self.to_hash.to_json
26
- end
27
6
  end
@@ -0,0 +1,5 @@
1
+ class KondutoTravelLeg < KondutoBase
2
+ attributes :number_of_connections, :fare_basis
3
+ attribute date: :date
4
+ attribute klass: :symbol
5
+ end
@@ -0,0 +1,15 @@
1
+ module ClassMethods
2
+ def validates_presence_of(*attributes)
3
+ self.send(:define_method, :required_attr) do
4
+ attributes
5
+ end
6
+ end
7
+
8
+ def validates(*attributes, &block)
9
+ if block_given?
10
+ self.send(:define_method, :custom_validations) do
11
+ { Proc.new(&block) => attributes }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'class_methods'
2
+
3
+ module Konduto
4
+ module Validations
5
+ def self.included(base)
6
+ base.class_eval do
7
+ extend ClassMethods
8
+ end
9
+ end
10
+
11
+ def valid?
12
+ if respond_to? :required_attr
13
+ required_attr.each do |attr|
14
+ return false if send(attr).nil?
15
+ return false if send(attr).is_a?(Array) && send(attr).empty?
16
+ end
17
+ end
18
+
19
+ if respond_to? :custom_validations
20
+ custom_validations.each do |block, attrs|
21
+ attrs.each { |attr| return false unless block.call(send(attr)) } if attrs.is_a? Array
22
+ end
23
+ end
24
+
25
+ true
26
+ end
27
+ end
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konduto-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Custodio
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-17 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2016-06-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: factory_girl
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.7'
14
28
  description: A wrapper for konduto antifraud API
15
29
  email:
16
30
  - gcmartins93@gmail.com jonathancardosodecampos@gmail.com
@@ -18,14 +32,20 @@ executables: []
18
32
  extensions: []
19
33
  extra_rdoc_files: []
20
34
  files:
35
+ - README.md
21
36
  - lib/konduto-ruby.rb
37
+ - lib/konduto-ruby/associations/associations.rb
38
+ - lib/konduto-ruby/attributes.rb
22
39
  - lib/konduto-ruby/konduto_address.rb
40
+ - lib/konduto-ruby/konduto_base.rb
41
+ - lib/konduto-ruby/konduto_bus_leg.rb
23
42
  - lib/konduto-ruby/konduto_customer.rb
24
43
  - lib/konduto-ruby/konduto_device.rb
44
+ - lib/konduto-ruby/konduto_flight_leg.rb
25
45
  - lib/konduto-ruby/konduto_geolocation.rb
26
46
  - lib/konduto-ruby/konduto_item.rb
27
47
  - lib/konduto-ruby/konduto_loyalty.rb
28
- - lib/konduto-ruby/konduto_navigation.rb
48
+ - lib/konduto-ruby/konduto_navigation_info.rb
29
49
  - lib/konduto-ruby/konduto_order.rb
30
50
  - lib/konduto-ruby/konduto_order_status.rb
31
51
  - lib/konduto-ruby/konduto_passenger.rb
@@ -33,7 +53,10 @@ files:
33
53
  - lib/konduto-ruby/konduto_seller.rb
34
54
  - lib/konduto-ruby/konduto_travel.rb
35
55
  - lib/konduto-ruby/konduto_travel_info.rb
56
+ - lib/konduto-ruby/konduto_travel_leg.rb
36
57
  - lib/konduto-ruby/konduto_utils.rb
58
+ - lib/konduto-ruby/validations/class_methods.rb
59
+ - lib/konduto-ruby/validations/validations.rb
37
60
  homepage: https://github.com/jonathan0211/konduto-ruby
38
61
  licenses:
39
62
  - MIT
@@ -57,5 +80,6 @@ rubyforge_project:
57
80
  rubygems_version: 2.5.1
58
81
  signing_key:
59
82
  specification_version: 4
60
- summary: ''
83
+ summary: This project aims to help ruby users of konduto API by offering a easy to
84
+ use interface
61
85
  test_files: []
@@ -1,37 +0,0 @@
1
- class KondutoNavigation
2
- attr_accessor :session_time, :referrer, :time_site_1d, :new_accounts_1d, :password_resets_1d, :sales_declined_1d,\
3
- :sessions_1d, :time_since_last_sale, :time_site_7d, :time_per_page_7d, :new_accounts_7d, \
4
- :password_resets_7d, :checkout_count_7d, :sales_declined_7d, :sessions_7d
5
- def initialize(*args)
6
- unless args[0].nil?
7
- args[0].each do |k,v|
8
- instance_variable_set("@#{k}", v) unless v.nil?
9
- end
10
- end
11
- end
12
-
13
- def to_hash
14
- hash = {
15
- session_time: self.session_time,
16
- referrer: self.referrer,
17
- time_site_1d: self.time_site_1d,
18
- new_accounts_1d: self.new_accounts_1d,
19
- password_resets_1d: self.password_resets_1d,
20
- sales_declined_1d: self.sales_declined_1d,
21
- sessions_1d: self.sessions_1d,
22
- time_since_last_sale: self.time_since_last_sale,
23
- time_site_7d: self.time_site_7d,
24
- time_per_page_7d: self.time_per_page_7d,
25
- new_accounts_7d: self.new_accounts_7d,
26
- password_resets_7d: self.password_resets_7d,
27
- checkout_count_7d: self.checkout_count_7d,
28
- sales_declined_7d: self.sales_declined_7d,
29
- sessions_7d: self.sessions_7d,
30
- }
31
- KondutoUtils.remove_nil_keys_from_hash(hash)
32
- end
33
-
34
- def to_json
35
- self.to_hash.to_json
36
- end
37
- end