Class: JSONAPIonify::Api::Server::Request
- Inherits:
-
Rack::Request
- Object
- Rack::Request
- JSONAPIonify::Api::Server::Request
- Defined in:
- lib/jsonapionify/api/server/request.rb
Class Method Summary collapse
Instance Method Summary collapse
- #accept ⇒ Object
- #accept_params ⇒ Object
- #authorizations ⇒ Object
- #content_type ⇒ Object
- #extension ⇒ Object
- #has_body? ⇒ Boolean
- #headers ⇒ Object
- #http_string ⇒ Object
- #jsonapi_params ⇒ Object
- #pretty_body ⇒ Object
- #root_url ⇒ Object
Class Method Details
.env_for(url, method, options = {}) ⇒ Object
8 9 10 11 |
# File 'lib/jsonapionify/api/server/request.rb', line 8 def self.env_for(url, method, = {}) [:method] = method new Rack::MockRequest.env_for(url, ) end |
Instance Method Details
#accept ⇒ Object
74 75 76 |
# File 'lib/jsonapionify/api/server/request.rb', line 74 def accept @accept ||= accept_params.keys end |
#accept_params ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jsonapionify/api/server/request.rb', line 59 def accept_params @accept_params ||= begin ext_mime = MIME::Types.type_for(path)[0]&.content_type accepts = (headers['accept'] || ext_mime || '*/*').split(',') types = [ext_mime].compact | accepts types.each_with_object({}) do |type, list| list[Server::MediaType.type(type)] = Server::MediaType.params(type) end end end |
#authorizations ⇒ Object
78 79 80 81 |
# File 'lib/jsonapionify/api/server/request.rb', line 78 def parts = headers['authorization'].to_s.split(' ') parts.length == 2 ? Rack::Utils::HeaderHash.new([parts].to_h) : {} end |
#content_type ⇒ Object
55 56 57 |
# File 'lib/jsonapionify/api/server/request.rb', line 55 def content_type super.try(:split, ';').try(:first) end |
#extension ⇒ Object
48 49 50 51 52 53 |
# File 'lib/jsonapionify/api/server/request.rb', line 48 def extension ext = File.extname(path) return nil unless ext ext = ext[0] == '.' ? ext[1..-1] : ext ext.to_s.empty? ? nil : ext end |
#has_body? ⇒ Boolean
83 84 85 86 87 |
# File 'lib/jsonapionify/api/server/request.rb', line 83 def has_body? body.read(1).present? ensure body.rewind end |
#headers ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/jsonapionify/api/server/request.rb', line 13 def headers env_headers = env.select do |name, _| name.start_with?('HTTP_') && !%w{HTTP_VERSION}.include?(name) end.each_with_object({}) do |(name, value), hash| hash[name[5..-1].gsub('_', '-').downcase] = value end env_headers['content-type'] = content_type if content_type Rack::Utils::HeaderHash.new(env_headers) end |
#http_string ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jsonapionify/api/server/request.rb', line 23 def http_string # GET /path/file.html HTTP/1.0 # From: [email protected] # User-Agent: HTTPTool/1.0 # [blank line here] [].tap do |lines| lines << "#{request_method} #{fullpath} HTTP/1.1" headers.each do |k, v| lines << "#{k.split('-').map(&:capitalize).join('-')}: #{v}" end lines << '' lines << pretty_body if has_body? end.join("\n") end |
#jsonapi_params ⇒ Object
70 71 72 |
# File 'lib/jsonapionify/api/server/request.rb', line 70 def jsonapi_params accept_params['application/vnd.api+json'] || {} end |
#pretty_body ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/jsonapionify/api/server/request.rb', line 38 def pretty_body return '' unless has_body? value = body.read JSON.pretty_generate(Oj.load(value)) rescue Oj::ParseError value ensure body.rewind end |
#root_url ⇒ Object
89 90 91 92 93 94 |
# File 'lib/jsonapionify/api/server/request.rb', line 89 def root_url URI.parse(url).tap do |uri| uri.query = nil uri.path.chomp! path_info end.to_s end |