module Termtter::API

Attributes

connection[R]
twitter[R]

Public Class Methods

authorize_by_oauth(show_information=false, save_to_token_file=true, put_to_config=true, verbose=true) click to toggle source
# File lib/termtter/api.rb, line 51
def authorize_by_oauth(show_information=false, save_to_token_file=true, put_to_config=true, verbose=true)
  puts '1. Connecting to twitter...' if verbose

  request_token = consumer.get_request_token

  puts '2. Authorization URL: ' + request_token.authorize_url if verbose
  puts '   Opening authorization web page...' if verbose

  begin
    open_browser(request_token.authorize_url)
  rescue BrowserNotFound
    puts "Browser not found. Please log in and/or grant access to get PIN via your browser at #{request_token.authorize_url}"
  end
  sleep 2

  ui = create_highline
  pin = ui.ask('3. Enter PIN: ')
  puts ""
  puts "4. Fetching access_token..."
  access_token = request_token.get_access_token(:oauth_verifier => pin)

  if put_to_config
    config.access_token = access_token.token
    config.access_token_secret = access_token.secret
  end

  if save_to_token_file
    puts "5. Saving to token file... (" + config.token_file + ")"
    open(File.expand_path(config.token_file),"w") do |f|
      f.puts access_token.token
      f.puts access_token.secret
    end
  end

  puts "Authorization successfully completed."

  return {:token  => access_token.token,
          :secret => access_token.secret}
end
call_by_channel(c, *opt) click to toggle source
# File lib/plugins/channel.rb, line 21
def call_by_channel(c, *opt)
  case c.to_s
  when "main"
    Termtter::API.twitter.home_timeline(*opt)
  when "replies"
    Termtter::API.twitter.replies(*opt)
  when /^(.+)_s(earch)?$/
    Termtter::API.twitter.search($1, *opt)
  else
    user_name, slug = c.to_s.split('/')
    if !user_name.nil? && slug.nil?
      slug = user_name
      user_name = config.user_name
    elsif user_name.empty?
      user_name = config.user_name
    end
    user_name = Termtter::Client.normalize_as_user_name(user_name)
    Termtter::API.twitter.list_statuses(user_name, slug, *opt)
  end
end
consumer() click to toggle source
# File lib/termtter/api.rb, line 91
def consumer
  @consumer ||= OAuth::Consumer.new(
    Termtter::Crypt.decrypt(CONSUMER_KEY),
    Termtter::Crypt.decrypt(CONSUMER_SECRET),
    :site => config.oauth_consumer_site,
    :proxy => proxy_string
  )
end
proxy_string() click to toggle source
# File lib/termtter/api.rb, line 118
def proxy_string
  return unless config.proxy.host
  if config.proxy.user_name && config.proxy.password
    "http://#{config.proxy.user_name}:#{config.proxy.password}@#{config.proxy.host}:#{config.proxy.port}"
  else
    "http://#{config.proxy.host}:#{config.proxy.port}"
  end
end
setup() click to toggle source
# File lib/termtter/api.rb, line 32
def setup
  # NOTE: for compatible
  @connection = twitter.instance_variable_get(:@connection)
  if config.access_token.empty? || config.access_token_secret.empty?
    if config.token_file &&
         File.exist?(File.expand_path(config.token_file))
      config.access_token, config.access_token_secret = File.read(File.expand_path(config.token_file)) \
                                                            .split(/\r?\n/).map(&:chomp)
    else
      self.authorize_by_oauth(true)
    end
  end

  access_token = OAuth::AccessToken.new(consumer, config.access_token, config.access_token_secret)
  @twitter = RubytterProxy.new(access_token, twitter_option)

  config.user_name = @twitter.verify_credentials[:screen_name]
end
twitter_option() click to toggle source
# File lib/termtter/api.rb, line 100
def twitter_option
  {
    :app_name => config.app_name.empty? ? Termtter::APP_NAME : config.app_name,
    :host => config.host,
    :header => {
      'User-Agent' => 'Termtter http://github.com/termtter/termtter',
      'X-Twitter-Client' => 'Termtter',
      'X-Twitter-Client-URL' => 'http://github.com/termtter/termtter',
      'X-Twitter-Client-Version' => Termtter::VERSION
    },
    :enable_ssl => config.enable_ssl,
    :proxy_host => config.proxy.host,
    :proxy_port => config.proxy.port,
    :proxy_user_name => config.proxy.user_name,
    :proxy_password => config.proxy.password
  }
end