Module: FFaker::Tweet

Extended by:
ModuleUtils, Tweet
Included in:
Tweet
Defined in:
lib/ffaker/tweet.rb

Instance Method Summary collapse

Methods included from ModuleUtils

const_missing, k, underscore, unique

Methods included from RandomUtils

#fetch_sample, #rand, #shuffle

Instance Method Details

#body(required_length = 140) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ffaker/tweet.rb', line 43

def body(required_length = 140)
  required_length = [required_length, 20].max
  body = ''
  prefix = ''
  until body.size > required_length
    body += "#{prefix}#{FFaker::Lorem.sentence}"
    prefix = ' '
  end
  body[required_length - 1] = '#'
  body.gsub(/\s\S*\#.*$/, '.')
end

#mentionObject


33
34
35
# File 'lib/ffaker/tweet.rb', line 33

def mention
  "@#{FFaker::Internet.user_name}"
end

#mentions(num = 2) ⇒ Object


37
38
39
40
41
# File 'lib/ffaker/tweet.rb', line 37

def mentions(num = 2)
  mention_names = []
  num.times { mention_names << mention }
  mention_names.join(' ')
end

#tags(num_tags = 2) ⇒ Object


55
56
57
# File 'lib/ffaker/tweet.rb', line 55

def tags(num_tags = 2)
  fetch_sample(HASHTAG, count: num_tags).join(' ')
end

#tweet(args = {}) ⇒ Object

Options num_hashtags: How many hashtags (default: (skewed (40%) 1-4)) num_mentions: How many mentions (default: (skewed (20% 1-2)) reply: Add reply? (default: (random 10%) body_length: Target length (rand(20..140)) (will be <= target)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ffaker/tweet.rb', line 13

def tweet(args = {})
  options = {
    num_hashtags: [0, rand(1..10) - 6].max,
    num_mentions: [0, rand(1..10) - 8].max,
    reply: (rand(1..10) == 1),
    body_length: rand(20..140)
  }.merge(args)

  my_reply = options[:reply] ? "#{mention} " : ''
  my_mentions = options[:num_mentions] > 0 ? "#{mentions(options[:num_mentions])} " : ''
  my_tags = tags(options[:num_hashtags])

  remaining = [
    options[:body_length],
    140 - (my_reply.size + my_mentions.size + my_tags.size)
  ].min

  "#{my_reply}#{body(remaining)}#{my_mentions}#{my_tags}"
end