hws-instruments 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 26f1c3f62a2642c4c100d197b1955f19f3bec484e3a713247623b33d7228ebbf
4
+ data.tar.gz: 558d4f2432f3b038661353885a1d4a7e7f36bd47d3766cd65a807ebb5f9a8d00
5
+ SHA512:
6
+ metadata.gz: 0f28b2a2a05a2c621b6c94be673581ac86c706e2bbc6c7fafa964a28dfdcfdd7ee31552da1f517c26656d3e825c4a8a1f69510f2a17f6257ac3ba609689a0356
7
+ data.tar.gz: 99d596a7e62827e639f68f618341f819b6679d700e58a9f062ce309beea4bba4c78c13db4966972e69c3aaae49c07275ae66e8a85fe8bdc96b02f66821695f25
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: single_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/ClassAndModuleChildren:
14
+ Enabled: true
15
+ EnforcedStyle: compact
16
+
17
+ Style/RedundantSelf:
18
+ Enabled: false
19
+
20
+ Layout/LineLength:
21
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in hws-instrument.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'rubocop', '~> 1.7'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Tholkappiyan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Hws::Instruments
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hws/instruments`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hws-instruments'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hws-instruments
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hws-instruments. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Hws::Instruments project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/hws-instruments/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hws/instruments"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Adds necessary ActiveRecord migrations to the rails application
3
+
4
+ Example:
5
+ rails generate hws:instruments:install
6
+
7
+ This will create:
8
+ db/migrate/<migration_number>_create_hws_instruments_tables.rb
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/migration'
4
+ require 'rails/generators/active_record'
5
+
6
+ module Hws::Instruments
7
+ class InstallGenerator < Rails::Generators::Base # :nodoc:
8
+ include Rails::Generators::Migration
9
+
10
+ source_root File.expand_path('templates', __dir__)
11
+
12
+ def self.next_migration_number(path)
13
+ ActiveRecord::Generators::Base.next_migration_number(path)
14
+ end
15
+
16
+ desc 'copying migrations to db/migrate'
17
+ def copy_migrations
18
+ migration_template 'migration.rb.erb', 'db/migrate/create_hws_instruments_tables.rb'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ class <%= @migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+ def self.up
3
+ enable_extension 'pgcrypto' unless extensions.include?('pgcrypto')
4
+
5
+ create_table :instrument_configs, id: :uuid do |t| # payout
6
+ t.string :connector_id # Hws::Connectors::Hypto::{VirtualAccounts, Payouts, UPI}, Hws::Connectors::YesBank::{...}, ...
7
+ t.string :executor_id # Hws::Instruments::Executors::Hypto::VirtualAccounts
8
+ t.jsonb :connector_credentials, default: {}
9
+ t.jsonb :connector_actions, default: {} # {'action:send_to_bank': Hws::Connector::Payout::SendToBankRequest, ..}
10
+ t.timestamps
11
+ end
12
+
13
+ create_table :instruments, id: :uuid do |t|
14
+ t.belongs_to :instrument_config, type: :uuid, foreign_key: true
15
+ t.string :external_identifier # VAN for virtual acc, UPI ID for UPI, nil for payouts
16
+ t.jsonb :value, default: {}
17
+ t.jsonb :connector_actions, default: {} # {'action:send_to_bank': {acc_no: '', ifsc, ref_no, amount, payent_mode, note}, ..}
18
+ t.jsonb :allowed_actions, default: [] # array of actions which are enabled for the instrument
19
+ t.timestamps
20
+ end
21
+
22
+ # TODO - Decide if connector_id can be made the primary key of this table
23
+ add_index :instrument_configs, :connector_id, unique: true
24
+
25
+ add_index :instruments, :value, using: :gin
26
+ add_index :instruments, %I[external_identifier instrument_config_id], name: 'e_i_index'
27
+ end
28
+
29
+ def self.down
30
+ drop_table :instruments
31
+ drop_table :instrument_configs
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hws::Instruments::Exceptions
4
+ class UnknownActionError < StandardError; end
5
+
6
+ class UnknownEntityError < StandardError; end
7
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hws::Instruments::Executors::Base
4
+ def execute(action, instrument, options)
5
+ actions = instrument.instrument_config.connector_actions.with_indifferent_access
6
+ unless actions.present? && actions.key?(action)
7
+ raise(
8
+ Hws::Instruments::Exceptions::UnknownActionError,
9
+ "Unknown action: #{action} for connector #{instrument.instrument_config.connector_id} | Allowed are: [#{actions}]"
10
+ )
11
+ end
12
+ raise "Action disabled: #{action}" if instrument.allowed_actions.include?(action)
13
+
14
+ Rails.logger.info "Instruments::Executors - Executing: action:#{action}, instrument: #{instrument}, options: #{options}"
15
+
16
+ if self.methods.include?(action) || self.private_methods.include?(action)
17
+ self.send(action.to_sym, instrument, options)
18
+ else
19
+ instrument.instrument_config.connector.send(action, options)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ =begin
4
+ Instrument Config:
5
+ - connector_id: Hws::Connectors::Hypto::VirtualAccount
6
+ - executor_id: Hws::Instruments::Executors::Hypto::VirtualAccount
7
+ - connector_actions:
8
+ {'action:send_to_bank': Hws::Connector::Payout::SendToBankRequest, ..}
9
+
10
+ Instrument:
11
+ - value:
12
+ =end
13
+
14
+ # Hws::Connectors::Dto::PayoutRequest
15
+
16
+ class Hws::Instruments::Executors::Hypto::Payouts # :nodoc:
17
+ require 'hws-instruments/executors/base'
18
+ extend Hws::Instruments::Executors::Base
19
+
20
+ class << self
21
+ private
22
+
23
+ def send_to_bank_account(instrument, options)
24
+ options['beneficiary'] = Hws::Connectors::Dto::AccountDetail.new(options.delete('beneficiary').symbolize_keys)
25
+ req = Hws::Connectors::Dto::PayoutRequest.new(options.symbolize_keys)
26
+ instrument.instrument_config.connector.send_to_bank_account(request: req)
27
+ end
28
+
29
+ def send_to_upi_id(instrument, options)
30
+ options['beneficiary'] = Hws::Connectors::Dto::AccountDetail.new(options.delete('beneficiary').symbolize_keys)
31
+ req = Hws::Connectors::Dto::PayoutRequest.new(options.symbolize_keys)
32
+ instrument.instrument_config.connector.send_to_upi_id(request: req)
33
+ end
34
+
35
+ def status(instrument, options)
36
+ instrument.instrument_config.connector.status(reference_number: options[:reference_number])
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Executor for Hypto Virtual account module
4
+ class Hws::Instruments::Executors::Hypto::VirtualAccount
5
+ require 'hws-instruments/executors/base'
6
+ extend Hws::Instruments::Executors::Base
7
+
8
+ class << self
9
+ private
10
+
11
+ def create(instrument, _)
12
+ resp = instrument.instrument_config.connector.create(
13
+ request: Hws::Connectors::Dto::VirtualAccountRequest.new(reference_number: instrument.id)
14
+ )
15
+ instrument.update!(
16
+ external_identifier: resp.beneficiary.account_number,
17
+ value: { id: resp.meta[:id], va_num: resp.beneficiary.account_number, account_ifsc: resp.beneficiary.account_ifsc }
18
+ )
19
+ instrument
20
+ end
21
+
22
+ def fetch(instrument, _); end
23
+
24
+ def update(instrument); end
25
+
26
+ def activate(instrument, _)
27
+ resp = instrument.instrument_config.connector.activate(reference_number: instrument.value['id'])
28
+ instrument.update!(value: instrument.value.merge({ status: 'ACTIVE' })) if resp.status == 'ACTIVE'
29
+ end
30
+
31
+ def deactivate(instrument, _)
32
+ resp = instrument.instrument_config.connector.deactivate(reference_number: instrument.value['id'])
33
+ instrument.update!(value: instrument.value.merge({ status: 'INACTIVE' })) if resp.status == 'INACTIVE'
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hws::Instruments::Models
4
+ class Base < ActiveRecord::Base # :nodoc:
5
+ self.abstract_class = true
6
+
7
+ before_create :set_uuid
8
+
9
+ def set_uuid
10
+ self.id = ::LSUUID.generate if self.id.blank?
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hws::Instruments::Models::Instrument < ActiveRecord::Base # :nodoc:
4
+ belongs_to :instrument_config
5
+
6
+ def execute(action:, options: {})
7
+ Rails.logger.debug("Instrument.execute(action: #{action}, options: #{options})")
8
+ self.instrument_config.executor.execute(action, self, options)
9
+ end
10
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hws-instruments/models/base'
4
+
5
+ class Hws::Instruments::Models::InstrumentConfig < Hws::Instruments::Models::Base # :nodoc:
6
+ has_many :instruments
7
+
8
+ MANDATORY_FIELDS = %I[connector_id executor_id connector_credentials connector_actions].freeze
9
+
10
+ def self.create_config(params)
11
+ raise 'Missing mandatory param' unless params.keys.all? { |x| MANDATORY_FIELDS.include?(x) }
12
+
13
+ begin
14
+ unless Object.const_defined?(params[:connector_id])
15
+ raise Hws::Instruments::Exceptions::UnknownEntityError, "Connector not found: #{params[:connector_id]}"
16
+ end
17
+ rescue NameError => _e
18
+ raise Hws::Instruments::Exceptions::UnknownEntityError, "Connector not found: #{params[:connector_id]}"
19
+ end
20
+
21
+ begin
22
+ unless Object.const_defined?(params[:executor_id])
23
+ raise Hws::Instruments::Exceptions::UnknownEntityError, "Executor not found: #{params[:executor_id]}"
24
+ end
25
+ rescue NameError => _e
26
+ raise Hws::Instruments::Exceptions::UnknownEntityError, "Executor not found: #{params[:executor_id]}"
27
+ end
28
+
29
+ self.create(params)
30
+ end
31
+
32
+ def create_instrument(_external_identifier = nil)
33
+ instrument = self.instruments.create
34
+ instrument.execute(action: :create)
35
+ end
36
+
37
+ def connector
38
+ unless Object.const_defined?(self.connector_id)
39
+ raise "Invalid connector configuration. conn_id: #{self.connector_id}"
40
+ end
41
+
42
+ self.connector_id.constantize.new(self.connector_credentials)
43
+ end
44
+
45
+ def executor
46
+ raise "Invalid instrument executor with id: #{self.executor_id}" unless Object.const_defined?(self.executor_id)
47
+
48
+ self.executor_id.constantize
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hws-instruments'
4
+
5
+ # Expose rake tasks to users of the gem
6
+ class Hws::Instruments::Railtie < Rails::Railtie
7
+ railtie_name :'hws-instruments'
8
+
9
+ rake_tasks do
10
+ path = File.expand_path(__dir__)
11
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
12
+ end
13
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :'hws-instruments' do # rubocop:disable Metrics/BlockLength
4
+ namespace :hypto do # rubocop:disable Metrics/BlockLength
5
+ desc 'Initializes a Hypto payouts instrument config in DB'
6
+ task :payouts, [:api_token] => :environment do |_t, args|
7
+ Hws::Instruments.create_instrument_config(
8
+ connector_id: 'Hws::Connectors::Hypto::Payout',
9
+ executor_id: 'Hws::Instruments::Executors::Hypto::Payouts',
10
+ connector_credentials: args.to_h,
11
+ connector_actions: {
12
+ create: '', # Local action without connector call
13
+ status: 'String',
14
+ send_to_upi_id: 'Hws::Connectors::Dto::PayoutRequest',
15
+ send_to_bank_account: 'Hws::Connectors::Dto::PayoutRequest'
16
+ }
17
+ )
18
+ end
19
+
20
+ desc 'Initializes a Hypto virtual accounts instrument config in DB'
21
+ task :virtual_account, [:api_token] => :environment do |_t, args|
22
+ Hws::Instruments.create_instrument_config(
23
+ connector_id: 'Hws::Connectors::Hypto::VirtualAccount',
24
+ executor_id: 'Hws::Instruments::Executors::Hypto::VirtualAccount',
25
+ connector_credentials: args.to_h,
26
+ connector_actions: {
27
+ list: 'Hws::Connectors::Dto::ListVirtualAccountRequest',
28
+ fetch: 'Hws::Connectors::Dto::FetchVirtualAccountRequest',
29
+ create: 'Hws::Connectors::Dto::CreateVirtualAccountRequest',
30
+ search: 'Hws::Connectors::Dto::SearchVirtualAccountRequest',
31
+ status: 'Hws::Connectors::Dto::StatusRequest',
32
+ update: 'Hws::Connectors::Dto::UpdateVirtualAccountRequest',
33
+ activate: 'Hws::Connectors::Dto::ActivateVirtualAccountRequest',
34
+ statement: 'Hws::Connectors::Dto::StatementRequest',
35
+ deactivate: 'Hws::Connectors::Dto::DeactivateVirtualAccountRequest',
36
+ send_to_upi_id: 'Hws::Connectors::Dto::SendToUpiIdRequest',
37
+ send_to_bank_account: 'Hws::Connectors::Dto::SendToBankAccountRequest'
38
+ }
39
+ )
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hws-connectors'
4
+
5
+ module Hws
6
+ module Instruments # :nodoc:
7
+ module Models # :nodoc:
8
+ require 'lsuuid'
9
+ require 'hws-instruments/models/instrument_config'
10
+ require 'hws-instruments/models/instrument'
11
+ end
12
+
13
+ module Executors
14
+ module Hypto # :nodoc:
15
+ require 'hws-instruments/executors/hypto/virtual_account'
16
+ require 'hws-instruments/executors/hypto/payouts'
17
+ end
18
+ end
19
+
20
+ module Exceptions
21
+ require 'hws-instruments/exceptions/exceptions'
22
+ end
23
+
24
+ def self.create_instrument_config(*args)
25
+ Models::InstrumentConfig.create_config(*args)
26
+ end
27
+ end
28
+ end
29
+
30
+ require 'hws-instruments/railtie' if defined?(Rails)
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hws-instruments
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hypto Engineering Team
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hws-connectors
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: lsuuid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ description: Instruments library
42
+ email:
43
+ - engineering@hypto.in
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rubocop.yml"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/generators/hws/instruments/USAGE
56
+ - lib/generators/hws/instruments/install_generator.rb
57
+ - lib/generators/hws/instruments/templates/migration.rb.erb
58
+ - lib/hws-instruments.rb
59
+ - lib/hws-instruments/exceptions/exceptions.rb
60
+ - lib/hws-instruments/executors/base.rb
61
+ - lib/hws-instruments/executors/hypto/payouts.rb
62
+ - lib/hws-instruments/executors/hypto/virtual_account.rb
63
+ - lib/hws-instruments/models/base.rb
64
+ - lib/hws-instruments/models/instrument.rb
65
+ - lib/hws-instruments/models/instrument_config.rb
66
+ - lib/hws-instruments/railtie.rb
67
+ - lib/hws-instruments/tasks/hypto.rake
68
+ homepage: https://github.com/hwslabs/hws-instruments-ruby
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ homepage_uri: https://github.com/hwslabs/hws-instruments-ruby
73
+ source_code_uri: https://github.com/hwslabs/hws-insruments-ruby
74
+ changelog_uri: https://github.com/hwslabs/hws-insruments-ruby/blob/main/CHANGELOG.md
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.5.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Instruments library
94
+ test_files: []