Class: Fab

Inherits:
Object
  • Object
show all
Defined in:
lib/sixarm_ruby_fab.rb,
lib/sixarm_ruby_fab/id.rb,
lib/sixarm_ruby_fab/geo.rb,
lib/sixarm_ruby_fab/date.rb,
lib/sixarm_ruby_fab/mime.rb,
lib/sixarm_ruby_fab/text.rb,
lib/sixarm_ruby_fab/time.rb,
lib/sixarm_ruby_fab/uuid.rb,
lib/sixarm_ruby_fab/agent.rb,
lib/sixarm_ruby_fab/basic.rb,
lib/sixarm_ruby_fab/email.rb,
lib/sixarm_ruby_fab/files.rb,
lib/sixarm_ruby_fab/names.rb,
lib/sixarm_ruby_fab/phone.rb,
lib/sixarm_ruby_fab/locale.rb,
lib/sixarm_ruby_fab/postal.rb,
lib/sixarm_ruby_fab/company.rb,
lib/sixarm_ruby_fab/twitter.rb,
lib/sixarm_ruby_fab/datetime.rb,
lib/sixarm_ruby_fab/internet.rb,
lib/sixarm_ruby_fab/password.rb,
lib/sixarm_ruby_fab/username.rb

Constant Summary collapse

AZ =
('a'..'z').to_a
NAME_CHARS =
"aabcdeeefghiijklmnoopqrrssttuuvwxyz".split(//)
NOTE_CHARS =
"aabcdeeefghiijklmnoopqrrssttuuvwxyz     ..,,;;-+'*".split(//)
AGENT_LIST =
[
  "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A405",
  "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B179",
  "Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; SCH-I500 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
  "Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; EVO Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
]
CHARS =
('a'..'z').to_a
LANGUAGE_CODE_ISO6391_LIST =
[
  'en',  # English
  'fr',  # French
  'de',  # German
  'ja',  # Japanese
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#agent_listObject

Returns the value of attribute agent_list.



10
11
12
# File 'lib/sixarm_ruby_fab/agent.rb', line 10

def agent_list
  @agent_list
end

#language_code_iso6391_listObject

Returns the value of attribute language_code_iso6391_list.



10
11
12
# File 'lib/sixarm_ruby_fab/locale.rb', line 10

def language_code_iso6391_list
  @language_code_iso6391_list
end

Instance Method Details

#agentObject



12
13
14
# File 'lib/sixarm_ruby_fab/agent.rb', line 12

def agent
  agent_list.sample
end

#basename(options = {}) ⇒ Object

Fab a base name e.g. “myfile.txt”

The base name has two parts:

* The "intension" e.g. "myfile"
* The "extension" e.g. "txt"

Options:

* chars: a..z
* intension_length: [sent to #basename_intension as :length]
* extension_length: [sent to #basename_extension as :length]


28
29
30
31
32
# File 'lib/sixarm_ruby_fab/files.rb', line 28

def basename(options = {})
  intention = basename_intension(options[:intension_length] ? options.merge(length: intension_length) : options)
  extention = basename_extension(options[:extension_length] ? options.merge(length: extension_length) : options)
  "#{basename_intension}.#{basename_extension}"
end

#basename_extension(options = {}) ⇒ Object

Fab a file base name extension e.g. “txt”

Options:

* chars: a..z
* length: rand(1..5)


56
57
58
# File 'lib/sixarm_ruby_fab/files.rb', line 56

def basename_extension(options = {})
  (options[:chars] || AZ).sample(options[:size] || rand(1..5)).join
end

#basename_intension(options = {}) ⇒ Object

Fab a file base name intension e.g. “myfile”

Options:

* chars: a..z
* length: rand(1..30)


43
44
45
# File 'lib/sixarm_ruby_fab/files.rb', line 43

def basename_intension(options = {})
  (options[:chars] || AZ).sample(options[:size] || rand(1..30)).join
end

#booleanboolean

Fab a boolean, either true or false.

Returns:

  • (boolean)

    either true or false.



7
8
9
# File 'lib/sixarm_ruby_fab/basic.rb', line 7

def boolean
  [true, false].sample
end

#cityObject

Fab a random city string. Delegates to Forgery::Address.city



17
18
19
# File 'lib/sixarm_ruby_fab/postal.rb', line 17

def city
  Forgery::Address.city 
end

#company_name(options = {}) ⇒ Object

Fab a company name. Delegates to Forgery::Name.company_name.



8
9
10
# File 'lib/sixarm_ruby_fab/company.rb', line 8

def company_name(options = {})
  Forgery::Name.company_name
end

#content_type(options = {}) ⇒ Object

Fab a random content type part e.g. “image/jpeg”.

Options:

* chars: a..z
* size: rand(1..20) [per part]


12
13
14
# File 'lib/sixarm_ruby_fab/mime.rb', line 12

def content_type(options = {})
  "#{content_type_part(options)}/#{content_type_part(options)}"
end

#content_type_part(options = {}) ⇒ Object

Fab a random content type part e.g. “image”.

Options:

* chars: a..z
* size: rand(1..20)


25
26
27
# File 'lib/sixarm_ruby_fab/mime.rb', line 25

def content_type_part(options = {})
  (options[:chars] || AZ).sample(options[:size] || rand(1..20)).join
end

#date(options = {}) ⇒ Object

Fab a random date.

Options:

* min: today - 1000 
* max: today + 1000


12
13
14
# File 'lib/sixarm_ruby_fab/date.rb', line 12

def date(options = {})
  rand((options[:min] || date_min)..(options[:max] || date_max))
end

#date_maxObject

Get. The default is today + 1000.



27
28
29
# File 'lib/sixarm_ruby_fab/date.rb', line 27

def date_max
  @date_max ||= Date.today + 1000
end

#date_max=(x) ⇒ Object

Set.



32
33
34
# File 'lib/sixarm_ruby_fab/date.rb', line 32

def date_max=x
  @date_max=x
end

#date_minObject

Get. The default is today - 1000.



17
18
19
# File 'lib/sixarm_ruby_fab/date.rb', line 17

def date_min
  @date_min ||= Date.today - 1000
end

#date_min=(x) ⇒ Object

Set.



22
23
24
# File 'lib/sixarm_ruby_fab/date.rb', line 22

def date_min=x
  @date_min=x
end

#datetime(options = {}) ⇒ Object

Fab a random datetime.

Options:

* min: now - 1000 
* max: now + 1000


12
13
14
# File 'lib/sixarm_ruby_fab/datetime.rb', line 12

def datetime(options = {})
  rand((options[:min] || datetime_min)..(options[:max] || datetime_max))
end

#datetime_maxObject

Get. The default is now + 1000.



27
28
29
# File 'lib/sixarm_ruby_fab/datetime.rb', line 27

def datetime_max
  @datetime_max ||= DateTime.now + 1000
end

#datetime_max=(x) ⇒ Object

Set.



32
33
34
# File 'lib/sixarm_ruby_fab/datetime.rb', line 32

def datetime_max=x
  @datetime_max=x
end

#datetime_minObject

Get. The default is now - 1000.



17
18
19
# File 'lib/sixarm_ruby_fab/datetime.rb', line 17

def datetime_min
  @datetime_min ||= DateTime.now - 1000
end

#datetime_min=(x) ⇒ Object

Set.



22
23
24
# File 'lib/sixarm_ruby_fab/datetime.rb', line 22

def datetime_min=x
  @datetime_min=x
end

#description(options = {}) ⇒ Object

Fab a random note.

Options:

* chars: NOTE_CHARS
* size: rand(20..200) [actual size may be less because we strip the string]


41
42
43
# File 'lib/sixarm_ruby_fab/text.rb', line 41

def description(options = {})
  (options[:chars] || NOTE_CHARS).sample(options[:size] || rand(20..200)).join.strip
end

#dirname(options = {}) ⇒ Object

Fab a directory name e.g. “/mydir1/mydir2”. This calls #dirname_part rand times.

Options:

* parts: rand(2..6) [how many parts to use]
* chars: a..z [sent to #dirname_part]
* length: 1..20 per part [sent to #dirname_part]


71
72
73
74
75
76
# File 'lib/sixarm_ruby_fab/files.rb', line 71

def dirname(options = {})
  pathname = Pathname.new("/")
  parts = options[:parts] || rand(2..6)
  parts.times.each{ pathname += dirname_part(options) }
  pathname.to_s
end

#dirname_part(options = {}) ⇒ Object

Fab a directory path part e.g. “mydir”.

Options:

* chars: a..z
* size: rand(1..20)


87
88
89
# File 'lib/sixarm_ruby_fab/files.rb', line 87

def dirname_part(options = {})
  (options[:chars] || AZ).sample(options[:size] || rand(1..20)).join
end

#email_addressObject

Fab an email address. Delegate to Forgery::Email.address



8
9
10
# File 'lib/sixarm_ruby_fab/email.rb', line 8

def email_address
  Forgery::Email.address
end

#family_nameObject

Fab a random last name. Delegates to Forgery::Name.last_name



26
27
28
# File 'lib/sixarm_ruby_fab/names.rb', line 26

def family_name 
  Forgery::Name.last_name
end

#file_extension(options = {}) ⇒ Object

Fab a file extension. Delegates to #basename_extension.

TODO refactor this to streamline it



98
99
100
# File 'lib/sixarm_ruby_fab/files.rb', line 98

def file_extension(options = {})
  basename_extension(options)
end

#file_path(options = {}) ⇒ Object

Fab a file path. Delegates to #path.

TODO refactor this to streamline it



109
110
111
# File 'lib/sixarm_ruby_fab/files.rb', line 109

def file_path(options = {})
  path(options)
end

#given_nameObject

Fab a random given name. Delegates to Forgery::Name.first_name



8
9
10
# File 'lib/sixarm_ruby_fab/names.rb', line 8

def given_name 
  Forgery::Name.first_name
end

#id(options = {}) ⇒ Object

Fab a random id.

Options:

* min: 1
* max: 30000


12
13
14
# File 'lib/sixarm_ruby_fab/id.rb', line 12

def id(options = {})
  rand((options[:min] || 1)..(options[:max] || 30000))
end

#ids(options = {}) ⇒ Object

Fab a list of random ids.

Options:

* size: 3


24
25
26
# File 'lib/sixarm_ruby_fab/id.rb', line 24

def ids(options = {})
  (options[:size] || 3).times.map{ id(options) }
end

#job_title(options = {}) ⇒ Object

Fab a job title. Delegates to Forgery::Name.job_title.



17
18
19
# File 'lib/sixarm_ruby_fab/company.rb', line 17

def job_title(options = {})
  Forgery::Name.job_title
end

#language_code(options = {}) ⇒ Object

Fab a language code e.g. “en” for English. Delegates to #language_code_iso6391



17
18
19
# File 'lib/sixarm_ruby_fab/locale.rb', line 17

def language_code(options = {})
  language_code_iso6391(options)
end

#language_code_iso6391(options = {}) ⇒ Object

Fab a language code e.g. “en” for English. Samples from LANGUAGE_CODE_ISO6391_LIST.



26
27
28
# File 'lib/sixarm_ruby_fab/locale.rb', line 26

def language_code_iso6391(options = {})
  LANGUAGE_CODE_ISO6391_LIST.sample
end

#latitudeObject

Fab a random latitude. Delegate to Forgery::Geo.latitude.



8
9
10
# File 'lib/sixarm_ruby_fab/geo.rb', line 8

def latitude
  Forgery::Geo.latitude
end

#longitudeObject

Fab a random longitude. Delegate to Forgery::Geo.longitude.



17
18
19
# File 'lib/sixarm_ruby_fab/geo.rb', line 17

def longitude
  Forgery::Geo.longitude
end

#lorem(options = {}) ⇒ Object

Fab a random lorem ipsum string.

Options:

* chars: NOTE_CHARS
* size: rand(20..200) [actual size may be less because we strip the string]


54
55
56
# File 'lib/sixarm_ruby_fab/text.rb', line 54

def lorem(options = {})
  (options[:chars] || NOTE_CHARS).sample(options[:size] || rand(20..200)).join.strip
end

#middle_nameObject

Fab a random middle name. Delegates to Forgery::Name.first_name



17
18
19
# File 'lib/sixarm_ruby_fab/names.rb', line 17

def middle_name 
  Forgery::Name.first_name
end

#name(options = {}) ⇒ Object

Fab a random name.

Options:

* chars: NAME_CHARS
* size: rand(10..30) [actual size may be less because we strip the string]


15
16
17
# File 'lib/sixarm_ruby_fab/text.rb', line 15

def name(options = {})
  (options[:chars] || NAME_CHARS).sample(options[:size] || rand(10..30)).join.strip
end

#note(options = {}) ⇒ Object

Fab a random note.

Options:

* chars: NOTE_CHARS
* size: rand(1..200) [actual size may be less because we strip the string]


28
29
30
# File 'lib/sixarm_ruby_fab/text.rb', line 28

def note(options = {})
  (options[:chars] || NOTE_CHARS).sample(options[:size] || rand(1..200)).join.strip
end

#password(options = {}) ⇒ Object

Fab a random password.

Options:

* chars: a..z
* size: rand(9..40)


12
13
14
# File 'lib/sixarm_ruby_fab/password.rb', line 12

def password(options = {})
  (options[:chars] || AZ).sample(options[:size] || rand(9..40)).join
end

#path(options = {}) ⇒ Object

Fab a path e.g. “/mydir1/mydir2/myfile.txt”



9
10
11
# File 'lib/sixarm_ruby_fab/files.rb', line 9

def path(options = {})
  (Pathname.new("") + dirname(options) + basename(options)).to_s
end

#phone(options = {}) ⇒ Object

Fab a random phone number string. Delegates to Forgery::Address.phone.



8
9
10
# File 'lib/sixarm_ruby_fab/phone.rb', line 8

def phone(options = {})
  Forgery::Address.phone
end

#rating(options = {}) ⇒ Fixnum

Fab a rating, such as 1 to 5 stars.

Options:

* min: 1
* max: 5

Returns:

  • (Fixnum)

    a random number



20
21
22
# File 'lib/sixarm_ruby_fab/basic.rb', line 20

def rating(options = {})
  rand((options[:min] || 1)..(options[:max] || 5))
end

#start_date(options = {}) ⇒ Object

Fab a random start date. Delegates to #date.



41
42
43
# File 'lib/sixarm_ruby_fab/date.rb', line 41

def start_date(options = {})
  date(options)
end

#start_date_and_stop_date(options = {}) ⇒ Object

Fab a random start date and stop date. The start is less than or equal to the stop. Delegates to #start_date and #stop_date.



60
61
62
# File 'lib/sixarm_ruby_fab/date.rb', line 60

def start_date_and_stop_date(options = {})
  [start_date(options), stop_date(options)].sort
end

#start_datetime(options = {}) ⇒ Object

Fab a random start datetime. Delegates to #datetime.



41
42
43
# File 'lib/sixarm_ruby_fab/datetime.rb', line 41

def start_datetime(options = {})
  datetime(options)
end

#start_datetime_and_stop_datetime(options = {}) ⇒ Object

Fab a random start datetime and stop datetime. The start is less than or equal to the stop. Delegates to #start_datetime and #stop_datetime.



60
61
62
# File 'lib/sixarm_ruby_fab/datetime.rb', line 60

def start_datetime_and_stop_datetime(options = {})
  [start_datetime(options), stop_datetime(options)].sort
end

#start_time(options = {}) ⇒ Object

Fab a random start time. Delegates to #time.



41
42
43
# File 'lib/sixarm_ruby_fab/time.rb', line 41

def start_time(options = {})
  time(options)
end

#start_time_and_stop_time(options = {}) ⇒ Object

Fab a random start time and stop time. The start is less than or equal to the stop. Delegates to #start_time and #stop_time.



60
61
62
# File 'lib/sixarm_ruby_fab/time.rb', line 60

def start_time_and_stop_time(options = {})
  [start_time(options), stop_time(options)].sort
end

#stop_date(options = {}) ⇒ Object

Fab a random stop date. Delegates to #date.



50
51
52
# File 'lib/sixarm_ruby_fab/date.rb', line 50

def stop_date(options = {})
  date(options)
end

#stop_datetime(options = {}) ⇒ Object

Fab a random stop datetime. Delegates to #datetime.



50
51
52
# File 'lib/sixarm_ruby_fab/datetime.rb', line 50

def stop_datetime(options = {})
  datetime(options)
end

#stop_time(options = {}) ⇒ Object

Fab a random stop time. Delegates to #time.



50
51
52
# File 'lib/sixarm_ruby_fab/time.rb', line 50

def stop_time(options = {})
  time(options)
end

#street_addressObject

Fab a random street address. Delegates to Forgery::Address.street_address.



8
9
10
# File 'lib/sixarm_ruby_fab/postal.rb', line 8

def street_address
  Forgery::Address.street_address 
end

#time(options = {}) ⇒ Object

Fab a random time.

Options:

* min
* max


12
13
14
# File 'lib/sixarm_ruby_fab/time.rb', line 12

def time(options = {})
  rand((options[:min] || time_min)..(options[:max] || time_max))
end

#time_maxObject

Get. The default is now + 1000.



27
28
29
# File 'lib/sixarm_ruby_fab/time.rb', line 27

def time_max
  @time_max ||= Time.now + 1000
end

#time_max=(x) ⇒ Object

Set.



32
33
34
# File 'lib/sixarm_ruby_fab/time.rb', line 32

def time_max=x
  @time_max=x
end

#time_minObject

Get. The default is now - 1000.



17
18
19
# File 'lib/sixarm_ruby_fab/time.rb', line 17

def time_min
  @time_min ||= Time.now - 1000
end

#time_min=(x) ⇒ Object

Set.



22
23
24
# File 'lib/sixarm_ruby_fab/time.rb', line 22

def time_min=x
  @time_min=x
end

#twitter_favorite_count(options = {}) ⇒ Fixnum

Fab a Twitter favorite count.

Options:

* min: 0
* max: 999

Returns:

  • (Fixnum)

    a favorite count



67
68
69
# File 'lib/sixarm_ruby_fab/twitter.rb', line 67

def twitter_favorite_count(options = {})
  rand((options[:min] || 0)..(options[:max] || 999))
end

#twitter_retweet_count(options = {}) ⇒ Fixnum

Fab a Twitter retweet count.

Options:

* min: 0
* max: 999

Returns:

  • (Fixnum)

    a retweet count



54
55
56
# File 'lib/sixarm_ruby_fab/twitter.rb', line 54

def twitter_retweet_count(options = {})
  rand((options[:min] || 0)..(options[:max] || 999))
end

#twitter_screen_name(options = {}) ⇒ Fixnum

Fab a Twitter screen name. This delegates to Fab.username.

Options:

* none

Returns:

  • (Fixnum)

    a screen name



12
13
14
# File 'lib/sixarm_ruby_fab/twitter.rb', line 12

def twitter_screen_name(options = {})
  username(options)
end

#twitter_tweet_hash(options = {}) ⇒ Hash

Fab a Twitter tweet hash.

Options:

* any; merge this hash into the tweet hash.

Returns:

  • (Hash)

    a tweet hash



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sixarm_ruby_fab/twitter.rb', line 92

def twitter_tweet_hash(options={})
  id = options[:id] || options["id"] || Fab.twitter_tweet_id
  {
    "created_at" => Fab.time.httpdate,
    "id" => id.to_i,
    "id_str" => id.to_s,
    "text" => Fab.lorem,
    "source" => "web",
    "truncated" => false,
    "in_reply_to_status_id" => nil,
    "in_reply_to_status_id_str" => nil,
    "in_reply_to_user_id" => nil,
    "in_reply_to_user_id_str" => nil,
    "in_reply_to_screen_name" => nil,
    "user" => Fab.twitter_user_hash,
    "geo" => nil,
    "coordinates" => nil,
    "place" => nil,
    "contributors" => nil,
    "retweet_count" => Fab.twitter_retweet_count,
    "favorite_count" => Fab.twitter_favorite_count,
    "favorited" => Fab.boolean,
    "retweeted" => Fab.boolean,
    "lang" => Fab.language_code
  }.merge(options)
end

#twitter_tweet_id(options = {}) ⇒ Fixnum

Fab a Twitter tweet id.

Options:

* min: 1000000000
* max: 9000000000

Returns:

  • (Fixnum)

    a tweet id



80
81
82
# File 'lib/sixarm_ruby_fab/twitter.rb', line 80

def twitter_tweet_id(options = {})
  rand((options[:min] || 1000000000)..(options[:max] || 9000000000))
end

#twitter_user_hash(options = {}) ⇒ Hash

Fab a Twitter user hash.

Options:

* any; merge this hash into the user hash.

Returns:

  • (Hash)

    a Twitter user hash



37
38
39
40
41
42
43
# File 'lib/sixarm_ruby_fab/twitter.rb', line 37

def twitter_user_hash(options = {})
  id = options[:id] || options["id"] || twitter_user_id
  {
    "id" => id.to_i,
    "id_str" => id.to_s
  }.merge(options)
end

#twitter_user_id(options = {}) ⇒ Fixnum

Fab a Twitter user id.

Options:

* min
* max

Returns:

  • (Fixnum)

    a user id



25
26
27
# File 'lib/sixarm_ruby_fab/twitter.rb', line 25

def twitter_user_id(options = {})
  rand((options[:min] || 10000000)..(options[:max] || 100000000))
end

#uriObject

Fab a random URI. Delegates to Forgery::Internet.uri.



8
9
10
# File 'lib/sixarm_ruby_fab/internet.rb', line 8

def uri 
  Forgery::Internet.uri
end

#username(options = {}) ⇒ Object

Fab a username.

Options:

* chars: a..z
* size: rand(10..30) [actual size may be less because we strip the string]


12
13
14
# File 'lib/sixarm_ruby_fab/username.rb', line 12

def username(options = {})
  (options[:chars] || AZ).sample(options[:size] || rand(5..15)).join
end

#usstateObject

Fab a random US state abbreviation. Delegates to Forgery::Address.state_abbrev.



26
27
28
# File 'lib/sixarm_ruby_fab/postal.rb', line 26

def usstate 
  Forgery::Address.state_abbrev 
end

#uszipObject

Fab a random US zip code. Delegates to Forgery::Address.zip.



35
36
37
# File 'lib/sixarm_ruby_fab/postal.rb', line 35

def uszip
  Forgery::Address.zip 
end

#uuid(options = {}) ⇒ Object

Fab a random UUID.

Options:

* none


11
12
13
# File 'lib/sixarm_ruby_fab/uuid.rb', line 11

def uuid(options = {})
  SecureRandom.uuid
end

#uuids(options = {}) ⇒ Object

Fab a list of random UUIDs.

Options:

* size: 3


23
24
25
# File 'lib/sixarm_ruby_fab/uuid.rb', line 23

def uuids(options = {})
  (options[:size] || 3).times.map{ uuid(options) }
end