class LunaPark::Http::Send

Send {LunaPark::Http::Request} and get {LunaPark::Http::Response}.

This service, in fact, works as an adapter for the {github.com/rest-client/rest-client RestClient} gem. If you want to remove dependence on RestClient, you should rewrite Send class.

Instead of using these service directly, better use the request method {LunaPark::Http::Request#call}. Which freeze request and define {LunaPark::Http::Request#sent_at} timestamp.

Attributes

original_request[R]

Public Class Methods

new(original_request) click to toggle source

Define new Send service

@param [LunaPark::Http::Request] original_request - the request which you would like to send.

# File lib/luna_park/http/send.rb, line 26
def initialize(original_request)
  @original_request = original_request
end

Public Instance Methods

call() click to toggle source

Send defined request. Always return response even if the response is not successful.

@example success response

LunaPark::Http::Send.new(request).call #=> <LunaPark::Http::Response @code=200
  # @body="{"version":1,"data":"Hello World!"}" @headers={:content_type=>"application/json",
  # :connection=>"close", :server=>"thin"} @cookies={}>

@example server is unavailable

LunaPark::Http::Send.new(request).call # => <LunaPark::Http::Response @code=503
  # @body="" @headers={} @cookies={}>

@return [LunaPark::Http::Response]

# File lib/luna_park/http/send.rb, line 42
def call
  rest_request = build_rest_request(original_request)
  rest_response = rest_request.execute
  build_original_response(rest_response)
rescue Errno::ECONNREFUSED               then build_unavailable_response
rescue ::RestClient::Exceptions::Timeout then build_timeout_response
rescue ::RestClient::Exception => e      then build_original_response(e.response)
end
call!() click to toggle source

Send defined request. If response is not successful the method raise {LunaPark::Errors::Http}

@example success response

LunaPark::Http::Send.new(request).call #=> <LunaPark::Http::Response @code=200
  # @body="{"version":1,"data":"Hello World!"}" @headers={:content_type=>"application/json",
  # :connection=>"close", :server=>"thin"} @cookies={}>

@example server is unavailable

LunaPark::Http::Send.new(request).call # => raise LunaPark::Errors::Http

@raise [LunaPark::Errors::Http] on bad response, timeout or server is unavailable @return [LunaPark::Http::Response]

# File lib/luna_park/http/send.rb, line 63
def call!
  call.tap do |response|
    raise Errors::Http.new(response.status, response: response) unless response.success?
  end
end

Private Instance Methods

build_original_response(rest_response) click to toggle source
# File lib/luna_park/http/send.rb, line 84
def build_original_response(rest_response)
  Response.new(
    body: rest_response&.body,
    code: rest_response&.code || 0,
    headers: rest_response&.headers,
    cookies: rest_response&.cookies,
    request: original_request
  )
end
build_rest_request(request) click to toggle source
# File lib/luna_park/http/send.rb, line 73
def build_rest_request(request)
  RestClient::Request.new(
    url: request.url.to_s,
    method: request.method,
    payload: request.body,
    headers: request.headers,
    open_timeout: request.open_timeout,
    read_timeout: request.read_timeout
  )
end
build_timeout_response() click to toggle source
# File lib/luna_park/http/send.rb, line 94
def build_timeout_response
  Response.new(code: 408, request: original_request)
end
build_unavailable_response() click to toggle source
# File lib/luna_park/http/send.rb, line 98
def build_unavailable_response
  Response.new(code: 503, request: original_request)
end