class Pry::SendTweet::SendTweet

Constants

MAX_TWEET_SIZE

Public Instance Methods

options(o) click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 13
def options(o)
  o.on 'f=', 'file=',
       'One or more paths to file(s) to attach to a tweet.',
        as: Array
  o.on 'r=', 'reply-to=',
       'An absolute url to a tweet you want to reply to.'
  o.on 's=', 'self-destruct=',
       'The number of seconds (represented as a number or a timestamp in the ' \
       'format of HH:MM:SS) to wait before automatically deleting the tweet ' \
       'asynchronously.'
  o.on 'd=', 'delay=',
       'The number of seconds (represented as a number or a timestamp in the ' \
       'format of HH:MM:SS) to wait before creating the tweet asynchronously.'
  o.on 'n', 'no-newline',
       "Remove newlines (\\n) from a tweet before sending it."
  o.on 'v', 'version', 'Print the version information of fry-send_tweet.rb'
end
process(args) click to toggle source
Calls superclass method Pry::SendTweet::BaseCommand#process
# File lib/pry/send_tweet/commands/send_tweet.rb, line 31
def process(args)
  super
  return __print_version_information if opts.version?
  tweet_creator = create_tweet_creator(compose_tweet_with_editor)
  if opts.present?('delay')
    time_obj, sleep_seconds = parse_duration_str(opts['delay'])
    Thread.new {
      sleep sleep_seconds
      tweet_creator.call
    }
    publish_time = (time_obj ? time_obj : Time.now + sleep_seconds)
                    .getlocal
                    .strftime(time_format)
    page_ok bold("Tweet will be published at approximately #{publish_time}.")
  else
    tweet = tweet_creator.call
    page_ok tweet.url
  end
end

Private Instance Methods

__print_version_information() click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 153
def __print_version_information
  side1 = [
    bright_green('fry'),
    bold('-send_tweet.rb '),
    bright_red("v#{Fry::SendTweet::VERSION}")
  ].join
  side2 = bright_yellow(Fry::SendTweet::CODENAME)
  _pry_.pager.page [side1, side2].join(" | ")
end
compose_tweet_with_editor() click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 93
def compose_tweet_with_editor
  tweet = Tempfile.open('pry-send_tweet--compose-tweet') do |file|
    if replying_to_other_tweet?
      # During experimentation I noticed that without prefixing who we are
      # replying to, the tweet we send will not be considered a reply even
      # with reply_to_status_id being set.
      prepend_username_to_reply!(opts['reply-to'], file)
    end
    file.rewind
    Pry::Editor.new(_pry_).invoke_editor(file.path, 0)
    lines = file.read.each_line
    no_newline? ? lines.map(&:chomp).join(' ') : lines.to_a.join
  end
  validate_tweet!(tweet)
  tweet
end
create_tweet_creator(tweet_contents) click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 110
def create_tweet_creator(tweet_contents)
  if opts.present?(:file)
    medias = paths_to_twitterio(opts[:file])
    lambda { send_tweet_with_media(tweet_contents, medias) }
  else
    lambda { send_textual_tweet(tweet_contents) }
  end
end
no_newline?() click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 59
def no_newline?
  opts.present?('no-newline')
end
parse_duration_str(str) click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 139
def parse_duration_str(str)
  if str =~ /\A\d+\z/
    sleep_seconds = Integer(str)
  elsif str =~ /\A\d{2}:\d{2}\z/ || str =~ /\A\d{2}:\d{2}:\d{2}\z/
    time_obj = Time.parse(str)
    time_obj += 3600*24 if time_obj <= Time.now
    sleep_seconds = Integer(time_obj - Time.now)
  else
    raise Pry::CommandError, "--delay='#{str}' or --self-destruct='#{str}' is not " \
                             "something I understand."
  end
  [time_obj, sleep_seconds]
end
paths_to_twitterio(paths) click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 53
def paths_to_twitterio(paths)
  paths.map do |path|
    Pry::SendTweet::TwitterIO.new File.binread(path), File.basename(path)
  end
end
prepend_username_to_reply!(tweet_url, file) click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 75
def prepend_username_to_reply!(tweet_url, file)
  tweet = twitter.status(tweet_url)
  mentions = [tweet.user.screen_name].concat tweet.user_mentions.map(&:screen_name)
  file.write mentions.uniq(&:downcase).map{|u| "@#{u}" }.join(' ')
end
replying_to_other_tweet?() click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 81
def replying_to_other_tweet?
  opts.present?('reply-to')
end
self_destruct!(tweet_id, duration) click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 129
def self_destruct!(tweet_id, duration)
  return if !duration
  _, sleep_seconds = parse_duration_str(duration)
  page bold("Tweet due to self destruct in #{sleep_seconds} seconds")
  Thread.new do
    sleep sleep_seconds
    twitter.destroy_status(tweet_id)
  end
end
send_textual_tweet(tweet_contents) click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 70
def send_textual_tweet(tweet_contents)
  tweet = twitter.update(tweet_contents, tweet_options)
  tweet.tap {|t| self_destruct! t.id, opts['self-destruct'] }
end
send_tweet_with_media(tweet_contents, medias) click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 63
def send_tweet_with_media(tweet_contents, medias)
  tweet = twitter.update_with_media(tweet_contents, medias, tweet_options)
  tweet.tap {|t| self_destruct! t.id, opts['self-destruct'] }
ensure
  medias.each(&:close)
end
tweet_options() click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 85
def tweet_options
  options = {}
  options.merge!({
    in_reply_to_status_id: Integer(File.basename(opts['reply-to']))
  }) if replying_to_other_tweet?
  options
end
validate_tweet!(tweet) click to toggle source
# File lib/pry/send_tweet/commands/send_tweet.rb, line 119
def validate_tweet!(tweet)
  if tweet.strip.empty?
    raise Pry::CommandError, "Can't post an empty tweet."
  elsif tweet.size > MAX_TWEET_SIZE
    raise Pry::CommandError, "The tweet: \n" +
                              word_wrap(tweet) +
                              "\nis too big to publish, try to use less characters."
  end
end