kobana 0.1.1 → 0.2.0
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.
- checksums.yaml +4 -4
- data/README.md +30 -33
- data/Rakefile +7 -1
- data/lib/kobana/configuration.rb +2 -2
- data/lib/kobana/resources/admin/subaccount.rb +11 -0
- data/lib/kobana/resources/base.rb +103 -0
- data/lib/kobana/resources/charge/bank_billet.rb +13 -11
- data/lib/kobana/resources/charge/pix.rb +1 -7
- data/lib/kobana/resources/connection.rb +92 -0
- data/lib/kobana/resources/financial/account.rb +10 -0
- data/lib/kobana/resources/financial/account_balance.rb +11 -0
- data/lib/kobana/resources/financial/statement_transactions_import.rb +24 -0
- data/lib/kobana/resources/operations.rb +136 -0
- data/lib/kobana/version.rb +1 -1
- data/lib/kobana.rb +17 -2
- data/lib/support/hash.rb +21 -0
- data/lib/support/string.rb +21 -0
- data/lib/tasks/development.rake +13 -0
- metadata +31 -9
- data/lib/kobana/base.rb +0 -61
- data/lib/kobana/resources/resource_operations.rb +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e0b0639fe29a20f6f650718a0669a52a4a0ec26be7b4dfb2d3aed802d23de1d
|
4
|
+
data.tar.gz: a000e4c09633901fd7a379d4ba91251864bcfec4c78914b31a9c72fd28993f1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0194334668913ae15ec91ff9473e25407343094d405a81c71fd1651c69dacbc9af920a326f1b02ef628f247f8c7ec7b4222ec650a89df6b756dd8c339bf12d29'
|
7
|
+
data.tar.gz: 153fc2b9af819e1881140d25935f9b939de25a2a0c76edfe66fc73e8919825a79d845eccfcae4de756585e7a6dab23d0e338a93075a4836e5de6b4e2a8887d1d
|
data/README.md
CHANGED
@@ -40,32 +40,23 @@ Configure your API key by creating an initializer in your Rails project:
|
|
40
40
|
|
41
41
|
`config/initializers/kobana.rb`
|
42
42
|
|
43
|
-
```ruby
|
44
|
-
Kobana.api_key = 'YOUR_API_KEY_HERE'
|
45
|
-
```
|
46
|
-
|
47
|
-
Replace `'YOUR_API_KEY_HERE'` with your actual API key.
|
48
|
-
|
49
|
-
### Usage
|
50
|
-
|
51
|
-
To use this gem:
|
52
|
-
|
53
|
-
#### **Configuration**
|
54
|
-
|
55
43
|
```ruby
|
56
44
|
Kobana.configure do |config|
|
57
|
-
config.api_token = '
|
45
|
+
config.api_token = 'YOUR_API_TOKEN'
|
58
46
|
config.environment = :sandbox # you can specify the environment as :development, :sandbox, or :production
|
59
|
-
config.api_version = :v1 #or :v2
|
60
47
|
end
|
61
48
|
```
|
62
49
|
|
50
|
+
Replace `'YOUR_API_TOKEN'` with your actual API key from the corresponding environment.
|
51
|
+
|
52
|
+
### Usage
|
53
|
+
|
63
54
|
#### **Charges**
|
64
55
|
|
65
56
|
##### Creating a Charge
|
66
57
|
|
67
58
|
```ruby
|
68
|
-
|
59
|
+
attributes = {
|
69
60
|
'amount' => 100.50,
|
70
61
|
'payer' => {
|
71
62
|
'document_number' => '1234567890',
|
@@ -80,26 +71,35 @@ charge_data = {
|
|
80
71
|
'custom_data' => '{"order_id": "12345"}'
|
81
72
|
}
|
82
73
|
|
83
|
-
|
84
|
-
|
85
|
-
|
74
|
+
pix = Kobana::Resources::Charge::Pix.create(attributes)
|
75
|
+
pix.id # 1
|
76
|
+
pix.new_record? false
|
77
|
+
pix.created? # true
|
78
|
+
pix.attributes # {}
|
79
|
+
|
80
|
+
|
81
|
+
pix = Kobana::Resources::Charge::Pix.new(attributes)
|
82
|
+
pix.id nil
|
83
|
+
pix.new_record? # true
|
84
|
+
pix.created? # false
|
85
|
+
pix.attributes # {}
|
86
|
+
|
87
|
+
pix.save
|
86
88
|
```
|
87
89
|
|
88
90
|
##### Fetching a Charge
|
89
91
|
|
90
92
|
```ruby
|
91
93
|
charge_id = 1 # Replace with your charge ID
|
92
|
-
|
93
|
-
|
94
|
-
puts result
|
94
|
+
pix = Kobana::Resources::Charge::Pix.find(charge_id)
|
95
|
+
puts pix
|
95
96
|
```
|
96
97
|
|
97
98
|
##### Listing All Charges
|
98
99
|
|
99
100
|
```ruby
|
100
|
-
charge = Kobana::Resources::Charge::Pix.new
|
101
101
|
params = { status: ["opened", "overdue"], page: 2 }
|
102
|
-
results =
|
102
|
+
results = Kobana::Resources::Charge::Pix.all(params)
|
103
103
|
puts result
|
104
104
|
```
|
105
105
|
|
@@ -108,26 +108,23 @@ puts result
|
|
108
108
|
##### Creating a Bank Billet
|
109
109
|
|
110
110
|
```ruby
|
111
|
-
|
111
|
+
attributes = { ... }
|
112
112
|
|
113
|
-
bank_billet = Kobana::Resources::BankBillet::BankBillet.
|
114
|
-
|
115
|
-
puts result
|
113
|
+
bank_billet = Kobana::Resources::BankBillet::BankBillet.create(attributes)
|
114
|
+
puts bank_billet
|
116
115
|
```
|
117
116
|
|
118
117
|
##### Fetching a Bank Billet
|
119
118
|
|
120
119
|
```ruby
|
121
120
|
bank_billet_id = 1 # Replace with your charge ID
|
122
|
-
bank_billet = Kobana::Resources::BankBillet::BankBillet.
|
123
|
-
|
124
|
-
puts result
|
121
|
+
bank_billet = Kobana::Resources::BankBillet::BankBillet.find(bank_billet_id)
|
122
|
+
puts bank_billet
|
125
123
|
```
|
126
124
|
|
127
125
|
##### Listing All Bank Billets
|
128
126
|
|
129
127
|
```ruby
|
130
|
-
|
131
|
-
|
132
|
-
puts result
|
128
|
+
bank_billets = Kobana::Resources::BankBillet::BankBillet.all
|
129
|
+
puts bank_billets
|
133
130
|
```
|
data/Rakefile
CHANGED
@@ -1,12 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "bundler"
|
4
|
+
Bundler.setup(:development)
|
5
|
+
|
6
|
+
require "dotenv/load"
|
7
|
+
|
3
8
|
require "bundler/gem_tasks"
|
4
9
|
require "rspec/core/rake_task"
|
5
10
|
|
6
11
|
RSpec::Core::RakeTask.new(:spec)
|
7
12
|
|
8
13
|
require "rubocop/rake_task"
|
9
|
-
|
10
14
|
RuboCop::RakeTask.new
|
11
15
|
|
16
|
+
Dir.glob("lib/tasks/*.rake").each { |r| load r }
|
17
|
+
|
12
18
|
task default: %i[spec rubocop]
|
data/lib/kobana/configuration.rb
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
module Kobana
|
4
4
|
class Configuration
|
5
|
-
attr_accessor :api_token, :environment, :custom_headers, :
|
5
|
+
attr_accessor :api_token, :environment, :custom_headers, :debug
|
6
6
|
|
7
7
|
def initialize
|
8
8
|
@custom_headers = {}
|
9
|
-
@environment = :
|
9
|
+
@environment = :sandbox
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "faraday"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module Kobana
|
7
|
+
module Resources
|
8
|
+
class Base
|
9
|
+
include Connection
|
10
|
+
include Operations
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :primary_key, :api_version, :resource_endpoint, :errors, :default_attributes
|
14
|
+
|
15
|
+
def inherited(subclass)
|
16
|
+
super
|
17
|
+
subclass.resource_endpoint ||= infer_resource_endpoint(subclass)
|
18
|
+
subclass.primary_key ||= :uid
|
19
|
+
subclass.api_version ||= :v2
|
20
|
+
subclass.errors ||= []
|
21
|
+
subclass.default_attributes ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def infer_resource_endpoint(klass)
|
25
|
+
return resource_endpoint if resource_endpoint
|
26
|
+
|
27
|
+
return unless klass.name =~ /Kobana::Resources::(.*)$/
|
28
|
+
|
29
|
+
::Regexp.last_match(1).underscore.pluralize
|
30
|
+
end
|
31
|
+
|
32
|
+
def uri(attributes = {})
|
33
|
+
"#{base_url}/#{interpolate(resource_endpoint, default_attributes.merge(attributes))}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def interpolate(template, attributes)
|
37
|
+
template.gsub(/\{([^\}]+)\}/) do
|
38
|
+
key = Regexp.last_match(1)
|
39
|
+
begin
|
40
|
+
attributes[key.to_sym].to_s
|
41
|
+
rescue NameError
|
42
|
+
"{#{key}}" # Keep original if variable is undefined
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
attr_accessor :attributes, :errors
|
49
|
+
|
50
|
+
def initialize(attributes = {})
|
51
|
+
@attributes = attributes.deep_symbolize_keys
|
52
|
+
@errors = []
|
53
|
+
end
|
54
|
+
|
55
|
+
def [](key)
|
56
|
+
attributes[key.to_sym]
|
57
|
+
end
|
58
|
+
|
59
|
+
def method_missing(key, *args, &)
|
60
|
+
if key.to_s.end_with?("=")
|
61
|
+
key = key.to_s.chomp("=").to_sym
|
62
|
+
attributes[key] = args.first
|
63
|
+
else
|
64
|
+
return unless attributes.key?(key.to_sym)
|
65
|
+
|
66
|
+
attributes[key]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def respond_to_missing?(key, include_private = false)
|
71
|
+
attributes.key?(key.to_sym) || super
|
72
|
+
end
|
73
|
+
|
74
|
+
def uri
|
75
|
+
"#{self.class.uri(attributes)}/#{attributes[self.class.primary_key]}"
|
76
|
+
end
|
77
|
+
|
78
|
+
def request(*)
|
79
|
+
self.class.request(*)
|
80
|
+
end
|
81
|
+
|
82
|
+
def valid?
|
83
|
+
errors.empty?
|
84
|
+
end
|
85
|
+
|
86
|
+
def created?
|
87
|
+
attributes[:created] || false
|
88
|
+
end
|
89
|
+
|
90
|
+
def updated?
|
91
|
+
attributes[:updated] || false
|
92
|
+
end
|
93
|
+
|
94
|
+
def new_record?
|
95
|
+
primary_key.nil? || primary_key.to_s.empty?
|
96
|
+
end
|
97
|
+
|
98
|
+
def primary_key
|
99
|
+
attributes[self.class.primary_key]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -4,19 +4,21 @@ module Kobana
|
|
4
4
|
module Resources
|
5
5
|
module Charge
|
6
6
|
class BankBillet < Base
|
7
|
-
|
7
|
+
self.primary_key = :id
|
8
|
+
self.api_version = :v1
|
9
|
+
self.resource_endpoint = "bank_billets"
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
{ status: response.status, data: {} }
|
11
|
+
# rubocop:disable Naming/PredicateMethod
|
12
|
+
def cancel
|
13
|
+
response = request(:put, "#{uri}/cancel")
|
14
|
+
case response[:status]
|
15
|
+
when 204
|
16
|
+
true
|
17
|
+
else
|
18
|
+
false
|
19
|
+
end
|
19
20
|
end
|
21
|
+
# rubocop:enable Naming/PredicateMethod
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
@@ -4,13 +4,7 @@ module Kobana
|
|
4
4
|
module Resources
|
5
5
|
module Charge
|
6
6
|
class Pix < Base
|
7
|
-
|
8
|
-
|
9
|
-
@resource_endpoint = "charge/pix"
|
10
|
-
|
11
|
-
class << self
|
12
|
-
attr_reader :resource_endpoint
|
13
|
-
end
|
7
|
+
self.resource_endpoint = "charge/pix"
|
14
8
|
end
|
15
9
|
end
|
16
10
|
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kobana
|
4
|
+
module Resources
|
5
|
+
module Connection
|
6
|
+
BASE_URI = {
|
7
|
+
v2: {
|
8
|
+
sandbox: "https://api-sandbox.kobana.com.br/v2",
|
9
|
+
production: "https://api.kobana.com.br/v2",
|
10
|
+
development: "http://localhost:5000/api/v2"
|
11
|
+
},
|
12
|
+
v1: {
|
13
|
+
sandbox: "https://api-sandbox.kobana.com.br/v1",
|
14
|
+
production: "https://api.kobana.com.br/v1",
|
15
|
+
development: "http://localhost:5000/api/v1"
|
16
|
+
}
|
17
|
+
}.freeze
|
18
|
+
|
19
|
+
def self.included(base)
|
20
|
+
base.extend(ClassMethods)
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def headers
|
25
|
+
{
|
26
|
+
"Authorization" => "Bearer #{Kobana.configuration.api_token}",
|
27
|
+
"Content-Type" => "application/json"
|
28
|
+
}.merge(Kobana.configuration.custom_headers)
|
29
|
+
end
|
30
|
+
|
31
|
+
def connection
|
32
|
+
Faraday.new(url: base_url) do |faraday|
|
33
|
+
faraday.request :url_encoded
|
34
|
+
faraday.request :json
|
35
|
+
faraday.adapter Faraday.default_adapter
|
36
|
+
faraday.headers = headers
|
37
|
+
faraday.response :logger, logger if Kobana.configuration.debug
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def multipart_connection
|
42
|
+
Faraday.new(url: base_url) do |faraday|
|
43
|
+
faraday.request :multipart
|
44
|
+
faraday.request :url_encoded
|
45
|
+
faraday.adapter :net_http
|
46
|
+
faraday.headers = headers.merge(
|
47
|
+
"Content-Type" => "multipart/form-data"
|
48
|
+
)
|
49
|
+
faraday.response :logger, logger if Kobana.configuration.debug
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def logger
|
54
|
+
logger = Logger.new($stdout)
|
55
|
+
logger.formatter = proc do |severity, datetime, _progname, msg|
|
56
|
+
redacted_msg = msg.gsub(/(Bearer|Token)\s+[A-Za-z0-9\-_\.]+/, '\1 [REDACTED]')
|
57
|
+
"#{severity} #{datetime}: #{redacted_msg}\n"
|
58
|
+
end
|
59
|
+
logger
|
60
|
+
end
|
61
|
+
|
62
|
+
def request(method, url, params_or_body = nil, options = {})
|
63
|
+
self.errors = []
|
64
|
+
response = if options[:multipart]
|
65
|
+
multipart_connection.send(method, url, params_or_body)
|
66
|
+
else
|
67
|
+
connection.send(method, url, params_or_body)
|
68
|
+
end
|
69
|
+
parse_response(response)
|
70
|
+
end
|
71
|
+
|
72
|
+
def parse_response(response)
|
73
|
+
body_parsed = begin
|
74
|
+
JSON.parse(response.body, symbolize_names: true)
|
75
|
+
rescue JSON::ParserError
|
76
|
+
response.body
|
77
|
+
end
|
78
|
+
|
79
|
+
if body_parsed.is_a?(String) || body_parsed.is_a?(Array) || !body_parsed.key?(:data)
|
80
|
+
{ status: response.status, data: body_parsed }
|
81
|
+
else
|
82
|
+
body_parsed
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def base_url
|
87
|
+
@base_url ||= BASE_URI[api_version&.to_sym][Kobana.configuration.environment&.to_sym]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kobana
|
4
|
+
module Resources
|
5
|
+
module Financial
|
6
|
+
class StatementTransactionsImport < Base
|
7
|
+
self.resource_endpoint = "financial/accounts/{financial_account_uid}/statement_transactions/imports"
|
8
|
+
|
9
|
+
def self.create(attributes = {})
|
10
|
+
require "faraday/multipart"
|
11
|
+
|
12
|
+
if attributes[:source].is_a?(String) && File.exist?(attributes[:source])
|
13
|
+
attributes[:source] = Faraday::UploadIO.new(
|
14
|
+
attributes[:source],
|
15
|
+
attributes.delete(:source_mime_type) || "application/octet-stream",
|
16
|
+
attributes.delete(:source_filename) || File.basename(attributes[:source])
|
17
|
+
)
|
18
|
+
end
|
19
|
+
super(attributes, multipart: true)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kobana
|
4
|
+
module Resources
|
5
|
+
module Operations
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def all(params = {})
|
12
|
+
response = request(:get, uri(params), params)
|
13
|
+
case response[:status]
|
14
|
+
when 200
|
15
|
+
response[:data].map { |data| new(data) }
|
16
|
+
else
|
17
|
+
handle_error_response(response)
|
18
|
+
[]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def find_by(params = {}, options = {})
|
23
|
+
result = all(params)
|
24
|
+
return nil if result.empty? || (result.size > 1 && options[:ignore_multiple])
|
25
|
+
|
26
|
+
match = params.all? do |key, value|
|
27
|
+
result.first[key] == value
|
28
|
+
end
|
29
|
+
return unless match
|
30
|
+
|
31
|
+
result.first
|
32
|
+
end
|
33
|
+
|
34
|
+
def create(attributes = {}, options = {})
|
35
|
+
response = request(:post, uri(attributes), attributes, options)
|
36
|
+
case response[:status]
|
37
|
+
when 201
|
38
|
+
new(response[:data].merge(created: true))
|
39
|
+
else
|
40
|
+
handle_error_response(response)
|
41
|
+
resource = new(attributes.merge(created: false))
|
42
|
+
resource.errors = @errors
|
43
|
+
resource
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def find(resource_id, params = {})
|
48
|
+
response = request(:get, "#{uri(params)}/#{resource_id}", params)
|
49
|
+
case response[:status]
|
50
|
+
when 200
|
51
|
+
new(response[:data])
|
52
|
+
else
|
53
|
+
handle_error_response(response)
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def find_or_create_by(params, attributes = {}, options = {})
|
59
|
+
options = { ignore_multiple: false, find_by_id: false, find_params: nil }.merge(options)
|
60
|
+
if options[:find_by_id]
|
61
|
+
find(params, options[:find_params]) || create(attributes)
|
62
|
+
else
|
63
|
+
find_by(params, options) || create(attributes.merge(params.deep_symbolize_keys))
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def handle_error_response(response)
|
68
|
+
return unless response[:data].is_a?(Hash)
|
69
|
+
|
70
|
+
@errors = response[:data][:errors] if response[:data].key?(:errors)
|
71
|
+
@errors = [title: response[:data][:error]] if response[:data].key?(:error)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def update(new_attributes = {})
|
76
|
+
return if new_attributes.empty?
|
77
|
+
|
78
|
+
data = attributes.merge(new_attributes.deep_symbolize_keys)
|
79
|
+
response = request(:put, uri, data.to_json)
|
80
|
+
case response[:status]
|
81
|
+
when 200..204
|
82
|
+
new(response[:data].merge(updated: true))
|
83
|
+
else
|
84
|
+
handle_error_response(response)
|
85
|
+
resource = new(attributes.merge(updated: false))
|
86
|
+
resource.errors = @errors
|
87
|
+
resource
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# rubocop:disable Naming/PredicateMethod
|
92
|
+
def delete
|
93
|
+
response = request(:delete, uri)
|
94
|
+
case response[:status]
|
95
|
+
when 204
|
96
|
+
true
|
97
|
+
else
|
98
|
+
handle_error_response(response)
|
99
|
+
false
|
100
|
+
end
|
101
|
+
end
|
102
|
+
# rubocop:enable Naming/PredicateMethod
|
103
|
+
|
104
|
+
def list_commands(params = {})
|
105
|
+
response = request(:get, "#{uri}/commands", params)
|
106
|
+
case response[:status]
|
107
|
+
when 200
|
108
|
+
# response[:data].map { |command| Kobana::Resources::Command.new(command) }
|
109
|
+
response[:data].map { |command| command }
|
110
|
+
else
|
111
|
+
handle_error_response(response)
|
112
|
+
[]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def find_command(command_id)
|
117
|
+
raise ArgumentError, "Command ID cannot be nil" if command_id.nil?
|
118
|
+
|
119
|
+
response = request(:get, "#{uri}/commands/#{command_id}")
|
120
|
+
case response[:status]
|
121
|
+
when 200
|
122
|
+
# response[:data].map { |command| Kobana::Resources::Command.new(command) }
|
123
|
+
response[:data].map { |command| command }
|
124
|
+
else
|
125
|
+
handle_error_response(response)
|
126
|
+
nil
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def handle_error_response(response)
|
131
|
+
self.class.handle_error_response(response)
|
132
|
+
@errors = self.class.errors
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
data/lib/kobana/version.rb
CHANGED
data/lib/kobana.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
$LOAD_PATH.unshift(File.expand_path(".", __dir__))
|
4
|
+
|
3
5
|
require "faraday"
|
4
6
|
require "json"
|
7
|
+
require "support/string"
|
8
|
+
require "support/hash"
|
5
9
|
require "kobana/configuration"
|
6
10
|
|
7
11
|
module Kobana
|
@@ -17,14 +21,25 @@ module Kobana
|
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
20
|
-
autoload :Base, "kobana/base"
|
21
24
|
autoload :Version, "kobana/version"
|
22
25
|
|
23
26
|
module Resources
|
24
|
-
autoload :
|
27
|
+
autoload :Base, "kobana/resources/base"
|
28
|
+
autoload :Connection, "kobana/resources/connection"
|
29
|
+
autoload :Operations, "kobana/resources/operations"
|
25
30
|
module Charge
|
26
31
|
autoload :Pix, "kobana/resources/charge/pix"
|
27
32
|
autoload :BankBillet, "kobana/resources/charge/bank_billet"
|
28
33
|
end
|
34
|
+
|
35
|
+
module Financial
|
36
|
+
autoload :Account, "kobana/resources/financial/account"
|
37
|
+
autoload :AccountBalance, "kobana/resources/financial/account_balance"
|
38
|
+
autoload :StatementTransactionsImport, "kobana/resources/financial/statement_transactions_import"
|
39
|
+
end
|
40
|
+
|
41
|
+
module Admin
|
42
|
+
autoload :Subaccount, "kobana/resources/admin/subaccount"
|
43
|
+
end
|
29
44
|
end
|
30
45
|
end
|
data/lib/support/hash.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
def deep_symbolize_keys
|
5
|
+
result = {}
|
6
|
+
each do |key, value|
|
7
|
+
sym_key = key.respond_to?(:to_sym) ? key.to_sym : key
|
8
|
+
result[sym_key] =
|
9
|
+
case value
|
10
|
+
when Hash
|
11
|
+
value.deep_symbolize_keys
|
12
|
+
when Array
|
13
|
+
value.map { |v| v.is_a?(Hash) ? v.deep_symbolize_keys : v }
|
14
|
+
else
|
15
|
+
value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
result
|
19
|
+
end
|
20
|
+
# rubocop:enable Metrics/MethodLength
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class String
|
4
|
+
def pluralize
|
5
|
+
if end_with?("y") && !%w[a e i o u].include?(self[-2].downcase)
|
6
|
+
"#{self[0..-2]}ies"
|
7
|
+
elsif end_with?("s", "sh", "ch", "x", "z")
|
8
|
+
"#{self}es"
|
9
|
+
else
|
10
|
+
"#{self}s"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def underscore
|
15
|
+
word = gsub("::", "/")
|
16
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
17
|
+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
18
|
+
word.tr!("-", "_")
|
19
|
+
word.downcase
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
desc "Start an interactive console"
|
4
|
+
task :console do
|
5
|
+
require "irb"
|
6
|
+
|
7
|
+
# Load the application environment if needed
|
8
|
+
require File.expand_path("../kobana.rb", __dir__)
|
9
|
+
require File.expand_path("../../spec/support/1password.rb", __dir__)
|
10
|
+
|
11
|
+
# Start the IRB session
|
12
|
+
IRB.start
|
13
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kobana
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kivanio Barbosa
|
8
|
-
|
8
|
+
- Rafael Lima
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -30,6 +30,20 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '3.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: faraday-multipart
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
33
47
|
- !ruby/object:Gem::Dependency
|
34
48
|
name: json
|
35
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -47,6 +61,7 @@ dependencies:
|
|
47
61
|
description: Kobana API Client
|
48
62
|
email:
|
49
63
|
- kivanio@gmail.com
|
64
|
+
- rafael.lima@kobana.com.br
|
50
65
|
executables: []
|
51
66
|
extensions: []
|
52
67
|
extra_rdoc_files: []
|
@@ -56,14 +71,23 @@ files:
|
|
56
71
|
- README.md
|
57
72
|
- Rakefile
|
58
73
|
- lib/kobana.rb
|
59
|
-
- lib/kobana/base.rb
|
60
74
|
- lib/kobana/configuration.rb
|
75
|
+
- lib/kobana/resources/admin/subaccount.rb
|
76
|
+
- lib/kobana/resources/base.rb
|
61
77
|
- lib/kobana/resources/charge/bank_billet.rb
|
62
78
|
- lib/kobana/resources/charge/pix.rb
|
63
|
-
- lib/kobana/resources/
|
79
|
+
- lib/kobana/resources/connection.rb
|
80
|
+
- lib/kobana/resources/financial/account.rb
|
81
|
+
- lib/kobana/resources/financial/account_balance.rb
|
82
|
+
- lib/kobana/resources/financial/statement_transactions_import.rb
|
83
|
+
- lib/kobana/resources/operations.rb
|
64
84
|
- lib/kobana/version.rb
|
85
|
+
- lib/support/hash.rb
|
86
|
+
- lib/support/string.rb
|
87
|
+
- lib/tasks/development.rake
|
65
88
|
homepage: https://www.kobana.com.br/
|
66
|
-
licenses:
|
89
|
+
licenses:
|
90
|
+
- MIT
|
67
91
|
metadata:
|
68
92
|
homepage_uri: https://github.com/universokobana/kobana-ruby-client
|
69
93
|
changelog_uri: https://github.com/universokobana/kobana-ruby-client/releases
|
@@ -71,7 +95,6 @@ metadata:
|
|
71
95
|
bug_tracker_uri: https://github.com/universokobana/kobana-ruby-client/issues
|
72
96
|
documentation_uri: https://github.com/universokobana/kobana-ruby-client/wiki
|
73
97
|
rubygems_mfa_required: 'true'
|
74
|
-
post_install_message:
|
75
98
|
rdoc_options: []
|
76
99
|
require_paths:
|
77
100
|
- lib
|
@@ -86,8 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
109
|
- !ruby/object:Gem::Version
|
87
110
|
version: '0'
|
88
111
|
requirements: []
|
89
|
-
rubygems_version: 3.
|
90
|
-
signing_key:
|
112
|
+
rubygems_version: 3.6.1
|
91
113
|
specification_version: 4
|
92
114
|
summary: Kobana API Client
|
93
115
|
test_files: []
|
data/lib/kobana/base.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "faraday"
|
4
|
-
require "json"
|
5
|
-
|
6
|
-
module Kobana
|
7
|
-
class Base
|
8
|
-
attr_reader :base_url, :custom_headers
|
9
|
-
|
10
|
-
BASE_URI = {
|
11
|
-
v2: {
|
12
|
-
sandbox: "https://api-sandbox.kobana.com.br/v2",
|
13
|
-
production: "https://api.kobana.com.br/v2",
|
14
|
-
development: "http://localhost:5000/api/v2"
|
15
|
-
},
|
16
|
-
v1: {
|
17
|
-
sandbox: "https://api-sandbox.kobana.com.br/v1",
|
18
|
-
production: "https://api.kobana.com.br/v1",
|
19
|
-
development: "http://localhost:5000/api/v1"
|
20
|
-
}
|
21
|
-
}.freeze
|
22
|
-
|
23
|
-
def initialize
|
24
|
-
config = Kobana.configuration
|
25
|
-
|
26
|
-
@api_key = config.api_token
|
27
|
-
@base_url = BASE_URI[config.api_version][config.environment]
|
28
|
-
@custom_headers = config.custom_headers
|
29
|
-
@debug = config.debug
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def default_headers
|
35
|
-
{
|
36
|
-
"Authorization" => "Bearer #{@api_key}",
|
37
|
-
"Content-Type" => "application/json"
|
38
|
-
}
|
39
|
-
end
|
40
|
-
|
41
|
-
def connection
|
42
|
-
headers = default_headers.merge(@custom_headers)
|
43
|
-
Faraday.new(url: @base_url) do |faraday|
|
44
|
-
faraday.request :url_encoded
|
45
|
-
faraday.adapter Faraday.default_adapter
|
46
|
-
faraday.headers = headers
|
47
|
-
faraday.response :logger if @debug
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def parse_response(response)
|
52
|
-
body_parsed = JSON.parse(response.body, symbolize_names: true)
|
53
|
-
|
54
|
-
if body_parsed.is_a?(Array) || !body_parsed.key?(:data)
|
55
|
-
{ status: response.status, data: body_parsed }
|
56
|
-
else
|
57
|
-
body_parsed
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Kobana
|
4
|
-
module Resources
|
5
|
-
module ResourceOperations
|
6
|
-
def index(params = {})
|
7
|
-
url = "#{base_url}/#{endpoint}"
|
8
|
-
response = connection.get(url, params)
|
9
|
-
parse_response(response)
|
10
|
-
end
|
11
|
-
|
12
|
-
def create(data)
|
13
|
-
url = "#{base_url}/#{endpoint}"
|
14
|
-
response = connection.post(url, data.to_json)
|
15
|
-
parse_response(response)
|
16
|
-
end
|
17
|
-
|
18
|
-
def find(resource_id)
|
19
|
-
url = "#{base_url}/#{endpoint}/#{resource_id}"
|
20
|
-
response = connection.get(url)
|
21
|
-
parse_response(response)
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def endpoint
|
27
|
-
self.class.resource_endpoint
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|