Class: Mastodon::Streaming::Client

Inherits:
Client
  • Object
show all
Defined in:
lib/mastodon/streaming/client.rb

Overview

Streaming client class, to handle all streaming purposes.

Constant Summary

Constants inherited from Client

Client::DEFAULT_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Client

#base_url, #bearer_token, #timeout

Instance Method Summary collapse

Methods inherited from Client

#user_agent

Constructor Details

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

Initializes a new Client object

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.



24
25
26
27
28
# File 'lib/mastodon/streaming/client.rb', line 24

def initialize(options = {})
  super
  options[:using_ssl] ||= base_url =~ /^https/
  @connection = Streaming::Connection.new(options)
end

Instance Attribute Details

#connection=(value) ⇒ Object (writeonly)

Sets the attribute connection

Parameters:

  • value

    the value to set the attribute connection to.



14
15
16
# File 'lib/mastodon/streaming/client.rb', line 14

def connection=(value)
  @connection = value
end

Instance Method Details

#before_request(&block) ⇒ Object

Set a Proc to be run when connection established.



98
99
100
101
102
103
104
105
106
107
# 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 = {}) {|Mastodon::Status, Mastodon::Notification, | ... } ⇒ Object

Returns direct messages for the authenticated user

Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

Yields:



77
78
79
# File 'lib/mastodon/streaming/client.rb', line 77

def direct(options = {}, &block)
  stream('direct', options, &block)
end

#firehose(options = {}) {|Mastodon::Status, Mastodon::Notification, | ... } ⇒ Object

Returns all public statuses

Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

Yields:



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

def firehose(options = {}, &block)
  stream('public', options, &block)
end

#hashtag(tag, options = {}) {|Mastodon::Status, Mastodon::Notification, | ... } ⇒ Object

Returns statuses that contain the specified hashtag

Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

Yields:



50
51
52
53
# File 'lib/mastodon/streaming/client.rb', line 50

def hashtag(tag, options = {}, &block)
  options['tag'] = tag
  stream('hashtag', options, &block)
end

#list(id, options = {}) {|Mastodon::Status, Mastodon::Notification, | ... } ⇒ Object

Returns statuses from the specified list

Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

Yields:



68
69
70
71
# File 'lib/mastodon/streaming/client.rb', line 68

def list(id, options = {}, &block)
  options['list'] = id
  stream('list', options, &block)
end

#local(options = {}) {|Mastodon::Status, Mastodon::Notification, | ... } ⇒ Object

Streams posts from the local instance

Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

Yields:



42
43
44
# File 'lib/mastodon/streaming/client.rb', line 42

def local(options = {}, &block)
  stream('public/local', options, &block)
end

#local_hashtag(tag, options = {}) {|Mastodon::Status, Mastodon::Notification, | ... } ⇒ Object

Returns local statuses that contain the specified hashtag

Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

Yields:



59
60
61
62
# 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 = {}) {|Mastodon::Status, Mastodon::Notification, | ... } ⇒ Object

Calls an arbitrary streaming endpoint and returns the results Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

Yields:



93
94
95
# File 'lib/mastodon/streaming/client.rb', line 93

def stream(path, options = {}, &block)
  request(:get, "/api/v1/streaming/#{path}", options, &block)
end

#user(options = {}) {|Mastodon::Status, Mastodon::Notification, | ... } ⇒ Object

Streams messages for a single user

Mastodon::Streaming::DeletedStatus] A stream of Mastodon objects.

Yields:



34
35
36
# File 'lib/mastodon/streaming/client.rb', line 34

def user(options = {}, &block)
  stream('user', options, &block)
end