class JSONAPI::SimpleClient::Request
Constants
- CONTENT_TYPE
Attributes
Public Class Methods
@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
@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
@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
@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
@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
@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
@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
@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
@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