Class: Twitter::Streaming::Client

Inherits:
Client
  • Object
show all
Includes:
Utils
Defined in:
lib/twitter/streaming/client.rb

Overview

Client for consuming the Twitter Streaming API

Instance Attribute Summary collapse

Attributes inherited from Client

#access_token, #access_token_secret, #consumer_key, #consumer_secret, #dev_environment, #proxy, #timeouts, #user_agent

Instance Method Summary collapse

Methods included from Utils

flat_pmap, pmap

Methods inherited from Client

#credentials, #credentials?, #user_token?

Constructor Details

#initialize(options = {}) ⇒ Twitter::Streaming::Client

Initializes a new Client object

Examples:

client = Twitter::Streaming::Client.new

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :tcp_socket_class (String)

    A class that Connection will use to create a new TCP socket.

  • :ssl_socket_class (String)

    A class that Connection will use to create a new SSL socket.



37
38
39
40
# File 'lib/twitter/streaming/client.rb', line 37

def initialize(options = {})
  super
  @connection = Streaming::Connection.new(options)
end

Instance Attribute Details

#connection=(value) ⇒ Twitter::Streaming::Connection (writeonly)

Sets the connection object

Examples:

client.connection = connection

Parameters:

Returns:



26
27
28
# File 'lib/twitter/streaming/client.rb', line 26

def connection=(value)
  @connection = value
end

Instance Method Details

#before_request(&block) ⇒ Proc, Twitter::Streaming::Client

Set a Proc to be run when connection established

Examples:

client.before_request { puts 'Connected!' }

Returns:



137
138
139
140
141
142
143
144
145
146
# File 'lib/twitter/streaming/client.rb', line 137

def before_request(&block)
  if block
    @before_request = block
    self
  elsif instance_variable_defined?(:@before_request)
    @before_request
  else
    proc {}
  end
end

#closevoid

This method returns an undefined value.

Closes the streaming connection

Examples:

client.close


154
155
156
# File 'lib/twitter/streaming/client.rb', line 154

def close
  @connection.close
end

#filter(options = {}) {|Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning| ... } ⇒ void

Note:

At least one predicate parameter (follow, locations, or track) must be specified.

This method returns an undefined value.

Returns public statuses that match one or more filter predicates

Examples:

client.filter(track: 'twitter')

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :follow (String)

    A comma separated list of user IDs, indicating the users to return statuses for in the stream.

  • :track (String)

    Includes additional Tweets matching the specified keywords. Phrases of keywords are specified by a comma-separated list.

  • :locations (String)

    Includes additional Tweets falling within the specified bounding boxes.

Yields:

See Also:



56
57
58
# File 'lib/twitter/streaming/client.rb', line 56

def filter(options = {}, &)
  request(:post, "https://stream.twitter.com:443/1.1/statuses/filter.json", options, &)
end

#firehose(options = {}) {|Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning| ... } ⇒ void

Note:

This endpoint requires special permission to access.

This method returns an undefined value.

Returns all public statuses

Examples:

client.firehose

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :count (Integer)

    The number of messages to backfill.

Yields:

See Also:



72
73
74
# File 'lib/twitter/streaming/client.rb', line 72

def firehose(options = {}, &)
  request(:get, "https://stream.twitter.com:443/1.1/statuses/firehose.json", options, &)
end

#sample(options = {}) {|Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning| ... } ⇒ void

This method returns an undefined value.

Returns a small random sample of all public statuses



85
86
87
# File 'lib/twitter/streaming/client.rb', line 85

def sample(options = {}, &)
  request(:get, "https://stream.twitter.com:443/1.1/statuses/sample.json", options, &)
end

#site(*follow, options = {}) {|Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning| ... } ⇒ void

Note:

Site Streams is currently in a limited beta. Access is restricted to whitelisted accounts.

This method returns an undefined value.

Streams messages for a set of users

Examples:

client.site(7505382)

Parameters:

  • follow (Enumerable<Integer, String, Twitter::User>)

    A list of user IDs, indicating the users to return statuses for in the stream.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :with (String)

    Specifies whether to return information for just the users specified in the follow parameter, or include messages from accounts they follow.

  • :replies (String)

    Specifies whether stall warnings should be delivered.

Yields:

See Also:



105
106
107
108
109
# File 'lib/twitter/streaming/client.rb', line 105

def site(*args, &)
  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(",")), &)
end

#user(options = {}) {|Twitter::Tweet, Twitter::Streaming::Event, Twitter::DirectMessage, Twitter::Streaming::FriendList, Twitter::Streaming::DeletedTweet, Twitter::Streaming::StallWarning| ... } ⇒ void

This method returns an undefined value.

Streams messages for a single user

Examples:

client.user

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :with (String)

    Specifies whether to return information for just the users specified in the follow parameter, or include messages from accounts they follow.

  • :replies (String)

    Specifies whether to return additional @replies.

  • :stall_warnings (String)

    Specifies whether stall warnings should be delivered.

  • :track (String)

    Includes additional Tweets matching the specified keywords. Phrases of keywords are specified by a comma-separated list.

  • :locations (String)

    Includes additional Tweets falling within the specified bounding boxes.

Yields:

See Also:



127
128
129
# File 'lib/twitter/streaming/client.rb', line 127

def user(options = {}, &)
  request(:get, "https://userstream.twitter.com:443/1.1/user.json", options, &)
end