class LastbillClient::Request

@author ciappa_m@modulotech.fr Represents request to a LastBill API @see LastbillClient::Client @see LastbillClient::Response @since 0.1.0

Constants

DEFAULT_HEADERS

Those headers are added on all requests by default

Attributes

body[RW]

@return [nil, Hash, String] The body for the request @example Login data from LastbillClient::LoginRequest

{ user: { login: 'test', password: 'test' } }
"{\"user\":{\"login\":\"test\",\"password\":\"test\"}}"
headers[RW]

@return [nil, Hash] The headers for the request @example Default headers from any LastbillClient::Request

{ 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
method[R]

@return [String, Symbol] The HTTP verb to use for the request @example Common HTTP verbs

[:get, :post, :put, :patch, :delete]
query[RW]

@return [nil, Hash] The query parameters for the request @example Page parameters from LastbillClient::Utils::ListRequest

{ page: 1, per_page: 20 }
url[R]

@return [String] The API endpoint @example To get the list of companies

'/api/companies'

Public Class Methods

new(method, url) click to toggle source

@param method [Symbol] The HTTP verb to use for the request (e.g. :get) @param url [String] The API endpoint (e.g. /jobs)

# File lib/lastbill_client/request/request.rb, line 44
def initialize(method, url)
  @method  = method
  @url     = url
  @query   = nil
  @body    = nil
  @headers = {}
end

Public Instance Methods

error_for(response) click to toggle source

@param response [HTTParty::Response] The response object from HTTParty call @return [LastbillClient::Error::Generic] The adequate error object according to the given response

# File lib/lastbill_client/request/request.rb, line 66
def error_for(response)
  # Not found is handled in a specific way since there is no message key and the error has a
  # specific format
  return LastbillClient::Error::NotFound.new if response.code == 404

  # Return the adequate error class for the error code in the response
  "LastbillClient::Error::#{response.parsed_response["error"].camelize}"
    .constantize.new(response.parsed_response["message"])
rescue NameError
  # This means no error class exists for the error code in the response, we fallback to a
  # generic error
  Error::Generic.new(status: response.code,
                     error: response.parsed_response["error"],
                     message: response.parsed_response["message"])
end
parameters() click to toggle source

@return [Hash] The arguments to give to the HTTParty call @example For instance, when +@body+ is blank

{
  query: { page: 1, per_page: 20 },
  headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
}
# File lib/lastbill_client/request/request.rb, line 58
def parameters
  {
    query: make_query, body: make_body, headers: make_headers
  }.reject { |_k, v| v.blank? }
end

Protected Instance Methods

make_body() click to toggle source

@return [nil, String] The body for the request

+String+ will be sent as-is while +Hash+ will be transformed to a JSON string.
# File lib/lastbill_client/request/request.rb, line 95
def make_body
  return nil if @body.blank?

  case @body
  when String
    @body
  when Hash
    @body.to_json
  end
end
make_headers() click to toggle source

@return [Hash] The headers for the request

If +@headers+ is blank or is not a Hash, it returns the +DEFAULT_HEADERS+. Else it merges
the +DEFAULT_HEADERS+ with +@headers+ allowing +@headers+ to override +DEFAULT_HEADERS+.
# File lib/lastbill_client/request/request.rb, line 87
def make_headers
  return DEFAULT_HEADERS if @headers.blank? || !@headers.is_a?(Hash)

  DEFAULT_HEADERS.merge(@headers)
end
make_query() click to toggle source

@return [nil, Hash] The query parameters for the request

# File lib/lastbill_client/request/request.rb, line 107
def make_query
  return nil if @query.blank?

  @query
end