-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rake task to sync using AusPayNet API (#61)
* Get something working * End to end test * Split bsb tasks into two tasks * Small random cleanup * Generate latest update json * Rubocop * Use LEADER_WIDTH to calc outputparam width * Extract output param width * Add README section * Misc cleanup * Fixup README * Add unit test for aus pay net api client * fix: update bank list Signed-off-by: David Looi <[email protected]> * fix: typo Signed-off-by: David Looi <[email protected]> --------- Signed-off-by: David Looi <[email protected]> Co-authored-by: David Looi <[email protected]>
- Loading branch information
Showing
15 changed files
with
428 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module BSB | ||
module AusPayNet | ||
module Client | ||
class MissingSubscriptionKeyError < StandardError; end | ||
|
||
OUTPUT_PARAM_WIDTH = 30 | ||
LEADER_WIDTH = OUTPUT_PARAM_WIDTH + 11 | ||
|
||
Response = Struct.new(:body, keyword_init: true) | ||
|
||
def self.fetch_all_bsbs | ||
subscription_key = ENV.fetch('AUSPAYNET_SUB_KEY', nil) | ||
if subscription_key.nil? | ||
raise MissingSubscriptionKeyError, "the environment variable 'AUSPAYNET_SUB_KEY' must be present" | ||
end | ||
|
||
conn = Faraday.new( | ||
url: 'https://auspaynet-bicbsb-api-prod.azure-api.net', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Ocp-Apim-Subscription-Key': subscription_key | ||
} | ||
) do |faraday| | ||
faraday.response :raise_error | ||
end | ||
|
||
response = conn.post('/bsbquery/manual/paths/invoke') do |req| | ||
req.body = { outputparam: ' ' * OUTPUT_PARAM_WIDTH }.to_json | ||
end | ||
|
||
Response.new(body: response.body[LEADER_WIDTH..]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
|
||
module BSB | ||
class BaseGenerator | ||
attr_reader :hash | ||
|
||
def initialize(hash) | ||
@hash = hash | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bsb' | ||
require 'bsb/database_generator' | ||
require 'bsb/bank_list_generator' | ||
|
||
namespace :bsb do | ||
desc 'Sync config/*.json.' | ||
task :sync, [:keyfile, :bsbfile] do |_t, args| | ||
require 'bsb/base_generator' | ||
bank_list_filename = args[:keyfile] | ||
task :sync_bsb_db_manual, [:bsbfile] do |_t, args| | ||
db_list_filename = args[:bsbfile] | ||
|
||
if db_list_filename | ||
bsb_db_gen = BSB::DatabaseGenerator.load_file(db_list_filename) | ||
File.write(BSB::DB_FILEPATH, bsb_db_gen.json) | ||
else | ||
warn 'Missing bsb db "BSBDirectory"' | ||
end | ||
end | ||
|
||
task :sync_bank_list, [:keyfile] do |_t, args| | ||
bank_list_filename = args[:keyfile] | ||
|
||
if bank_list_filename | ||
require 'bsb/bank_list_generator' | ||
bsb_bl_gen = BSB::BankListGenerator.load_file(bank_list_filename) | ||
File.write('config/bsb_bank_list.json', bsb_bl_gen.json) | ||
else | ||
warn 'Missing bank list "KEY TO ABBREVIATIONS AND BSB NUMBERS"' | ||
end | ||
|
||
if db_list_filename | ||
require 'bsb/database_generator' | ||
bsb_db_gen = BSB::DatabaseGenerator.load_file(db_list_filename) | ||
File.write('config/bsb_db.json', bsb_db_gen.json) | ||
else | ||
warn 'Missing bsb db "BSBDirectory"' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bsb' | ||
require 'bsb/database_generator' | ||
|
||
namespace :bsb do | ||
desc 'Sync config/bsb_db.json with data provided by AusPayNet' | ||
task :sync_bsb_db do | ||
latest_db = BSB::DatabaseGenerator.fetch_latest | ||
existing_db_hash = JSON.parse(File.read(BSB::DB_FILEPATH)) | ||
latest_db_hash = latest_db.hash | ||
|
||
deletions = existing_db_hash.reject { |bsb, _| latest_db_hash.key?(bsb) } | ||
additions = latest_db_hash.reject { |bsb, _| existing_db_hash.key?(bsb) } | ||
modifications = {} | ||
|
||
latest_db_hash.each do |bsb, data| | ||
next unless existing_db_hash.key?(bsb) && existing_db_hash[bsb] != data | ||
|
||
modifications[bsb] = data | ||
end | ||
|
||
changes_json = JSON.pretty_generate( | ||
{ | ||
additions: additions, | ||
deletions: deletions, | ||
modifications: modifications | ||
} | ||
) | ||
|
||
File.write(BSB::DB_FILEPATH, latest_db.json) | ||
File.write(BSB::CHANGES_FILEPATH, changes_json) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bsb/aus_pay_net/client' | ||
require 'test_helper' | ||
|
||
describe BSB::AusPayNet::Client do | ||
describe '.fetch_all_bsbs' do | ||
before { ENV['AUSPAYNET_SUB_KEY'] = 'something' } | ||
|
||
it 'returns the expected response' do | ||
VCR.use_cassette('auspaynet_fetch_all_bsbs') do | ||
response = BSB::AusPayNet::Client.fetch_all_bsbs | ||
assert_equal(response.class, BSB::AusPayNet::Client::Response) | ||
assert_equal(response.body.class, String) | ||
assert_equal(JSON.parse(response.body).count, 2) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"333333": [ | ||
"AAA", | ||
"Aviato3", | ||
"123 Fakest Street", | ||
"Canberra", | ||
"ACT", | ||
"1234", | ||
"P H" | ||
], | ||
"987654": [ | ||
"EST", | ||
"Aviato2", | ||
"123 Old Faker Street", | ||
"Ballina", | ||
"NSW", | ||
"1234", | ||
"P " | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.