Class: Gemite::Request

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

Overview

生のHTTPリクエストをパースした結果を保持する。

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/gemite/request.rb', line 6

def body
  @body
end

#form_paramsObject (readonly)

Returns the value of attribute form_params.



6
7
8
# File 'lib/gemite/request.rb', line 6

def form_params
  @form_params
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/gemite/request.rb', line 6

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



6
7
8
# File 'lib/gemite/request.rb', line 6

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/gemite/request.rb', line 6

def path
  @path
end

#query_paramsObject (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_bodyObject

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