class Mastodon::Streaming::Client

Streaming client class, to handle all streaming purposes.

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 [Mastodon::Streaming::Client]

Calls superclass method Mastodon::Client::new
# File lib/mastodon/streaming/client.rb, line 24
def initialize(options = {})
  super
  options[:using_ssl] ||= base_url =~ /^https/
  @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/mastodon/streaming/client.rb, line 98
def before_request(&block)
  if block_given?
    @before_request = block
    self
  elsif instance_variable_defined?(:@before_request)
    @before_request
  else
    proc {}
  end
end
direct(options = {}, &block) click to toggle source

Returns direct messages for the authenticated user

@yield [Mastodon::Status, Mastodon::Notification, Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 77
def direct(options = {}, &block)
  stream('direct', options, &block)
end
firehose(options = {}, &block) click to toggle source

Returns all public statuses

@yield [Mastodon::Status, Mastodon::Notification, Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 85
def firehose(options = {}, &block)
  stream('public', options, &block)
end
hashtag(tag, options = {}, &block) click to toggle source

Returns statuses that contain the specified hashtag

@yield [Mastodon::Status, Mastodon::Notification, Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 50
def hashtag(tag, options = {}, &block)
  options['tag'] = tag
  stream('hashtag', options, &block)
end
list(id, options = {}, &block) click to toggle source

Returns statuses from the specified list

@yield [Mastodon::Status, Mastodon::Notification, Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 68
def list(id, options = {}, &block)
  options['list'] = id
  stream('list', options, &block)
end
local(options = {}, &block) click to toggle source

Streams posts from the local instance

@yield [Mastodon::Status, Mastodon::Notification, Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 42
def local(options = {}, &block)
  stream('public/local', options, &block)
end
local_hashtag(tag, options = {}, &block) click to toggle source

Returns local statuses that contain the specified hashtag

@yield [Mastodon::Status, Mastodon::Notification, Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 59
def local_hashtag(tag, options = {}, &block)
  options['tag'] = tag
  stream('hashtag/local', options, &block)
end
stream(path, options = {}, &block) click to toggle source

Calls an arbitrary streaming endpoint and returns the results @yield [Mastodon::Status, Mastodon::Notification, Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 93
def stream(path, options = {}, &block)
  request(:get, "/api/v1/streaming/#{path}", options, &block)
end
user(options = {}, &block) click to toggle source

Streams messages for a single user

@yield [Mastodon::Status, Mastodon::Notification, Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 34
def user(options = {}, &block)
  stream('user', options, &block)
end

Private Instance Methods

request(method, path, params) { |item| ... } click to toggle source
# File lib/mastodon/streaming/client.rb, line 111
def request(method, path, params)
  before_request.call
  uri = Addressable::URI.parse(base_url + path)

  headers = Mastodon::Headers.new(self).request_headers

  request = HTTP::Request.new(verb: method,
                              uri: "#{uri}?#{to_url_params(params)}",
                              headers: headers)
  response = Streaming::Response.new do |type, data|
    # rubocop:disable AssignmentInCondition
    if item = Streaming::MessageParser.parse(type, data)
      yield(item)
    end
    # rubocop:enable AssignmentInCondition
  end
  @connection.stream(request, response)
end
to_url_params(params) click to toggle source
# File lib/mastodon/streaming/client.rb, line 130
def to_url_params(params)
  uri = Addressable::URI.new
  uri.query_values = params
  uri.query
end