Module: Termtter::API

Defined in:
lib/termtter/api.rb,
lib/plugins/channel.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.connectionObject (readonly)

Returns the value of attribute connection.



32
33
34
# File 'lib/termtter/api.rb', line 32

def connection
  @connection
end

.twitterObject (readonly)

Returns the value of attribute twitter.



32
33
34
# File 'lib/termtter/api.rb', line 32

def twitter
  @twitter
end

Class Method Details

.authorize_by_oauth(show_information = false, save_to_token_file = true, put_to_config = true, verbose = true) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/termtter/api.rb', line 52

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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/plugins/channel.rb', line 19

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

.consumerObject



92
93
94
95
96
97
98
99
# File 'lib/termtter/api.rb', line 92

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_stringObject



119
120
121
122
123
124
125
126
# File 'lib/termtter/api.rb', line 119

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

.setupObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/termtter/api.rb', line 33

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_optionObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/termtter/api.rb', line 101

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