Skip to content

Commit

Permalink
Merge pull request #11 from dreyks/corp-api
Browse files Browse the repository at this point in the history
Add corporate api
  • Loading branch information
dreyks authored Feb 23, 2024
2 parents 6749af8 + 55dd7ce commit 51774cd
Show file tree
Hide file tree
Showing 26 changed files with 835 additions and 14 deletions.
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ PATH
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
bigdecimal (3.1.6)
crack (0.4.6)
bigdecimal
rexml
diff-lcs (1.5.0)
hashdiff (1.1.0)
httparty (0.21.0)
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
mini_mime (1.1.5)
multi_xml (0.6.0)
public_suffix (5.0.4)
rake (13.1.0)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
Expand All @@ -27,6 +36,10 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
webmock (3.19.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
arm64-darwin-22
Expand All @@ -36,6 +49,7 @@ DEPENDENCIES
monobank!
rake (~> 13.0, >= 13.0.0)
rspec (~> 3.8, >= 3.8.0)
webmock

BUNDLED WITH
2.5.5
1 change: 1 addition & 0 deletions lib/monobank.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'monobank/version'
require 'monobank/client'
require 'monobank/corporate'
require 'forwardable'

module Monobank
Expand Down
35 changes: 35 additions & 0 deletions lib/monobank/auth/corporate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Monobank
module Auth
class Corporate
def initialize(private_key:, key_id: nil, request_id: nil)
@private_key = init_key(private_key:)
@key_id = key_id
@request_id = request_id
end

def to_headers(pathname:)
time = Time.now.to_i

{
'X-Time' => time.to_s,
'X-Sign' => Base64.strict_encode64(sign(pathname:, time:))
}.tap do |headers|
headers['X-Key-Id'] = key_id unless key_id.nil?
headers['X-Request-Id'] = request_id unless request_id.nil?
end
end

private

attr_accessor :private_key, :key_id, :request_id

def init_key(private_key:)
OpenSSL::PKey::EC.new(private_key)
end

def sign(pathname:, time:)
private_key.sign(OpenSSL::Digest::SHA256.new, "#{time}#{request_id}#{pathname}")
end
end
end
end
17 changes: 17 additions & 0 deletions lib/monobank/auth/private.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Monobank
module Auth
class Private
def initialize(token:)
@token = token
end

def to_headers(*)
{ 'X-Token' => token }
end

private

attr_reader :token
end
end
end
13 changes: 10 additions & 3 deletions lib/monobank/client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'monobank/connection'
require 'monobank/auth/private'
require 'monobank/bank/currency'
require 'monobank/personal/client_info'
require 'monobank/personal/statement'
Expand All @@ -11,15 +12,21 @@ def bank_currency
end

def client_info(token:)
Personal::ClientInfo.new(token: token).call
Personal::ClientInfo.new(auth: auth(token:)).call
end

def statement(token:, account_id:, from:, to: nil)
Personal::Statement.new(token: token, account_id: account_id, from: from, to: to).call
Personal::Statement.new(account_id:, from:, to:, auth: auth(token:)).call
end

def set_webhook(token:, url:)
Personal::Webhook.new(token: token, url: url).call
Personal::Webhook.new(url:, auth: auth(token:)).call
end

private

def auth(token:)
Auth::Private.new(token:)
end
end
end
21 changes: 21 additions & 0 deletions lib/monobank/corporate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'forwardable'
require 'monobank/corporate/client'

module Monobank
module Corporate
extend SingleForwardable
def_delegators \
:client, :registration, :registration_status, :set_webhook, :settings,
:auth_request, :auth_check, :client_info, :statement

def self.configure(private_key:, key_id: nil)
@private_key = private_key
@key_id = key_id
@client = nil
end

def self.client
@client ||= Client.new(private_key: @private_key, key_id: @key_id)
end
end
end
60 changes: 60 additions & 0 deletions lib/monobank/corporate/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'monobank/auth/corporate'
require 'monobank/personal/registration'
require 'monobank/personal/registration_status'
require 'monobank/personal/corporate_webhook'
require 'monobank/personal/settings'
require 'monobank/personal/auth_request'
require 'monobank/personal/auth_check'
require 'monobank/personal/client_info'
require 'monobank/personal/statement'

module Monobank
module Corporate
class Client
def initialize(private_key:, key_id:)
@private_key = private_key
@key_id = key_id
end

def registration(public_key:, name:, description:, contact_person:, phone:, email:, logo:)
Personal::Registration.new(public_key:, name:, description:, contact_person:, phone:, email:, logo:, auth:).call
end

def registration_status(public_key:)
Personal::RegistrationStatus.new(public_key:, auth:).call
end

def set_webhook(url:)
Personal::CorporateWebhook.new(url: url, auth:).call
end

def settings
Personal::Settings.new(auth:).call
end

def auth_request(callback: nil)
Personal::AuthRequest.new(callback:, auth:).call
end

def auth_check(request_id:)
Personal::AuthCheck.new(auth: auth(request_id:)).call
end

def client_info(request_id:)
Personal::ClientInfo.new(auth: auth(request_id:)).call
end

def statement(request_id:, account_id:, from:, to: nil)
Personal::Statement.new(account_id:, from:, to:, auth: auth(request_id:)).call
end

private

attr_reader :private_key, :key_id

def auth(request_id: nil)
Auth::Corporate.new(private_key:, key_id:, request_id:)
end
end
end
end
16 changes: 14 additions & 2 deletions lib/monobank/methods/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module Monobank
module Methods
class Base
def initialize(auth: nil)
@auth = auth
end

def call
attributes = response
return define_resources(attributes) if attributes.code == 200
Expand All @@ -12,19 +16,27 @@ def call

private

attr_reader :token
attr_reader :auth

def pathname
raise NotImplementedError
end

def response
raise NotImplementedError
end

def options
{
headers: { 'X-Token' => token.to_s },
headers: (headers || {}).merge(auth&.to_headers(pathname:) || {}),
body: body.to_json
}
end

def headers
{}
end

def body; end

def connection
Expand Down
22 changes: 22 additions & 0 deletions lib/monobank/personal/auth_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'monobank/methods/get'
require 'monobank/resources/personal/auth_check'

module Monobank
module Personal
class AuthCheck < Methods::Get
ENDPOINT = '/personal/auth/request'.freeze

private

attr_reader :request_id

def pathname
ENDPOINT
end

def define_resources(attributes)
Monobank::Resources::Personal::AuthCheck.new(attributes)
end
end
end
end
31 changes: 31 additions & 0 deletions lib/monobank/personal/auth_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'monobank/methods/post'
require 'monobank/resources/personal/auth_request'

module Monobank
module Personal
class AuthRequest < Methods::Post
ENDPOINT = '/personal/auth/request'.freeze

def initialize(callback: nil, **rest)
super(**rest)
@callback = callback
end

private

attr_reader :callback

def pathname
ENDPOINT
end

def headers
{'X-Callback' => callback} if callback
end

def define_resources(attributes)
Monobank::Resources::Personal::AuthRequest.new(attributes)
end
end
end
end
4 changes: 0 additions & 4 deletions lib/monobank/personal/client_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ module Personal
class ClientInfo < Methods::Get
ENDPOINT = '/personal/client-info'.freeze

def initialize(token:)
@token = token
end

private

def pathname
Expand Down
15 changes: 15 additions & 0 deletions lib/monobank/personal/corporate_webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'monobank/personal/webhook'

module Monobank
module Personal
class CorporateWebhook < Webhook
ENDPOINT = '/personal/corp/webhook'.freeze

private

def pathname
ENDPOINT
end
end
end
end
46 changes: 46 additions & 0 deletions lib/monobank/personal/registration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'monobank/methods/post'
require 'monobank/resources/personal/registration'

module Monobank
module Personal
class Registration < Methods::Post
ENDPOINT = '/personal/auth/registration'.freeze

def initialize(public_key:, name:, description:, contact_person:, phone:, email:, logo:, **rest)
super(**rest)

@public_key = public_key
@name = name
@description = description
@contact_person = contact_person
@phone = phone
@email = email
@logo = logo
end

def pathname
ENDPOINT
end

private

attr_reader :public_key, :name, :description, :contact_person, :phone, :email, :logo

def body
{
pubkey: public_key,
name:,
description:,
contactPerson: contact_person,
phone:,
email:,
logo:
}
end

def define_resources(attributes)
Resources::Personal::Registration.new(attributes)
end
end
end
end
Loading

0 comments on commit 51774cd

Please sign in to comment.