1
0
mirror of https://github.com/askn/faker.git synced 2023-08-10 21:13:01 +03:00

first commit

This commit is contained in:
Aşkın Gedik 2016-01-05 16:49:33 +02:00
commit 475d7312d8
17 changed files with 510 additions and 0 deletions

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
/doc/
/libs/
/.crystal/
/.shards/
# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock

1
.travis.yml Normal file
View File

@ -0,0 +1 @@
language: crystal

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 Aşkın Gedik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

115
README.md Normal file
View File

@ -0,0 +1,115 @@
# faker
This shard is a port of [Faker](https://github.com/stympy/faker) gem that generates fake data.
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
faker:
github: askn/faker
```
## Usage
```crystal
require "faker"
Faker::Name.name
```
### Faker::Address
```crystal
Faker::Address.city
Faker::Address.street_name
Faker::Address.street_address
Faker::Address.secondary_address
Faker::Address.zip_code
Faker::Address.postcode
Faker::Address.street_suffix
Faker::Address.city_suffix
Faker::Address.city_prefix
Faker::Address.state
Faker::Address.state_abbr
Faker::Address.country
```
### Faker::Company
```crystal
Faker::Company.name
Faker::Company.suffix
```
### Faker::Internet
```crystal
Faker::Internet.email
Faker::Internet.email('Nancy')
Faker::Internet.free_email
Faker::Internet.free_email('Nancy')
Faker::Internet.user_name
Faker::Internet.user_name('Nancy')
Faker::Internet.domain_name
Faker::Internet.domain_word
Faker::Internet.domain_suffix
Faker::Internet.ip_v4_address
```
### Faker::Lorem
```crystal
Faker::Lorem.words
Faker::Lorem.words(4)
Faker::Lorem.sentence
Faker::Lorem.sentence(3)
Faker::Lorem.sentences
Faker::Lorem.sentences(1)
Faker::Lorem.paragraph
Faker::Lorem.paragraph(2)
Faker::Lorem.paragraphs
Faker::Lorem.paragraphs(1)
```
### Faker::Name
```crystal
Faker::Name.name
Faker::Name.first_name
Faker::Name.last_name
Faker::Name.prefix
Faker::Name.suffix
```
### Faker::PhoneNumber
```crystal
Faker::PhoneNumber.phone_number
```
## Contributing
1. Fork it ( https://github.com/askn/faker/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request
## Contributors
- [askn](https://github.com/askn) Aşkın Gedik - creator, maintainer

68
examples/test.cr Normal file
View File

@ -0,0 +1,68 @@
require "../src/faker"
puts "\n### Faker::Address\n\n"
puts Faker::Address.city
puts Faker::Address.street_name
puts Faker::Address.street_address
puts Faker::Address.secondary_address
puts Faker::Address.zip_code
puts Faker::Address.postcode
puts Faker::Address.street_suffix
puts Faker::Address.city_suffix
puts Faker::Address.city_prefix
puts Faker::Address.state
puts Faker::Address.state_abbr
puts Faker::Address.country
puts "\n### Faker::Company\n\n"
puts Faker::Company.name
puts Faker::Company.suffix
puts "\n\t### Faker::Internet\n\n"
puts Faker::Internet.email
puts Faker::Internet.email("Nancy")
puts Faker::Internet.free_email
puts Faker::Internet.free_email("Nancy")
puts Faker::Internet.user_name
puts Faker::Internet.user_name("Nancy")
puts Faker::Internet.domain_name
puts Faker::Internet.domain_word
puts Faker::Internet.domain_suffix
puts Faker::Internet.ip_v4_address
puts "\n\t### Faker::Lorem\n\n"
puts Faker::Lorem.words
puts Faker::Lorem.words(4)
puts Faker::Lorem.sentence
puts Faker::Lorem.sentence(3)
puts Faker::Lorem.sentences
puts Faker::Lorem.sentences(1)
puts Faker::Lorem.paragraph
puts Faker::Lorem.paragraph(2)
puts Faker::Lorem.paragraphs
puts Faker::Lorem.paragraphs(1)
puts "\n\t### Faker::Name\n\n"
puts Faker::Name.name
puts Faker::Name.first_name
puts Faker::Name.last_name
puts Faker::Name.prefix
puts Faker::Name.suffix
puts "\n\t### Faker::PhoneNumber\n\n"
puts Faker::PhoneNumber.phone_number

7
shard.yml Normal file
View File

@ -0,0 +1,7 @@
name: faker
version: 0.1.0
authors:
- Aşkın Gedik <askn@bil.omu.edu.tr>
license: MIT

7
spec/faker_spec.cr Normal file
View File

@ -0,0 +1,7 @@
require "./spec_helper"
describe Faker do
it "works" do
# Faker.numerify("###").match(/\d{3}/) { |md| md.size }.should eq 3
end
end

2
spec/spec_helper.cr Normal file
View File

@ -0,0 +1,2 @@
require "spec"
require "../src/faker"

22
src/faker.cr Normal file
View File

@ -0,0 +1,22 @@
require "./faker/data.cr"
require "./faker/*"
module Faker
def self.numerify(number_string)
number_string.gsub(/#/) { rand(10).to_s }
end
def self.letterify(letter_string)
letter_string.gsub(/\?/) { ("a".."z").to_a.rand }
end
def self.bothify(string)
self.letterify(self.numerify(string))
end
end
class Array
def rand
self.at(rand(self.size))
end
end

54
src/faker/address.cr Normal file
View File

@ -0,0 +1,54 @@
module Faker
class Address
def self.zip_code
Faker.numerify(["#####", "#####-####"].rand)
end
{% for data_type in %w(state state_abbr city_suffix city_prefix country street_suffix) %}
def self.{{data_type.id}}
Faker::Data["{{data_type.id}}"].rand
end
{% end %}
def self.city
[
"%s %s%s" % [city_prefix, Name.first_name, city_suffix],
"%s %s" % [city_prefix, Name.first_name],
"%s%s" % [Name.first_name, city_suffix],
"%s%s" % [Name.last_name, city_suffix],
].rand
end
def self.street_name
[
->{ [Name.last_name, street_suffix].join(" ") },
->{ [Name.first_name, street_suffix].join(" ") },
].rand.call
end
def self.street_address
Faker.numerify([
->{ "##### %s" % street_name },
->{ "##### %s" % street_name },
->{ "##### %s" % street_name },
->{ "##### %s" % street_name },
->{ "##### %s Apt. ###" % street_name },
->{ "##### %s Suite ###" % street_name },
].rand.call)
end
def self.secondary_address
Faker.numerify([
"Apt. ###",
"Suite ###",
].rand)
end
def self.postcode
Faker.bothify([
->{ "??# #??" },
->{ "??## #??" },
].rand.call).upcase
end
end
end

17
src/faker/company.cr Normal file
View File

@ -0,0 +1,17 @@
module Faker
class Company
def self.name
Formats.rand.call
end
def self.suffix
%w(Inc and\ Sons LLC Group).rand
end
Formats = [
->{ [Name.last_name, suffix].join(' ') },
->{ [Name.last_name, Name.last_name].join('-') },
->{ "%s, %s and %s" % [Name.last_name, Name.last_name, Name.last_name] },
]
end
end

23
src/faker/data.cr Normal file

File diff suppressed because one or more lines are too long

44
src/faker/internet.cr Normal file
View File

@ -0,0 +1,44 @@
module Faker
class Internet
def self.email(name = nil)
[user_name(name), domain_name].join("@")
end
def self.free_email(name = nil)
[user_name(name), %w(gmail.com yahoo.com hotmail.com).rand].join("@")
end
def self.user_name(name = nil)
return name.scan(/\w+/).shuffle.map(&.[0]).join(%w(. _).rand).downcase if name
[
->{ Name.first_name.gsub(/\W/, "").downcase },
->{
[Name.first_name, Name.last_name].map { |n|
n.gsub(/\W/, "")
}.join(".").downcase
},
].rand.call
end
def self.domain_name
[domain_word, domain_suffix].join(".")
end
def self.domain_word
Company.name.split(" ").first.gsub(/\W/, "").downcase
end
def self.domain_suffix
%w(co.uk com us ca biz info name).rand
end
def self.ip_v4_address
[
(0..255).to_a.rand,
(0..255).to_a.rand,
(0..255).to_a.rand,
(0..255).to_a.rand,
].join('.')
end
end
end

32
src/faker/lorem.cr Normal file
View File

@ -0,0 +1,32 @@
module Faker
# Based on Perl"s Text::Lorem
class Lorem
def self.words(num = 3)
Faker::Data["words"].shuffle[0, num]
end
def self.sentence(word_count = 4)
words(word_count + rand(6)).join(" ").capitalize + "."
end
def self.sentences(sentence_count = 3)
([] of String).tap do |sentences|
1.upto(sentence_count) do
sentences << sentence
end
end
end
def self.paragraph(sentence_count = 3)
sentences(sentence_count + rand(3)).join(" ")
end
def self.paragraphs(paragraph_count = 3)
([] of String).tap do |paragraphs|
1.upto(paragraph_count) do
paragraphs << paragraph
end
end
end
end
end

54
src/faker/name.cr Normal file
View File

@ -0,0 +1,54 @@
module Faker
class Name
def self.name
Formats.rand.call.join(' ')
end
def self.first_name
Faker::Data["first_name"].rand
end
def self.last_name
Faker::Data["last_name"].rand
end
def self.prefix
%w(Mr. Mrs. Ms. Miss Dr.).rand
end
def self.suffix
%w(Jr. Sr. I II III IV V MD DDS PhD DVM).rand
end
def self.catch_phrase
[
["Adaptive", "Advanced", "Ameliorated", "Assimilated", "Automated", "Balanced", "Business-focused", "Centralized", "Cloned", "Compatible", "Configurable", "Cross-group", "Cross-platform", "Customer-focused", "Customizable", "Decentralized", "De-engineered", "Devolved", "Digitized", "Distributed", "Diverse", "Down-sized", "Enhanced", "Enterprise-wide", "Ergonomic", "Exclusive", "Expanded", "Extended", "Face to face", "Focused", "Front-line", "Fully-configurable", "Function-based", "Fundamental", "Future-proofed", "Grass-roots", "Horizontal", "Implemented", "Innovative", "Integrated", "Intuitive", "Inverse", "Managed", "Mandatory", "Monitored", "Multi-channelled", "Multi-lateral", "Multi-layered", "Multi-tiered", "Networked", "Object-based", "Open-architected", "Open-source", "Operative", "Optimized", "Optional", "Organic", "Organized", "Persevering", "Persistent", "Phased", "Polarised", "Pre-emptive", "Proactive", "Profit-focused", "Profound", "Programmable", "Progressive", "Public-key", "Quality-focused", "Reactive", "Realigned", "Re-contextualized", "Re-engineered", "Reduced", "Reverse-engineered", "Right-sized", "Robust", "Seamless", "Secured", "Self-enabling", "Sharable", "Stand-alone", "Streamlined", "Switchable", "Synchronised", "Synergistic", "Synergized", "Team-oriented", "Total", "Triple-buffered", "Universal", "Up-sized", "Upgradable", "User-centric", "User-friendly", "Versatile", "Virtual", "Visionary", "Vision-oriented"].rand,
["24 hour", "24/7", "3rd generation", "4th generation", "5th generation", "6th generation", "actuating", "analyzing", "assymetric", "asynchronous", "attitude-oriented", "background", "bandwidth-monitored", "bi-directional", "bifurcated", "bottom-line", "clear-thinking", "client-driven", "client-server", "coherent", "cohesive", "composite", "context-sensitive", "contextually-based", "content-based", "dedicated", "demand-driven", "didactic", "directional", "discrete", "disintermediate", "dynamic", "eco-centric", "empowering", "encompassing", "even-keeled", "executive", "explicit", "exuding", "fault-tolerant", "foreground", "fresh-thinking", "full-range", "global", "grid-enabled", "heuristic", "high-level", "holistic", "homogeneous", "human-resource", "hybrid", "impactful", "incremental", "intangible", "interactive", "intermediate", "leading edge", "local", "logistical", "maximized", "methodical", "mission-critical", "mobile", "modular", "motivating", "multimedia", "multi-state", "multi-tasking", "national", "needs-based", "neutral", "next generation", "non-volatile", "object-oriented", "optimal", "optimizing", "radical", "real-time", "reciprocal", "regional", "responsive", "scalable", "secondary", "solution-oriented", "stable", "static", "systematic", "systemic", "system-worthy", "tangible", "tertiary", "transitional", "uniform", "upward-trending", "user-facing", "value-added", "web-enabled", "well-modulated", "zero administration", "zero defect", "zero tolerance"].rand,
["ability", "access", "adapter", "algorithm", "alliance", "analyzer", "application", "approach", "architecture", "archive", "artificial intelligence", "array", "attitude", "benchmark", "budgetary management", "capability", "capacity", "challenge", "circuit", "collaboration", "complexity", "concept", "conglomeration", "contingency", "core", "customer loyalty", "database", "data-warehouse", "definition", "emulation", "encoding", "encryption", "extranet", "firmware", "flexibility", "focus group", "forecast", "frame", "framework", "function", "functionalities", "Graphic Interface", "groupware", "Graphical User Interface", "hardware", "help-desk", "hierarchy", "hub", "implementation", "info-mediaries", "infrastructure", "initiative", "installation", "instruction set", "interface", "internet solution", "intranet", "knowledge user", "knowledge base", "local area network", "leverage", "matrices", "matrix", "methodology", "middleware", "migration", "model", "moderator", "monitoring", "moratorium", "neural-net", "open architecture", "open system", "orchestration", "paradigm", "parallelism", "policy", "portal", "pricing structure", "process improvement", "product", "productivity", "project", "projection", "protocol", "secured line", "service-desk", "software", "solution", "standardization", "strategy", "structure", "success", "superstructure", "support", "synergy", "system engine", "task-force", "throughput", "time-frame", "toolset", "utilisation", "website", "workforce"].rand,
].join(" ")
end
# When a straight answer won't do, BS to the rescue!
# Wordlist from http://dack.com/web/bullshit.html
def self.bs
[
["implement", "utilize", "integrate", "streamline", "optimize", "evolve", "transform", "embrace", "enable", "orchestrate", "leverage", "reinvent", "aggregate", "architect", "enhance", "incentivize", "morph", "empower", "envisioneer", "monetize", "harness", "facilitate", "seize", "disintermediate", "synergize", "strategize", "deploy", "brand", "grow", "target", "syndicate", "synthesize", "deliver", "mesh", "incubate", "engage", "maximize", "benchmark", "expedite", "reintermediate", "whiteboard", "visualize", "repurpose", "innovate", "scale", "unleash", "drive", "extend", "engineer", "revolutionize", "generate", "exploit", "transition", "e-enable", "iterate", "cultivate", "matrix", "productize", "redefine", "recontextualize"].rand,
["clicks-and-mortar", "value-added", "vertical", "proactive", "robust", "revolutionary", "scalable", "leading-edge", "innovative", "intuitive", "strategic", "e-business", "mission-critical", "sticky", "one-to-one", "24/7", "end-to-end", "global", "B2B", "B2C", "granular", "frictionless", "virtual", "viral", "dynamic", "24/365", "best-of-breed", "killer", "magnetic", "bleeding-edge", "web-enabled", "interactive", "dot-com", "sexy", "back-end", "real-time", "efficient", "front-end", "distributed", "seamless", "extensible", "turn-key", "world-class", "open-source", "cross-platform", "cross-media", "synergistic", "bricks-and-clicks", "out-of-the-box", "enterprise", "integrated", "impactful", "wireless", "transparent", "next-generation", "cutting-edge", "user-centric", "visionary", "customized", "ubiquitous", "plug-and-play", "collaborative", "compelling", "holistic", "rich"].rand,
["synergies", "web-readiness", "paradigms", "markets", "partnerships", "infrastructures", "platforms", "initiatives", "channels", "eyeballs", "communities", "ROI", "solutions", "e-tailers", "e-services", "action-items", "portals", "niches", "technologies", "content", "vortals", "supply-chains", "convergence", "relationships", "architectures", "interfaces", "e-markets", "e-commerce", "systems", "bandwidth", "infomediaries", "models", "mindshare", "deliverables", "users", "schemas", "networks", "applications", "metrics", "e-business", "functionalities", "experiences", "web services", "methodologies"].rand,
].join(" ")
end
Formats = [
->{ [Name.prefix, Name.first_name, Name.last_name] },
->{ [Name.first_name, Name.last_name, Name.suffix] },
->{ [Name.first_name, Name.last_name] },
->{ [Name.first_name, Name.last_name] },
->{ [Name.first_name, Name.last_name] },
->{ [Name.first_name, Name.last_name] },
->{ [Name.first_name, Name.last_name] },
->{ [Name.first_name, Name.last_name] },
->{ [Name.first_name, Name.last_name] },
->{ [Name.first_name, Name.last_name] },
]
end
end

30
src/faker/phone_number.cr Normal file
View File

@ -0,0 +1,30 @@
module Faker
class PhoneNumber
def self.phone_number
Faker.numerify(Formats.rand)
end
Formats = [
"###-###-####",
"(###)###-####",
"1-###-###-####",
"###.###.####",
"###-###-####",
"(###)###-####",
"1-###-###-####",
"###.###.####",
"###-###-#### x###",
"(###)###-#### x###",
"1-###-###-#### x###",
"###.###.#### x###",
"###-###-#### x####",
"(###)###-#### x####",
"1-###-###-#### x####",
"###.###.#### x####",
"###-###-#### x#####",
"(###)###-#### x#####",
"1-###-###-#### x#####",
"###.###.#### x#####",
]
end
end

3
src/faker/version.cr Normal file
View File

@ -0,0 +1,3 @@
module Faker
VERSION = "0.1.0"
end