Class: NewsmastMastodon::ReblogRequestService

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/newsmast_mastodon/reblog_request_service.rb

Instance Method Summary collapse

Instance Method Details

#call(access_token, status_id) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/newsmast_mastodon/reblog_request_service.rb', line 9

def call(access_token, status_id)
  url = if Rails.env.development?
          URI("http://localhost:3000/api/v1/statuses/#{status_id}/reblog")
        else
          URI("https://#{ENV['LOCAL_DOMAIN']}/api/v1/statuses/#{status_id}/reblog")
        end

  req = Net::HTTP::Post.new(url)
  req.content_type = 'application/json'
  req['Authorization'] = "Bearer #{access_token}"
  req.body = { visibility: 'public' }.to_json

  response = Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') do |http|
    http.request(req)
  end

  case response
  when Net::HTTPSuccess
    JSON.parse(response.body)
  else
    raise "Reblog creation failed: #{response.body}"
  end
end