Class: Gemite::Request
- Inherits:
-
Object
- Object
- Gemite::Request
- Defined in:
- lib/gemite/request.rb
Overview
生のHTTPリクエストをパースした結果を保持する。
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#form_params ⇒ Object
readonly
Returns the value of attribute form_params.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#query_params ⇒ Object
readonly
Returns the value of attribute query_params.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(method:, path:, query_params:, headers:, body:) ⇒ Request
constructor
A new instance of Request.
-
#parse_form_body ⇒ Object
application/x-www-form-urlencoded のボディをパースする。 multipart/form-data(ファイルアップロード)はv0.1では対象外。.
Constructor Details
#initialize(method:, path:, query_params:, headers:, body:) ⇒ Request
Returns a new instance of Request.
8 9 10 11 12 13 14 15 |
# File 'lib/gemite/request.rb', line 8 def initialize(method:, path:, query_params:, headers:, body:) @method = method @path = path @query_params = query_params @headers = headers @body = body @form_params = parse_form_body end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
6 7 8 |
# File 'lib/gemite/request.rb', line 6 def body @body end |
#form_params ⇒ Object (readonly)
Returns the value of attribute form_params.
6 7 8 |
# File 'lib/gemite/request.rb', line 6 def form_params @form_params end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/gemite/request.rb', line 6 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
6 7 8 |
# File 'lib/gemite/request.rb', line 6 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/gemite/request.rb', line 6 def path @path end |
#query_params ⇒ Object (readonly)
Returns the value of attribute query_params.
6 7 8 |
# File 'lib/gemite/request.rb', line 6 def query_params @query_params end |
Class Method Details
.parse_www_form(str) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gemite/request.rb', line 28 def self.parse_www_form(str) result = {} str.split("&").each do |pair| next if pair.empty? k, v = pair.split("=", 2) result[uri_decode(k)] = uri_decode(v || "") end result end |
.uri_decode(str) ⇒ Object
39 40 41 42 |
# File 'lib/gemite/request.rb', line 39 def self.uri_decode(str) str.to_s.tr("+", " ").gsub(/%([0-9A-Fa-f]{2})/) { [Regexp.last_match(1)].pack("H2").force_encoding("BINARY") } .force_encoding("BINARY").force_encoding("UTF-8") end |
Instance Method Details
#parse_form_body ⇒ Object
application/x-www-form-urlencoded のボディをパースする。 multipart/form-data(ファイルアップロード)はv0.1では対象外。
19 20 21 22 23 24 25 26 |
# File 'lib/gemite/request.rb', line 19 def parse_form_body return {} if body.nil? || body.empty? content_type = headers["content-type"] || "" return {} unless content_type.include?("application/x-www-form-urlencoded") || content_type.empty? parse_www_form(body) end |