Class: KazeClient::Request

Inherits:
Object show all
Defined in:
lib/kaze_client/request/request.rb

Overview

Represents request to a Kaze API

See Also:

Author:

Since:

  • 0.1.0

Direct Known Subclasses

Utils::FinalRequest

Constant Summary collapse

DEFAULT_HEADERS =

Those headers are added on all requests by default

Since:

  • 0.1.0

{
  'Content-Type' => 'application/json',
  'Accept'       => 'application/json'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url) ⇒ Request

Returns a new instance of Request.

Parameters:

  • method (Symbol)

    The HTTP verb to use for the request (e.g. :get)

  • url (String)

    The API endpoint (e.g. /jobs)

Since:

  • 0.1.0



46
47
48
49
50
51
52
# File 'lib/kaze_client/request/request.rb', line 46

def initialize(method, url)
  @method  = method
  @url     = url
  @query   = nil
  @body    = nil
  @headers = {}
end

Instance Attribute Details

#bodynil, ...

Returns The body for the request.

Examples:

Login data from KazeClient::LoginRequest

{ user: { login: 'test', password: 'test' } }
"{\"user\":{\"login\":\"test\",\"password\":\"test\"}}"

Returns:

  • (nil, Hash, String)

    The body for the request

Since:

  • 0.1.0



37
38
39
# File 'lib/kaze_client/request/request.rb', line 37

def body
  @body
end

#headersnil, Hash

Returns The headers for the request.

Examples:

Default headers from any KazeClient::Request

{ 'Content-Type' => 'application/json', 'Accept' => 'application/json' }

Returns:

  • (nil, Hash)

    The headers for the request

Since:

  • 0.1.0



42
43
44
# File 'lib/kaze_client/request/request.rb', line 42

def headers
  @headers
end

#methodString, Symbol (readonly)

Returns The HTTP verb to use for the request.

Examples:

Common HTTP verbs

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

Returns:

  • (String, Symbol)

    The HTTP verb to use for the request

Since:

  • 0.1.0



21
22
23
# File 'lib/kaze_client/request/request.rb', line 21

def method
  @method
end

#querynil, Hash

Returns The query parameters for the request.

Examples:

Page parameters from KazeClient::Utils::ListRequest

{ page: 1, per_page: 20 }

Returns:

  • (nil, Hash)

    The query parameters for the request

Since:

  • 0.1.0



31
32
33
# File 'lib/kaze_client/request/request.rb', line 31

def query
  @query
end

#urlString (readonly)

Returns The API endpoint.

Examples:

To get the list of companies

'/api/companies'

Returns:

  • (String)

    The API endpoint

Since:

  • 0.1.0



26
27
28
# File 'lib/kaze_client/request/request.rb', line 26

def url
  @url
end

Instance Method Details

#error_for(response) ⇒ KazeClient::Error::Generic

Returns The adequate error object according to the given response.

Parameters:

  • response (HTTParty::Response)

    The response object from HTTParty call

Returns:

Since:

  • 0.1.0



68
69
70
71
72
73
74
75
# File 'lib/kaze_client/request/request.rb', line 68

def error_for(response)
  error   = response.parsed_response['error']
  message = response.parsed_response['message']

  return non_generic_error(error, message, response) if error != 'generic'

  generic_http_error(error, message, response)
end

#parametersHash

Returns The arguments to give to the HTTParty call.

Examples:

For instance, when @body is blank

{
  query: { page: 1, per_page: 20 },
  headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
}

Returns:

  • (Hash)

    The arguments to give to the HTTParty call

Since:

  • 0.1.0



60
61
62
63
64
# File 'lib/kaze_client/request/request.rb', line 60

def parameters
  {
    query: make_query, body: make_body, headers: make_headers
  }.reject { |_k, v| v.blank? }
end