module MagicBell::ApiOperations

Public Instance Methods

get(url, options = {}) click to toggle source
# File lib/magicbell/api_operations.rb, line 5
def get(url, options = {})
  defaults = { headers: default_headers }
  response = HTTParty.get(url, options.merge(defaults))
  raise_http_error_unless_2xx_response(response)

  response
end
post(url, options = {}) click to toggle source
# File lib/magicbell/api_operations.rb, line 13
def post(url, options = {})
  defaults = { headers: default_headers }
  response = HTTParty.post(url, options.merge(defaults))
  raise_http_error_unless_2xx_response(response)

  response
end
put(url, options = {}) click to toggle source
# File lib/magicbell/api_operations.rb, line 21
def put(url, options = {})
  defaults = { headers: default_headers }
  response = HTTParty.put(url, options.merge(defaults))
  raise_http_error_unless_2xx_response(response)

  response
end

Protected Instance Methods

default_headers() click to toggle source
# File lib/magicbell/api_operations.rb, line 31
def default_headers
  authentication_headers.merge({ "Content-Type" => "application/json", "Accept"=> "application/json" })
end

Private Instance Methods

raise_http_error_unless_2xx_response(response) click to toggle source
# File lib/magicbell/api_operations.rb, line 37
def raise_http_error_unless_2xx_response(response)
  return if response.success?
  
  e = MagicBell::Client::HTTPError.new
  e.response_status = response.code
  e.response_headers = response.headers.to_h
  e.response_body = response.body
  e.errors = []
  unless e.response_body.nil? || e.response_body.empty?
    body = JSON.parse(response.body)
    e.errors = body["errors"]
    e.errors.each do |error, index|
      puts "#{error["suggestion"].red}"
      puts "#{error["help_link"].blue.on_white}"
    end
  end
  raise e
end