Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented bank provider #52

Merged
merged 5 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,28 @@ Providers
TODO list.

- [ ] base
- [X] faker.provider.address
- [o] faker.provider.address
- [ ] faker.provider.automotive
- [ ] faker.provider.bank
- [o] faker.provider.bank
- [ ] faker.provider.barcode
- [ ] faker.provider.color
- [X] faker.provider.company
- [o] faker.provider.company
- [ ] faker.provider.credit_card
- [ ] faker.provider.currency
- [ ] faker.provider.date_time
- [ ] faker.provider.file
- [ ] faker.provider.geo
- [ ] faker.provider.internet
- [ ] faker.provider.isbn
- [X] faker.provider.job
- [o] faker.provider.job
- [ ] faker.provider.lorem
- [ ] faker.provider.misc
- [X] faker.provider.person
- [X] faker.provider.phone_number
- [o] faker.provider.person
- [o] faker.provider.phone_number
- [ ] faker.provider.profile
- [ ] faker.provider.python
- [ ] faker.provider.ssn
- [X] faker.provider.user_agent
- [o] faker.provider.user_agent

************
API document
Expand Down
26 changes: 21 additions & 5 deletions faker.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bin = @["faker/cli/faker"]
binDir = "bin"

from os import `/`, splitFile
from algorithm import sort
import strformat, strutils, sequtils

# Dependencies
Expand All @@ -19,6 +20,13 @@ requires "nim >= 1.0.0"
let
providerDir = "src" / "faker" / "provider"

proc addGeneratedText(lines: var seq[string]) =
lines.add("# ----------------------------------------------- #")
lines.add("# This module was generated by 'nimble genProvs'. #")
lines.add("# See 'faker.nimble'. #")
lines.add("# ----------------------------------------------- #")
lines.add("")

task docs, "Generate API documents":
exec "nimble doc --index:on --project --out:docs --hints:off src/faker.nim"

Expand Down Expand Up @@ -56,19 +64,17 @@ task genMod, "Generate module":
echo "Generated: " & newPath

task genProvs, "Generate provider file":
var providers: seq[string]
for dir in listDirs(providerDir):
let dstFile = dir & ".nim"
var lines: seq[string]
lines.add("# ----------------------------------------------- #")
lines.add("# This module was generated by 'nimble genProvs'. #")
lines.add("# See 'faker.nimble'. #")
lines.add("# ----------------------------------------------- #")
lines.add("")
lines.addGeneratedText()
lines.add("import util")
lines.add("import ../base")

# Get submodule names
let (_, prefix, _) = splitFile(dir)
providers.add(prefix)
var modules: seq[string]
var locales: seq[string] # en_US, ja_JP
for file in listFiles(dir):
Expand Down Expand Up @@ -103,3 +109,13 @@ task genProvs, "Generate provider file":
let body = lines.join("\n")
writeFile(dir & ".nim", body)

providers.sort()

# generate provider.nim
var lines: seq[string]
let prov = providers.join(", ")
lines.addGeneratedText()
lines.add(&"import provider/[{prov}]")
lines.add(&"export {prov}")
let body = lines.join("\n")
writeFile(providerDir & ".nim", body)
9 changes: 7 additions & 2 deletions src/faker/provider.nim
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
import provider/[address, phone_number, job, person, company, user_agent]
export address, phone_number, job, person, company, user_agent
# ----------------------------------------------- #
# This module was generated by 'nimble genProvs'. #
# See 'faker.nimble'. #
# ----------------------------------------------- #

import provider/[address, bank, company, job, person, phone_number, user_agent]
export address, bank, company, job, person, phone_number, user_agent
18 changes: 18 additions & 0 deletions src/faker/provider/bank.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ----------------------------------------------- #
# This module was generated by 'nimble genProvs'. #
# See 'faker.nimble'. #
# ----------------------------------------------- #

import util
import ../base
import bank/[bank_en_US]
export base

genProc bank,
[
bankCountry,
bban,
iban,
], [
en_US,
]
33 changes: 33 additions & 0 deletions src/faker/provider/bank/bank_en_US.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
include interfaces

import tables
from strutils import join, parseBiggestUInt
from strformat import `&`
from sequtils import mapIt

const
countryCode = "GB"
bbanFormat = "#############"
asciiUpperCase = {'A'..'Z'}

let
alpha = block:
var tbl = initTable[char, string]()
for ch in asciiUpperCase:
tbl.add(ch, $(ch.ord mod 55))
tbl

proc bankCountry*(f: Faker): string =
countryCode

proc bban*(f: Faker): string =
var r = f.rand
for i in 0..3:
result.add(r.sample(asciiUpperCase))
result.add(r.formatNumbers(bbanFormat))

proc iban*(f: Faker): string =
let bban = f.bban()
let checkInt = 98 - (f.rand.rand(1..100) mod 97)
let check = &"{checkInt:>02}"
result = countryCode & check & bban
14 changes: 14 additions & 0 deletions src/faker/provider/bank/interfaces.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# interfaces はプロシージャの定義のみをまとめたモジュール。
# C言語でいうヘッダファイルのように使用する。
#
# このモジュールをincludeしたモジュールは、このモジュールに定義しているプロシー
# ジャを全て実装しないと、コンパイルが通らない。

import random
import ../../base
import ../util
export base

proc bankCountry*(f: Faker): string
proc bban*(f: Faker): string
proc iban*(f: Faker): string
3 changes: 3 additions & 0 deletions tests/test1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ suite "basic usage":
echo fake.opera()
echo fake.safari()
echo fake.internetExplorer()
echo fake.bankCountry()
echo fake.bban()
echo fake.iban()
echo "-----"