Class: Causeway::Publishers::Mastodon

Inherits:
Base
  • Object
show all
Defined in:
lib/causeway/publishers/mastodon.rb

Instance Method Summary collapse

Instance Method Details

#nameObject



10
11
12
# File 'lib/causeway/publishers/mastodon.rb', line 10

def name
  "Mastodon"
end

#post(text) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/causeway/publishers/mastodon.rb', line 14

def post(text)
  token = Causeway.configuration.mastodon_token
  instance = Causeway.configuration.mastodon_instance || "https://mastodon.social"

  return { error: "Mastodon token not configured." } unless token && !token.empty?

  uri = URI("#{instance}/api/v1/statuses")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"

  request = Net::HTTP::Post.new(uri)
  request["Authorization"] = "Bearer #{token}"
  request.set_form_data("status" => text)

  response = http.request(request)
  unless response.is_a?(Net::HTTPSuccess)
    return { error: "Mastodon API error: #{response.code} #{response.body}" }
  end

  data = JSON.parse(response.body)
  { id: data["id"], url: data["url"] }
end