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

Cli interface for faker #14

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/cucumber.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default: --format progress
53 changes: 53 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
PATH
remote: .
specs:
faker (1.0.0)
i18n (~> 0.4)
thor

GEM
remote: http://rubygems.org/
specs:
aruba (0.4.6)
bcat (>= 0.6.1)
childprocess (>= 0.2.0)
cucumber (>= 1.0.2)
rdiscount (>= 1.6.8)
rspec (>= 2.6.0)
bcat (0.6.2)
rack (~> 1.0)
builder (3.0.0)
childprocess (0.2.2)
ffi (~> 1.0.6)
cucumber (1.0.6)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
gherkin (~> 2.4.18)
json (>= 1.4.6)
term-ansicolor (>= 1.0.6)
diff-lcs (1.1.3)
ffi (1.0.9)
gherkin (2.4.18)
json (>= 1.4.6)
i18n (0.6.0)
json (1.6.0)
rack (1.3.2)
rdiscount (1.6.8)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
term-ansicolor (1.0.6)
thor (0.14.6)

PLATFORMS
ruby

DEPENDENCIES
aruba
cucumber
faker!
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ Usage
* Faker::Name.name => "Christophe Bartell"
* Faker::Internet.email => "kirsten.greenholt@corkeryfisher.info"

Usage with Rails
----------------

If you want to change your locale with Rails from the default of :en, change
config/application.rb, setting config.i18n.locale to whatever locale you
want. Change locale rather than default_locale (as suggested by the comments
in that file) so that I18n's fallbacks will work properly and Faker can use
the formats and data in en.yml (if there is no Faker localization for your
locale). If you'd prefer to set default_locale rather than locale, then
you'll also need to add config.i18n.fallbacks.defaults = [:en] to your
configuration to make the fallbacks work for Faker.

Command Line Usage
----------------
Faker is also usable from the command line. All methods are available

faker address -f street_name
faker company -f name
faker internet -f email
faker lorum -f sentences -n 10
faker name -f first_name
faker phone_number

Run `faker help` for general help or there is also help for each data type. For the address type for example run `faker help address`.

Note that if you installed using bundler you may need to use `bundle exec faker` rather than just `faker`.

Customization
------------
Since you may want to make addresses and other types of data look different
Expand Down
7 changes: 7 additions & 0 deletions bin/faker
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

lib_dir = File.join(File.dirname(__FILE__), '..', 'lib')
$LOAD_PATH.unshift lib_dir if File.directory?(lib_dir)

require 'faker'
Faker::CLI.start
4 changes: 4 additions & 0 deletions faker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ Gem::Specification.new do |s|
s.rubyforge_project = "faker"

s.add_dependency('i18n', '~> 0.4')
s.add_dependency 'thor'
s.add_development_dependency 'cucumber'
s.add_development_dependency 'aruba'

s.files = `git ls-files -- lib/*`.split("\n") + %w(History.txt License.txt README.md)
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

end
13 changes: 13 additions & 0 deletions features/address.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Address
In order to generate fake data
As a CLI
I want to be able to get fake address data

Scenario: No options
When I run `faker address`
Then the output should contain "Run `faker help address` for a list of valid types"

Scenario: Help
When I run `faker help address`
Then the output should contain "Usage:"

45 changes: 45 additions & 0 deletions features/internet.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Feature: Internet
In order to generate fake data
As a CLI
I want to be able to get fake internet data

Scenario: No options
When I run `faker internet`
Then the output should contain "Run `faker help internet` for a list of valid types"

Scenario: Help
When I run `faker help internet`
Then the output should contain "Usage:"

Scenario: Email
When I run `faker internet -f email`
Then the output should match /.+@.+\.\w+/

Scenario: Free Email
When I run `faker internet -f free_email`
Then the output should match /.+@(gmail|hotmail|yahoo)\.com/

Scenario: Username
When I run `faker internet -f user_name`
Then the output should match /[a-z]+((_|\.)[a-z]+)?/

Scenario: Domain Name
When I run `faker internet -f domain_name`
Then the output should match /\w+\.\w+/

Scenario: Domain Word
When I run `faker internet -f domain_word`
Then the output should match /^\w+$/

Scenario: Domain Suffix
When I run `faker internet -f domain_suffix`
Then the output should match /^\w+(\.\w+)?/

Scenario: IP4 Address
When I run `faker internet -f ip_v4_address`
Then the output should match /^\d+\.\d+\.\d+\.\d+$/

Scenario: IP6 Address
When I run `faker internet -f ip_v6_address`
Then the output should match /^(((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5]))|([\da-fA-F]{1,4}(\:[\da-fA-F]{1,4}){7})|(([\da-fA-F]{1,4}:){0,5}::([\da-fA-F]{1,4}:){0,5}[\da-fA-F]{1,4})$/

2 changes: 2 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'aruba/cucumber'
require 'faker'
1 change: 1 addition & 0 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ def method_missing(m, *args, &block)
require 'faker/name'
require 'faker/phone_number'
require 'faker/version'
require 'faker/cli'

require 'extensions/array'
131 changes: 131 additions & 0 deletions lib/faker/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
require 'thor'

module Faker
class CLI < Thor

desc "address [-f street_name|street_address|secondary_address|zip_code|street_suffix|city_suffix|state_abbr|state|country]",
"Returns fake address data"
method_option :faker_type, :aliases => "-f", :desc => "the faker data type"
def address
case options.faker_type
when 'street_name'
puts Faker::Address.street_name
when 'street_address'
puts Faker::Address.street_address
when 'secondary_address'
puts Faker::Address.secondary_address
when 'zip_code'
puts Faker::Address.zip_code
when 'street_suffix'
puts Faker::Address.street_suffix
when 'city_suffix'
puts Faker::Address.city_suffix
when 'state_abbr'
puts Faker::Address.state_abbr
when 'state'
puts Faker::Address.state
when 'country'
puts Faker::Address.country
else
puts "Usage: `faker address -f=street_address`"
puts "Run `faker help address` for a list of valid types"
end
end

desc "company [-f name|suffix|catch_phrase|bs]",
"Returns fake company data"
method_option :faker_type, :aliases => "-f", :desc => "the faker data type"
def company
case options.faker_type
when 'name'
puts Faker::Company.name
when 'suffix'
puts Faker::Company.suffix
when 'catch_phrase'
puts Faker::Company.catch_phrase
when 'bs'
puts Faker::Company.bs
else
puts "Usage: `faker company -f=name`"
puts "Run `faker help company` for a list of valid types"
end
end

desc "internet [-f email|free_email|user_name|domain_name|ip_v4_address|ip_v6_address]",
"Returns fake internet data"
method_option :faker_type, :aliases => "-f", :desc => "the faker data type"
def internet
case options.faker_type
when 'email'
puts Faker::Internet.email
when 'free_email'
puts Faker::Internet.free_email
when 'user_name'
puts Faker::Internet.user_name
when 'domain_name'
puts Faker::Internet.domain_name
when 'domain_word'
puts Faker::Internet.domain_word
when 'domain_suffix'
puts Faker::Internet.domain_suffix
when 'ip_v4_address'
puts Faker::Internet.ip_v4_address
when 'ip_v6_address'
puts Faker::Internet.ip_v6_address
else
puts "Usage: `faker internet -f=email`"
puts "Run `faker help internet` for a list of valid types"
end
end

desc "lorum [-f words|sentence|sentences|paragraph|paragraphs -c 5]",
"Returns fake lorum data"
method_option :faker_type, :aliases => "-f", :desc => "the faker data type"
method_option :number, :type => :numeric, :aliases => "-n", :desc => "the number to return"
def lorum
case options.faker_type
when 'words'
puts options.number ? Faker::Lorem.words(options.number) : Faker::Lorem.words
when 'sentence'
puts options.number ? Faker::Lorem.sentence(options.number) : Faker::Lorem.sentence
when 'sentences'
puts options.number ? Faker::Lorem.sentences(options.number) : Faker::Lorem.sentences
when 'paragraph'
puts options.number ? Faker::Lorem.paragraph(options.number) : Faker::Lorem.paragraph
when 'paragraphs'
puts options.number ? Faker::Lorem.paragraphs(options.number) : Faker::Lorem.paragraphs
else
puts "Usage: `faker lorum -f words -n 5`"
puts "Run `faker help lorum` for a list of valid types"
end
end

desc "name [-f name|first_name|last_name|prefix|suffix]",
"Returns fake name data"
method_option :faker_type, :aliases => "-f", :desc => "the faker data type"
def name
case options.faker_type
when 'name'
puts Faker::Name.name
when 'first_name'
puts Faker::Name.first_name
when 'last_name'
puts Faker::Name.last_name
when 'prefix'
puts Faker::Name.prefix
when 'suffix'
puts Faker::Name.suffix
else
puts "Usage: `faker name -f name`"
puts "Run `faker help name` for a list of valid types"
end
end

desc "phone_number",
"Returns fake phone number data"
def phone_number
puts Faker::PhoneNumber.phone_number
end
end
end
#