Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Github workflow gem develop

on: [push]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@477b21f02be01bcb8030d50f37cfec92bfa615b6
with:
ruby-version: 2.7
- run: bundle install
- name: Rubocop
run: rubocop
test:
needs: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@477b21f02be01bcb8030d50f37cfec92bfa615b6
with:
ruby-version: 2.7
- name: Bundle install
run: bundle install
- name: Gem build
run: gem build
- name: Gem install
run: gem install konduto-ruby --local konduto-ruby-*.gem
- name: Run Tests
run: rake test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea/
*/**/coverage/
/coverage/
*.gem

25 changes: 25 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require: rubocop-minitest

AllCops:
SuggestExtensions: false

Naming/VariableName:
Enabled: false
Metrics/AbcSize:
Enabled: false
Metrics/MethodLength:
Enabled: false
Metrics/BlockLength:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
Style/Documentation:
Enabled: false
Style/GuardClause:
Enabled: false
Naming/FileName:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Naming/PredicateName:
Enabled: false
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in konduto-ruby.gemspec
gemspec
68 changes: 68 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
PATH
remote: .
specs:
konduto-ruby (2.2.0)

GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.3.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ast (2.4.2)
concurrent-ruby (1.1.10)
docile (1.4.0)
factory_girl (4.9.0)
activesupport (>= 3.0.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
json (2.3.0)
minitest (5.16.3)
parallel (1.22.1)
parser (3.1.2.1)
ast (~> 2.4.1)
rainbow (3.1.1)
rake (12.3.3)
regexp_parser (2.5.0)
rexml (3.2.5)
rubocop (1.35.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.1.2.1)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.20.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
rubocop-minitest (0.21.0)
rubocop (>= 0.90, < 2.0)
ruby-progressbar (1.11.0)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
unicode-display_width (2.2.0)

PLATFORMS
ruby

DEPENDENCIES
factory_girl (~> 4.9)
konduto-ruby!
minitest (~> 5.16.3)
rake (~> 12.3.3)
rubocop (~> 1.35.0)
rubocop-minitest
simplecov

BUNDLED WITH
2.1.2
429 changes: 328 additions & 101 deletions README.md

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rake/testtask'

Rake::TestTask.new do |t|
Expand All @@ -6,5 +8,5 @@ Rake::TestTask.new do |t|
t.verbose = true
end

desc "Run tests"
desc 'Run tests'
task default: :test
15 changes: 15 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'konduto-ruby'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require 'irb'
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
6 changes: 6 additions & 0 deletions build_and_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
rvm use ruby-2.7.0@konduto-ruby
gem uninstall konduto-ruby
bundle install
gem build
gem install konduto-ruby --local konduto-ruby-2.2.0.gem
20 changes: 15 additions & 5 deletions konduto-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
# frozen_string_literal: true

require_relative 'lib/konduto-ruby'

Gem::Specification.new do |spec|
spec.name = 'konduto-ruby'
spec.version = '2.1.2'
spec.version = KondutoRuby::VERSION
spec.authors = ['Gabriel Custodio', 'Jonathan Cardoso de Campos', 'Konduto Development Team']
spec.date = Time.now.utc.strftime('%Y-%m-%d')
spec.email = [%(gcmartins93@gmail.com jonathancardosodecampos@gmail.com support@konduto.com)]
spec.homepage = 'https://github.com/konduto/konduto-ruby'
spec.summary = ''
spec.description = 'A wrapper for konduto antifraud API'

spec.files = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.md"]
spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
spec.files = Dir['{lib}/**/*.rb', 'LICENSE', '*.md']
spec.require_path = 'lib'

spec.license = 'MIT'

spec.add_development_dependency 'factory_girl', '~> 4.7'
# tests
spec.add_development_dependency 'factory_girl', '~> 4.9'
spec.add_development_dependency 'minitest', '~> 5.16.3'
spec.add_development_dependency 'rake', '~> 12.3.3'
spec.add_development_dependency 'rubocop', '~> 1.35.0'
spec.add_development_dependency 'rubocop-minitest'
spec.add_development_dependency 'simplecov'
end
68 changes: 37 additions & 31 deletions lib/konduto-ruby.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

require 'json'
require 'date'
require 'konduto-ruby/konduto_base'
require 'konduto-ruby/konduto_travel_leg'
require_relative 'konduto-ruby/konduto_base'
require_relative 'konduto-ruby/konduto_travel_leg'

Dir[__dir__ + '/konduto-ruby/*.rb'].each { |file| require file }
Dir["#{__dir__}/konduto-ruby/*.rb"].sort.each { |file| require file }

class KondutoRuby
require 'uri'
Expand All @@ -12,93 +14,97 @@ class KondutoRuby
attr_accessor :request_body, :response_body, :endpoint
attr_reader :api_key

def initialize api_key, endpoint = 'https://api.konduto.com/v1'
VERSION = '2.2.0'

def initialize(api_key, endpoint = 'https://api.konduto.com/v1')
@endpoint = URI.parse(endpoint)
@http_client = Net::HTTP.new(@endpoint.host, @endpoint.port)
@api_key = api_key
end

def api_key= api_key
def api_key=(api_key)
if api_key.length == 21
@api_key = api_key
else
raise ArgumentError.new "Invalid key: #{api_key}! API key length must be of 21"
raise ArgumentError, "Invalid key: #{api_key}! API key length must be of 21"
end
end

def proxy
return @proxy if @proxy
URI.parse(ENV['http_proxy']) rescue nil

begin
URI.parse(ENV['http_proxy'])
rescue StandardError
nil
end
end

def proxy= proxy_url
def proxy=(proxy_url)
@proxy = URI.parse(proxy_url)
end

def order_url order_id=''
def order_url(order_id = '')
url = "#{@endpoint}/orders"
url += "/#{order_id}" unless order_id == ''
URI.parse(url)
end

def send_request http_method, request_body=''
def send_request(http_method, request_body = '')
headers = {
'Authorization' => "Basic #{Base64.encode64(@api_key)}",
'Content-Type' =>'application/json',
'Content-Type' => 'application/json',
'Referer' => @endpoint.path
}
http_method.initialize_http_header headers
http_method.body = request_body
if proxy
http = Net::HTTP::Proxy(proxy.host, proxy.port).new(@endpoint.host, @endpoint.port)
else
http = Net::HTTP.new(@endpoint.host, @endpoint.port)
end
http = if proxy
Net::HTTP::Proxy(proxy.host, proxy.port).new(@endpoint.host, @endpoint.port)
else
Net::HTTP.new(@endpoint.host, @endpoint.port)
end
http.use_ssl = true
response = http.request(http_method)

response
http.request(http_method)
end

def get_order order_id
def get_order(order_id)
get = Net::HTTP::Get.new(order_url(order_id))
response = send_request(get)

if response.kind_of? Net::HTTPSuccess
if response.is_a? Net::HTTPSuccess
order = KondutoOrder.new JSON.parse(response.entity)['order']
order.id = order_id unless order.id
return order
order
else
raise (JSON.parse(response.body)['message']).to_s
end


end

def analyze order
def analyze(order)
post = Net::HTTP::Post.new(order_url)
response = send_request(post, order.to_json)

if response.kind_of? Net::HTTPSuccess
return KondutoOrder.new JSON.parse(response.entity)['order']
if response.is_a? Net::HTTPSuccess
KondutoOrder.new JSON.parse(response.entity)['order']
else
raise (JSON.parse(response.body)['message']).to_s
end
end

def update_order_status order, new_status, comments
def update_order_status(order, new_status, comments)
raise ArgumentError("Illegal status #{new_status}") unless KondutoOrderStatus.allowed_status.include? new_status
raise ArgumentError("Commets can't be nil") unless comments

put = Net::HTTP::Put.new(order_url(order.id))
body = {status: new_status.downcase, comments: comments}.to_json
body = { status: new_status.downcase, comments: comments }.to_json
response = send_request(put, body)

if response.kind_of? Net::HTTPSuccess
if response.is_a? Net::HTTPSuccess
resposta = JSON.parse(response.entity)['order']
raise 'Update order status can\'t be done' if resposta['old_status'].nil? && resposta['new_status'].nil?

order.status = resposta['new_status']
return order
order
else
raise (JSON.parse(response.body)['message']).to_s
end
Expand Down
Loading