Class: Emmett::HTTPRequestProcessor
- Inherits:
-
Object
- Object
- Emmett::HTTPRequestProcessor
- Defined in:
- lib/emmett/http_request_processor.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_version ⇒ Object
readonly
Returns the value of attribute http_version.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#section ⇒ Object
readonly
Returns the value of attribute section.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #authenticated? ⇒ Boolean
- #has_body? ⇒ Boolean
- #has_valid_json? ⇒ Boolean
-
#initialize(request, section) ⇒ HTTPRequestProcessor
constructor
A new instance of HTTPRequestProcessor.
- #json? ⇒ Boolean
- #parse! ⇒ Object
- #request_line ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/emmett/http_request_processor.rb', line 7 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/emmett/http_request_processor.rb', line 7 def headers @headers end |
#http_version ⇒ Object (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 |
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/emmett/http_request_processor.rb', line 7 def method @method end |
#section ⇒ Object (readonly)
Returns the value of attribute section.
7 8 9 |
# File 'lib/emmett/http_request_processor.rb', line 7 def section @section end |
#url ⇒ Object (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
44 45 46 |
# File 'lib/emmett/http_request_processor.rb', line 44 def authenticated? headers['Authorization'] && headers['Authorization'] =~ /bearer/i end |
#has_body? ⇒ 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
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
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. = proc do |env| @parsed = true end @parsed = false parser << @raw_request parser << "\r\n" until @parsed end |
#request_line ⇒ Object
48 49 50 |
# File 'lib/emmett/http_request_processor.rb', line 48 def request_line "#{method} #{url}" end |