diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ff2455..3124a91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ Given a version number MAJOR.MINOR.PATCH, increment: ## [Unreleased] +### Changed +- internal structure to use starkcore as a dependency. ## [2.6.0] - 2021-09-04 ### Added diff --git a/Gemfile.lock b/Gemfile.lock index c2ef8c2..9c759bb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,7 @@ PATH remote: . specs: starkbank (2.6.0) - starkbank-ecdsa (~> 0.0.5) + starkcore (~> 0.0.1) GEM remote: https://rubygems.org/ @@ -25,7 +25,8 @@ GEM ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 2.0) ruby-progressbar (1.10.1) - starkbank-ecdsa (0.0.5) + starkcore (0.0.1) + starkbank-ecdsa (~> 2.0.0) unicode-display_width (1.7.0) PLATFORMS @@ -38,4 +39,4 @@ DEPENDENCIES starkbank! BUNDLED WITH - 2.1.4 + 2.4.1 diff --git a/README.md b/README.md index fa2c1b0..ece65f0 100644 --- a/README.md +++ b/README.md @@ -1813,4 +1813,4 @@ If you have any questions about our SDK, just send us an email. We will respond you quickly, pinky promise. We are here to help you integrate with us ASAP. We also love feedback, so don't be shy about sharing your thoughts with us. -Email: developers@starkbank.com +Email: help@starkbank.com diff --git a/lib/balance/balance.rb b/lib/balance/balance.rb index c518b98..346a6e0 100644 --- a/lib/balance/balance.rb +++ b/lib/balance/balance.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # Balance object @@ -17,13 +17,13 @@ module StarkBank # - amount [integer, default nil]: current balance amount of the workspace in cents. ex: 200 (= R$ 2.00) # - currency [string, default nil]: currency of the current workspace. Expect others to be added eventually. ex:'BRL' # - updated [DateTime, default nil]: update datetime for the balance. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Balance < StarkBank::Utils::Resource + class Balance < StarkCore::Utils::Resource attr_reader :amount, :currency, :updated def initialize(amount:, currency:, updated:, id:) super(id) @amount = amount @currency = currency - @updated = StarkBank::Utils::Checks.check_datetime(updated) + @updated = StarkCore::Utils::Checks.check_datetime(updated) end # # Retrieve the Balance object diff --git a/lib/boleto/boleto.rb b/lib/boleto/boleto.rb index 39fe217..671db68 100644 --- a/lib/boleto/boleto.rb +++ b/lib/boleto/boleto.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # Boleto object @@ -42,7 +42,7 @@ module StarkBank # - status [string, default nil]: current Boleto status. ex: 'registered' or 'paid' # - created [DateTime, default nil]: creation datetime for the Boleto. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # - our_number [string, default nil]: Reference number registered at the settlement bank. ex:'10131474' - class Boleto < StarkBank::Utils::Resource + class Boleto < StarkCore::Utils::Resource attr_reader :amount, :name, :tax_id, :street_line_1, :street_line_2, :district, :city, :state_code, :zip_code, :due, :fine, :interest, :overdue_limit, :receiver_name, :receiver_tax_id, :tags, :descriptions, :discounts, :id, :fee, :line, :bar_code, :status, :transaction_ids, :created, :our_number def initialize( amount:, name:, tax_id:, street_line_1:, street_line_2:, district:, city:, state_code:, zip_code:, @@ -60,7 +60,7 @@ def initialize( @city = city @state_code = state_code @zip_code = zip_code - @due = StarkBank::Utils::Checks.check_date(due) + @due = StarkCore::Utils::Checks.check_date(due) @fine = fine @interest = interest @overdue_limit = overdue_limit @@ -74,7 +74,7 @@ def initialize( @bar_code = bar_code @status = status @transaction_ids = transaction_ids - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) @our_number = our_number end @@ -144,8 +144,8 @@ def self.pdf(id, layout: nil, hidden_fields: nil, user: nil) # ## Return: # - generator of Boleto objects with updated attributes def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -176,8 +176,8 @@ def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: # ## Return: # - list of Boleto objects with updated attributes and cursor to retrieve the next page of Boleto objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/boleto/log.rb b/lib/boleto/log.rb index d45ce17..d5d56b6 100644 --- a/lib/boleto/log.rb +++ b/lib/boleto/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('boleto') + module StarkBank class Boleto # # Boleto::Log object @@ -20,14 +20,14 @@ class Boleto # - errors [list of strings]: list of errors linked to this Boleto event # - type [string]: type of the Boleto event which triggered the log creation. ex: 'registered' or 'paid' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :created, :type, :errors, :boleto def initialize(id:, created:, type:, errors:, boleto:) super(id) @type = type @errors = errors @boleto = boleto - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -61,8 +61,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, boleto_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -91,8 +91,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, boleto_ids: nil, # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, boleto_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -115,7 +115,7 @@ def self.resource created: json['created'], type: json['type'], errors: json['errors'], - boleto: StarkBank::Utils::API.from_api_json(boleto_maker, json['boleto']) + boleto: StarkCore::Utils::API.from_api_json(boleto_maker, json['boleto']) ) } } diff --git a/lib/boleto_holmes/boleto_holmes.rb b/lib/boleto_holmes/boleto_holmes.rb index cb5d91a..27abffb 100644 --- a/lib/boleto_holmes/boleto_holmes.rb +++ b/lib/boleto_holmes/boleto_holmes.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # BoletoHolmes object @@ -23,7 +23,7 @@ module StarkBank # - result [string, default nil]: result of boleto status investigation. ex: 'paid' or 'cancelled' # - created [DateTime, default nil]: creation datetime for the Boleto. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # - updated [DateTime, default nil]: latest update datetime for the holmes. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class BoletoHolmes < StarkBank::Utils::Resource + class BoletoHolmes < StarkCore::Utils::Resource attr_reader :boleto_id, :tags, :id, :status, :result, :created, :updated def initialize( boleto_id:, tags: nil, id: nil, status: nil, result: nil, created: nil, updated: nil @@ -33,8 +33,8 @@ def initialize( @tags = tags @status = status @result = result - @created = StarkBank::Utils::Checks.check_datetime(created) - @updated = StarkBank::Utils::Checks.check_datetime(updated) + @created = StarkCore::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) end # # Create BoletoHolmes @@ -86,8 +86,8 @@ def self.get(id, user: nil) # ## Return: # - generator of BoletoHolmes objects with updated attributes def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, boleto_id: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -120,8 +120,8 @@ def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: # ## Return: # - list of BoletoHolmes objects with updated attributes and cursor to retrieve the next page of BoletoHolmes objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, boleto_id: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/boleto_holmes/log.rb b/lib/boleto_holmes/log.rb index e613df6..84036fa 100644 --- a/lib/boleto_holmes/log.rb +++ b/lib/boleto_holmes/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('boleto_holmes') + module StarkBank class BoletoHolmes # # BoletoHolmes::Log object @@ -19,13 +19,13 @@ class BoletoHolmes # - holmes [BoletoHolmes]: BoletoHolmes entity to which the log refers to. # - type [string]: type of the Boleto event which triggered the log creation. ex: 'registered' or 'paid' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :holmes, :type, :created def initialize(id:, holmes:, type:, created:) super(id) @holmes = holmes @type = type - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -59,8 +59,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, holmes_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -89,8 +89,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, holmes_ids: nil, # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, holmes_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -110,7 +110,7 @@ def self.resource resource_maker: proc { |json| Log.new( id: json['id'], - holmes: StarkBank::Utils::API.from_api_json(holmes_maker, json['holmes']), + holmes: StarkCore::Utils::API.from_api_json(holmes_maker, json['holmes']), type: json['type'], created: json['created'] ) diff --git a/lib/boleto_payment/boleto_payment.rb b/lib/boleto_payment/boleto_payment.rb index d9b524a..9b8d77f 100644 --- a/lib/boleto_payment/boleto_payment.rb +++ b/lib/boleto_payment/boleto_payment.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # BoletoPayment object @@ -29,7 +29,7 @@ module StarkBank # - amount [int, default nil]: amount automatically calculated from line or bar_code. ex: 23456 (= R$ 234.56) # - fee [integer, default nil]: fee charged when the boleto payment is created. ex: 200 (= R$ 2.00) # - created [DateTime, default nil]: creation datetime for the payment. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class BoletoPayment < StarkBank::Utils::Resource + class BoletoPayment < StarkCore::Utils::Resource attr_reader :tax_id, :description, :line, :bar_code, :scheduled, :tags, :id, :status, :amount, :fee, :created def initialize(tax_id:, description:, line: nil, bar_code: nil, scheduled: nil, tags: nil, id: nil, status: nil, amount: nil, fee: nil, created: nil) super(id) @@ -37,12 +37,12 @@ def initialize(tax_id:, description:, line: nil, bar_code: nil, scheduled: nil, @description = description @line = line @bar_code = bar_code - @scheduled = StarkBank::Utils::Checks.check_date(scheduled) + @scheduled = StarkCore::Utils::Checks.check_date(scheduled) @tags = tags @status = status @amount = amount @fee = fee - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Create BoletoPayments @@ -110,8 +110,8 @@ def self.pdf(id, user: nil) # ## Return: # - generator of BoletoPayment objects with updated attributes def self.query(limit: nil, after: nil, before: nil, tags: nil, ids: nil, status: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( user: user, limit: limit, @@ -142,8 +142,8 @@ def self.query(limit: nil, after: nil, before: nil, tags: nil, ids: nil, status: # ## Return: # - list of BoletoPayment objects with updated attributes and cursor to retrieve the next page of BoletoPayment objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, tags: nil, ids: nil, status: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, user: user, diff --git a/lib/boleto_payment/log.rb b/lib/boleto_payment/log.rb index 86bfdd5..11656a0 100644 --- a/lib/boleto_payment/log.rb +++ b/lib/boleto_payment/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('boleto_payment') + module StarkBank class BoletoPayment # # BoletoPayment::Log object @@ -20,14 +20,14 @@ class BoletoPayment # - errors [list of strings]: list of errors linked to this BoletoPayment event. # - type [string]: type of the BoletoPayment event which triggered the log creation. ex: 'processing' or 'success' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :created, :type, :errors, :payment def initialize(id:, created:, type:, errors:, payment:) super(id) @type = type @errors = errors @payment = payment - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -61,8 +61,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -91,8 +91,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -115,7 +115,7 @@ def self.resource created: json['created'], type: json['type'], errors: json['errors'], - payment: StarkBank::Utils::API.from_api_json(payment_maker, json['payment']) + payment: StarkCore::Utils::API.from_api_json(payment_maker, json['payment']) ) } } diff --git a/lib/brcode_payment/brcode_payment.rb b/lib/brcode_payment/brcode_payment.rb index 83fd1be..156620c 100644 --- a/lib/brcode_payment/brcode_payment.rb +++ b/lib/brcode_payment/brcode_payment.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # BrcodePayment object @@ -32,7 +32,7 @@ module StarkBank # - fee [integer, default nil]: fee charged when the brcode payment is created. ex: 200 (= R$ 2.00) # - updated [datetime.datetime, default nil]: latest update datetime for the payment. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) # - created [datetime.datetime, default nil]: creation datetime for the payment. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) - class BrcodePayment < StarkBank::Utils::Resource + class BrcodePayment < StarkCore::Utils::Resource attr_reader :brcode, :tax_id, :description, :amount, :scheduled, :tags, :id, :name, :status, :type, :transaction_ids, :fee, :updated, :created def initialize(brcode:, tax_id:, description:, amount: nil, scheduled: nil, tags: nil, id: nil, name: nil, status: nil, type: nil, transaction_ids: nil, fee: nil, updated: nil, created: nil) super(id) @@ -40,15 +40,15 @@ def initialize(brcode:, tax_id:, description:, amount: nil, scheduled: nil, tags @tax_id = tax_id @description = description @amount = amount - @scheduled = StarkBank::Utils::Checks.check_date_or_datetime(scheduled) + @scheduled = StarkCore::Utils::Checks.check_date_or_datetime(scheduled) @tags = tags @name = name @status = status @type = type @transaction_ids = transaction_ids @fee = fee - @updated = StarkBank::Utils::Checks.check_datetime(updated) - @created = StarkBank::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Create BrcodePayments @@ -132,8 +132,8 @@ def self.update(id, status: nil, user: nil) # ## Return: # - generator of BrcodePayment objects with updated attributes def self.query(limit: nil, after: nil, before: nil, tags: nil, ids: nil, status: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -164,8 +164,8 @@ def self.query(limit: nil, after: nil, before: nil, tags: nil, ids: nil, status: # ## Return: # - list of BrcodePayment objects with updated attributes and cursor to retrieve the next page of BrcodePayment objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, tags: nil, ids: nil, status: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/brcode_payment/log.rb b/lib/brcode_payment/log.rb index 77f5edf..8b37ba4 100644 --- a/lib/brcode_payment/log.rb +++ b/lib/brcode_payment/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('brcode_payment') + module StarkBank class BrcodePayment # # BrcodePayment::Log object @@ -20,14 +20,14 @@ class BrcodePayment # - errors [list of strings]: list of errors linked to this BrcodePayment event. # - type [string]: type of the BrcodePayment event which triggered the log creation. ex: 'processing' or 'success' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :created, :type, :errors, :payment def initialize(id:, created:, type:, errors:, payment:) super(id) @type = type @errors = errors @payment = payment - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -61,8 +61,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -91,8 +91,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -115,7 +115,7 @@ def self.resource created: json['created'], type: json['type'], errors: json['errors'], - payment: StarkBank::Utils::API.from_api_json(payment_maker, json['payment']) + payment: StarkCore::Utils::API.from_api_json(payment_maker, json['payment']) ) } } diff --git a/lib/brcode_preview/brcode_preview.rb b/lib/brcode_preview/brcode_preview.rb index e36c5f6..254fd3c 100644 --- a/lib/brcode_preview/brcode_preview.rb +++ b/lib/brcode_preview/brcode_preview.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # BrcodePreview object @@ -20,7 +20,7 @@ module StarkBank # - allow_change [bool]: If True, the payment is able to receive amounts that are diferent from the nominal one. ex: True or False # - amount [integer]: Value in cents that this payment is expecting to receive. If 0, any value is accepted. ex: 123 (= R$1,23) # - reconciliation_id [string]: Reconciliation ID linked to this payment. ex: 'txId', 'payment-123' - class BrcodePreview < StarkBank::Utils::Resource + class BrcodePreview < StarkCore::Utils::Resource attr_reader :status, :name, :tax_id, :bank_code, :branch_code, :account_number, :account_type, :allow_change, :amount, :reconciliation_id def initialize(status:, name:, tax_id:, bank_code:, branch_code:, account_number:, account_type:, allow_change:, amount:, reconciliation_id:) @status = status diff --git a/lib/darf_payment/darf_payment.rb b/lib/darf_payment/darf_payment.rb index e4a8d66..100541f 100644 --- a/lib/darf_payment/darf_payment.rb +++ b/lib/darf_payment/darf_payment.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # DarfPayment object @@ -34,7 +34,7 @@ module StarkBank # - fee [integer, default nil]: fee charged when the DarfPayment is processed. ex: 0 (= R$ 0.00) # - created [DateTime, default nil]: creation datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # - updated [DateTime, default nil]: latest update datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class DarfPayment < StarkBank::Utils::Resource + class DarfPayment < StarkCore::Utils::Resource attr_reader :id, :revenue_code, :tax_id, :competence, :reference_number, :fine_amount, :interest_amount, :due, :description, :tags, :scheduled, :status, :amount, :nominal_amount, :fee, :updated, :created def initialize( @@ -44,20 +44,20 @@ def initialize( super(id) @revenue_code = revenue_code @tax_id = tax_id - @competence = StarkBank::Utils::Checks.check_date(competence) + @competence = StarkCore::Utils::Checks.check_date(competence) @reference_number = reference_number @fine_amount = fine_amount @interest_amount = interest_amount - @due = StarkBank::Utils::Checks.check_date(due) + @due = StarkCore::Utils::Checks.check_date(due) @description = description @tags = tags - @scheduled = StarkBank::Utils::Checks.check_date(scheduled) + @scheduled = StarkCore::Utils::Checks.check_date(scheduled) @status = status @amount = amount @nominal_amount = nominal_amount @fee = fee - @updated = StarkBank::Utils::Checks.check_datetime(updated) - @created = StarkBank::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Create DarfPayments @@ -125,8 +125,8 @@ def self.pdf(id, user: nil) # ## Return: # - generator of DarfPayment objects with updated attributes def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -157,8 +157,8 @@ def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: # ## Return: # - list of Darf Payment objects with updated attributes and cursor to retrieve the next page of Darf Payment objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/darf_payment/log.rb b/lib/darf_payment/log.rb index d0f7c82..5ccef97 100644 --- a/lib/darf_payment/log.rb +++ b/lib/darf_payment/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('darf_payment') + module StarkBank class DarfPayment # # DarfPayment::Log object @@ -20,14 +20,14 @@ class DarfPayment # - errors [list of strings]: list of errors linked to this DarfPayment event # - type [string]: type of the DarfPayment event which triggered the log creation. ex: 'processing' or 'success' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :created, :type, :errors, :payment def initialize(id:, created:, type:, errors:, payment:) super(id) @type = type @errors = errors @payment = payment - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -61,8 +61,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -91,8 +91,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -115,7 +115,7 @@ def self.resource created: json['created'], type: json['type'], errors: json['errors'], - payment: StarkBank::Utils::API.from_api_json(darf_payment_maker, json['payment']) + payment: StarkCore::Utils::API.from_api_json(darf_payment_maker, json['payment']) ) } } diff --git a/lib/deposit/deposit.rb b/lib/deposit/deposit.rb index d32966f..faf0359 100644 --- a/lib/deposit/deposit.rb +++ b/lib/deposit/deposit.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # Deposit object @@ -25,7 +25,7 @@ module StarkBank # - transaction_ids [list of strings]: ledger transaction ids linked to this Deposit (if there are more than one, all but first are reversals). ex: ['19827356981273'] # - created [datetime.datetime]: creation datetime for the Deposit. ex: datetime.datetime(2020, 12, 10, 10, 30, 0, 0) # - updated [datetime.datetime]: latest update datetime for the Deposit. ex: datetime.datetime(2020, 12, 10, 10, 30, 0, 0) - class Deposit < StarkBank::Utils::Resource + class Deposit < StarkCore::Utils::Resource attr_reader :id, :name, :tax_id, :bank_code, :branch_code, :account_number, :account_type, :amount, :type, :status, :tags, :fee, :transaction_ids, :created, :updated def initialize( id:, name:, tax_id:, bank_code:, branch_code:, account_number:, account_type:, amount:, type:, @@ -44,8 +44,8 @@ def initialize( @tags = tags @fee = fee @transaction_ids = transaction_ids - @created = StarkBank::Utils::Checks.check_datetime(created) - @updated = StarkBank::Utils::Checks.check_datetime(updated) + @created = StarkCore::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) end # # Retrieve a specific Deposit @@ -81,8 +81,8 @@ def self.get(id, user: nil) # ## Return: # - generator of Deposit objects with updated attributes def self.query(limit: nil, after: nil, before: nil, status: nil, sort: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -115,8 +115,8 @@ def self.query(limit: nil, after: nil, before: nil, status: nil, sort: nil, tags # ## Return: # - list of Deposit objects with updated attributes and cursor to retrieve the next page of Deposit objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, status: nil, sort: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/deposit/log.rb b/lib/deposit/log.rb index 498b072..d72cd3f 100644 --- a/lib/deposit/log.rb +++ b/lib/deposit/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('deposit') + module StarkBank class Deposit # # Deposit::Log object @@ -20,14 +20,14 @@ class Deposit # - errors [list of strings]: list of errors linked to this Deposit event # - type [string]: type of the Deposit event which triggered the log creation. ex: 'canceled' or 'paid' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :created, :type, :errors, :deposit def initialize(id:, created:, type:, errors:, deposit:) super(id) @type = type @errors = errors @deposit = deposit - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -61,8 +61,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, deposit_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -91,8 +91,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, deposit_ids: nil # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, deposit_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -115,7 +115,7 @@ def self.resource created: json['created'], type: json['type'], errors: json['errors'], - deposit: StarkBank::Utils::API.from_api_json(deposit_maker, json['deposit']) + deposit: StarkCore::Utils::API.from_api_json(deposit_maker, json['deposit']) ) } } diff --git a/lib/dict_key/dict_key.rb b/lib/dict_key/dict_key.rb index e18eba2..e23d740 100644 --- a/lib/dict_key/dict_key.rb +++ b/lib/dict_key/dict_key.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -require_relative('../utils/resource.rb') require_relative('../utils/rest.rb') -require_relative('../utils/checks.rb') + module StarkBank # # DictKey object @@ -26,7 +25,7 @@ module StarkBank # - account_created [DateTime or string, default nil]: creation datetime of the bank account associated with the DICT key. ex: '2020-11-05T14:55:08.812665+00:00' # - owned [DateTime or string, default nil]: datetime since when the current owner hold this DICT key. ex : '2020-11-05T14:55:08.812665+00:00' # - created [DateTime or string, default nil]: creation datetime for the DICT key. ex: '2020-03-10 10:30:00.000' - class DictKey < StarkBank::Utils::Resource + class DictKey < StarkCore::Utils::Resource attr_reader :id, :type, :name, :tax_id, :owner_type, :bank_name, :ispb, :branch_code, :account_number, :account_type, :status, :account_created, :owned, :created def initialize( id:, type:, name:, tax_id:, owner_type:, bank_name:, ispb:, branch_code:, account_number:, @@ -43,9 +42,9 @@ def initialize( @account_number = account_number @account_type = account_type @status = status - @account_created = StarkBank::Utils::Checks.check_datetime(account_created) - @owned = StarkBank::Utils::Checks.check_datetime(owned) - @created = StarkBank::Utils::Checks.check_datetime(created) + @account_created = StarkCore::Utils::Checks.check_datetime(account_created) + @owned = StarkCore::Utils::Checks.check_datetime(owned) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific DictKey @@ -80,8 +79,8 @@ def self.get(id, user: nil) # ## Return: # - generator of DitcKey objects with updated attributes def self.query(limit: nil, type: nil, after: nil, before: nil, ids: nil, status: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, type: type, @@ -112,8 +111,8 @@ def self.query(limit: nil, type: nil, after: nil, before: nil, ids: nil, status: # ## Return: # - list of DictKey objects with updated attributes and cursor to retrieve the next page of DictKey objects def self.page(cursor: nil, limit: nil, type: nil, after: nil, before: nil, ids: nil, status: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/error.rb b/lib/error.rb index f5c5000..9e03bdb 100644 --- a/lib/error.rb +++ b/lib/error.rb @@ -1,52 +1,19 @@ # frozen_string_literal: true -require('json') +require('starkcore') module StarkBank module Error - class StarkBankError < StandardError - attr_reader :message - def initialize(message) - @message = message - super(message) - end - end + StarkBankError = StarkCore::Error::StarkCoreError - class Error < StarkBankError - attr_reader :code, :message - def initialize(code, message) - @code = code - @message = message - super("#{code}: #{message}") - end - end + Error = StarkCore::Error::Error - class InputErrors < StarkBankError - attr_reader :errors - def initialize(content) - errors = [] - content.each do |error| - errors << Error.new(error['code'], error['message']) - end - @errors = errors + InputErrors = StarkCore::Error::InputErrors - super(content.to_json) - end - end + InternalServerError = StarkCore::Error::InternalServerError - class InternalServerError < StarkBankError - def initialize(message = 'Houston, we have a problem.') - super(message) - end - end + UnknownError = StarkCore::Error::UnknownError - class UnknownError < StarkBankError - def initialize(message) - super("Unknown exception encountered: #{message}") - end - end - - class InvalidSignatureError < StarkBankError - end + InvalidSignatureError = StarkCore::Error::InvalidSignatureError end end diff --git a/lib/event/attempt.rb b/lib/event/attempt.rb index 530e3c9..6fa614e 100644 --- a/lib/event/attempt.rb +++ b/lib/event/attempt.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('event') + module StarkBank class Event # # Event::Attempt object @@ -19,7 +19,7 @@ class Event # - event_id [string]: ID of the Event whose delivery failed. ex: "4848484848484848" # - webhook_id [string]: ID of the Webhook that triggered this event. ex: "5656565656565656" # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Attempt < StarkBank::Utils::Resource + class Attempt < StarkCore::Utils::Resource attr_reader :id, :code, :message, :event_id, :webhook_id, :created def initialize(id:, code:, message:, event_id:, webhook_id:, created:) super(id) @@ -27,7 +27,7 @@ def initialize(id:, code:, message:, event_id:, webhook_id:, created:) @message = message @event_id = event_id @webhook_id = webhook_id - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Event::Attempt @@ -61,8 +61,8 @@ def self.get(id, user: nil) # ## Return: # - generator of Event::Attempt objects with updated attributes def self.query(limit: nil, after: nil, before: nil, event_ids: nil, webhook_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -91,8 +91,8 @@ def self.query(limit: nil, after: nil, before: nil, event_ids: nil, webhook_ids: # ## Return: # - list of Attempt objects with updated attributes and cursor to retrieve the next page of Attempt objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, event_ids: nil, webhook_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/event/event.rb b/lib/event/event.rb index 3d79e9a..26bcbe5 100644 --- a/lib/event/event.rb +++ b/lib/event/event.rb @@ -1,11 +1,9 @@ # frozen_string_literal: true require('json') -require('starkbank-ecdsa') -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') -require_relative('../utils/cache') +require_relative('../utils/parse') require_relative('../error') require_relative('../boleto/log') require_relative('../boleto_holmes/log') @@ -32,11 +30,11 @@ module StarkBank # - is_delivered [bool]: true if the event has been successfully delivered to the user url. ex: False # - workspace_id [string]: ID of the Workspace that generated this event. Mostly used when multiple Workspaces have Webhooks registered to the same endpoint. ex: '4545454545454545' # - subscription [string]: service that triggered this event. ex: 'transfer', 'utility-payment' - class Event < StarkBank::Utils::Resource + class Event < StarkCore::Utils::Resource attr_reader :id, :log, :created, :is_delivered, :workspace_id, :subscription def initialize(id:, log:, created:, is_delivered:, workspace_id:, subscription:) super(id) - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) @is_delivered = is_delivered @workspace_id = workspace_id @subscription = subscription @@ -55,7 +53,7 @@ def initialize(id:, log:, created:, is_delivered:, workspace_id:, subscription:) }[subscription.to_sym] @log = log - @log = StarkBank::Utils::API.from_api_json(resource[:resource_maker], log) unless resource.nil? + @log = StarkCore::Utils::API.from_api_json(resource[:resource_maker], log) unless resource.nil? end # # Retrieve a specific notification Event @@ -88,8 +86,8 @@ def self.get(id, user: nil) # ## Return: # - generator of Event objects with updated attributes def self.query(limit: nil, after: nil, before: nil, is_delivered: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( user: user, limit: limit, @@ -116,8 +114,8 @@ def self.query(limit: nil, after: nil, before: nil, is_delivered: nil, user: nil # ## Return: # - list of Event objects with updated attributes and cursor to retrieve the next page of Event objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, is_delivered: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -179,53 +177,29 @@ def self.update(id, is_delivered:, user: nil) # ## Return: # - Parsed Event object def self.parse(content:, signature:, user: nil) - event = StarkBank::Utils::API.from_api_json(resource[:resource_maker], JSON.parse(content)['event']) - - begin - signature = EllipticCurve::Signature.fromBase64(signature) - rescue - raise(StarkBank::Error::InvalidSignatureError, 'The provided signature is not valid') - end - - return event if verify_signature(content: content, signature: signature, user: user) - - return event if verify_signature(content: content, signature: signature, user: user, refresh: true) - - raise(StarkBank::Error::InvalidSignatureError, 'The provided signature and content do not match the Stark Bank public key') + StarkBank::Utils::Parse.parse_and_verify( + content: content, + signature: signature, + user: user, + resource: resource, + key: 'event' + ) end - class << self - private - - def verify_signature(content:, signature:, user:, refresh: false) - public_key = StarkBank::Utils::Cache.starkbank_public_key - if public_key.nil? || refresh - pem = get_public_key_pem(user) - public_key = EllipticCurve::PublicKey.fromPem(pem) - StarkBank::Utils::Cache.starkbank_public_key = public_key - end - EllipticCurve::Ecdsa.verify(content, signature, public_key) - end - - def get_public_key_pem(user) - StarkBank::Utils::Request.fetch(method: 'GET', path: 'public-key', query: { limit: 1 }, user: user).json['publicKeys'][0]['content'] - end - - def resource - { - resource_name: 'Event', - resource_maker: proc { |json| - Event.new( - id: json['id'], - log: json['log'], - created: json['created'], - is_delivered: json['is_delivered'], - workspace_id: json['workspace_id'], - subscription: json['subscription'] - ) - } + def self.resource + { + resource_name: 'Event', + resource_maker: proc { |json| + Event.new( + id: json['id'], + log: json['log'], + created: json['created'], + is_delivered: json['is_delivered'], + workspace_id: json['workspace_id'], + subscription: json['subscription'] + ) } - end + } end end end diff --git a/lib/institution/institution.rb b/lib/institution/institution.rb index 90b0a44..b07200e 100644 --- a/lib/institution/institution.rb +++ b/lib/institution/institution.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -require_relative('../utils/sub_resource') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # Institution object @@ -17,7 +16,7 @@ module StarkBank # - name [string]: full version of the institution name. ex: 'Stark Bank S.A.' # - spi_code [string]: SPI code used to identify the institution on Pix transactions. ex: '20018183' # - str_code [string]: STR code used to identify the institution on TED transactions. ex: '123' - class Institution < StarkBank::Utils::SubResource + class Institution < StarkCore::Utils::SubResource attr_reader :display_name, :name, :spi_code, :str_code def initialize(display_name: nil, name: nil, spi_code: nil, str_code: nil) @display_name = display_name diff --git a/lib/invoice/invoice.rb b/lib/invoice/invoice.rb index 1fb272c..4f9270f 100644 --- a/lib/invoice/invoice.rb +++ b/lib/invoice/invoice.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # Invoice object @@ -40,7 +40,7 @@ module StarkBank # - status [string, default nil]: current Invoice status. ex: 'registered' or 'paid' # - created [DateTime, default nil]: creation datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # - updated [DateTime, default nil]: latest update datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Invoice < StarkBank::Utils::Resource + class Invoice < StarkCore::Utils::Resource attr_reader :amount, :tax_id, :name, :due, :expiration, :fine, :interest, :discounts, :tags, :pdf, :link, :descriptions, :nominal_amount, :fine_amount, :interest_amount, :discount_amount, :id, :brcode, :fee, :status, :transaction_ids, :created, :updated def initialize( amount:, tax_id:, name:, due: nil, expiration: nil, fine: nil, interest: nil, discounts: nil, @@ -49,7 +49,7 @@ def initialize( ) super(id) @amount = amount - @due = StarkBank::Utils::Checks.check_date_or_datetime(due) + @due = StarkCore::Utils::Checks.check_date_or_datetime(due) @tax_id = tax_id @name = name @expiration = expiration @@ -67,12 +67,12 @@ def initialize( @fee = fee @status = status @transaction_ids = transaction_ids - @updated = StarkBank::Utils::Checks.check_datetime(updated) - @created = StarkBank::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) + @created = StarkCore::Utils::Checks.check_datetime(created) if !discounts.nil? checked_discounts = [] discounts.each do |discount| - discount["due"] = StarkBank::Utils::Checks.check_date_or_datetime(discount["due"]) + discount["due"] = StarkCore::Utils::Checks.check_date_or_datetime(discount["due"]) checked_discounts.push(discount) end @discounts = checked_discounts @@ -159,8 +159,8 @@ def self.qrcode(id, user: nil) # ## Return: # - generator of Invoice objects with updated attributes def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -191,8 +191,8 @@ def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: # ## Return: # - list of Invoice objects with updated attributes and cursor to retrieve the next page of Invoice objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/invoice/log.rb b/lib/invoice/log.rb index 68dcf71..697d3ab 100644 --- a/lib/invoice/log.rb +++ b/lib/invoice/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('invoice') + module StarkBank class Invoice # # Invoice::Log object @@ -20,14 +20,14 @@ class Invoice # - errors [list of strings]: list of errors linked to this Invoice event # - type [string]: type of the Invoice event which triggered the log creation. ex: 'canceled' or 'paid' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :created, :type, :errors, :invoice def initialize(id:, created:, type:, errors:, invoice:) super(id) @type = type @errors = errors @invoice = invoice - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -61,8 +61,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, invoice_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -91,8 +91,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, invoice_ids: nil # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, invoice_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -131,7 +131,7 @@ def self.resource created: json['created'], type: json['type'], errors: json['errors'], - invoice: StarkBank::Utils::API.from_api_json(invoice_maker, json['invoice']) + invoice: StarkCore::Utils::API.from_api_json(invoice_maker, json['invoice']) ) } } diff --git a/lib/invoice/payment.rb b/lib/invoice/payment.rb index d49939b..495bc1c 100644 --- a/lib/invoice/payment.rb +++ b/lib/invoice/payment.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require_relative('../utils/sub_resource') require_relative('invoice') module StarkBank @@ -20,7 +19,7 @@ class Invoice # - account_type [string]: payer bank account type. ex: 'checking', 'savings', 'salary' or 'payment' # - end_to_end_id [string]: central bank's unique transaction ID. ex: 'E79457883202101262140HHX553UPqeq' # - method [string]: payment method that was used. ex: 'pix' - class Payment < StarkBank::Utils::SubResource + class Payment < StarkCore::Utils::SubResource attr_reader :name, :tax_id, :bank_code, :branch_code, :account_number, :account_type, :amount, :end_to_end_id, :method def initialize(name:, tax_id:, bank_code:, branch_code:, account_number:, account_type:, amount:, end_to_end_id:, method:) @name = name diff --git a/lib/key.rb b/lib/key.rb deleted file mode 100644 index 6defc52..0000000 --- a/lib/key.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -require('fileutils') -require('starkbank-ecdsa') - -module StarkBank - module Key - # # Generate a new key pair - # Generates a secp256k1 ECDSA private/public key pair to be used in the API - # authentications - # - # ## Parameters (optional): - # - path [string]: path to save the keys .pem files. No files will be saved if this parameter isn't provided - # - # ## Return: - # - private and public key pems - def self.create(path = nil) - private_key = EllipticCurve::PrivateKey.new - public_key = private_key.publicKey - - private_key_pem = private_key.toPem - public_key_pem = public_key.toPem - - unless path.nil? - FileUtils.mkdir_p(path) - File.write(File.join(path, 'private.pem'), private_key_pem) - File.write(File.join(path, 'public.pem'), public_key_pem) - end - - [private_key_pem, public_key_pem] - end - end -end diff --git a/lib/payment_preview/boleto_preview.rb b/lib/payment_preview/boleto_preview.rb index bde1bd2..e3b6b21 100644 --- a/lib/payment_preview/boleto_preview.rb +++ b/lib/payment_preview/boleto_preview.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -require_relative('../utils/sub_resource') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank class PaymentPreview @@ -26,7 +25,7 @@ class PaymentPreview # - payer_tax_id [string]: payer tax ID (CPF or CNPJ). ex: '20.018.183/0001-80' # - line [string]: Number sequence that identifies the payment. ex: '34191.09008 63571.277308 71444.640008 5 81960000000062' # - bar_code [string]: Bar code number that identifies the payment. ex: '34195819600000000621090063571277307144464000' - class BoletoPreview < StarkBank::Utils::SubResource + class BoletoPreview < StarkCore::Utils::SubResource attr_reader :status, :amount, :discount_amount, :fine_amount, :interest_amount, :due, :expiration, :name, :tax_id, :receiver_name, :receiver_tax_id, :payer_name, :payer_tax_id, :line, :bar_code def initialize(status:, amount:, discount_amount:, fine_amount:, interest_amount:, due:, expiration:, name:, tax_id:, receiver_name:, receiver_tax_id:, payer_name:, payer_tax_id:, line:, bar_code:) @status = status @@ -34,8 +33,8 @@ def initialize(status:, amount:, discount_amount:, fine_amount:, interest_amount @discount_amount = discount_amount @fine_amount = fine_amount @interest_amount = interest_amount - @due = StarkBank::Utils::Checks.check_datetime(due) - @expiration = StarkBank::Utils::Checks.check_datetime(expiration) + @due = StarkCore::Utils::Checks.check_datetime(due) + @expiration = StarkCore::Utils::Checks.check_datetime(expiration) @name = name @tax_id = tax_id @receiver_name = receiver_name diff --git a/lib/payment_preview/brcode_preview.rb b/lib/payment_preview/brcode_preview.rb index a788696..1a93203 100644 --- a/lib/payment_preview/brcode_preview.rb +++ b/lib/payment_preview/brcode_preview.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -require_relative('../utils/sub_resource') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank class PaymentPreview @@ -26,7 +25,7 @@ class PaymentPreview # - reduction_amount [integer]: Current value reduction value in cents that this payment is expecting. ex: 123 (= R$1,23) # - discount_amount [integer]: Current discount value in cents that this payment is expecting. ex: 123 (= R$1,23) # - reconciliation_id [string]: Reconciliation ID linked to this payment. ex: 'txId', 'payment-123' - class BrcodePreview < StarkBank::Utils::SubResource + class BrcodePreview < StarkCore::Utils::SubResource attr_reader :status, :name, :tax_id, :bank_code, :branch_code, :account_number, :account_type, :allow_change, :amount, :nominal_amount, :interest_amount, :fine_amount, :reduction_amount, :discount_amount, :reconciliation_id def initialize(status:, name:, tax_id:, bank_code:, branch_code:, account_number:, account_type:, allow_change:, amount:, nominal_amount:, interest_amount:, fine_amount:, reduction_amount:, discount_amount:, reconciliation_id:) @status = status diff --git a/lib/payment_preview/payment_preview.rb b/lib/payment_preview/payment_preview.rb index 38da2b0..b17fd5b 100644 --- a/lib/payment_preview/payment_preview.rb +++ b/lib/payment_preview/payment_preview.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # PaymentPreview object @@ -15,11 +15,11 @@ module StarkBank # - scheduled [DateTime or string]: intended payment date. Right now, this parameter only has effect on BrcodePreviews. ex: '2020-04-30' # - type [string]: Payment type. ex: 'brcode-payment', 'boleto-payment', 'utility-payment' or 'tax-payment' # - payment [BrcodePreview, BoletoPreview, UtilityPreview or TaxPreview]: Information preview of the informed payment. - class PaymentPreview < StarkBank::Utils::Resource + class PaymentPreview < StarkCore::Utils::Resource attr_reader :id, :scheduled, :type, :payment def initialize(id: nil, scheduled: nil, type: nil, payment: nil) super(id) - @scheduled = StarkBank::Utils::Checks.check_date(scheduled) + @scheduled = StarkCore::Utils::Checks.check_date(scheduled) @type = type @payment = payment return if type.nil? @@ -31,7 +31,7 @@ def initialize(id: nil, scheduled: nil, type: nil, payment: nil) 'utility-payment': StarkBank::PaymentPreview::UtilityPreview.resource }[type.to_sym] - @payment = StarkBank::Utils::API.from_api_json(resource[:resource_maker], payment) unless resource.nil? + @payment = StarkCore::Utils::API.from_api_json(resource[:resource_maker], payment) unless resource.nil? end # # Create PaymentPreviews diff --git a/lib/payment_preview/tax_preview.rb b/lib/payment_preview/tax_preview.rb index e61e205..a9b4255 100644 --- a/lib/payment_preview/tax_preview.rb +++ b/lib/payment_preview/tax_preview.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -require_relative('../utils/sub_resource') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank class PaymentPreview @@ -16,7 +15,7 @@ class PaymentPreview # - description [string]: tax payment description. ex: "ISS Payment - Iron Throne" # - line [string]: Number sequence that identifies the payment. ex: "85660000006 6 67940064007 5 41190025511 7 00010601813 8" # - bar_code [string]: Bar code number that identifies the payment. ex: "85660000006679400640074119002551100010601813" - class TaxPreview < StarkBank::Utils::SubResource + class TaxPreview < StarkCore::Utils::SubResource attr_reader :amount, :name, :description, :line, :bar_code def initialize(amount:, name:, description:, line:, bar_code:) @amount = amount diff --git a/lib/payment_preview/utility_preview.rb b/lib/payment_preview/utility_preview.rb index 90bf438..b4b5e5b 100644 --- a/lib/payment_preview/utility_preview.rb +++ b/lib/payment_preview/utility_preview.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -require_relative('../utils/sub_resource') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank class PaymentPreview @@ -16,7 +15,7 @@ class PaymentPreview # - description [string]: utility payment description. ex: "Utility Payment - Light Company" # - line [string]: Number sequence that identifies the payment. ex: "82660000002 8 44361143007 7 41190025511 7 00010601813 8" # - bar_code [string]: Bar code number that identifies the payment. ex: "82660000002443611430074119002551100010601813" - class UtilityPreview < StarkBank::Utils::SubResource + class UtilityPreview < StarkCore::Utils::SubResource attr_reader :amount, :name, :description, :line, :bar_code def initialize(amount:, name:, description:, line:, bar_code:) @amount = amount diff --git a/lib/payment_request/payment_request.rb b/lib/payment_request/payment_request.rb index c771623..da35c28 100644 --- a/lib/payment_request/payment_request.rb +++ b/lib/payment_request/payment_request.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # PaymentRequest object @@ -29,7 +29,7 @@ module StarkBank # - updated [DateTime, default nil]: latest update datetime for the PaymentRequest. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # - created [DateTime, default nil]: creation datetime for the PaymentRequest. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # - class PaymentRequest < StarkBank::Utils::Resource + class PaymentRequest < StarkCore::Utils::Resource attr_reader :center_id, :payment, :type, :due, :tags, :amount, :status, :actions, :updated, :created def initialize( payment:, center_id:, id: nil, type: nil, due: nil, tags: nil, amount: nil, status: nil, @@ -82,8 +82,8 @@ def self.create(payment_requests, user: nil) # ## Return: # - generator of PaymentRequest objects with updated attributes def self.query(center_id:, limit: nil, after: nil, before: nil, status: nil, type: nil, sort: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( center_id: center_id, limit: limit, @@ -117,8 +117,8 @@ def self.query(center_id:, limit: nil, after: nil, before: nil, status: nil, typ # ## Return: # - list of PaymentRequest objects with updated attributes and cursor to retrieve the next page of PaymentRequest objects def self.page(cursor: nil, center_id:, limit: nil, after: nil, before: nil, status: nil, type: nil, sort: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, center_id: center_id, @@ -156,7 +156,7 @@ def parse_payment(payment:, type:) 'darf-payment': StarkBank::DarfPayment.resource }[type.to_sym] - payment = StarkBank::Utils::API.from_api_json(resource[:resource_maker], payment) unless resource.nil? + payment = StarkCore::Utils::API.from_api_json(resource[:resource_maker], payment) unless resource.nil? [payment, type] end diff --git a/lib/starkbank.rb b/lib/starkbank.rb index ee10f27..190f666 100644 --- a/lib/starkbank.rb +++ b/lib/starkbank.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -require_relative('key') -require_relative('user/project') -require_relative('user/organization') require_relative('workspace/workspace') require_relative('balance/balance') require_relative('transaction/transaction') @@ -42,7 +39,19 @@ # SDK to facilitate Ruby integrations with Stark Bank module StarkBank + + API_VERSION = 'v2' + SDK_VERSION = '2.6.0' + HOST = "bank" + public_constant :API_VERSION, :SDK_VERSION, :HOST; + @user = nil @language = 'en-US' - class << self; attr_accessor :user, :language; end + @timeout = 15 + class << self; attr_accessor :user, :language, :timeout; end + + Project = StarkCore::Project + Organization = StarkCore::Organization + Key = StarkCore::Key + end diff --git a/lib/tax_payment/log.rb b/lib/tax_payment/log.rb index 45698ba..ef6814d 100644 --- a/lib/tax_payment/log.rb +++ b/lib/tax_payment/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('tax_payment') + module StarkBank class TaxPayment # # TaxPayment::Log object @@ -20,14 +20,14 @@ class TaxPayment # - errors [list of strings]: list of errors linked to this TaxPayment event # - type [string]: type of the TaxPayment event which triggered the log creation. ex: 'canceled' or 'paid' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :created, :type, :errors, :payment def initialize(id:, created:, type:, errors:, payment:) super(id) @type = type @errors = errors @payment = payment - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -61,8 +61,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -91,8 +91,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -115,7 +115,7 @@ def self.resource created: json['created'], type: json['type'], errors: json['errors'], - payment: StarkBank::Utils::API.from_api_json(tax_payment_maker, json['payment']) + payment: StarkCore::Utils::API.from_api_json(tax_payment_maker, json['payment']) ) } } diff --git a/lib/tax_payment/tax_payment.rb b/lib/tax_payment/tax_payment.rb index 1578c84..2e05aa7 100644 --- a/lib/tax_payment/tax_payment.rb +++ b/lib/tax_payment/tax_payment.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # TaxPayment object @@ -30,7 +30,7 @@ module StarkBank # - fee [integer, default nil]: fee charged when tax payment is created. ex: 200 (= R$ 2.00) # - created [DateTime, default nil]: creation datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # - updated [DateTime, default nil]: latest update datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class TaxPayment < StarkBank::Utils::Resource + class TaxPayment < StarkCore::Utils::Resource attr_reader :id, :line, :bar_code, :description, :tags, :scheduled, :status, :amount, :fee, :type, :updated, :created def initialize( id: nil, line: nil, bar_code: nil, description:, tags: nil, scheduled: nil, @@ -41,13 +41,13 @@ def initialize( @bar_code = bar_code @description = description @tags = tags - @scheduled = StarkBank::Utils::Checks.check_date(scheduled) + @scheduled = StarkCore::Utils::Checks.check_date(scheduled) @status = status @amount = amount @fee = fee @type = type - @updated = StarkBank::Utils::Checks.check_datetime(updated) - @created = StarkBank::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Create TaxPayments @@ -115,8 +115,8 @@ def self.pdf(id, user: nil) # ## Return: # - generator of TaxPayment objects with updated attributes def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -147,8 +147,8 @@ def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: # ## Return: # - list of Tax Payment objects with updated attributes and cursor to retrieve the next page of Tax Payment objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/transaction/transaction.rb b/lib/transaction/transaction.rb index 49a09a8..b7fece4 100644 --- a/lib/transaction/transaction.rb +++ b/lib/transaction/transaction.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # Transaction object @@ -31,7 +31,7 @@ module StarkBank # - fee [integer, default nil]: fee charged when transaction is created. ex: 200 (= R$ 2.00) # - balance [integer, default nil]: account balance after transaction was processed. ex: 100000000 (= R$ 1,000,000.00) # - created [DateTime, default nil]: creation datetime for the boleto. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Transaction < StarkBank::Utils::Resource + class Transaction < StarkCore::Utils::Resource attr_reader :amount, :description, :external_id, :receiver_id, :sender_id, :tags, :id, :fee, :created, :source def initialize(amount:, description:, external_id:, receiver_id:, sender_id: nil, tags: nil, id: nil, fee: nil, source: nil, balance: nil, created: nil) super(id) @@ -44,7 +44,7 @@ def initialize(amount:, description:, external_id:, receiver_id:, sender_id: nil @fee = fee @source = source @balance = balance - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Create Transactions @@ -95,8 +95,8 @@ def self.get(id, user: nil) # ## Return: # - generator of Transaction objects with updated attributes def self.query(limit: nil, after: nil, before: nil, tags: nil, external_ids: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -127,8 +127,8 @@ def self.query(limit: nil, after: nil, before: nil, tags: nil, external_ids: nil # ## Return: # - list of Transaction objects with updated attributes and cursor to retrieve the next page of Transaction objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, tags: nil, external_ids: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/transfer/log.rb b/lib/transfer/log.rb index 4eedba5..2c9276c 100644 --- a/lib/transfer/log.rb +++ b/lib/transfer/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('transfer') + module StarkBank class Transfer # # Transfer::Log object @@ -19,14 +19,14 @@ class Transfer # - errors [list of strings]: list of errors linked to this BoletoPayment event. # - type [string]: type of the Transfer event which triggered the log creation. ex: 'processing' or 'success' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :created, :type, :errors, :transfer def initialize(id:, created:, type:, errors:, transfer:) super(id) @type = type @errors = errors @transfer = transfer - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -60,8 +60,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, transfer_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -90,8 +90,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, transfer_ids: ni # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, transfer_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -114,7 +114,7 @@ def self.resource created: json['created'], type: json['type'], errors: json['errors'], - transfer: StarkBank::Utils::API.from_api_json(transfer_maker, json['transfer']) + transfer: StarkCore::Utils::API.from_api_json(transfer_maker, json['transfer']) ) } } diff --git a/lib/transfer/transfer.rb b/lib/transfer/transfer.rb index 6a01d59..3bedb9a 100644 --- a/lib/transfer/transfer.rb +++ b/lib/transfer/transfer.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # Transfer object @@ -33,7 +33,7 @@ module StarkBank # - transaction_ids [list of strings, default nil]: ledger transaction ids linked to this transfer (if there are two, second is the chargeback). ex: ['19827356981273'] # - created [DateTime, default nil]: creation datetime for the transfer. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # - updated [DateTime, default nil]: latest update datetime for the transfer. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Transfer < StarkBank::Utils::Resource + class Transfer < StarkCore::Utils::Resource attr_reader :amount, :name, :tax_id, :bank_code, :branch_code, :account_number, :account_type, :external_id, :scheduled, :description, :transaction_ids, :fee, :tags, :status, :id, :created, :updated def initialize(amount:, name:, tax_id:, bank_code:, branch_code:, account_number:, account_type: nil, external_id: nil, scheduled: nil, description: nil, transaction_ids: nil, fee: nil, tags: nil, status: nil, id: nil, created: nil, updated: nil) super(id) @@ -45,14 +45,15 @@ def initialize(amount:, name:, tax_id:, bank_code:, branch_code:, account_number @account_number = account_number @account_type = account_type @external_id = external_id - @scheduled = StarkBank::Utils::Checks.check_date_or_datetime(scheduled) + @scheduled = StarkCore::Utils::Checks.check_date_or_datetime(scheduled) @description = description @transaction_ids = transaction_ids @fee = fee @tags = tags @status = status - @created = StarkBank::Utils::Checks.check_datetime(created) - @updated = StarkBank::Utils::Checks.check_datetime(updated) + @transaction_ids = transaction_ids + @created = StarkCore::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) end # # Create Transfers @@ -138,8 +139,8 @@ def self.pdf(id, user: nil) # ## Return: # - generator of Transfer objects with updated attributes def self.query(limit: nil, after: nil, before: nil, transaction_ids: nil, status: nil, tax_id: nil, sort: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -173,8 +174,8 @@ def self.query(limit: nil, after: nil, before: nil, transaction_ids: nil, status # ## Return: # - list of Transfer objects with updated attributes and cursor to retrieve the next page of Transfer objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, transaction_ids: nil, status: nil, tax_id: nil, sort: nil, tags: nil, ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/user/organization.rb b/lib/user/organization.rb deleted file mode 100644 index 1e5a19c..0000000 --- a/lib/user/organization.rb +++ /dev/null @@ -1,54 +0,0 @@ -# frozen_string_literal: true - -require_relative('user') - -module StarkBank - # # Organization object - # The Organization object is an authentication entity for the SDK that - # represents your entire Organization, being able to access any Workspace - # underneath it and even create new Workspaces. Only a legal representative - # of your organization can register or change the Organization credentials. - # All requests to the Stark Bank API must be authenticated via an SDK user, - # which must have been previously created at the Stark Bank website - # [https://web.sandbox.starkbank.com] or [https://web.starkbank.com] - # before you can use it in this SDK. Organizations may be passed as the user parameter on - # each request or may be defined as the default user at the start (See README). - # If you are accessing a specific Workspace using Organization credentials, you should - # specify the workspace ID when building the Organization object or by request, using - # the Organization.replace(organization, workspace_id) method, which creates a copy of the organization - # object with the altered workspace ID. If you are listing or creating new Workspaces, the - # workspace_id should be nil. - # - # ## Parameters (required): - # - environment [string]: environment where the organization is being used. ex: 'sandbox' or 'production' - # - id [string]: unique id required to identify organization. ex: '5656565656565656' - # - private_key [string]: PEM string of the private key linked to the organization. ex: '-----BEGIN PUBLIC KEY-----\nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEyTIHK6jYuik6ktM9FIF3yCEYzpLjO5X/\ntqDioGM+R2RyW0QEo+1DG8BrUf4UXHSvCjtQ0yLppygz23z0yPZYfw==\n-----END PUBLIC KEY-----' - # - workspace_id [string]: unique id of the accessed Workspace, if any. ex: nil or '4848484848484848' - # - # ## Attributes (return-only): - # - pem [string]: private key in pem format. ex: '-----BEGIN PUBLIC KEY-----\nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEyTIHK6jYuik6ktM9FIF3yCEYzpLjO5X/\ntqDioGM+R2RyW0QEo+1DG8BrUf4UXHSvCjtQ0yLppygz23z0yPZYfw==\n-----END PUBLIC KEY-----' - class Organization < StarkBank::User - attr_reader :workspace_id - def initialize(id:, environment:, private_key:, workspace_id: nil) - super(environment, id, private_key) - @workspace_id = workspace_id - end - - def access_id - if @workspace_id - "organization/#{@id}/workspace/#{@workspace_id}" - else - "organization/#{@id}" - end - end - - def self.replace(organization, workspace_id) - Organization.new( - environment: organization.environment, - id: organization.id, - private_key: organization.pem, - workspace_id: workspace_id - ) - end - end -end diff --git a/lib/user/project.rb b/lib/user/project.rb deleted file mode 100644 index 03f045e..0000000 --- a/lib/user/project.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -require_relative('user') - -module StarkBank - # # Project object - # - # The Project object is an authentication entity for the SDK that is permanently - # linked to a specific Workspace. - # All requests to the Stark Bank API must be authenticated via an SDK user, - # which must have been previously created at the Stark Bank website - # [https://web.sandbox.starkbank.com] or [https://web.starkbank.com] - # before you can use it in this SDK. Projects may be passed as the user parameter on - # each request or may be defined as the default user at the start (See README). - # - # ## Parameters (required): - # - id [string]: unique id required to identify project. ex: '5656565656565656' - # - private_key [string]: PEM string of the private key linked to the project. ex: '-----BEGIN PUBLIC KEY-----\nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEyTIHK6jYuik6ktM9FIF3yCEYzpLjO5X/\ntqDioGM+R2RyW0QEo+1DG8BrUf4UXHSvCjtQ0yLppygz23z0yPZYfw==\n-----END PUBLIC KEY-----' - # - environment [string]: environment where the project is being used. ex: 'sandbox' or 'production' - # - # ## Attributes (return-only): - # - name [string, default '']: project name. ex: 'MyProject' - # - allowed_ips [list of strings]: list containing the strings of the ips allowed to make requests on behalf of this project. ex: ['190.190.0.50'] - # - pem [string]: private key in pem format. ex: '-----BEGIN PUBLIC KEY-----\nMFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEyTIHK6jYuik6ktM9FIF3yCEYzpLjO5X/\ntqDioGM+R2RyW0QEo+1DG8BrUf4UXHSvCjtQ0yLppygz23z0yPZYfw==\n-----END PUBLIC KEY-----' - class Project < StarkBank::User - attr_reader :name, :allowed_ips - def initialize(environment:, id:, private_key:, name: '', allowed_ips: nil) - super(environment, id, private_key) - @name = name - @allowed_ips = allowed_ips - end - - def access_id - "project/#{@id}" - end - end -end diff --git a/lib/user/user.rb b/lib/user/user.rb deleted file mode 100644 index beb8ce5..0000000 --- a/lib/user/user.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -require('starkbank-ecdsa') -require_relative('../utils/resource') - -module StarkBank - class User < StarkBank::Utils::Resource - attr_reader :pem, :environment - def initialize(environment, id, private_key) - require_relative('../utils/checks') - super(id) - @pem = StarkBank::Utils::Checks.check_private_key(private_key) - @environment = StarkBank::Utils::Checks.check_environment(environment) - end - - def private_key - EllipticCurve::PrivateKey.fromPem(@pem) - end - end -end diff --git a/lib/utility_payment/log.rb b/lib/utility_payment/log.rb index a4f5cc8..65cd9bf 100644 --- a/lib/utility_payment/log.rb +++ b/lib/utility_payment/log.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') require_relative('utility_payment') + module StarkBank class UtilityPayment # # UtilityPayment::Log object @@ -19,14 +19,14 @@ class UtilityPayment # - errors [list of strings]: list of errors linked to this UtilityPayment event. # - type [string]: type of the UtilityPayment event which triggered the log creation. ex: 'processing' or 'success' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class Log < StarkBank::Utils::Resource + class Log < StarkCore::Utils::Resource attr_reader :id, :created, :type, :errors, :payment def initialize(id:, created:, type:, errors:, payment:) super(id) @type = type @errors = errors @payment = payment - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Retrieve a specific Log @@ -60,8 +60,8 @@ def self.get(id, user: nil) # ## Return: # - list of Log objects with updated attributes def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -90,8 +90,8 @@ def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil # ## Return: # - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, @@ -114,7 +114,7 @@ def self.resource created: json['created'], type: json['type'], errors: json['errors'], - payment: StarkBank::Utils::API.from_api_json(payment_maker, json['payment']) + payment: StarkCore::Utils::API.from_api_json(payment_maker, json['payment']) ) } } diff --git a/lib/utility_payment/utility_payment.rb b/lib/utility_payment/utility_payment.rb index 3afdb9f..7609354 100644 --- a/lib/utility_payment/utility_payment.rb +++ b/lib/utility_payment/utility_payment.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # UtilityPayment object @@ -28,7 +28,7 @@ module StarkBank # - amount [int, default nil]: amount automatically calculated from line or bar_code. ex: 23456 (= R$ 234.56) # - fee [integer, default nil]: fee charged when utility payment is created. ex: 200 (= R$ 2.00) # - created [DateTime, default nil]: creation datetime for the payment. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - class UtilityPayment < StarkBank::Utils::Resource + class UtilityPayment < StarkCore::Utils::Resource attr_reader :description, :line, :bar_code, :tags, :scheduled, :id, :amount, :fee, :status, :created def initialize(description:, line: nil, bar_code: nil, tags: nil, scheduled: nil, id: nil, amount: nil, fee: nil, status: nil, created: nil) super(id) @@ -36,11 +36,11 @@ def initialize(description:, line: nil, bar_code: nil, tags: nil, scheduled: nil @line = line @bar_code = bar_code @tags = tags - @scheduled = StarkBank::Utils::Checks.check_date(scheduled) + @scheduled = StarkCore::Utils::Checks.check_date(scheduled) @amount = amount @fee = fee @status = status - @created = StarkBank::Utils::Checks.check_datetime(created) + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Create UtilityPayments @@ -108,8 +108,8 @@ def self.pdf(id, user: nil) # ## Return: # - generator of UtilityPayment objects with updated attributes def self.query(limit: nil, after: nil, before: nil, tags: nil, ids: nil, status: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) StarkBank::Utils::Rest.get_stream( limit: limit, after: after, @@ -140,8 +140,8 @@ def self.query(limit: nil, after: nil, before: nil, tags: nil, ids: nil, status: # ## Return: # - list of UtilityPayment objects with updated attributes and cursor to retrieve the next page of UtilityPayment objects def self.page(cursor: nil, limit: nil, after: nil, before: nil, tags: nil, ids: nil, status: nil, user: nil) - after = StarkBank::Utils::Checks.check_date(after) - before = StarkBank::Utils::Checks.check_date(before) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) return StarkBank::Utils::Rest.get_page( cursor: cursor, limit: limit, diff --git a/lib/utils/api.rb b/lib/utils/api.rb deleted file mode 100644 index 1ccc0bc..0000000 --- a/lib/utils/api.rb +++ /dev/null @@ -1,79 +0,0 @@ -# frozen_string_literal: true - -require_relative('case') - -module StarkBank - module Utils - module API - def self.build_entity_hash(entity) - if entity.is_a?(Hash) - entity_hash = entity - else - entity_hash = {} - entity.instance_variables.each do |key| - variable = entity.instance_variable_get(key) - entity_hash[key[1..-1]] = variable.is_a?(StarkBank::Utils::Resource) ? build_entity_hash(variable) : entity.instance_variable_get(key) - end - end - entity_hash - end - - def self.api_json(entity) - built_hash = build_entity_hash(entity) - cast_json_to_api_format(built_hash) - end - - def self.cast_json_to_api_format(hash) - entity_hash = {} - hash.each do |key, value| - next if value.nil? - - entity_hash[StarkBank::Utils::Case.snake_to_camel(key)] = parse_value(value) - end - entity_hash - end - - def self.parse_value(value) - return value.strftime('%Y-%m-%d') if value.is_a?(Date) - return value.strftime('%Y-%m-%dT%H:%M:%S+00:00') if value.is_a?(DateTime) || value.is_a?(Time) - return cast_json_to_api_format(value) if value.is_a?(Hash) - return value unless value.is_a?(Array) - - list = [] - value.each do |v| - list << (v.is_a?(Hash) ? cast_json_to_api_format(v) : v) - end - list - end - - def self.from_api_json(resource_maker, json) - snakes = {} - json.each do |key, value| - snakes[StarkBank::Utils::Case.camel_to_snake(key)] = value - end - resource_maker.call(snakes) - end - - def self.endpoint(resource_name) - kebab = StarkBank::Utils::Case.camel_to_kebab(resource_name) - kebab.sub!('-log', '/log') - kebab.sub!('-attempt', '/attempt') - kebab - end - - def self.last_name_plural(resource_name) - base = last_name(resource_name) - - return base if base[-1].eql?('s') - return "#{base}s" if base[-2..-1].eql?('ey') - return "#{base[0...-1]}ies" if base[-1].eql?('y') - - "#{base}s" - end - - def self.last_name(resource_name) - StarkBank::Utils::Case.camel_to_kebab(resource_name).split('-').last - end - end - end -end diff --git a/lib/utils/cache.rb b/lib/utils/cache.rb deleted file mode 100644 index 4674253..0000000 --- a/lib/utils/cache.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -module StarkBank - module Utils - module Cache - @starkbank_public_key = nil - class << self; attr_accessor :starkbank_public_key; end - end - end -end diff --git a/lib/utils/case.rb b/lib/utils/case.rb deleted file mode 100644 index f5fbdf5..0000000 --- a/lib/utils/case.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -module StarkBank - module Utils - module Case - def self.camel_to_snake(camel) - camel.to_s.gsub(/([a-z])([A-Z\d])/, '\1_\2').downcase - end - - def self.snake_to_camel(snake) - camel = snake.to_s.split('_').map(&:capitalize).join - camel[0] = camel[0].downcase - camel - end - - def self.camel_to_kebab(camel) - camel_to_snake(camel).tr('_', '-') - end - end - end -end diff --git a/lib/utils/checks.rb b/lib/utils/checks.rb deleted file mode 100644 index ecbb799..0000000 --- a/lib/utils/checks.rb +++ /dev/null @@ -1,101 +0,0 @@ -# frozen_string_literal: true - -require('date') -require('starkbank-ecdsa') -require_relative('environment') -require_relative('../user/user') - -module StarkBank - module Utils - class Checks - def self.check_user(user) - return user if user.is_a?(StarkBank::User) - - user = user.nil? ? StarkBank.user : user - raise(ArgumentError, 'A user is required to access our API. Check our README: https://github.com/starkbank/sdk-ruby/') if user.nil? - - user - end - - def self.check_language - language = StarkBank.language - accepted_languages = %w[en-US pt-BR] - raise(ArgumentError, "Select a valid language: #{accepted_languages.join(', ')}") unless accepted_languages.include?(language) - - language - end - - def self.check_environment(environment) - environments = StarkBank::Utils::Environment.constants(false).map { |c| StarkBank::Utils::Environment.const_get(c) } - raise(ArgumentError, "Select a valid environment: #{environments.join(', ')}") unless environments.include?(environment) - - environment - end - - def self.check_private_key(pem) - EllipticCurve::PrivateKey.fromPem(pem) - pem - rescue - raise(ArgumentError, 'Private-key must be a valid secp256k1 ECDSA string in pem format') - end - - def self.check_date_or_datetime(data) - return if data.nil? - - return data if data.is_a?(Time) || data.is_a?(DateTime) - - return data if data.is_a?(Date) - - data, type = check_datetime_string(data) - type == 'date' ? Date.new(data.year, data.month, data.day) : data - end - - def self.check_datetime(data) - return if data.nil? - - return data if data.is_a?(Time) || data.is_a?(DateTime) - - return Time.new(data.year, data.month, data.day) if data.is_a?(Date) - - data, _type = check_datetime_string(data) - data - end - - def self.check_date(data) - return if data.nil? - - return Date.new(data.year, data.month, data.day) if data.is_a?(Time) || data.is_a?(DateTime) - - return data if data.is_a?(Date) - - data, type = check_datetime_string(data) - - type == 'date' ? Date.new(data.year, data.month, data.day) : data - end - - class << self - private - - def check_datetime_string(data) - data = data.to_s - - begin - return [DateTime.strptime(data, '%Y-%m-%dT%H:%M:%S.%L+00:00'), 'datetime'] - rescue ArgumentError - end - - begin - return [DateTime.strptime(data, '%Y-%m-%dT%H:%M:%S+00:00'), 'datetime'] - rescue ArgumentError - end - - begin - return [DateTime.strptime(data, '%Y-%m-%d'), 'date'] - rescue ArgumentError - raise(ArgumentError, 'invalid datetime string ' + data) - end - end - end - end - end -end diff --git a/lib/utils/environment.rb b/lib/utils/environment.rb deleted file mode 100644 index 1c80ca6..0000000 --- a/lib/utils/environment.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -module StarkBank - module Utils - class Environment - PRODUCTION = 'production' - public_constant :PRODUCTION - - SANDBOX = 'sandbox' - public_constant :SANDBOX - end - end -end diff --git a/lib/utils/parse.rb b/lib/utils/parse.rb new file mode 100755 index 0000000..1ad1610 --- /dev/null +++ b/lib/utils/parse.rb @@ -0,0 +1,35 @@ +require('starkcore') + +module StarkBank + module Utils + module Parse + def self.parse_and_verify(content:, signature:, user: nil, resource:, key: nil) + return StarkCore::Utils::Parse.parse_and_verify( + content: content, + signature: signature, + sdk_version: StarkBank::SDK_VERSION, + api_version: StarkBank::API_VERSION, + host: StarkBank::HOST, + resource: resource, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.language, + key: key + ) + end + + def self.verify(content:, signature:, user: nil) + return StarkCore::Utils::Parse.verify( + content: content, + signature: signature, + sdk_version: StarkBank::SDK_VERSION, + api_version: StarkBank::API_VERSION, + host: StarkBank::HOST, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.language + ) + end + end + end +end diff --git a/lib/utils/request.rb b/lib/utils/request.rb deleted file mode 100644 index ad7477e..0000000 --- a/lib/utils/request.rb +++ /dev/null @@ -1,79 +0,0 @@ -# frozen_string_literal: true - -require('json') -require('starkbank-ecdsa') -require('net/http') -require_relative('url') -require_relative('checks') -require_relative('../error') - -module StarkBank - module Utils - module Request - class Response - attr_reader :status, :content - def initialize(status, content) - @status = status - @content = content - end - - def json - JSON.parse(@content) - end - end - - def self.fetch(method:, path:, payload: nil, query: nil, user: nil) - user = Checks.check_user(user) - language = Checks.check_language - - base_url = { - Environment::PRODUCTION => 'https://api.starkbank.com/', - Environment::SANDBOX => 'https://sandbox.api.starkbank.com/' - }[user.environment] + 'v2' - - url = "#{base_url}/#{path}#{StarkBank::Utils::URL.urlencode(query)}" - uri = URI(url) - - access_time = Time.now.to_i - body = payload.nil? ? '' : payload.to_json - message = "#{user.access_id}:#{access_time}:#{body}" - signature = EllipticCurve::Ecdsa.sign(message, user.private_key).toBase64 - - case method - when 'GET' - req = Net::HTTP::Get.new(uri) - when 'DELETE' - req = Net::HTTP::Delete.new(uri) - when 'POST' - req = Net::HTTP::Post.new(uri) - req.body = body - when 'PATCH' - req = Net::HTTP::Patch.new(uri) - req.body = body - when 'PUT' - req = Net::HTTP::Put.new(uri) - req.body = body - else - raise(ArgumentError, 'unknown HTTP method ' + method) - end - - req['Access-Id'] = user.access_id - req['Access-Time'] = access_time - req['Access-Signature'] = signature - req['Content-Type'] = 'application/json' - req['User-Agent'] = "Ruby-#{RUBY_VERSION}-SDK-2.6.0" - req['Accept-Language'] = language - - request = Net::HTTP.start(uri.hostname, use_ssl: true) { |http| http.request(req) } - - response = Response.new(Integer(request.code, 10), request.body) - - raise(StarkBank::Error::InternalServerError) if response.status == 500 - raise(StarkBank::Error::InputErrors, response.json['errors']) if response.status == 400 - raise(StarkBank::Error::UnknownError, response.content) unless response.status == 200 - - response - end - end - end -end diff --git a/lib/utils/resource.rb b/lib/utils/resource.rb deleted file mode 100644 index d79a637..0000000 --- a/lib/utils/resource.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true -require_relative("sub_resource") - -module StarkBank - module Utils - class Resource < StarkBank::Utils::SubResource - attr_reader :id - def initialize(id = nil) - @id = id - end - end - end -end diff --git a/lib/utils/rest.rb b/lib/utils/rest.rb old mode 100644 new mode 100755 index 79e1d12..5293fac --- a/lib/utils/rest.rb +++ b/lib/utils/rest.rb @@ -1,135 +1,158 @@ # frozen_string_literal: true -require_relative('request') -require_relative('api') +require('starkcore') module StarkBank module Utils module Rest - def self.get_page(resource_name:, resource_maker:, user: nil, **query) - json = StarkBank::Utils::Request.fetch( - method: 'GET', - path: StarkBank::Utils::API.endpoint(resource_name), - query: query, - user: user - ).json - entities = [] - json[StarkBank::Utils::API.last_name_plural(resource_name)].each do |entity_json| - entities << StarkBank::Utils::API.from_api_json(resource_maker, entity_json) - end - return entities, json['cursor'] - end - - def self.get_stream(resource_name:, resource_maker:, user: nil, **query) - limit = query[:limit] - query[:limit] = limit.nil? ? limit : [limit, 100].min - - Enumerator.new do |enum| - loop do - json = StarkBank::Utils::Request.fetch( - method: 'GET', - path: StarkBank::Utils::API.endpoint(resource_name), - query: query, - user: user - ).json - entities = json[StarkBank::Utils::API.last_name_plural(resource_name)] - entities.each do |entity| - enum << StarkBank::Utils::API.from_api_json(resource_maker, entity) - end + def self.get_page(resource_name:, resource_maker:, user:, **query) + return StarkCore::Utils::Rest.get_page( + resource_name: resource_name, + resource_maker: resource_maker, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + **query + ) + end - unless limit.nil? - limit -= 100 - query[:limit] = [limit, 100].min - end + def self.get_stream(resource_name:, resource_maker:, user:, **query) + return StarkCore::Utils::Rest.get_stream( + resource_name: resource_name, + resource_maker: resource_maker, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + **query + ) + end - cursor = json['cursor'] - query['cursor'] = cursor - break if cursor.nil? || cursor.empty? || (!limit.nil? && limit <= 0) - end - end + def self.get_id(resource_name:, resource_maker:, user:, id:, **query) + return StarkCore::Utils::Rest.get_id( + resource_name: resource_name, + resource_maker: resource_maker, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + id: id, + **query + ) end - def self.get_id(resource_name:, resource_maker:, id:, user: nil) - json = StarkBank::Utils::Request.fetch( - method: 'GET', - path: "#{StarkBank::Utils::API.endpoint(resource_name)}/#{id}", - user: user - ).json - entity = json[StarkBank::Utils::API.last_name(resource_name)] - StarkBank::Utils::API.from_api_json(resource_maker, entity) + def self.get_content(resource_name:, resource_maker:, user:, sub_resource_name:, id:, **query) + return StarkCore::Utils::Rest.get_content( + resource_name: resource_name, + resource_maker: resource_maker, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + sub_resource_name: sub_resource_name, + id: id, + **query + ) end - def self.get_content(resource_name:, resource_maker:, sub_resource_name:, id:, user: nil, **query) - StarkBank::Utils::Request.fetch( - method: 'GET', - path: "#{StarkBank::Utils::API.endpoint(resource_name)}/#{id}/#{sub_resource_name}", - query: StarkBank::Utils::API.cast_json_to_api_format(query), - user: user - ).content + def self.get_sub_resource(resource_name:, sub_resource_maker:, sub_resource_name:, user:, id:, **query) + return StarkCore::Utils::Rest.get_sub_resource( + resource_name: resource_name, + sub_resource_maker: sub_resource_maker, + sub_resource_name: sub_resource_name, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + id: id, + **query + ) end - def self.post(resource_name:, resource_maker:, entities:, user: nil) - jsons = [] - entities.each do |entity| - jsons << StarkBank::Utils::API.api_json(entity) - end - payload = { StarkBank::Utils::API.last_name_plural(resource_name) => jsons } - json = StarkBank::Utils::Request.fetch( - method: 'POST', - path: StarkBank::Utils::API.endpoint(resource_name), - payload: payload, - user: user - ).json - returned_jsons = json[StarkBank::Utils::API.last_name_plural(resource_name)] - entities = [] - returned_jsons.each do |returned_json| - entities << StarkBank::Utils::API.from_api_json(resource_maker, returned_json) - end - entities + def self.get_sub_resources(resource_name:, sub_resource_maker:, sub_resource_name:, user:, id:, **query) + return StarkCore::Utils::Rest.get_sub_resource( + resource_name: resource_name, + sub_resource_maker: sub_resource_maker, + sub_resource_name: sub_resource_name, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + id: id, + **query + ) end - def self.post_single(resource_name:, resource_maker:, entity:, user: nil) - json = StarkBank::Utils::Request.fetch( - method: 'POST', - path: StarkBank::Utils::API.endpoint(resource_name), - payload: StarkBank::Utils::API.api_json(entity), - user: user - ).json - entity_json = json[StarkBank::Utils::API.last_name(resource_name)] - StarkBank::Utils::API.from_api_json(resource_maker, entity_json) + def self.post(resource_name:, resource_maker:, user:, entities:, **query) + return StarkCore::Utils::Rest.post( + resource_name: resource_name, + resource_maker: resource_maker, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + entities: entities, + **query + ) end - def self.delete_id(resource_name:, resource_maker:, id:, user: nil) - json = StarkBank::Utils::Request.fetch( - method: 'DELETE', - path: "#{StarkBank::Utils::API.endpoint(resource_name)}/#{id}", - user: user - ).json - entity = json[StarkBank::Utils::API.last_name(resource_name)] - StarkBank::Utils::API.from_api_json(resource_maker, entity) + def self.post_single(resource_name:, resource_maker:, user:, entity:) + return StarkCore::Utils::Rest.post_single( + resource_name: resource_name, + resource_maker: resource_maker, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + entity: entity + ) end - def self.patch_id(resource_name:, resource_maker:, id:, user: nil, **payload) - json = StarkBank::Utils::Request.fetch( - method: 'PATCH', - path: "#{StarkBank::Utils::API.endpoint(resource_name)}/#{id}", - user: user, - payload: StarkBank::Utils::API.cast_json_to_api_format(payload) - ).json - entity = json[StarkBank::Utils::API.last_name(resource_name)] - StarkBank::Utils::API.from_api_json(resource_maker, entity) + def self.delete_id(resource_name:, resource_maker:, user:, id:) + return StarkCore::Utils::Rest.delete_id( + resource_name: resource_name, + resource_maker: resource_maker, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + id: id + ) end - def self.get_sub_resource(resource_name:, sub_resource_maker:, sub_resource_name:, id:, user: nil, **query) - json = StarkBank::Utils::Request.fetch( - method: 'GET', - path: "#{StarkBank::Utils::API.endpoint(resource_name)}/#{id}/#{StarkBank::Utils::API.endpoint(sub_resource_name)}", - user: user, - query: StarkBank::Utils::API.cast_json_to_api_format(query) - ).json - entity = json[StarkBank::Utils::API.last_name(sub_resource_name)] - StarkBank::Utils::API.from_api_json(sub_resource_maker, entity) + def self.patch_id(resource_name:, resource_maker:, user:, id:, **payload) + return StarkCore::Utils::Rest.patch_id( + resource_name: resource_name, + resource_maker: resource_maker, + sdk_version: StarkBank::SDK_VERSION, + host: StarkBank::HOST, + api_version: StarkBank::API_VERSION, + user: user ? user : StarkBank.user, + language: StarkBank.language, + timeout: StarkBank.timeout, + id: id, + **payload + ) end end end diff --git a/lib/utils/sub_resource.rb b/lib/utils/sub_resource.rb deleted file mode 100644 index a16fe3f..0000000 --- a/lib/utils/sub_resource.rb +++ /dev/null @@ -1,28 +0,0 @@ - - -module StarkBank - module Utils - class SubResource - def to_s - string_vars = [] - instance_variables.each do |key| - value = instance_variable_get(key).to_s.lines.map(&:chomp).join("\n ") - string_vars << "#{key[1..-1]}: #{value}" - end - fields = string_vars.join(",\n ") - "#{class_name}(\n #{fields}\n)" - end - - def inspect - "#{class_name}[#{@id}]" - end - - private - - def class_name - self.class.name.split('::').last.downcase - end - end - end -end - \ No newline at end of file diff --git a/lib/utils/url.rb b/lib/utils/url.rb deleted file mode 100644 index 3ae134e..0000000 --- a/lib/utils/url.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true -require 'erb' - - -module StarkBank - module Utils - module URL - # generates query string from hash - def self.urlencode(params) - return '' if params.nil? - - params = StarkBank::Utils::API.cast_json_to_api_format(params) - return '' if params.empty? - - string_params = {} - params.each do |key, value| - string_params[key] = value.is_a?(Array) ? value.join(',') : value - end - - query_list = [] - string_params.each do |key, value| - query_list << "#{key}=#{ERB::Util.url_encode(value)}" - end - '?' + query_list.join('&') - end - end - end -end diff --git a/lib/webhook/webhook.rb b/lib/webhook/webhook.rb index b008bb1..a62e7f5 100644 --- a/lib/webhook/webhook.rb +++ b/lib/webhook/webhook.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # Webhook subscription object @@ -17,7 +17,7 @@ module StarkBank # # ## Attributes: # - id [string, default nil]: unique id returned when the webhook is created. ex: '5656565656565656' - class Webhook < StarkBank::Utils::Resource + class Webhook < StarkCore::Utils::Resource attr_reader :url, :subscriptions, :id def initialize(url:, subscriptions:, id: nil) super(id) diff --git a/lib/workspace/workspace.rb b/lib/workspace/workspace.rb index c1c5b64..83a90cb 100644 --- a/lib/workspace/workspace.rb +++ b/lib/workspace/workspace.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative('../utils/resource') +require('starkcore') require_relative('../utils/rest') -require_relative('../utils/checks') + module StarkBank # # Workspace object @@ -20,7 +20,7 @@ module StarkBank # # ## Attributes: # - id [string, default nil]: unique id returned when the workspace is created. ex: '5656565656565656' - class Workspace < StarkBank::Utils::Resource + class Workspace < StarkCore::Utils::Resource attr_reader :username, :name, :allowed_tax_ids, :id def initialize(username:, name:, allowed_tax_ids: nil, id: nil) super(id) diff --git a/starkbank.gemspec b/starkbank.gemspec index 6b3cf22..f401b0e 100644 --- a/starkbank.gemspec +++ b/starkbank.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.files = Dir['lib/**/*.rb'] s.license = 'MIT' s.required_ruby_version = '>= 2.3' - s.add_dependency('starkbank-ecdsa', '~> 0.0.5') + s.add_dependency('starkcore', '~> 0.0.1') s.add_development_dependency('minitest', '~> 5.14.1') s.add_development_dependency('rake', '~> 13.0') s.add_development_dependency('rubocop', '~> 0.81') diff --git a/test/starkbank/test_institution.rb b/test/starkbank/test_institution.rb index b58cdaf..3f1dc24 100644 --- a/test/starkbank/test_institution.rb +++ b/test/starkbank/test_institution.rb @@ -5,7 +5,7 @@ describe(StarkBank::Institution, '#institution#') do it 'query' do institutions = StarkBank::Institution.query(search: 'stark').to_a - expect(institutions.length).must_equal(1) + expect(institutions.length).must_equal(2) institutions = StarkBank::Institution.query(spi_codes: '20018183').to_a expect(institutions.length).must_equal(1)