class Bluebird::Strategies::TruncateText::Strategy

Public Class Methods

run(tweet, config) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 7
def run(tweet, config)
  if run?(tweet, config.max_length)
    truncate(tweet, config.max_length)
  end
end

Private Class Methods

add_omission(partial) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 94
def add_omission(partial)
  if add_omission?
    length = omission.char_length
    if handle_truncation?(partial, length)
      truncate_partial(partial, length)
      if ends_with_space?(partial)
        delete_last_character(partial)
        partial.content += omission + ' '
      else
        partial.content += omission
      end
    end
  end
end
add_omission?() click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 109
def add_omission?
  omission && omission.char_length > 0
end
count_to_truncate(tweet, max) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 82
def count_to_truncate(tweet, max)
  tweet.length > max ? tweet.length - max : 0
end
deletable?(partial) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 86
def deletable?(partial)
  !partial.separator? && !partial.reply_preventer?
end
delete_last_character(partial) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 135
def delete_last_character(partial)
  partial.content = partial.content[0, partial.content.length - 1]
end
ends_with_space?(partial) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 127
def ends_with_space?(partial)
  partial.content.to_char_a.last.eql?(' ')
end
handle_truncation?(partial, count) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 90
def handle_truncation?(partial, count)
  truncatable_length(partial) > count
end
keep_last_space?(partial) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 131
def keep_last_space?(partial)
  ends_with_space?(partial) && partial.next_partial
end
needs_truncation?(count) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 32
def needs_truncation?(count)
  count > 0
end
omission() click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 139
def omission
  Bluebird::Strategies::TruncateText::Config.omission
end
run?(tweet, max) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 15
def run?(tweet, max)
  tweet.length > max
end
suitable_partial(tweet, count) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 75
def suitable_partial(tweet, count)
  truncatable_partials(tweet).each do |partial|
    return partial if handle_truncation?(partial, count)
  end
  nil
end
truncatable_length(partial) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 113
def truncatable_length(partial)
  if partial.last?
    partial.length
  elsif partial.first?
    if partial.reply_preventer?
      partial.length - 1
    else
      partial.length
    end
  else
    partial.length - 1
  end
end
truncatable_partials(tweet) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 71
def truncatable_partials(tweet)
  tweet.text_partials.reverse
end
truncate(tweet, max) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 19
def truncate(tweet, max)
  count = count_to_truncate(tweet, max)

  if needs_truncation?(count)
    if (suitable = suitable_partial(tweet, count))
      truncate_partial(suitable, count)
      add_omission(suitable)
    else
      truncate_partials(tweet, count)
    end
  end
end
truncate_partial(partial, count) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 46
def truncate_partial(partial, count)
  truncated_count = 0

  if handle_truncation?(partial, count)

    partial.content = if keep_last_space?(partial)
      partial.content[0, partial.length - count - 1] + ' '
    else
      partial.content[0, partial.length - count]
    end

    truncated_count = count
  else
    if deletable?(partial)
      truncated_count = partial.length
      partial.content = ''
    else
      truncated_count = partial.length - 1
      partial.content = partial.reply_preventer? ? '.' : ' '
    end
  end

  truncated_count
end
truncate_partials(tweet, count) click to toggle source
# File lib/bluebird/strategies/truncate_text/strategy.rb, line 36
def truncate_partials(tweet, count)
  truncatable_partials(tweet).each do |partial|
    count -= truncate_partial(partial, count)
    unless needs_truncation?(count)
      add_omission(partial)
      break
    end
  end
end