class TweetStream::SiteStreamClient

Public Class Methods

new(config_uri, oauth = {}) click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 9
def initialize(config_uri, oauth = {})
  @config_uri = config_uri

  options = TweetStream.oauth_options.merge(oauth)
  Configuration::OAUTH_OPTIONS_KEYS.each do |key|
    send("#{key}=", options[key])
  end

  EventMachine::HttpRequest.use EventMachine::Middleware::JSONResponse
end

Public Instance Methods

add_user(user_id, &block) click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 34
def add_user(user_id, &block)
  options = {
              :error_msg => 'Failed to add user to SiteStream',
              :body => {'user_id' => normalized_user_ids(user_id)}
            }

  request(:post, add_user_path, options, &block)
end
friends_ids(user_id, &block) click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 52
def friends_ids(user_id, &block)
  options = {:error_msg => 'Failed to retrieve SiteStream friends ids.',
             :body => {'user_id' => user_id}
            }
  request(:post, friends_ids_path, options, &block)
end
info(&block) click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 29
def info(&block)
  options = {:error_msg => 'Failed to retrieve SiteStream info.'}
  request(:get, info_path, options, &block)
end
on_error(&block) click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 20
def on_error(&block)
  if block_given?
    @on_error = block
    self
  else
    @on_error
  end
end
remove_user(user_id, &block) click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 43
def remove_user(user_id, &block)
  options = {
              :error_msg => 'Failed to remove user from SiteStream.',
              :body => {'user_id' => normalized_user_ids(user_id)}
            }

  request(:post, remove_user_path, options, &block)
end

Private Instance Methods

add_user_path() click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 82
def add_user_path
  @config_uri + '/add_user.json'
end
connection() click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 61
def connection
  return @conn if defined?(@conn)

  @conn = EventMachine::HttpRequest.new('https://sitestream.twitter.com/')
  @conn.use EventMachine::Middleware::OAuth, oauth_configuration
  @conn
end
friends_ids_path() click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 90
def friends_ids_path
  @config_uri + '/friends/ids.json'
end
info_path() click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 78
def info_path
  @config_uri + '/info.json'
end
normalized_user_ids(user_id) click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 114
def normalized_user_ids(user_id)
  if user_id.kind_of?(Array)
    user_id.join(',')
  else
    user_id.to_s
  end
end
oauth_configuration() click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 69
def oauth_configuration
  {
    :consumer_key => consumer_key,
    :consumer_secret => consumer_secret,
    :access_token => oauth_token,
    :access_token_secret => oauth_token_secret
  }
end
remove_user_path() click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 86
def remove_user_path
  @config_uri + '/remove_user.json'
end
request(method, path, options, &block) click to toggle source
# File lib/tweetstream/site_stream_client.rb, line 94
def request(method, path, options, &block) # rubocop:disable CyclomaticComplexity, ParameterLists
  error_msg = options.delete(:error_msg)

  http = connection.send(method, options.merge(:path => path))
  http.callback do
    if http.response_header.status == 200 && block && block.kind_of?(Proc)
      if block.arity == 1
        block.call http.response
      else
        block.call
      end
    else
      @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
    end
  end
  http.errback do
    @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
  end
end