starkinfra 0.3.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/brcodepreview/brcodepreview.rb +2 -5
- data/lib/cardmethod/cardmethod.rb +2 -2
- data/lib/creditholmes/creditholmes.rb +8 -11
- data/lib/creditnote/creditnote.rb +10 -11
- data/lib/creditnote/invoice/description.rb +3 -5
- data/lib/creditnote/invoice/discount.rb +3 -4
- data/lib/creditnote/invoice/invoice.rb +5 -6
- data/lib/creditnote/log.rb +8 -9
- data/lib/creditnote/transfer.rb +4 -5
- data/lib/creditpreview/creditnotepreview.rb +2 -3
- data/lib/creditpreview/creditpreview.rb +3 -4
- data/lib/creditsigner/creditsigner.rb +3 -6
- data/lib/dynamicbrcode/dynamicbrcode.rb +17 -19
- data/lib/error.rb +7 -40
- data/lib/event/attempt.rb +7 -8
- data/lib/event/event.rb +8 -11
- data/lib/individualdocument/individualdocument.rb +7 -8
- data/lib/individualdocument/log.rb +8 -9
- data/lib/individualidentity/individualidentity.rb +7 -8
- data/lib/individualidentity/log.rb +8 -9
- data/lib/issuingbalance/issuingbalance.rb +2 -3
- data/lib/issuingcard/issuingcard.rb +9 -10
- data/lib/issuingcard/log.rb +8 -9
- data/lib/issuingdesign/issuingdesign.rb +5 -6
- data/lib/issuingembossingkit/issuingembossingkit.rb +4 -5
- data/lib/issuingembossingrequest/issuingembossingrequest.rb +8 -9
- data/lib/issuingembossingrequest/log.rb +8 -9
- data/lib/issuingholder/issuingholder.rb +9 -10
- data/lib/issuingholder/log.rb +8 -9
- data/lib/issuinginvoice/issuinginvoice.rb +8 -9
- data/lib/issuinginvoice/log.rb +8 -9
- data/lib/issuingproduct/issuingproduct.rb +3 -4
- data/lib/issuingpurchase/issuingpurchase.rb +33 -22
- data/lib/issuingpurchase/log.rb +8 -9
- data/lib/issuingrestock/issuingrestock.rb +8 -9
- data/lib/issuingrestock/log.rb +8 -9
- data/lib/issuingrule/issuingrule.rb +6 -7
- data/lib/issuingstock/issuingstock.rb +8 -9
- data/lib/issuingstock/log.rb +8 -9
- data/lib/issuingtransaction/issuingtransaction.rb +7 -8
- data/lib/issuingwithdrawal/issuingwithdrawal.rb +8 -9
- data/lib/merchantcategory/merchantcategory.rb +2 -2
- data/lib/merchantcountry/merchantcountry.rb +2 -2
- data/lib/pixbalance/pixbalance.rb +2 -3
- data/lib/pixchargeback/log.rb +8 -9
- data/lib/pixchargeback/pixchargeback.rb +8 -9
- data/lib/pixclaim/log.rb +8 -9
- data/lib/pixclaim/pixclaim.rb +8 -9
- data/lib/pixdirector/pixdirector.rb +2 -2
- data/lib/pixdomain/certificate.rb +2 -2
- data/lib/pixdomain/pixdomain.rb +3 -3
- data/lib/pixinfraction/log.rb +8 -9
- data/lib/pixinfraction/pixinfraction.rb +8 -9
- data/lib/pixkey/log.rb +8 -9
- data/lib/pixkey/pixkey.rb +8 -9
- data/lib/pixrequest/log.rb +8 -9
- data/lib/pixrequest/pixrequest.rb +9 -10
- data/lib/pixreversal/log.rb +7 -9
- data/lib/pixreversal/pixreversal.rb +9 -10
- data/lib/pixstatement/pixstatement.rb +6 -7
- data/lib/request/request.rb +152 -0
- data/lib/starkinfra.rb +14 -6
- data/lib/staticbrcode/staticbrcode.rb +8 -9
- data/lib/utils/parse.rb +23 -42
- data/lib/utils/rest.rb +211 -110
- data/lib/webhook/webhook.rb +2 -3
- metadata +6 -21
- data/lib/key.rb +0 -33
- data/lib/user/organization.rb +0 -54
- data/lib/user/project.rb +0 -37
- data/lib/user/user.rb +0 -20
- data/lib/utils/api.rb +0 -89
- data/lib/utils/bacenid.rb +0 -19
- data/lib/utils/cache.rb +0 -10
- data/lib/utils/case.rb +0 -21
- data/lib/utils/checks.rb +0 -100
- data/lib/utils/endtoendid.rb +0 -11
- data/lib/utils/environment.rb +0 -14
- data/lib/utils/request.rb +0 -79
- data/lib/utils/resource.rb +0 -13
- data/lib/utils/returnid.rb +0 -11
- data/lib/utils/sub_resource.rb +0 -27
- data/lib/utils/url.rb +0 -28
data/lib/utils/api.rb
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative('case')
|
4
|
-
|
5
|
-
module StarkInfra
|
6
|
-
module Utils
|
7
|
-
module API
|
8
|
-
def self.build_entity_hash(entity)
|
9
|
-
if entity.is_a?(Hash)
|
10
|
-
entity_hash = entity
|
11
|
-
else
|
12
|
-
entity_hash = {}
|
13
|
-
entity.instance_variables.each do |key|
|
14
|
-
variable = entity.instance_variable_get(key)
|
15
|
-
entity_hash[key[1..-1]] = variable.is_a?(StarkInfra::Utils::Resource) ? build_entity_hash(variable) : entity.instance_variable_get(key)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
entity_hash
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.api_json(entity)
|
22
|
-
built_hash = build_entity_hash(entity)
|
23
|
-
cast_json_to_api_format(built_hash)
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.cast_json_to_api_format(hash)
|
27
|
-
entity_hash = {}
|
28
|
-
hash.each do |key, value|
|
29
|
-
next if value.nil?
|
30
|
-
|
31
|
-
entity_hash[StarkInfra::Utils::Case.snake_to_camel(key)] = parse_value(value)
|
32
|
-
end
|
33
|
-
entity_hash
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.parse_value(value)
|
37
|
-
return api_json(value) if value.is_a?(SubResource)
|
38
|
-
return value.strftime('%Y-%m-%d') if value.is_a?(Date)
|
39
|
-
return value.strftime('%Y-%m-%dT%H:%M:%S+00:00') if value.is_a?(DateTime) || value.is_a?(Time)
|
40
|
-
return cast_json_to_api_format(value) if value.is_a?(Hash)
|
41
|
-
return value unless value.is_a?(Array)
|
42
|
-
|
43
|
-
list = []
|
44
|
-
value.each do |v|
|
45
|
-
if v.is_a?(Hash)
|
46
|
-
list << cast_json_to_api_format(v)
|
47
|
-
next
|
48
|
-
end
|
49
|
-
if v.is_a?(SubResource)
|
50
|
-
list << api_json(v)
|
51
|
-
next
|
52
|
-
end
|
53
|
-
list << v
|
54
|
-
end
|
55
|
-
list
|
56
|
-
end
|
57
|
-
|
58
|
-
def self.from_api_json(resource_maker, json)
|
59
|
-
snakes = {}
|
60
|
-
json.each do |key, value|
|
61
|
-
snakes[StarkInfra::Utils::Case.camel_to_snake(key)] = value
|
62
|
-
end
|
63
|
-
|
64
|
-
resource_maker.call(snakes)
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.endpoint(resource_name)
|
68
|
-
kebab = StarkInfra::Utils::Case.camel_to_kebab(resource_name)
|
69
|
-
kebab.sub!('-log', '/log')
|
70
|
-
kebab.sub!('-attempt', '/attempt')
|
71
|
-
kebab
|
72
|
-
end
|
73
|
-
|
74
|
-
def self.last_name_plural(resource_name)
|
75
|
-
base = last_name(resource_name)
|
76
|
-
|
77
|
-
return base if base[-1].eql?('s')
|
78
|
-
return "#{base}s" if base[-2..-1].eql?('ey')
|
79
|
-
return "#{base[0...-1]}ies" if base[-1].eql?('y')
|
80
|
-
|
81
|
-
"#{base}s"
|
82
|
-
end
|
83
|
-
|
84
|
-
def self.last_name(resource_name)
|
85
|
-
StarkInfra::Utils::Case.camel_to_kebab(resource_name).split('-').last
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
data/lib/utils/bacenid.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: false
|
2
|
-
|
3
|
-
require('date')
|
4
|
-
|
5
|
-
module StarkInfra
|
6
|
-
module Utils
|
7
|
-
module BacenId
|
8
|
-
def self._create(bank_code)
|
9
|
-
random_source = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'.split('')
|
10
|
-
random_string = ''
|
11
|
-
|
12
|
-
11.times do
|
13
|
-
random_string << random_source[rand(random_source.length)]
|
14
|
-
end
|
15
|
-
bank_code + DateTime.now.strftime('%Y%m%d%H%M').to_s << random_string
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
data/lib/utils/cache.rb
DELETED
data/lib/utils/case.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module StarkInfra
|
4
|
-
module Utils
|
5
|
-
module Case
|
6
|
-
def self.camel_to_snake(camel)
|
7
|
-
camel.to_s.gsub(/([a-z])([A-Z\d])/, '\1_\2').downcase
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.snake_to_camel(snake)
|
11
|
-
camel = snake.to_s.split('_').map(&:capitalize).join
|
12
|
-
camel[0] = camel[0].downcase
|
13
|
-
camel
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.camel_to_kebab(camel)
|
17
|
-
camel_to_snake(camel).tr('_', '-')
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
data/lib/utils/checks.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require('date')
|
4
|
-
require('starkbank-ecdsa')
|
5
|
-
require_relative('environment')
|
6
|
-
|
7
|
-
module StarkInfra
|
8
|
-
module Utils
|
9
|
-
class Checks
|
10
|
-
def self.check_user(user)
|
11
|
-
return user if user.is_a?(StarkInfra::User)
|
12
|
-
|
13
|
-
user = user.nil? ? StarkInfra.user : user
|
14
|
-
raise(ArgumentError, 'A user is required to access our API. Check our README: https://github.com/starkinfra/sdk-ruby/') if user.nil?
|
15
|
-
|
16
|
-
user
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.check_language
|
20
|
-
language = StarkInfra.language
|
21
|
-
accepted_languages = %w[en-US pt-BR]
|
22
|
-
raise(ArgumentError, "Select a valid language: #{accepted_languages.join(', ')}") unless accepted_languages.include?(language)
|
23
|
-
|
24
|
-
language
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.check_environment(environment)
|
28
|
-
environments = StarkInfra::Utils::Environment.constants(false).map { |c| StarkInfra::Utils::Environment.const_get(c) }
|
29
|
-
raise(ArgumentError, "Select a valid environment: #{environments.join(', ')}") unless environments.include?(environment)
|
30
|
-
|
31
|
-
environment
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.check_private_key(pem)
|
35
|
-
EllipticCurve::PrivateKey.fromPem(pem)
|
36
|
-
pem
|
37
|
-
rescue
|
38
|
-
raise(ArgumentError, 'Private-key must be a valid secp256k1 ECDSA string in pem format')
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.check_date_or_datetime(data)
|
42
|
-
return if data.nil?
|
43
|
-
|
44
|
-
return data if data.is_a?(Time) || data.is_a?(DateTime)
|
45
|
-
|
46
|
-
return data if data.is_a?(Date)
|
47
|
-
|
48
|
-
data, type = check_datetime_string(data)
|
49
|
-
type == 'date' ? Date.new(data.year, data.month, data.day) : data
|
50
|
-
end
|
51
|
-
|
52
|
-
def self.check_datetime(data)
|
53
|
-
return if data.nil?
|
54
|
-
|
55
|
-
return data if data.is_a?(Time) || data.is_a?(DateTime)
|
56
|
-
|
57
|
-
return Time.new(data.year, data.month, data.day) if data.is_a?(Date)
|
58
|
-
|
59
|
-
data, _type = check_datetime_string(data)
|
60
|
-
data
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.check_date(data)
|
64
|
-
return if data.nil?
|
65
|
-
|
66
|
-
return Date.new(data.year, data.month, data.day) if data.is_a?(Time) || data.is_a?(DateTime)
|
67
|
-
|
68
|
-
return data if data.is_a?(Date)
|
69
|
-
|
70
|
-
data, type = check_datetime_string(data)
|
71
|
-
|
72
|
-
type == 'date' ? Date.new(data.year, data.month, data.day) : data
|
73
|
-
end
|
74
|
-
|
75
|
-
class << self
|
76
|
-
private
|
77
|
-
|
78
|
-
def check_datetime_string(data)
|
79
|
-
data = data.to_s
|
80
|
-
|
81
|
-
begin
|
82
|
-
return [DateTime.strptime(data, '%Y-%m-%dT%H:%M:%S.%L+00:00'), 'datetime']
|
83
|
-
rescue ArgumentError
|
84
|
-
end
|
85
|
-
|
86
|
-
begin
|
87
|
-
return [DateTime.strptime(data, '%Y-%m-%dT%H:%M:%S+00:00'), 'datetime']
|
88
|
-
rescue ArgumentError
|
89
|
-
end
|
90
|
-
|
91
|
-
begin
|
92
|
-
[DateTime.strptime(data, '%Y-%m-%d'), 'date']
|
93
|
-
rescue ArgumentError
|
94
|
-
raise(ArgumentError, 'invalid datetime string ' + data)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
data/lib/utils/endtoendid.rb
DELETED
data/lib/utils/environment.rb
DELETED
data/lib/utils/request.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require('json')
|
4
|
-
require('starkbank-ecdsa')
|
5
|
-
require('net/http')
|
6
|
-
require_relative('url')
|
7
|
-
require_relative('checks')
|
8
|
-
require_relative('../error')
|
9
|
-
|
10
|
-
module StarkInfra
|
11
|
-
module Utils
|
12
|
-
module Request
|
13
|
-
class Response
|
14
|
-
attr_reader :status, :content
|
15
|
-
def initialize(status, content)
|
16
|
-
@status = status
|
17
|
-
@content = content
|
18
|
-
end
|
19
|
-
|
20
|
-
def json
|
21
|
-
JSON.parse(@content)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.fetch(method:, path:, payload: nil, query: nil, user: nil)
|
26
|
-
user = Checks.check_user(user)
|
27
|
-
language = Checks.check_language
|
28
|
-
|
29
|
-
base_url = {
|
30
|
-
Environment::PRODUCTION => 'https://api.starkinfra.com/',
|
31
|
-
Environment::SANDBOX => 'https://sandbox.api.starkinfra.com/'
|
32
|
-
}[user.environment] + 'v2'
|
33
|
-
|
34
|
-
url = "#{base_url}/#{path}#{StarkInfra::Utils::URL.urlencode(query)}"
|
35
|
-
uri = URI(url)
|
36
|
-
|
37
|
-
access_time = Time.now.to_i
|
38
|
-
body = payload.nil? ? '' : payload.to_json
|
39
|
-
message = "#{user.access_id}:#{access_time}:#{body}"
|
40
|
-
signature = EllipticCurve::Ecdsa.sign(message, user.private_key).toBase64
|
41
|
-
|
42
|
-
case method
|
43
|
-
when 'GET'
|
44
|
-
req = Net::HTTP::Get.new(uri)
|
45
|
-
when 'DELETE'
|
46
|
-
req = Net::HTTP::Delete.new(uri)
|
47
|
-
when 'POST'
|
48
|
-
req = Net::HTTP::Post.new(uri)
|
49
|
-
req.body = body
|
50
|
-
when 'PATCH'
|
51
|
-
req = Net::HTTP::Patch.new(uri)
|
52
|
-
req.body = body
|
53
|
-
when 'PUT'
|
54
|
-
req = Net::HTTP::Put.new(uri)
|
55
|
-
req.body = body
|
56
|
-
else
|
57
|
-
raise(ArgumentError, 'unknown HTTP method ' + method)
|
58
|
-
end
|
59
|
-
|
60
|
-
req['Access-Id'] = user.access_id
|
61
|
-
req['Access-Time'] = access_time
|
62
|
-
req['Access-Signature'] = signature
|
63
|
-
req['Content-Type'] = 'application/json'
|
64
|
-
req['User-Agent'] = "Ruby-#{RUBY_VERSION}-SDK-Infra-0.3.0"
|
65
|
-
req['Accept-Language'] = language
|
66
|
-
|
67
|
-
request = Net::HTTP.start(uri.hostname, use_ssl: true) { |http| http.request(req) }
|
68
|
-
|
69
|
-
response = Response.new(Integer(request.code, 10), request.body)
|
70
|
-
|
71
|
-
raise(StarkInfra::Error::InternalServerError) if response.status == 500
|
72
|
-
raise(StarkInfra::Error::InputErrors, response.json['errors']) if response.status == 400
|
73
|
-
raise(StarkInfra::Error::UnknownError, response.content) unless response.status == 200
|
74
|
-
|
75
|
-
response
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
data/lib/utils/resource.rb
DELETED
data/lib/utils/returnid.rb
DELETED
data/lib/utils/sub_resource.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module StarkInfra
|
4
|
-
module Utils
|
5
|
-
class SubResource
|
6
|
-
def to_s
|
7
|
-
string_vars = []
|
8
|
-
instance_variables.each do |key|
|
9
|
-
value = instance_variable_get(key).to_s.lines.map(&:chomp).join("\n ")
|
10
|
-
string_vars << "#{key[1..-1]}: #{value}"
|
11
|
-
end
|
12
|
-
fields = string_vars.join(",\n ")
|
13
|
-
"#{class_name}(\n #{fields}\n)"
|
14
|
-
end
|
15
|
-
|
16
|
-
def inspect
|
17
|
-
"#{class_name}[#{@id}]"
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def class_name
|
23
|
-
self.class.name.split('::').last.downcase
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
data/lib/utils/url.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'erb'
|
3
|
-
|
4
|
-
|
5
|
-
module StarkInfra
|
6
|
-
module Utils
|
7
|
-
module URL
|
8
|
-
# generates query string from hash
|
9
|
-
def self.urlencode(params)
|
10
|
-
return '' if params.nil?
|
11
|
-
|
12
|
-
params = StarkInfra::Utils::API.cast_json_to_api_format(params)
|
13
|
-
return '' if params.empty?
|
14
|
-
|
15
|
-
string_params = {}
|
16
|
-
params.each do |key, value|
|
17
|
-
string_params[key] = value.is_a?(Array) ? value.join(',') : value
|
18
|
-
end
|
19
|
-
|
20
|
-
query_list = []
|
21
|
-
string_params.each do |key, value|
|
22
|
-
query_list << "#{key}=#{ERB::Util.url_encode(value)}"
|
23
|
-
end
|
24
|
-
'?' + query_list.join('&')
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|