Sha256: 2364ea09ff634297f24cea72844e4b31199dd2f91e969a8da4de56b20ba7d8f4

Contents?: true

Size: 757 Bytes

Versions: 15

Compression:

Stored size: 757 Bytes

Contents

module ConsadoleAggregator
  class Helper
    PLACE_HOLDER = '@'
    PLACE_HOLD_MATCHER = Regexp.compile("#{PLACE_HOLDER}{20}")

    def self.truncate_for_twitter text, opt={}
      url_place_holder = opt[:url] && PLACE_HOLDER*20 # twitter compress url to t.co as length 20 characters.
      base = "#{text} #{url_place_holder} #{opt[:hashtag]}".squeeze(' ').rstrip
      if base.size > 140
        over_size = base.size - 140
        self.truncate_for_twitter(omit(text, over_size), opt)
      else
        base.gsub(PLACE_HOLD_MATCHER) { opt[:url] } # revert place holder to url
      end
    end

    private
    def self.omit text, over_size
      truncated = text.slice(0...-over_size)
      truncated[-3..-1] = '...'
      truncated
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
consadole_aggregator-0.2.14 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.13 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.12 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.11 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.10 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.9 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.8 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.7 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.6 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.5 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.4 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.3 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.2 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.1 lib/consadole_aggregator/helper.rb
consadole_aggregator-0.2.0 lib/consadole_aggregator/helper.rb