Class: RequestParsers::OcciParser

Inherits:
Object
  • Object
show all
Defined in:
lib/request_parsers/occi_parser.rb

Constant Summary collapse

AVAILABLE_PARSERS =
{
  'application/occi+json' => ::RequestParsers::Occi::JSON,
  'application/json' => ::RequestParsers::Occi::JSON,
  'text/occi' => ::RequestParsers::Occi::Text,
  'text/plain' => ::RequestParsers::Occi::Text,
  #'application/occi+xml' => ::RequestParsers::Occi::XML,
  #'application/xml' => ::RequestParsers::Occi::XML,
  'text/html' => ::RequestParsers::Occi::Dummy,
  '' => ::RequestParsers::Occi::Dummy,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ OcciParser

Returns a new instance of OcciParser


16
17
18
# File 'lib/request_parsers/occi_parser.rb', line 16

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/request_parsers/occi_parser.rb', line 20

def call(env)
  request = ::ActionDispatch::Request.new(env)

  # make a copy of the request body
  @body = request.body.respond_to?(:read) ? request.body.read : request.body.string
  @body = Marshal.load(Marshal.dump(@body))

  # save copy some additional information
  @media_type = request.media_type.to_s
  @headers = sanitize_request_headers(request.headers)
  @fullpath = request.fullpath.to_s

  env['rocci_server.request.parser'] = self
  @app.call(env)
end

#parse_occi_messages(entity_type = nil) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/request_parsers/occi_parser.rb', line 36

def parse_occi_messages(entity_type = nil)
  fail ::Errors::UnsupportedMediaTypeError, "Media type '#{@media_type}' is not supported by the RequestParser" unless AVAILABLE_PARSERS.key?(@media_type)

  collection = if entity_type
                 AVAILABLE_PARSERS[@media_type].parse(@media_type, @body, @headers, @fullpath, entity_type)
               else
                 AVAILABLE_PARSERS[@media_type].parse(@media_type, @body, @headers, @fullpath)
               end
  Rails.logger.debug "[Parser] [#{self.class}] Parsed request into coll=#{collection.inspect}"

  collection
end