Class: CrossPost::Mastodon

Inherits:
Object
  • Object
show all
Defined in:
lib/cross-post/mastodon.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Mastodon

Returns a new instance of Mastodon.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cross-post/mastodon.rb', line 7

def initialize(config)
  settings = config[:settings]
  @posts   = config[:posts]

  url   = settings['mastodon.url']
  token = settings['mastodon.token']
  user  = settings['mastodon.user']

  LOGGER.debug "Mastodon base URL: #{url}"
  @client = ::Mastodon::REST::Client.new base_url: url, bearer_token: token

  stream_url = settings.fetch 'mastodon.stream_url', url
  LOGGER.debug "Mastodon stream URL: #{stream_url}"
  @stream = ::Mastodon::Streaming::Client.new base_url: stream_url, bearer_token: token

  @user_url = URI.join(ENV.fetch('BASE_USER_URL', url), "/@#{user}").to_s
  LOGGER.debug "Mastodon user URL: #{@user_url}"
end

Instance Method Details

#feed(twitter) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cross-post/mastodon.rb', line 26

def feed(twitter)
  @stream.user do |object|
    begin
      case object
      when ::Mastodon::Status
        LOGGER.info { 'Receiving status' }
        LOGGER.debug { object.ai }
        next if reject? object
        twitter.post_status object
      end
    rescue => e
      LOGGER.error e
      raise
    end
  end
end