From bed100158c6f24fe1f5558fe428afa5c0ffadffc Mon Sep 17 00:00:00 2001 From: Liang Shi <365354+2pd@users.noreply.github.com> Date: Thu, 19 Sep 2019 15:03:55 +1000 Subject: [PATCH] Add Faker::Blockchain::Aeternity (#1742) * add aeternity * update readme --- README.md | 1 + doc/blockchain/aeternity.md | 12 +++++ lib/faker/blockchain/aeternity.rb | 70 ++++++++++++++++++++++++++++++ test/faker/blockchain/aeternity.rb | 25 +++++++++++ 4 files changed, 108 insertions(+) create mode 100644 doc/blockchain/aeternity.md create mode 100644 lib/faker/blockchain/aeternity.rb create mode 100644 test/faker/blockchain/aeternity.rb diff --git a/README.md b/README.md index 5d4e075192..6d4866845c 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,7 @@ gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'mast - [Faker::WorldCup](doc/default/world_cup.md) ### Blockchain + - [Faker::Blockchain::Aeternity](doc/blockchain/aeternity.md) - [Faker::Blockchain::Bitcoin](doc/blockchain/bitcoin.md) - [Faker::Blockchain::Ethereum](doc/blockchain/ethereum.md) - [Faker::Blockchain::Tezos](doc/blockchain/tezos.md) diff --git a/doc/blockchain/aeternity.md b/doc/blockchain/aeternity.md new file mode 100644 index 0000000000..95e93fd341 --- /dev/null +++ b/doc/blockchain/aeternity.md @@ -0,0 +1,12 @@ +# Faker::Blockchain::Aeternity + +```ruby +Faker::Blockchain::Aeternity.address #=> "ak_zvU8YQLagjcfng7Tg8yCdiZ1rpiWNp1PBn3vtUs44utSvbJVR" + +Faker::Blockchain::Aeternity.transaction #=> "th_147nDP22h3pHrLt2qykTH4txUwQh1ccaXp" + +Faker::Blockchain::Aeternity.contract #=> "ct_Hk2JsNeWGEYQEHHQCfcBeGrwbhtYSwFTPdDhW2SvjFYVojyhW" + +Faker::Blockchain::Aeternity.oracle #=> "ok_28QDg7fkF5qiKueSdUvUBtCYPJdmMEoS73CztzXCRAwMGKHKZh" + +``` diff --git a/lib/faker/blockchain/aeternity.rb b/lib/faker/blockchain/aeternity.rb new file mode 100644 index 0000000000..ec7f9359aa --- /dev/null +++ b/lib/faker/blockchain/aeternity.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +module Faker + class Blockchain + class Aeternity < Base + class << self + ## + # Produces a random Aeternity wallet address + # + # @return [String] + # + # @example + # Faker::Blockchain::Aeternity.address + # #=> "ak_zvU8YQLagjcfng7Tg8yCdiZ1rpiWNp1PBn3vtUs44utSvbJVR" + # + def address + 'ak_' + rand_strings + end + + ## + # Produces a random Aeternity transaction + # + # @return [String] + # + # @example + # Faker::Blockchain::Aeternity.transaction + # #=> "th_147nDP22h3pHrLt2qykTH4txUwQh1ccaXp" + # + def transaction + 'th_' + rand_strings(51) + end + + ## + # Produces a random Aeternity contract + # + # @return [String] + # + # @example + # Faker::Blockchain::Aeternity.contract + # #=> "ct_Hk2JsNeWGEYQEHHQCfcBeGrwbhtYSwFTPdDhW2SvjFYVojyhW" + # + def contract + 'ct_' + rand_strings + end + + ## + # Produces a random Aeternity oracle + # + # @return [String] + # + # @example + # Faker::Blockchain::Aeternity.oracle + # #=> "ok_28QDg7fkF5qiKueSdUvUBtCYPJdmMEoS73CztzXCRAwMGKHKZh" + # + def oracle + 'ok_' + rand_strings(51) + end + + protected + + def rand_strings(length = 50) + hex_alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' + var = +'' + length.times { var << sample(shuffle(hex_alphabet.split(''))) } + var + end + end + end + end +end diff --git a/test/faker/blockchain/aeternity.rb b/test/faker/blockchain/aeternity.rb new file mode 100644 index 0000000000..735c0e8952 --- /dev/null +++ b/test/faker/blockchain/aeternity.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require_relative '../../test_helper' + +class TestFakerAeternity < Test::Unit::TestCase + def setup + @tester = Faker::Blockchain::Aeternity + end + + def test_address + assert_match(/ak_([0-9a-zA-Z]{50}$)/, @tester.address) + end + + def test_transaction + assert_match(/th_([0-9a-zA-Z]{51}$)/, @tester.transaction) + end + + def test_contract + assert_match(/ct_([0-9a-zA-Z]{50}$)/, @tester.contract) + end + + def test_oracle + assert_match(/ok_([0-9a-zA-Z]{51}$)/, @tester.oracle) + end +end