Class: NetHttp2::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/net-http2/request.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, path, options = {}) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
14
15
16
17
18
# File 'lib/net-http2/request.rb', line 9

def initialize(method, uri, path, options={})
  @method  = method
  @uri     = uri
  @path    = path
  @body    = options[:body]
  @headers = options[:headers] || {}
  @timeout = options[:timeout] || DEFAULT_TIMEOUT

  @events = {}
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/net-http2/request.rb', line 7

def body
  @body
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/net-http2/request.rb', line 7

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/net-http2/request.rb', line 7

def path
  @path
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



7
8
9
# File 'lib/net-http2/request.rb', line 7

def timeout
  @timeout
end

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/net-http2/request.rb', line 7

def uri
  @uri
end

Instance Method Details

#emit(event, arg) ⇒ Object



45
46
47
48
# File 'lib/net-http2/request.rb', line 45

def emit(event, arg)
  return unless @events[event]
  @events[event].each { |b| b.call(arg) }
end

#headersObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/net-http2/request.rb', line 20

def headers
  @headers.merge!({
    ':scheme' => @uri.scheme,
    ':method' => @method.to_s.upcase,
    ':path'   => @path,
  })

  @headers.merge!('host' => @uri.host) unless @headers['host']

  if @body
    @headers.merge!('content-length' => @body.bytesize.to_s)
  else
    @headers.delete('content-length')
  end

  @headers
end

#on(event, &block) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
# File 'lib/net-http2/request.rb', line 38

def on(event, &block)
  raise ArgumentError, 'on event must provide a block' unless block_given?

  @events[event] ||= []
  @events[event] << block
end