class Mastodon::REST::Request

Public Class Methods

new(client, request_method, path, options = {}) click to toggle source
# File lib/mastodon/rest/request.rb, line 10
def initialize(client, request_method, path, options = {})
  @client         = client
  @request_method = request_method
  @uri            = Addressable::URI.parse(@client.base_url + path)
  @headers        = Mastodon::Headers.new(@client).request_headers
  @path           = @uri.path
  @options        = options
  @headers        = @options.delete(:headers).merge @headers if @options.is_a?(Hash) && @options[:headers]
end

Public Instance Methods

perform() click to toggle source
# File lib/mastodon/rest/request.rb, line 20
def perform
  options_key = @request_method == :get ? :params : :form
  response    = http_client.headers(@headers).public_send(@request_method, @uri.to_s, options_key => @options)

  STDERR.puts response.body if ENV['DEBUG'] == 'true'

  fail_or_return(response.code, response.body.empty? ? '' : Oj.load(response.to_s, mode: :null))
end

Private Instance Methods

fail_or_return(code, body) click to toggle source
# File lib/mastodon/rest/request.rb, line 31
def fail_or_return(code, body)
  raise Mastodon::Error::ERRORS[code].from_response(body) if Mastodon::Error::ERRORS.include?(code)
  body
end
http_client() click to toggle source
# File lib/mastodon/rest/request.rb, line 36
def http_client
  HTTP.timeout(:per_operation, connect: @client.timeout[:connect], read: @client.timeout[:read], write: @client.timeout[:write])
end