Sha256: 61793e144a9762e47891e4acd0c76fde1b3c2efd321d9e145e8e49dc2f02c39e

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 't/core_ext/string'
require 't/rcfile'
require 'thor'
require 'twitter'

module T
  class CLI
    class Follow < Thor
      DEFAULT_HOST = 'api.twitter.com'
      DEFAULT_PROTOCOL = 'https'

      check_unknown_options!

      def initialize(*)
        super
        @rcfile = RCFile.instance
      end

      desc "users SCREEN_NAME [SCREEN_NAME...]", "Allows you to start following users."
      def users(screen_name, *screen_names)
        screen_names.unshift(screen_name)
        users = screen_names.map do |screen_name|
          screen_name = screen_name.strip_at
          user = client.follow(screen_name, :include_entities => false)
          say "@#{@rcfile.default_profile[0]} is now following @#{user.screen_name}."
          user
        end
        number = users.length
        say "@#{@rcfile.default_profile[0]} is now following #{number} more #{number == 1 ? 'user' : 'users'}."
        say
        say "Run `#{$0} unfollow users #{screen_names.join(' ')}` to stop."
      end

      desc "all SUBCOMMAND ...ARGS", "Follow all users."
      require 't/cli/follow/all'
      subcommand 'all', CLI::Follow::All

    private

      def base_url
        "#{protocol}://#{host}"
      end

      def client
        return @client if @client
        @rcfile.path = parent_options['profile'] if parent_options['profile']
        @client = Twitter::Client.new(
          :endpoint => base_url,
          :consumer_key => @rcfile.default_consumer_key,
          :consumer_secret => @rcfile.default_consumer_secret,
          :oauth_token => @rcfile.default_token,
          :oauth_token_secret  => @rcfile.default_secret
        )
      end

      def host
        parent_options['host'] || DEFAULT_HOST
      end

      def protocol
        parent_options['no_ssl'] ? 'http' : DEFAULT_PROTOCOL
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
t-0.2.0 lib/t/cli/follow.rb