Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
askn committed Jan 5, 2016
0 parents commit 475d731
Show file tree
Hide file tree
Showing 17 changed files with 510 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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 changes: 115 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 68 additions & 0 deletions examples/test.cr
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: faker
version: 0.1.0

authors:
- Aşkın Gedik <[email protected]>

license: MIT
7 changes: 7 additions & 0 deletions spec/faker_spec.cr
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/faker"
22 changes: 22 additions & 0 deletions src/faker.cr
Original file line number Diff line number Diff line change
@@ -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 changes: 54 additions & 0 deletions src/faker/address.cr
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions src/faker/company.cr
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions src/faker/data.cr

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions src/faker/internet.cr
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 475d731

Please sign in to comment.