Class: Riddl::Wrapper::Description::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/riddl/wrapper/description/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, recursive = false) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 6

def initialize(path=nil,recursive=false)
  #{{{
  @path = path
  @role = nil
  @resources = {}
  @access_methods = {}
  @composition = {}
  @recursive = recursive
  @custom = []
  #}}}
end

Instance Attribute Details

#access_methodsObject (readonly)

}}}



354
355
356
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 354

def access_methods
  @access_methods
end

#compositionObject (readonly)

}}}



354
355
356
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 354

def composition
  @composition
end

#customObject

Returns the value of attribute custom.



355
356
357
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 355

def custom
  @custom
end

#pathObject (readonly)

}}}



354
355
356
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 354

def path
  @path
end

#recursiveObject (readonly)

}}}



354
355
356
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 354

def recursive
  @recursive
end

#resourcesObject (readonly)

}}}



354
355
356
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 354

def resources
  @resources
end

#roleObject (readonly)

}}}



354
355
356
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 354

def role
  @role
end

Instance Method Details

#add_access_methods(des, desres, index, interface) ⇒ Object



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
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 18

def add_access_methods(des,desres,index,interface)
  #{{{
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @in and not(@in='*')]").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_in_out(index,interface,des,method,m.attributes['in'],m.attributes['out'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @pass and not(@pass='*')]").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_in_out(index,interface,des,method,m.attributes['pass'],m.attributes['pass'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @transformation]").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_transform(index,interface,des,method,m.attributes['transformation'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @in and @in='*']").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_star_out(index,interface,des,method,m.attributes['out'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and not(@in)]").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_star_out(index,interface,des,method,m.attributes['out'],m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and not(name()='websocket') and @pass and @pass='*']").each do |m|
    method = m.attributes['method'] || m.qname.name
    add_request_pass(index,interface,method,m.find('*|text()'))
  end
  desres.find("des:*[not(name()='resource') and name()='websocket']").each do |m|
    add_websocket(index,interface,m.find('*|text()'))
  end
  @role = desres.find("string(@role)")
  @role = nil if @role.strip == ''
  #}}}
end

#add_custom(desres) ⇒ Object



52
53
54
55
56
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 52

def add_custom(desres)
  #{{{
  @custom = desres.find("*[not(self::des:*)]").to_a
  #}}}
end

#add_websocket(index, interface, custom) ⇒ Object



346
347
348
349
350
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 346

def add_websocket(index,interface,custom)
  @access_methods['websocket'] ||= []
  @access_methods['websocket'][index] ||= []
  @access_methods['websocket'][index] << WebSocket.new(interface,custom)
end

#compose!Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 101

def compose!
  #{{{
  @access_methods.each do |k,v|
    ### remove all emtpy layers  
    v.compact!
    case v.size
      when 0
      when 1
        @composition[k] = compose_plain(v[0])
      else
        @composition[k] = compose_layers(k,v)
    end
  end  
  #}}}
end

#description_xml(namespaces) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 291

def description_xml(namespaces)
  #{{{
  namespaces = namespaces.delete_if do |k,n|
    k =~ /^xmlns\d+$/ || [Riddl::Wrapper::DESCRIPTION, Riddl::Wrapper::DECLARATION, Riddl::Wrapper::XINCLUDE].include?(n)
  end.map do |k,n|
    "xmlns:#{k}=\"#{n}\""
  end.join(' ')

  messages = {}
  messages_result = ''
  collect = description_xml_string(messages," " * 4)

  names = []
  messages.each do |hash,mess|
    t = mess.content.dup
    name = mess.name
    name += '_' while names.include?(name)
    names << name
    t.root.attributes['name'] = name
    messages_result << t.root.dump + "\n"
  end
  XML::Smart.string("<description #{Riddl::Wrapper::COMMON} #{namespaces}>\n\n" + messages_result.gsub(/^/,'  ') + "\n  <resource>\n" + collect + "  </resource>\n</description>").to_s
  #}}}
end

#description_xml_string(messages, t) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 235

def description_xml_string(messages,t)
 #{{{
  result = ''
  @custom.each do |c|
    result << c.dump
  end
  if @composition.any?
    @composition.each do |k,v|
      v.each do |m|
        m = m.result
        if %w{get post put delete websocket}.include?(k)
          result << t + "<#{k} "
        else
          result << t + "<request method=\"#{k}\" "
        end  
        case m
          when Riddl::Wrapper::Description::RequestInOut
            messages[m.in.hash] ||= m.in
            result << "in=\"#{messages[m.in.hash].name}\""
            unless m.out.nil?
              messages[m.out.hash] ||= m.out
              result << " out=\"#{messages[m.out.hash].name}\""
            end  
          when Riddl::Wrapper::Description::RequestStarOut
            result << "in=\"*\""
            unless m.out.nil?
              messages[m.out.hash] ||= m.out
              result << " out=\"#{messages[m.out.hash].name}\""
            end  
          when Riddl::Wrapper::Description::RequestPass
            messages[m.pass.hash] ||= m.pass
            result << "pass=\"#{messages[m.pass.hash].name}\""
          when Riddl::Wrapper::Description::RequestTransformation
            messages[m.trans.hash] ||= m.trans
            result << "transformation=\"#{messages[m.trans.hash].name}\""
        end
        if m.custom.length > 0
          result << ">\n"
          m.custom.each do |e|
            result << e.dump + "\n"
          end  
          if %w{get post put delete websocket}.include?(k)
            result << t + "</#{k}>"
          else
            result << t + "</request>\n"
          end  
        else  
          result << "/>\n"
        end
      end  
    end
  end
  result
 #}}}
end

#remove_access_methods(des, filter) ⇒ Object

TODO add websockets

Raises:



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
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 60

def remove_access_methods(des,filter)
  #{{{
  freq = if filter['in'] && filter['in'] != '*'
    t = [RequestInOut,Riddl::Wrapper::Description::Message.new(des,filter['in'])]
    t << (filter['out'] ? Riddl::Wrapper::Description::Message.new(des,filter['out']) : nil)
  elsif filter['pass'] && filter['pass'] != '*'
    [RequestInOut,Riddl::Wrapper::Description::Message.new(des,filter['pass']),Riddl::Wrapper::Description::Message.new(des,filter['pass'])]
  elsif filter['in'] && filter['in'] == '*'
    t = [RequestStarOut]
    t << (filter['out'] ? Riddl::Wrapper::Description::Message.new(des,filter['out']) : nil)
  elsif filter['transformation']
    [RequestTransformation,Riddl::Wrapper::Description::Transformation.new(des,filter['transformation'])]
  elsif filter['pass'] && filter['pass'] == '*'
    [RequestPass]
  end
  raise BlockError, "blocking #{filter.inspect} not possible" if freq.nil?

  if reqs = @access_methods[filter['method']]
    reqs = reqs.last # current layer
    reqs.delete_if do |req|
      if req.class == freq[0]
        if req.class == RequestInOut
          # TODO These hash comparisons are maybe too trivial, as we want to catch name="*" parameters
          if freq[1] && freq[1].hash == req.in.hash && freq[2] && req.out && freq[2].hash == req.out.hash
            true
          elsif freq[1] && freq[1].hash == req.in.hash && !freq[2]
            true
          end
        elsif req.class == RequestStarOut
          true if freq[1] && req.out && freq[1].hash == req.out.hash
        elsif req.class == RequestTransformation
          true if freq[1] && freq[1].hash == req.trans.hash
        elsif req.class == RequestPass
          true
        end
      end  
    end
  end  
  #}}}
end