factory-helper 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/History.txt +126 -0
  3. data/License.txt +20 -0
  4. data/README.md +445 -0
  5. data/lib/extensions/array.rb +22 -0
  6. data/lib/extensions/symbol.rb +9 -0
  7. data/lib/factory-helper.rb +182 -0
  8. data/lib/factory-helper/address.rb +59 -0
  9. data/lib/factory-helper/app.rb +22 -0
  10. data/lib/factory-helper/avatar.rb +14 -0
  11. data/lib/factory-helper/bitcoin.rb +49 -0
  12. data/lib/factory-helper/business.rb +22 -0
  13. data/lib/factory-helper/code.rb +63 -0
  14. data/lib/factory-helper/commerce.rb +51 -0
  15. data/lib/factory-helper/company.rb +40 -0
  16. data/lib/factory-helper/date.rb +42 -0
  17. data/lib/factory-helper/finance.rb +26 -0
  18. data/lib/factory-helper/hacker.rb +31 -0
  19. data/lib/factory-helper/internet.rb +116 -0
  20. data/lib/factory-helper/lorem.rb +66 -0
  21. data/lib/factory-helper/name.rb +22 -0
  22. data/lib/factory-helper/number.rb +57 -0
  23. data/lib/factory-helper/phone_number.rb +52 -0
  24. data/lib/factory-helper/team.rb +20 -0
  25. data/lib/factory-helper/time.rb +48 -0
  26. data/lib/factory-helper/version.rb +3 -0
  27. data/lib/locales/de-AT.yml +49 -0
  28. data/lib/locales/de-CH.yml +19 -0
  29. data/lib/locales/de.yml +57 -0
  30. data/lib/locales/en-AU.yml +22 -0
  31. data/lib/locales/en-BORK.yml +4 -0
  32. data/lib/locales/en-CA.yml +14 -0
  33. data/lib/locales/en-GB.yml +13 -0
  34. data/lib/locales/en-IND.yml +20 -0
  35. data/lib/locales/en-NEP.yml +39 -0
  36. data/lib/locales/en-US.yml +83 -0
  37. data/lib/locales/en-au-ocker.yml +31 -0
  38. data/lib/locales/en.yml +155 -0
  39. data/lib/locales/es.yml +62 -0
  40. data/lib/locales/fa.yml +6 -0
  41. data/lib/locales/fr.yml +55 -0
  42. data/lib/locales/it.yml +59 -0
  43. data/lib/locales/ja.yml +25 -0
  44. data/lib/locales/ko.yml +37 -0
  45. data/lib/locales/nb-NO.yml +52 -0
  46. data/lib/locales/nl.yml +77 -0
  47. data/lib/locales/pl.yml +66 -0
  48. data/lib/locales/pt-BR.yml +57 -0
  49. data/lib/locales/ru.yml +65 -0
  50. data/lib/locales/sk.yml +72 -0
  51. data/lib/locales/sv.yml +76 -0
  52. data/lib/locales/vi.yml +63 -0
  53. data/lib/locales/zh-CN.yml +27 -0
  54. data/lib/locales/zh-TW.yml +27 -0
  55. metadata +140 -0
@@ -0,0 +1,51 @@
1
+ module Faker
2
+ class Commerce < Base
3
+
4
+ class << self
5
+ def color
6
+ fetch('commerce.color')
7
+ end
8
+
9
+ def department(max = 3, fixed_amount = false)
10
+ num = max if fixed_amount
11
+ num ||= 1 + rand(max)
12
+
13
+ categories = categories(num)
14
+
15
+ if num > 1
16
+ merge_categories(categories)
17
+ else
18
+ categories[0]
19
+ end
20
+ end
21
+
22
+ def product_name
23
+ fetch('commerce.product_name.adjective') + ' ' + fetch('commerce.product_name.material') + ' ' + fetch('commerce.product_name.product')
24
+ end
25
+
26
+ def price
27
+ random = Random.new
28
+ (random.rand(0..100.0) * 100).floor/100.0
29
+ end
30
+
31
+ private
32
+
33
+ def categories(num)
34
+ categories = []
35
+ while categories.length < num do
36
+ category = fetch('commerce.department')
37
+ categories << category unless categories.include?(category)
38
+ end
39
+
40
+ categories
41
+ end
42
+
43
+ def merge_categories(categories)
44
+ separator = fetch('separator')
45
+ comma_separated = categories.slice!(0...-1).join(', ')
46
+
47
+ [comma_separated, categories[0]].join(separator)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,40 @@
1
+ module Faker
2
+ class Company < Base
3
+ flexible :company
4
+
5
+ class << self
6
+ def name
7
+ parse('company.name')
8
+ end
9
+
10
+ def suffix
11
+ fetch('company.suffix')
12
+ end
13
+
14
+ # Generate a buzzword-laden catch phrase.
15
+ def catch_phrase
16
+ translate('faker.company.buzzwords').collect {|list| list.sample }.join(' ')
17
+ end
18
+
19
+ # When a straight answer won't do, BS to the rescue!
20
+ def bs
21
+ translate('faker.company.bs').collect {|list| list.sample }.join(' ')
22
+ end
23
+
24
+ def ein
25
+ ('%09d' % rand(10 ** 9)).gsub(/(\d\d)(\d\d\d\d\d\d\d)/, '\\1-\\2')
26
+ end
27
+
28
+ def duns_number
29
+ ('%09d' % rand(10 ** 9)).gsub(/(\d\d)(\d\d\d)(\d\d\d\d)/, '\\1-\\2-\\3')
30
+ end
31
+
32
+ # Get a random company logo url in PNG format.
33
+ def logo
34
+ rand_num = Random.rand(13) + 1
35
+ "http://pigment.github.io/fake-logos/logos/medium/color/#{rand_num}.png"
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,42 @@
1
+ module Faker
2
+ class Date < Base
3
+ class << self
4
+ def between(from, to)
5
+ from = get_date_object(from)
6
+ to = get_date_object(to)
7
+
8
+ Faker::Base::rand_in_range(from, to)
9
+ end
10
+
11
+ def forward(days = 365)
12
+ from = ::Date.today + 1
13
+ to = ::Date.today + days
14
+
15
+ between(from, to).to_date
16
+ end
17
+
18
+ def backward(days = 365)
19
+ from = ::Date.today - days
20
+ to = ::Date.today - 1
21
+
22
+ between(from, to).to_date
23
+ end
24
+
25
+ def birthday(min_age = 18, max_age = 65)
26
+ t = ::Date.today
27
+ from = ::Date.new(t.year - min_age, t.month, t.day)
28
+ to = ::Date.new(t.year - max_age, t.month, t.day)
29
+
30
+ between(from, to).to_date
31
+ end
32
+
33
+ private
34
+
35
+ def get_date_object(date)
36
+ date = ::Date.parse(date) if date.is_a?(String)
37
+ date = date.to_date if date.respond_to?(:to_date)
38
+ date
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ module Faker
2
+ class Finance < Base
3
+ CREDIT_CARD_TYPES = [ :visa, :mastercard, :discover, :american_express, :diners_club, :jcb, :switch, :solo, :dankort, :maestro, :forbrugsforeningen, :laser ]
4
+
5
+ class << self
6
+ def credit_card(*types)
7
+ types = CREDIT_CARD_TYPES if types.empty?
8
+ type = types.sample
9
+ template = numerify(fetch("credit_card.#{type}"))
10
+
11
+ # calculate the luhn checksum digit
12
+ multiplier = 1
13
+ luhn_sum = template.gsub(/[^0-9]/, '').split('').reverse.map(&:to_i).inject(0) do |sum, digit|
14
+ multiplier = (multiplier == 2 ? 1 : 2)
15
+ sum + (digit * multiplier).to_s.split('').map(&:to_i).inject(0) { |digit_sum, cur| digit_sum + cur }
16
+ end
17
+ # the sum plus whatever the last digit is must be a multiple of 10. So, the
18
+ # last digit must be 10 - the last digit of the sum.
19
+ luhn_digit = (10 - (luhn_sum % 10)) % 10
20
+
21
+ template.gsub! 'L', luhn_digit.to_s
22
+ template
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ #Port of http://shinytoylabs.com/jargon/
2
+ module Faker
3
+ class Hacker < Base
4
+ flexible :hacker
5
+
6
+ class << self
7
+ def say_something_smart
8
+ phrases.sample
9
+ end
10
+
11
+ def abbreviation; fetch('hacker.abbreviation'); end
12
+ def adjective; fetch('hacker.adjective'); end
13
+ def noun; fetch('hacker.noun'); end
14
+ def verb; fetch('hacker.verb'); end
15
+ def ingverb; fetch('hacker.ingverb'); end
16
+
17
+ def phrases
18
+ [ "If we #{verb} the #{noun}, we can get to the #{abbreviation} #{noun} through the #{adjective} #{abbreviation} #{noun}!",
19
+ "We need to #{verb} the #{adjective} #{abbreviation} #{noun}!",
20
+ "Try to #{verb} the #{abbreviation} #{noun}, maybe it will #{verb} the #{adjective} #{noun}!",
21
+ "You can't #{verb} the #{noun} without #{ingverb} the #{adjective} #{abbreviation} #{noun}!",
22
+ "Use the #{adjective} #{abbreviation} #{noun}, then you can #{verb} the #{adjective} #{noun}!",
23
+ "The #{abbreviation} #{noun} is down, #{verb} the #{adjective} #{noun} so we can #{verb} the #{abbreviation} #{noun}!",
24
+ "#{ingverb} the #{noun} won't do anything, we need to #{verb} the #{adjective} #{abbreviation} #{noun}!",
25
+ "I'll #{verb} the #{adjective} #{abbreviation} #{noun}, that should #{noun} the #{abbreviation} #{noun}!"
26
+ ]
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+ module Faker
3
+ class Internet < Base
4
+ class << self
5
+ def email(name = nil)
6
+ [ user_name(name), domain_name ].join('@')
7
+ end
8
+
9
+ def free_email(name = nil)
10
+ [ user_name(name), fetch('internet.free_email') ].join('@')
11
+ end
12
+
13
+ def safe_email(name = nil)
14
+ [user_name(name), 'example.'+ %w[org com net].shuffle.first].join('@')
15
+ end
16
+
17
+ def user_name(specifier = nil, separators = %w(. _))
18
+ if specifier.kind_of? String
19
+ return specifier.scan(/\w+/).shuffle.join(separators.sample).downcase
20
+ elsif specifier.kind_of? Integer
21
+ tries = 0 # Don't try forever in case we get something like 1_000_000.
22
+ begin
23
+ result = user_name nil, separators
24
+ tries += 1
25
+ end while result.length < specifier and tries < 7
26
+ until result.length >= specifier
27
+ result = result * 2
28
+ end
29
+ return result
30
+ elsif specifier.kind_of? Range
31
+ tries = 0
32
+ begin
33
+ result = user_name specifier.min, separators
34
+ tries += 1
35
+ end while not specifier.include? result.length and tries < 7
36
+ return result[0...specifier.max]
37
+ end
38
+
39
+ fix_umlauts([
40
+ Proc.new { Name.first_name.gsub(/\W/, '').downcase },
41
+ Proc.new {
42
+ [ Name.first_name, Name.last_name ].map {|n|
43
+ n.gsub(/\W/, '')
44
+ }.join(separators.sample).downcase }
45
+ ].sample.call)
46
+ end
47
+
48
+ def password(min_length = 8, max_length = 16)
49
+ temp = Lorem.characters(min_length)
50
+ diff_length = max_length - min_length
51
+ if diff_length > 0
52
+ diff_rand = rand(diff_length + 1)
53
+ temp += Lorem.characters(diff_rand)
54
+ end
55
+ temp = temp[0..min_length] if min_length > 0
56
+ return temp
57
+ end
58
+
59
+ def domain_name
60
+ [ fix_umlauts(domain_word), domain_suffix ].join('.')
61
+ end
62
+
63
+ def fix_umlauts(string)
64
+ string.gsub(/[äöüß]/i) do |match|
65
+ case match.downcase
66
+ when "ä" 'ae'
67
+ when "ö" 'oe'
68
+ when "ü" 'ue'
69
+ when "ß" 'ss'
70
+ end
71
+ end
72
+ end
73
+
74
+ def domain_word
75
+ Company.name.split(' ').first.gsub(/\W/, '').downcase
76
+ end
77
+
78
+ def domain_suffix
79
+ fetch('internet.domain_suffix')
80
+ end
81
+
82
+ def mac_address(prefix='')
83
+ prefix_digits = prefix.split(':').map{ |d| d.to_i(16) }
84
+ address_digits = (6 - prefix_digits.size).times.map{ rand(256) }
85
+ (prefix_digits + address_digits).map{ |d| '%02x' % d }.join(':')
86
+ end
87
+
88
+ def ip_v4_address
89
+ ary = (2..254).to_a
90
+ [ary.sample,
91
+ ary.sample,
92
+ ary.sample,
93
+ ary.sample].join('.')
94
+ end
95
+
96
+ def ip_v6_address
97
+ @@ip_v6_space ||= (0..65535).to_a
98
+ container = (1..8).map{ |_| @@ip_v6_space.sample }
99
+ container.map{ |n| n.to_s(16) }.join(':')
100
+ end
101
+
102
+ def url(host = domain_name, path = "/#{user_name}")
103
+ "http://#{host}#{path}"
104
+ end
105
+
106
+ def slug(words = nil, glue = nil)
107
+ glue ||= %w[- _ .].sample
108
+ (words || Faker::Lorem::words(2).join(' ')).gsub(' ', glue).downcase
109
+ end
110
+
111
+ def device_token
112
+ rand(16 ** 64).to_s(16).rjust(64, '0').chars.to_a.shuffle.join
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,66 @@
1
+ module Faker
2
+ # Based on Perl's Text::Lorem
3
+ class Lorem < Base
4
+ class << self
5
+ def word
6
+ translate('faker.lorem.words').sample
7
+ end
8
+
9
+ def words(num = 3, supplemental = false)
10
+ resolved_num = resolve(num)
11
+ word_list = (
12
+ translate('faker.lorem.words') +
13
+ (supplemental ? translate('faker.lorem.supplemental') : [])
14
+ )
15
+ word_list = word_list * ((resolved_num / word_list.length) + 1)
16
+ word_list.shuffle[0, resolved_num]
17
+ end
18
+
19
+ def character
20
+ characters(1)
21
+ end
22
+
23
+ def characters(char_count = 255)
24
+ return '' if char_count.respond_to?(:to_i) && char_count.to_i < 1
25
+ char_count = resolve(char_count)
26
+ rand(36**char_count).to_s(36).rjust(char_count, '0').chars.to_a.shuffle.join
27
+ end
28
+
29
+ def sentence(word_count = 4, supplemental = false, random_words_to_add = 6)
30
+ words(word_count + rand(random_words_to_add.to_i).to_i, supplemental).join(' ').capitalize + '.'
31
+ end
32
+
33
+ def sentences(sentence_count = 3, supplemental = false)
34
+ [].tap do |sentences|
35
+ 1.upto(resolve(sentence_count)) do
36
+ sentences << sentence(3, supplemental)
37
+ end
38
+ end
39
+ end
40
+
41
+ def paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3)
42
+ sentences(resolve(sentence_count) + rand(random_sentences_to_add.to_i).to_i, supplemental).join(' ')
43
+ end
44
+
45
+ def paragraphs(paragraph_count = 3, supplemental = false)
46
+ [].tap do |paragraphs|
47
+ 1.upto(resolve(paragraph_count)) do
48
+ paragraphs << paragraph(3, supplemental)
49
+ end
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ # If an array or range is passed, a random value will be selected.
56
+ # All other values are simply returned.
57
+ def resolve(value)
58
+ case value
59
+ when Array then value[rand(value.size)]
60
+ when Range then rand((value.last+1) - value.first) + value.first
61
+ else value
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,22 @@
1
+ module Faker
2
+ class Name < Base
3
+ flexible :name
4
+
5
+ class << self
6
+
7
+ def name
8
+ parse('name.name')
9
+ end
10
+
11
+ def first_name; fetch('name.first_name'); end
12
+ def last_name; fetch('name.last_name'); end
13
+ def prefix; fetch('name.prefix'); end
14
+ def suffix; fetch('name.suffix'); end
15
+
16
+ # Generate a buzzword-laden job title
17
+ # Wordlist from http://www.bullshitjob.com/title/
18
+ def title; fetch('name.title.descriptor') + ' ' + fetch('name.title.level') + ' ' + fetch('name.title.job'); end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,57 @@
1
+ module Faker
2
+ class Number < Base
3
+ class << self
4
+ def number(digits)
5
+ (1..digits).collect {digit}.join
6
+ end
7
+
8
+ def decimal(l_digits, r_digits = 2)
9
+ l_d = self.number(l_digits)
10
+ r_d = self.number(r_digits)
11
+ "#{l_d}.#{r_d}"
12
+ end
13
+
14
+ def digit
15
+ (rand() * 9).round.to_s
16
+ end
17
+
18
+ def hexadecimal(digits)
19
+ hex = ""
20
+ digits.times { hex += rand(15).to_s(16) }
21
+ hex
22
+ end
23
+
24
+ def between(from = 1.00, to = 5000.00)
25
+ Faker::Base::rand_in_range(from, to)
26
+ end
27
+
28
+ def positive(from = 1.00, to = 5000.00)
29
+ random_number = between(from, to)
30
+ greater_than_zero(random_number)
31
+ end
32
+
33
+ def negative(from = -5000.00, to = -1.00)
34
+ random_number = between(from, to)
35
+ less_than_zero(random_number)
36
+ end
37
+
38
+ private
39
+
40
+ def greater_than_zero(number)
41
+ should_be(number, :>)
42
+ end
43
+
44
+ def less_than_zero(number)
45
+ should_be(number, :<)
46
+ end
47
+
48
+ def should_be(number, method_to_compare)
49
+ if number.send(method_to_compare, 0)
50
+ number
51
+ else
52
+ number * -1
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end