Module: Occi::Parser

Defined in:
lib/occi/parser.rb,
lib/occi/parser/xml.rb,
lib/occi/parser/ovf.rb,
lib/occi/parser/ova.rb,
lib/occi/parser/text.rb,
lib/occi/parser/json.rb

Defined Under Namespace

Modules: Json, Ova, Ovf, Text, Xml

Class Method Summary collapse

Class Method Details

.locations(media_type, body, header) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/occi/parser.rb', line 63

def self.locations(media_type, body, header)
  locations = Occi::Parser::Text.locations header.map { |k, v| v.to_s.split(',').collect { |w| "#{k}: #{w}" } }.flatten
  locations << header['Location'] if !header['Location'].nil? && header['Location'].any?
  case media_type
    when 'text/uri-list'
      locations << body.split("\n")
    when 'text/plain', nil
      locations << Occi::Parser::Text.locations(body)
    else
      nil
  end
  locations
end

.parse(media_type, body, category = false, entity_type = Occi::Core::Resource, header = {}) ⇒ Occi::Collection

Parses an OCCI message and extracts OCCI relevant information

Parameters:

  • media_type (String)

    the media type of the OCCI message

  • body (String)

    the body of the OCCI message

  • category (true, false) (defaults to: false)

    for text/plain and text/occi media types information e.g. from the HTTP request location is needed to determine if the OCCI message includes a category or an entity

  • entity_type (Occi::Core::Resource, Occi::Core::Link) (defaults to: Occi::Core::Resource)

    entity type to use for parsing of text plain entities

  • header (Hash) (defaults to: {})

    optional header of the OCCI message

Returns:

  • (Occi::Collection)

    list consisting of an array of locations and the OCCI object collection


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/occi/parser.rb', line 17

def self.parse(media_type, body, category=false, entity_type=Occi::Core::Resource, header={})
  Occi::Log.debug '### Parsing request data to OCCI Collection ###'
  collection = Occi::Collection.new

  # remove trailing HTTP_ prefix if present
  header = Hash[header.map { |k, v| [k.gsub('HTTP_', '').upcase, v] }]

  if category
    collection = Occi::Parser::Text.categories(header.map { |k, v| v.to_s.split(',').collect { |w| "#{k}: #{w}" } }.flatten)
  else
    if entity_type == Occi::Core::Resource
      collection = Occi::Parser::Text.resource(header.map { |k, v| v.to_s.split(',').collect { |w| "#{k}: #{w}" } }.flatten)
    elsif entity_type == Occi::Core::Link
      collection = Occi::Parser::Text.link(header.map { |k, v| v.to_s.split(',').collect { |w| "#{k}: #{w}" } }.flatten)
    end
  end

  case media_type
    when 'text/uri-list'
      nil
    when 'text/occi'
      nil
    when 'text/plain', nil
      if category
        collection = Occi::Parser::Text.categories body.split "\n"
      else
        if entity_type == Occi::Core::Resource
          collection = Occi::Parser::Text.resource body.split "\n"
        elsif entity_type == Occi::Core::Link
          collection = Occi::Parser::Text.link body.split "\n"
        end
      end
    when 'application/occi+json', 'application/json'
      collection = Occi::Parser::Json.collection body
    when 'application/occi+xml', 'application/xml'
      collection = Occi::Parser::Xml.collection body
    when 'application/ovf', 'application/ovf+xml'
      collection = Occi::Parser::Ovf.collection body
    when 'application/ova'
      collection = Occi::Parser::Ova.collection body
    else
      raise "Content Type not supported"
  end
  collection
end