Class: LunaPark::Http::Send

Inherits:
Object
  • Object
show all
Extended by:
Extensions::Callable
Defined in:
lib/luna_park/http/send.rb

Overview

Send Request and get Response.

This service, in fact, works as an adapter for the 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 Request#call. Which freeze request and define Request#sent_at timestamp.

Instance Method Summary collapse

Constructor Details

#initialize(original_request) ⇒ Send

Define new Send service

Parameters:



26
27
28
# File 'lib/luna_park/http/send.rb', line 26

def initialize(original_request)
  @original_request = original_request
end

Instance Method Details

#callLunaPark::Http::Response

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

Examples:

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={}>

server is unavailable

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

Returns:



42
43
44
45
46
47
48
49
# 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!LunaPark::Http::Response

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

Examples:

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={}>

server is unavailable

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

Returns:

Raises:



63
64
65
66
67
# File 'lib/luna_park/http/send.rb', line 63

def call!
  call.tap do |response|
    raise response.exception unless response.exception.nil?
  end
end