module HTTP::RestClient::CRUD

Create/Read/Update/Delete helpers.

Public Instance Methods

all(params = {}) click to toggle source

Resource collection finder, uses the default limit

@param params [Hash] URI parameters to pass to the endpoint. @return [Array] of [Object] instances

# File lib/http/rest_client/crud.rb, line 9
def all(params = {})
  objectify(request(:get, uri, params: params))
end
create(params = {}) click to toggle source

Resource creation helper

@param params [Hash] request parameters to pass to the endpoint as JSON. @return [Object] instance

# File lib/http/rest_client/crud.rb, line 34
def create(params = {})
  objectify(request(:post, uri, json: params))
end
delete(id) click to toggle source

Resource deletion handler

@param id [String] resource indentifier @return [Object] instance

# File lib/http/rest_client/crud.rb, line 26
def delete(id)
  objectify(request(:delete, uri(id)))
end
find(id, params = {}) click to toggle source

Resource finder

@param id [String] resource indentifier @param params [Hash] URI parameters to pass to the endpoint. @return [Object] instance

# File lib/http/rest_client/crud.rb, line 18
def find(id, params = {})
  objectify(request(:get, uri(id), params: params))
end
update(id, params = {}) click to toggle source

Resource update helper

@param id [String] resource indentifier @param params [Hash] request parameters to pass to the endpoint as JSON. @return [Object] instance

# File lib/http/rest_client/crud.rb, line 43
def update(id, params = {})
  objectify(request(:patch, uri(id), json: params))
end

Private Instance Methods

objectify(payload) click to toggle source

Resource constructor wrapper

@param payload [Hash] response payload to build a resource. @return [Object] instance or a list of instances.

# File lib/http/rest_client/crud.rb, line 53
def objectify(payload)
  if payload.is_a?(Array)
    payload.map { |data| new(data) }
  elsif payload.is_a?(Hash)
    new(payload)
  else
    payload
  end
end