Class: Emmett::HTTPRequestProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/emmett/http_request_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, section) ⇒ HTTPRequestProcessor

Returns a new instance of HTTPRequestProcessor.



9
10
11
12
13
14
# File 'lib/emmett/http_request_processor.rb', line 9

def initialize(request, section)
  @raw_request = request.gsub(/\r?\n/m, "\r\n")
  @body        = ""
  @section     = section
  parse!
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/emmett/http_request_processor.rb', line 7

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/emmett/http_request_processor.rb', line 7

def headers
  @headers
end

#http_versionObject (readonly)

Returns the value of attribute http_version.



7
8
9
# File 'lib/emmett/http_request_processor.rb', line 7

def http_version
  @http_version
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/emmett/http_request_processor.rb', line 7

def method
  @method
end

#sectionObject (readonly)

Returns the value of attribute section.



7
8
9
# File 'lib/emmett/http_request_processor.rb', line 7

def section
  @section
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/emmett/http_request_processor.rb', line 7

def url
  @url
end

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/emmett/http_request_processor.rb', line 44

def authenticated?
  headers['Authorization'] && headers['Authorization'] =~ /bearer/i
end

#has_body?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/emmett/http_request_processor.rb', line 40

def has_body?
  @body.strip.length > 0
end

#has_valid_json?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/emmett/http_request_processor.rb', line 56

def has_valid_json?
  return @has_valid_json if instance_variable_defined?(:@has_valid_json)

  begin
    Oj.load body
    @has_valid_json = true
  rescue SyntaxError
    @has_valid_json = false
  end

  @has_valid_json
end

#json?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/emmett/http_request_processor.rb', line 52

def json?
  headers['Content-Type'] && headers['Content-Type'].include?("application/json")
end

#parse!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/emmett/http_request_processor.rb', line 16

def parse!
  parser = Http::Parser.new

  parser.on_headers_complete = proc do
    @http_version = parser.http_version
    @method       = parser.http_method
    @url          = parser.request_url
    @headers      = parser.headers
  end

  parser.on_body = proc do |chunk|
    # One chunk of the body
    @body << chunk
  end

  parser.on_message_complete = proc do |env|
    @parsed = true
  end

  @parsed = false
  parser << @raw_request
  parser << "\r\n" until @parsed
end

#request_lineObject



48
49
50
# File 'lib/emmett/http_request_processor.rb', line 48

def request_line
  "#{method} #{url}"
end