class JSONAPI::SimpleClient::Request

Constants

CONTENT_TYPE

Attributes

headers[R]
url[R]

Public Class Methods

new(url, headers = {}) click to toggle source

@param url [String] @param headers [Hash] @return [HTTP::Response]

# File lib/jsonapi/simple_client/request.rb, line 13
def initialize(url, headers = {})
  @headers = headers
  @url = url
end

Public Instance Methods

delete() click to toggle source

@raise [JSONAPI::SimpleClient::Error] if the response is not parseable. @return [HTTP::Response] if the request is successfull or a parseable error.

# File lib/jsonapi/simple_client/request.rb, line 34
def delete
  execute { http_client.delete(url) }
end
get(params = {}) click to toggle source

@param params [Hash] @raise [JSONAPI::SimpleClient::Error] if the response is not parseable. @return [HTTP::Response] if the request is successfull or a parseable error.

# File lib/jsonapi/simple_client/request.rb, line 41
def get(params = {})
  execute { http_client.get(url, params: JSONAPI::SimpleClient::QueryParams.parse(params)) }
end
patch(payload = {}) click to toggle source

@param payload [Hash] @raise [JSONAPI::SimpleClient::Error] if the response is not parseable. @return [HTTP::Response] if the request is successfull or a parseable error.

# File lib/jsonapi/simple_client/request.rb, line 28
def patch(payload = {})
  execute { http_client.patch(url, json: payload) }
end
post(payload = {}) click to toggle source

@param payload [Hash] @raise [JSONAPI::SimpleClient::Error] if the response is not parseable. @return [HTTP::Response] if the request is successfull or a parseable error.

# File lib/jsonapi/simple_client/request.rb, line 21
def post(payload = {})
  execute { http_client.post(url, json: payload) }
end

Private Instance Methods

error_class_for(status) click to toggle source

@param status [Integer] @return [JSONAPI::SimpleClient::Error]

# File lib/jsonapi/simple_client/request.rb, line 74
def error_class_for(status)
  {
    403 => JSONAPI::SimpleClient::Forbidden,
    404 => JSONAPI::SimpleClient::NotFound,
    409 => JSONAPI::SimpleClient::Conflict,
    500 => JSONAPI::SimpleClient::ServerError
  }.fetch(status, JSONAPI::SimpleClient::Error)
end
execute() { || ... } click to toggle source

@raise [JSONAPI::SimpleClient::Error] if the response is not parseable. @return [HTTP::Response] if the request is successfull or a parseable error.

# File lib/jsonapi/simple_client/request.rb, line 56
def execute
  response = yield
  response.status.success? ? response : handle_error(response)
end
handle_error(response) click to toggle source

@param response [HTTP::Response] @raise [JSONAPI::SimpleClient::Error] if the response is not a parseable error. @return [HTTP::Response]

# File lib/jsonapi/simple_client/request.rb, line 64
def handle_error(response)
  case response.status
  when 400, 422 then response
  else
    raise error_class_for(response.status)
  end
end
http_client() click to toggle source

@return [HTTP]

# File lib/jsonapi/simple_client/request.rb, line 48
def http_client
  HTTP
    .accept(CONTENT_TYPE)
    .headers(headers.merge(content_type: CONTENT_TYPE))
end