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)

}}}



404
405
406
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 404

def access_methods
  @access_methods
end

#compositionObject (readonly)

}}}



404
405
406
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 404

def composition
  @composition
end

#customObject

Returns the value of attribute custom.



405
406
407
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 405

def custom
  @custom
end

#pathObject (readonly)

}}}



404
405
406
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 404

def path
  @path
end

#recursiveObject (readonly)

}}}



404
405
406
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 404

def recursive
  @recursive
end

#resourcesObject (readonly)

}}}



404
405
406
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 404

def resources
  @resources
end

#roleObject (readonly)

}}}



404
405
406
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 404

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
51
52
53
# 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 not(name()='sse') 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 not(name()='sse') 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 not(name()='sse') 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 not(name()='sse') 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(name()='sse') and not(@in) and not(@pass)]").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(name()='sse') 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:*[name()='websocket']").each do |m|
    add_websocket(index,interface,m.find('*|text()'))
  end
  desres.find("des:*[name()='sse']").each do |m|
    add_sse(index,interface,m.find('*|text()'))
  end
  @role = desres.find("string(@role)")
  @role = nil if @role.strip == ''
  #}}}
end

#add_custom(desres) ⇒ Object



55
56
57
58
59
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 55

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

#compose!Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 105

def compose!
  #{{{
  @access_methods.each do |k,v|
    ### remove all emtpy layers
    v.map!{|e| (e.nil? || e.empty?) ? nil: e }
    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



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 332

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) + description_xml_string_sub(messages," " * 4)
  collect = XML::Smart.string("<resource>\n" + collect + "  </resource>")

  names = []
  messages.each do |hash,mess|
    t = mess.content.dup
    name = mess.name
    name += '_' while names.include?(name)
    collect.find("//@*[.=#{hash}]").each { |e| e.value = 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  " + collect.root.dump + "\n</description>").to_s
  #}}}
end

#description_xml_string(messages, t) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 296

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|
        result << description_xml_string_analyse(messages,t,k,m.result)
      end
    end
  else
    @access_methods.each do |k,v|
      v.first.each do |m|
        result << description_xml_string_analyse(messages,t,k,m)
      end
    end
  end
  result
 #}}}
end

#description_xml_string_sub(messages, t) ⇒ Object

}}}



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/ruby/riddl/wrapper/description/resource.rb', line 319

def description_xml_string_sub(messages,t)
 #{{{
  result = ''
  @resources.each do |k,r|
    tmp = r.custom.map{ |c| c.dump }.join
    result << t + "<resource"
    result << " relative=\"#{k}\"" unless k == "{}"
    result << (tmp == '' ? "/>\n" : ">\n" + tmp + t + "</resource>\n")
  end
  result
 #}}}
end

#remove_access_methods(des, filter, index) ⇒ Object

TODO add websockets TODO add SSE

Raises:



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

def remove_access_methods(des,filter,index)
  #{{{
  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[index]
    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