Class: RubstApi::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rubst_api/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, path_params: {}) ⇒ Request

Returns a new instance of Request.



23
24
25
# File 'lib/rubst_api/http.rb', line 23

def initialize(env, path_params: {})
  @env, @path_params, @state = env, path_params, {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



22
23
24
# File 'lib/rubst_api/http.rb', line 22

def env
  @env
end

#path_paramsObject (readonly)

Returns the value of attribute path_params.



22
23
24
# File 'lib/rubst_api/http.rb', line 22

def path_params
  @path_params
end

#stateObject (readonly)

Returns the value of attribute state.



22
23
24
# File 'lib/rubst_api/http.rb', line 22

def state
  @state
end

Instance Method Details

#bodyObject



39
40
41
42
43
44
45
46
# File 'lib/rubst_api/http.rb', line 39

def body
  @body ||= begin
    io = env["rack.input"] || StringIO.new
    value = io.read
    io.rewind if io.respond_to?(:rewind)
    value
  end
end

#clientObject



49
# File 'lib/rubst_api/http.rb', line 49

def client = [env["REMOTE_ADDR"], env["REMOTE_PORT"]]

#content_typeObject



48
# File 'lib/rubst_api/http.rb', line 48

def content_type = headers["content-type"]&.split(";")&.first

#cookiesObject



36
37
38
# File 'lib/rubst_api/http.rb', line 36

def cookies
  @cookies ||= (headers["cookie"] || "").split(/;\s*/).filter_map { |pair| pair.split("=", 2) if pair.include?("=") }.to_h
end

#headersObject



30
31
32
33
34
35
# File 'lib/rubst_api/http.rb', line 30

def headers
  @headers ||= Headers.new(env.filter_map do |key, value|
    next unless key.start_with?("HTTP_") || %w[CONTENT_TYPE CONTENT_LENGTH].include?(key)
    [key.sub(/\AHTTP_/, "").downcase, value]
  end.to_h)
end

#jsonObject



47
# File 'lib/rubst_api/http.rb', line 47

def json = body.empty? ? nil : JSON.parse(body)

#methodObject



26
# File 'lib/rubst_api/http.rb', line 26

def method = env.fetch("REQUEST_METHOD", "GET").upcase

#pathObject



27
# File 'lib/rubst_api/http.rb', line 27

def path = env.fetch("PATH_INFO", "/")

#query_paramsObject



29
# File 'lib/rubst_api/http.rb', line 29

def query_params = @query_params ||= URI.decode_www_form(query_string).each_with_object({}) { |(k, v), h| h[k] = h.key?(k) ? Array(h[k]) << v : v }

#query_stringObject



28
# File 'lib/rubst_api/http.rb', line 28

def query_string = env.fetch("QUERY_STRING", "")

#urlObject



50
# File 'lib/rubst_api/http.rb', line 50

def url = "#{env["rack.url_scheme"] || "http"}://#{env["HTTP_HOST"] || "localhost"}#{path}#{query_string.empty? ? "" : "?#{query_string}"}"