Class: Rubycent::Request

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

Overview

Rubycent::Request

Holds request call and response handling logic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, params, body = nil, headers = {}) ⇒ Request

Returns a new instance of Request.

Parameters:

  • endpoint (String)

    Centrifugo API endpoint

  • params (Hash)

    Additional params to configure request.

  • body (String) (defaults to: nil)

    (default: nil) JSON string representing request parameters.

  • headers (Hash) (defaults to: {})

    (default: {}) Additional HTTP headers(such as Content-Type and Authorization).

Options Hash (params):

  • :timeout (Integer)

    Number of seconds to wait for the connection to open.

  • :open_timeout (Integer)

    Number of seconds to wait for one block to be read.



29
30
31
32
33
34
# File 'lib/rubycent/request.rb', line 29

def initialize(endpoint, params, body = nil, headers = {})
  @endpoint = endpoint
  @params = params
  @body = body
  @headers = headers
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



11
12
13
# File 'lib/rubycent/request.rb', line 11

def body
  @body
end

#endpointObject

Returns the value of attribute endpoint.



11
12
13
# File 'lib/rubycent/request.rb', line 11

def endpoint
  @endpoint
end

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/rubycent/request.rb', line 11

def headers
  @headers
end

#paramsObject

Returns the value of attribute params.



11
12
13
# File 'lib/rubycent/request.rb', line 11

def params
  @params
end

Instance Method Details

#postHash

Perform POST request to centrifugo API

Returns:

  • (Hash)

    Parsed response body

Raises:



42
43
44
45
46
47
48
49
50
# File 'lib/rubycent/request.rb', line 42

def post
  response = rest_client.post(@endpoint) do |request|
    configure_request(request: request, body: body, headers: headers)
  end

  handle_response(response)
rescue Faraday::ConnectionFailed => e
  handle_error(e)
end