Sha256: e602acd198b8d2686c4b6386471c7e28a77a0c57cb31295f0b99001737d39c41

Contents?: true

Size: 1.6 KB

Versions: 9

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module FFakerTW
  module Tweet
    extend ModuleUtils
    extend self

    # 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)
    def tweet(args = {})
      options = {
        num_hashtags: [0, rand(-5..4)].max,
        num_mentions: [0, rand(-7..2)].max,
        reply: (rand(1..10) == 1),
        body_length: rand(20..140)
      }.merge(args)

      my_reply = options[:reply] ? "#{mention} " : ''
      my_mentions = (options[:num_mentions]).positive? ? "#{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

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

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

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ffakerTW-1.8.0 lib/ffakerTW/tweet.rb
ffakerTW-1.7.0 lib/ffakerTW/tweet.rb
ffakerTW-1.6.0 lib/ffakerTW/tweet.rb
ffakerTW-1.5.0 lib/ffakerTW/tweet.rb
ffakerTW-1.4.0 lib/ffakerTW/tweet.rb
ffakerTW-1.3.0 lib/ffakerTW/tweet.rb
ffakerTW-1.2.0 lib/ffakerTW/tweet.rb
ffakerTW-1.1.0 lib/ffakerTW/tweet.rb
ffakerTW-0.1.0 lib/ffakerTW/tweet.rb