Class: RubstApi::Request
- Inherits:
-
Object
- Object
- RubstApi::Request
- Defined in:
- lib/rubst_api/http.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#path_params ⇒ Object
readonly
Returns the value of attribute path_params.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #body ⇒ Object
- #client ⇒ Object
- #content_type ⇒ Object
- #cookies ⇒ Object
- #headers ⇒ Object
-
#initialize(env, path_params: {}) ⇒ Request
constructor
A new instance of Request.
- #json ⇒ Object
- #method ⇒ Object
- #path ⇒ Object
- #query_params ⇒ Object
- #query_string ⇒ Object
- #url ⇒ Object
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
#env ⇒ Object (readonly)
Returns the value of attribute env.
22 23 24 |
# File 'lib/rubst_api/http.rb', line 22 def env @env end |
#path_params ⇒ Object (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 |
#state ⇒ Object (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
#body ⇒ Object
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 |
#client ⇒ Object
49 |
# File 'lib/rubst_api/http.rb', line 49 def client = [env["REMOTE_ADDR"], env["REMOTE_PORT"]] |
#content_type ⇒ Object
48 |
# File 'lib/rubst_api/http.rb', line 48 def content_type = headers["content-type"]&.split(";")&.first |
#cookies ⇒ Object
36 37 38 |
# File 'lib/rubst_api/http.rb', line 36 def @cookies ||= (headers["cookie"] || "").split(/;\s*/).filter_map { |pair| pair.split("=", 2) if pair.include?("=") }.to_h end |
#headers ⇒ Object
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 |
#json ⇒ Object
47 |
# File 'lib/rubst_api/http.rb', line 47 def json = body.empty? ? nil : JSON.parse(body) |
#method ⇒ Object
26 |
# File 'lib/rubst_api/http.rb', line 26 def method = env.fetch("REQUEST_METHOD", "GET").upcase |
#path ⇒ Object
27 |
# File 'lib/rubst_api/http.rb', line 27 def path = env.fetch("PATH_INFO", "/") |
#query_params ⇒ Object
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_string ⇒ Object
28 |
# File 'lib/rubst_api/http.rb', line 28 def query_string = env.fetch("QUERY_STRING", "") |
#url ⇒ Object
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}"}" |