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

Calls superclass method Mastodon::Client::new
# File lib/mastodon/streaming/client.rb, line 18
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 60
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 conversations for a single user

@yield [Mastodon::Conversation] A stream of Mastodon objects.

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

Returns statuses that contain the specified hashtag

@yield [Mastodon::Status, Mastodon::Streaming::Events::StatusDelete] A stream of Mastodon objects.

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

Returns all public statuses

@yield [Mastodon::Status, Mastodon::Streaming::Events::StatusDelete] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 41
def public(options = {}, &block)
  stream('public', 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 55
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::Events::StatusDelete, Mastodon::Streaming::Events::FiltersChange] A stream of Mastodon objects.

# File lib/mastodon/streaming/client.rb, line 27
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 73
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|
    if item = Streaming::MessageParser.parse(type, data) # rubocop:disable AssignmentInCondition
      yield(item)
    end
  end

  @connection.stream(request, response)
end
to_url_params(params) click to toggle source
# File lib/mastodon/streaming/client.rb, line 89
def to_url_params(params)
  uri = Addressable::URI.new
  uri.query_values = params
  uri.query
end