Class: OCCI::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/occi/request.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Request) initialize(request)

Parses a Rack/Sinatra Request and extract OCCI relevant information



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/occi/request.rb', line 39

def initialize(request)
  @kinds = []
  @mixins = []
  @actions = []
  @resources = []
  @links = []
  @locations = []

=begin
      if content_type.includes?('multipart')
        # TODO: implement multipart handling
        # handle file upload
        if params['file'] != nil
          OCCI::Log.debug("Location of Image #{params['file'][:tempfile].path}")
          $image_path = OCCI::Server.config[:one_image_tmp_dir] + '/' + params['file'][:filename]
          FileUtils.cp(params['file'][:tempfile].path, $image_path)
        end

        # handle file upload in multipart requests
        request.POST.values.each do |body|
          if body.kind_of?(String)
            parse_text(body)
          elsif body.kind_of?(Hash)
            if body['type'].include?('application/json')
              # try to parse body as JSON object
              parse_json(body.read)
            elsif body['type'].include?('text/plain') # text/plain
              parse_text(body.read)
            end unless body['type'].nil?
          end
        end
      end
=end

  body = request.body.read.to_s

  # always check headers
  parse_header_locations(request.env)
  if @locations.empty?
    if request.path_info.include?('/-/')
      parse_header_categories(request.env)
    else
      parse_header_entity(request.env)
    end
  end

  case request.media_type
    when 'text/uri-list'
      body.each_line do |line|
        @locations << URI.parse(line)
      end
    when 'text/plain', nil
      parse_text_locations(body)
      if @locations.empty?
        if request.path_info.include?('/-/')
          parse_text_categories(body)
        else
          parse_text_entity(body)
        end
      end
    when 'application/occi+json' || 'application/json'
      parse_json(body)
    else
      raise OCCI::ContentTypeNotSupported
  end

  OCCI::Log.debug("### Successfully parsed #{@kinds.size} kinds")
  OCCI::Log.debug("### Successfully parsed #{@mixins.size} mixins")
  OCCI::Log.debug("### Successfully parsed #{@actions.size} actions")
  OCCI::Log.debug("### Successfully parsed #{@resources.size} resources")
  OCCI::Log.debug("### Successfully parsed #{@links.size} links")
  OCCI::Log.debug("### Successfully parsed #{@locations.size} locations")

end

Instance Attribute Details

- (Object) actions (readonly)

Returns the value of attribute actions



31
32
33
# File 'lib/occi/request.rb', line 31

def actions
  @actions
end

- (Object) kinds (readonly)

Returns the value of attribute kinds



29
30
31
# File 'lib/occi/request.rb', line 29

def kinds
  @kinds
end

Returns the value of attribute links



33
34
35
# File 'lib/occi/request.rb', line 33

def links
  @links
end

- (Object) locations (readonly)

Returns the value of attribute locations



34
35
36
# File 'lib/occi/request.rb', line 34

def locations
  @locations
end

- (Object) mixins (readonly)

Returns the value of attribute mixins



30
31
32
# File 'lib/occi/request.rb', line 30

def mixins
  @mixins
end

- (Object) resources (readonly)

Returns the value of attribute resources



32
33
34
# File 'lib/occi/request.rb', line 32

def resources
  @resources
end

Instance Method Details

- (Object) categories



114
115
116
# File 'lib/occi/request.rb', line 114

def categories
  @kinds + @mixins + @actions
end