Class: LunaPark::Http::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, method: nil, url: nil, body: nil, headers: nil, open_timeout: nil, read_timeout: nil, driver:) ⇒ Request

Create new request

rubocop:disable Metrics/ParameterLists, Layout/LineLength

Parameters:

  • title

    business description (see #title)

  • method (defaults to: nil)

    Http method of current request (see #method)

  • driver

    is HTTP driver which use to send this request



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/luna_park/http/request.rb', line 99

def initialize(title:, method: nil, url: nil, body: nil, headers: nil, open_timeout: nil, read_timeout: nil, driver:)
  @title        = title
  @method       = method
  @url          = url
  @body         = body
  @headers      = headers
  @read_timeout = read_timeout
  @open_timeout = open_timeout
  @driver       = driver
  @sent_at      = nil
end

Instance Attribute Details

#bodyObject

Body of http request (defaults is ‘nil`)

Examples:

Get users request


request = Request.new(
  title: 'Get users list',
  method: :get,
  url: 'http://example.com/users',
  body: JSON.generate({message: 'Hello!'})
)
request.body # => "{\"message\":\"Hello!\"}"'


53
54
55
# File 'lib/luna_park/http/request.rb', line 53

def body
  @body
end

#headersObject

Http request headers (defaults is ‘{}`)

Examples:

Get users request

json_request.headers # => {}


59
60
61
# File 'lib/luna_park/http/request.rb', line 59

def headers
  @headers
end

#method(name = nil) ⇒ Object

Http method of current request, defines a set of request methods to indicate the desired action to be performed for a given resource

Examples:

Get users request

request.method # => :get

With argument (delegates ‘Object.method(name)` to save access to the original Ruby method)

request.method(:foo) # => #<Method: LunaPark::Http::Request#foo>


30
31
32
# File 'lib/luna_park/http/request.rb', line 30

def method(name = nil)
  name.nil? ? @method : super(name)
end

#open_timeoutObject

Http open timeout, is the timeout for opening the connection. This is useful if you are calling servers with slow or shaky response times. (defaults is 10)

Examples:

Get users request

json_request.open_timeout # => 10


77
78
79
# File 'lib/luna_park/http/request.rb', line 77

def open_timeout
  @open_timeout
end

#read_timeoutObject

Http read timeout, is the timeout for reading the answer. This is useful to make sure you will not get stuck half way in the reading process, or get stuck reading a 5 MB file when you’re expecting 5 KB of JSON (default is 10)

Examples:

Get users request

json_request.read_timout # => 10


69
70
71
# File 'lib/luna_park/http/request.rb', line 69

def read_timeout
  @read_timeout
end

#sent_atObject (readonly)

Time when request is sent

Examples:

before request is sent

request.sent_at # => nil

after request has been sent

request.sent_at # => 2020-05-04 16:56:20 +0300


86
87
88
# File 'lib/luna_park/http/request.rb', line 86

def sent_at
  @sent_at
end

#titleObject

Business description for this request, help you make the domain model more expressive

Examples:

Get users request

request = Request.new(
  title: 'Get users list',
  method: :get,
  url: 'https://example.com/users'
)

request.title # => 'Get users list'


19
20
21
# File 'lib/luna_park/http/request.rb', line 19

def title
  @title
end

#urlObject

Http url to send request

Examples:

Get users request

request.url # => 'http://example.com/users'


40
41
42
# File 'lib/luna_park/http/request.rb', line 40

def url
  @url
end

Instance Method Details

#callObject

Note:

This method implements a facade pattern. And you better use it than call the Http::Send class directly.

Send current request (we cannot call this method ‘send` because it reserved word in ruby). It always return Response object, even if the server returned an error such as 404 or 502.

After sending the request, the object is frozen. You should dup object to resend request.

Examples:

correct answer

request = Http::Request.new(
   title: 'Get users list',
   method: :get,
   url: 'http:://yandex.ru'
 )
 request.call # => <LunaPark::Http::Response @code=200 @body="Hello World!" @headers={}>

Server unavailable

request.call # => <LunaPark::Http::Response @code=503 @body="" @headers={}>

Returns:

  • LunaPark::Http::Response



133
134
135
136
# File 'lib/luna_park/http/request.rb', line 133

def call
  @sent_at = Time.now
  driver.call(self).tap { freeze }
end

#call!Object

Note:

This method implements a facade pattern. And you better use it than call the Http::Send class directly.

Send the current request. It returns a Response object only on a successful response. If the response failed, the call! method should raise an Erros::Http exception.

After call! request you cannot change request attributes.

After sending the request, the object is frozen. You should dup object to resend request.

Examples:

correct answer

request = Http::Request.new(
   title: 'Get users list',
   method: :get,
   url: 'https://example.com/users'
 )
 request.call # => <LunaPark::Http::Response @code=200 @body="Hello World!" @headers={}>

Server unavailable

request.call # => <LunaPark::Http::Response @code=503 @body="" @headers={}>

Returns:

  • LunaPark::Http::Response



160
161
162
163
# File 'lib/luna_park/http/request.rb', line 160

def call!
  @sent_at = Time.now
  driver.call!(self).tap { freeze }
end

#driverObject

This method return which driver are use, to send current request.



179
180
181
# File 'lib/luna_park/http/request.rb', line 179

def driver
  @driver ||= self.class.default_driver
end

#initialize_dup(_other) ⇒ Object

When object is duplicated, we should reset send timestamp



166
167
168
169
# File 'lib/luna_park/http/request.rb', line 166

def initialize_dup(_other)
  super
  @sent_at = nil
end

#inspectObject

Examples:

inspect get users index request

request = LunaPark::Http::Request.new(
  title: 'Get users',
  method: :get,
  url: 'https://example.com/users'
)

request.inspect # => "<LunaPark::Http::Request @title=\"Get users\"
                #  @url=\"http://localhost:8080/get_200\" @method=\"get\"
                #  @headers=\"{}\" @body=\"\" @sent_at=\"\">"


193
194
195
196
197
198
199
200
201
# File 'lib/luna_park/http/request.rb', line 193

def inspect
  "<#{self.class.name} "           \
    "@title=#{title.inspect} "     \
    "@url=#{url.inspect} "         \
    "@method=#{method.inspect} "   \
    "@headers=#{headers.inspect} " \
    "@body=#{body.inspect} "       \
    "@sent_at=#{sent_at.inspect}>"
end

#sent?Boolean

This method shows if this request has been already sent.

Returns:

  • (Boolean)

    Boolean



174
175
176
# File 'lib/luna_park/http/request.rb', line 174

def sent?
  !@sent_at.nil?
end

#to_hObject

Examples:

request.to_h # => {:title=>"Get users",
                   :method=>:get,
                   :url=>"http://localhost:8080/get_200",
                   :body=>nil,
                   :headers=>{},
                   :read_timeout=>10,
                   :open_timeout=>10}


211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/luna_park/http/request.rb', line 211

def to_h
  {
    title: title,
    method: method,
    url: url,
    body: body,
    headers: headers,
    read_timeout: read_timeout,
    open_timeout: open_timeout,
    sent_at: sent_at
  }
end