class Twitter::Streaming::Client

Attributes

connection[W]

Public Class Methods

new(options = {}) click to toggle source

Initializes a new Client object

@param options [Hash] A customizable set of options. @option options [String] :tcp_socket_class A class that Connection will use to create a new TCP socket. @option options [String] :ssl_socket_class A class that Connection will use to create a new SSL socket. @return [Twitter::Streaming::Client]

Calls superclass method Twitter::Client::new
# File lib/twitter/streaming/client.rb, line 22
def initialize(options = {})
  super
  @connection = Streaming::Connection.new(options)
end

Public Instance Methods

before_request(&block) click to toggle source

Set a Proc to be run when connection established.

# File lib/twitter/streaming/client.rb, line 97
def before_request(&block)
  if block_given?
    @before_request = block
    self
  elsif instance_variable_defined?(:@before_request)
    @before_request
  else
    proc {}
  end
end
close() click to toggle source
# File lib/twitter/streaming/client.rb, line 108
def close
  @connection.close
end
filter(options = {}, &block) click to toggle source

Returns public statuses that match one or more filter predicates

@see dev.twitter.com/streaming/reference/post/statuses/filter @see dev.twitter.com/streaming/overview/request-parameters @note At least one predicate parameter (follow, locations, or track) must be specified. @param options [Hash] A customizable set of options. @option options [String] :follow A comma separated list of user IDs, indicating the users to return statuses for in the stream. @option options [String] :track Includes additional Tweets matching the specified keywords. Phrases of keywords are specified by a comma-separated list. @option options [String] :locations Includes additional Tweets falling within the specified bounding boxes. @yield [Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning] A stream of Twitter objects.

# File lib/twitter/streaming/client.rb, line 37
def filter(options = {}, &block)
  request(:post, 'https://stream.twitter.com:443/1.1/statuses/filter.json', options, &block)
end
firehose(options = {}, &block) click to toggle source

Returns all public statuses

@see dev.twitter.com/streaming/reference/get/statuses/firehose @see dev.twitter.com/streaming/overview/request-parameters @note This endpoint requires special permission to access. @param options [Hash] A customizable set of options. @option options [Integer] :count The number of messages to backfill. @yield [Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning] A stream of Twitter objects.

# File lib/twitter/streaming/client.rb, line 49
def firehose(options = {}, &block)
  request(:get, 'https://stream.twitter.com:443/1.1/statuses/firehose.json', options, &block)
end
sample(options = {}, &block) click to toggle source

Returns a small random sample of all public statuses

@see dev.twitter.com/streaming/reference/get/statuses/sample @see dev.twitter.com/streaming/overview/request-parameters @yield [Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning] A stream of Twitter objects.

# File lib/twitter/streaming/client.rb, line 58
def sample(options = {}, &block)
  request(:get, 'https://stream.twitter.com:443/1.1/statuses/sample.json', options, &block)
end
site(*args, &block) click to toggle source

Streams messages for a set of users

@see dev.twitter.com/streaming/reference/get/site @see dev.twitter.com/streaming/sitestreams @see dev.twitter.com/streaming/overview/request-parameters @note Site Streams is currently in a limited beta. Access is restricted to whitelisted accounts. @overload site(*follow, options = {}, &block)

@param follow [Enumerable<Integer, String, Twitter::User>] A list of user IDs, indicating the users to return statuses for in the stream.
@param options [Hash] A customizable set of options.
@option options [String] :with Specifies whether to return information for just the users specified in the follow parameter, or include messages from accounts they follow.
@option options [String] :replies Specifies whether stall warnings should be delivered.
@yield [Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning] A stream of Twitter objects.
# File lib/twitter/streaming/client.rb, line 74
def site(*args, &block)
  arguments = Arguments.new(args)
  user_ids = collect_user_ids(arguments)
  request(:get, 'https://sitestream.twitter.com:443/1.1/site.json', arguments.options.merge(follow: user_ids.join(',')), &block)
end
user(options = {}, &block) click to toggle source

Streams messages for a single user

@see dev.twitter.com/streaming/reference/get/user @see dev.twitter.com/streaming/userstreams @see dev.twitter.com/streaming/overview/request-parameters @param options [Hash] A customizable set of options. @option options [String] :with Specifies whether to return information for just the users specified in the follow parameter, or include messages from accounts they follow. @option options [String] :replies Specifies whether to return additional @replies. @option options [String] :stall_warnings Specifies whether stall warnings should be delivered. @option options [String] :track Includes additional Tweets matching the specified keywords. Phrases of keywords are specified by a comma-separated list. @option options [String] :locations Includes additional Tweets falling within the specified bounding boxes. @yield [Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning] A stream of Twitter objects.

# File lib/twitter/streaming/client.rb, line 92
def user(options = {}, &block)
  request(:get, 'https://userstream.twitter.com:443/1.1/user.json', options, &block)
end

Private Instance Methods

collect_user_ids(users) click to toggle source

Takes a mixed array of Integers and Twitter::User objects and returns a consistent array of Twitter user IDs.

@param users [Array] @return [Array<Integer>]

# File lib/twitter/streaming/client.rb, line 137
def collect_user_ids(users)
  users.collect do |user|
    case user
    when Integer       then user
    when Twitter::User then user.id
    end
  end.compact
end
request(method, uri, params) { |item| ... } click to toggle source
# File lib/twitter/streaming/client.rb, line 114
def request(method, uri, params)
  before_request.call
  headers = Twitter::Headers.new(self, method, uri, params).request_headers
  request = HTTP::Request.new(verb: method, uri: uri + '?' + to_url_params(params), headers: headers, proxy: proxy)
  response = Streaming::Response.new do |data|
    if item = Streaming::MessageParser.parse(data) # rubocop:disable Lint/AssignmentInCondition
      yield(item)
    end
  end
  @connection.stream(request, response)
end
to_url_params(params) click to toggle source
# File lib/twitter/streaming/client.rb, line 126
def to_url_params(params)
  uri = Addressable::URI.new
  uri.query_values = params
  uri.query
end