Class: Riddl::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/riddl/wrapper.rb,
lib/ruby/riddl/wrapper.rb,
lib/ruby/riddl/wrapper/declaration.rb,
lib/ruby/riddl/wrapper/description.rb,
lib/ruby/riddl/wrapper/layerchecker.rb,
lib/ruby/riddl/wrapper/messageparser.rb,
lib/ruby/riddl/wrapper/resourcechecker.rb,
lib/ruby/riddl/wrapper/declaration/tile.rb,
lib/ruby/riddl/wrapper/declaration/facade.rb,
lib/ruby/riddl/wrapper/description/access.rb,
lib/ruby/riddl/wrapper/description/resource.rb,
lib/ruby/riddl/wrapper/declaration/interface.rb,
lib/ruby/riddl/wrapper/description/message_and_transformation.rb

Defined Under Namespace

Classes: Declaration, Description, IOMessages, LayerChecker, MessageParser, ResourceChecker, WrapperUtils

Constant Summary collapse

VERSION_MAJOR =

{{{

1
VERSION_MINOR =
0
VERSION =
"#{VERSION_MAJOR}.#{VERSION_MINOR}"
DESCRIPTION =
"http://riddl.org/ns/description/#{VERSION}"
DECLARATION =
"http://riddl.org/ns/declaration/#{VERSION}"
XINCLUDE =
"http://www.w3.org/2001/XInclude"
DESCRIPTION_FILE =
"#{File.dirname(__FILE__)}/ns/description/#{VERSION_MAJOR}.#{VERSION_MINOR}/description.rng"
DECLARATION_FILE =
"#{File.dirname(__FILE__)}/ns/declaration/#{VERSION_MAJOR}.#{VERSION_MINOR}/declaration.rng"
RIDDL_DESCRIPTION_SHOW =
"#{File.dirname(__FILE__)}/ns/common-patterns/riddl-description/show.xml"
RIDDL_DESCRIPTION_RESOURCE_SHOW =
"#{File.dirname(__FILE__)}/ns/common-patterns/riddl-description/resource-show.xml"
COMMON =
"datatypeLibrary=\"http://www.w3.org/2001/XMLSchema-datatypes\" xmlns=\"#{DESCRIPTION}\" xmlns:xi=\"http://www.w3.org/2001/XInclude\""
CHECK_DATA =
"<element name=\"check\" datatypeLibrary=\"http://www.w3.org/2001/XMLSchema-datatypes\" xmlns=\"http://relaxng.org/ns/structure/1.0\"><data/></element>"
CHECK_CHOICE =
"<element name=\"check\" datatypeLibrary=\"http://www.w3.org/2001/XMLSchema-datatypes\" xmlns=\"http://relaxng.org/ns/structure/1.0\"></element>"

Instance Method Summary collapse

Constructor Details

#initialize(name, get_description = false) ⇒ Wrapper

}}}



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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ruby/riddl/wrapper.rb', line 69

def initialize(name,get_description=false)
  #{{{
  @doc = nil

  fpath = nil
  if name.is_a?(XML::Smart::Dom)
    @doc = name
  else
    if name.is_a?(String) && name =~ /^http:\/\/(www\.)?riddl\.org(\/ns\/common-patterns\/.*)/
      name = File.expand_path(File.dirname(__FILE__)) + $2
    end
    begin
      if name.respond_to?(:read)
        fpath = File.dirname(name.path) if name.is_a?(File) || name.is_a?(Tempfile)
        xml = name.read
        name.close
      elsif File.exist?(name)
        fpath = File.dirname(name)
        xml = File.read(name)
      else
        fpath = nil
        xml = Typhoeus.get(name, ssl_verifypeer: false).response_body
      end
      @doc = XML::Smart.string(xml)
    rescue
      begin
        @doc = XML::Smart.string(name)
      rescue
        raise SpecificationError, "#{name.inspect} is no RIDDL description or declaration (neither a file, url or string)."
      end
    end
  end

  @doc.register_namespace 'x', XINCLUDE
  @doc.find('//x:include/@href').each do |i|
    if i.value =~ /^http:\/\/(www\.)?riddl\.org(\/ns\/common-patterns\/.*)/
      t = File.expand_path(File.dirname(__FILE__)) + $2
      i.value = t if File.exists?(t)
    end
  end
  fpath.nil? ? @doc.xinclude! : @doc.xinclude!(fpath)

  qname = @doc.root.qname
  @is_description = qname.href == DESCRIPTION && qname.name ==  'description'
  @is_declaration = qname.href == DECLARATION && qname.name ==  'declaration'

  @doc.register_namespace 'des', DESCRIPTION
  @doc.register_namespace 'dec', DECLARATION
  if @is_description && get_description
    rds = XML::Smart::open_unprotected(RIDDL_DESCRIPTION_SHOW)
    rrds = XML::Smart::open_unprotected(RIDDL_DESCRIPTION_RESOURCE_SHOW)
    rds.register_namespace('des','http://riddl.org/ns/description/1.0')
    rrds.register_namespace('des','http://riddl.org/ns/description/1.0')
    @doc.root.prepend rds.find('/des:description/des:message')
    @doc.root.prepend rrds.find('/des:description/des:message')
    @doc.find("/des:description/des:resource").each do |r|
      r.prepend rds.find('/des:description/des:resource/*')
    end
    @doc.find("/des:description//des:resource").each do |r|
      r.prepend rrds.find('/des:description/des:resource/*')
    end
  end
  if @is_declaration  && get_description
    @doc.root.prepend("dec:interface",:name=>"riddldescription").add XML::Smart::open_unprotected(RIDDL_DESCRIPTION_SHOW).root
    @doc.root.prepend("dec:interface",:name=>"riddlresourcedescription").add XML::Smart::open_unprotected(RIDDL_DESCRIPTION_RESOURCE_SHOW).root
    @doc.root.find("dec:facade").first.append('dec:tile').append("layer",:name => "riddldescription")
    @doc.root.find("dec:facade").first.append('dec:tile').append("layer",:name => "riddlresourcedescription").tap do |x|
      x.append("apply-to","/")
      x.append("apply-to","/**/*")
    end
  end

  @declaration = @description = nil
  #}}}
end

Instance Method Details

#check_message(params, headers, message) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/ruby/riddl/wrapper.rb', line 236

def check_message(params,headers,message)
  description
  declaration
  #{{{
  return true if message.class == Riddl::Wrapper::Description::Star
  return true if message.nil? && params == []
  mp = MessageParser.new(params,headers)
  mp.check(message,true)
  #}}}
end

#declarationObject



145
146
147
148
149
150
151
152
# File 'lib/ruby/riddl/wrapper.rb', line 145

def declaration
  #{{{
  if @declaration.nil? && @is_declaration
    @declaration = Riddl::Wrapper::Declaration.new(@doc)
  end
  @declaration
  #}}}
end

#declaration?Boolean

Returns:

  • (Boolean)


331
# File 'lib/ruby/riddl/wrapper.rb', line 331

def declaration?; @is_declaration; end

#descriptionObject



154
155
156
157
158
159
160
161
# File 'lib/ruby/riddl/wrapper.rb', line 154

def description
  #{{{
  if @description.nil? && @is_description
    @description = Riddl::Wrapper::Description.new(@doc)
  end
  @description
  #}}}
end

#description?Boolean

Returns:

  • (Boolean)


332
# File 'lib/ruby/riddl/wrapper.rb', line 332

def description?; @is_description; end

#io_messages(path, operation, params, headers) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/ruby/riddl/wrapper.rb', line 177

def io_messages(path,operation,params,headers)
  #{{{
  description
  declaration

  req = @description.get_resource(path).access_methods if @is_description
  req = @declaration.get_resource(path).composition    if @is_declaration

  mp = MessageParser.new(params,headers)
  if req.has_key?(operation)
    if @is_description
      r = req[operation][0]
      r.select{|o|o.class==Riddl::Wrapper::Description::RequestInOut}.each do |o|
        return IOMessages.new(o.in, o.out) if mp.check(o.in)
      end
      r.select{|o|o.class==Riddl::Wrapper::Description::RequestTransformation}.each do |o|
        # TODO guess structure from input, create new output structure
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, Riddl::Wrapper::Description::Star.new)
      end
      r.select{|o|o.class==Riddl::Wrapper::Description::RequestStarOut}.each do |o|
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, o.out)
      end
      r.select{|o|o.class==Riddl::Wrapper::Description::RequestPass}.each do |o|
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, Riddl::Wrapper::Description::Star.new)
      end
      r.select{|o|o.class==Riddl::Wrapper::Description::WebSocket}.each do |o|
        return IOMessages.new(nil,nil,nil,nil)
      end
      r.select{|o|o.class==Riddl::Wrapper::Description::SSE}.each do |o|
        return IOMessages.new(nil,nil,nil,nil)
      end
    end
    if @is_declaration
      r = req[operation]
      r.select{|o|o.result.class==Riddl::Wrapper::Description::RequestInOut}.each do |o|
        return IOMessages.new(o.result.in, o.result.out, o.route, o.result.interface) if mp.check(o.result.in)
      end
      r.select{|o|o.result.class==Riddl::Wrapper::Description::RequestTransformation}.each do |o|
        # TODO guess structure from input, create new output structure
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, Riddl::Wrapper::Description::Star.new, o.route, o.result.interface)
      end
      r.select{|o|o.result.class==Riddl::Wrapper::Description::RequestStarOut}.each do |o|
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, o.result.out, o.route, o.result.interface)
      end
      r.select{|o|o.result.class==Riddl::Wrapper::Description::RequestPass}.each do |o|
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, Riddl::Wrapper::Description::Star.new, o.route, o.result.interface)
      end
      r.select{|o|o.result.class==Riddl::Wrapper::Description::WebSocket}.each do |o|
        return IOMessages.new(nil,nil,nil,o.result.interface)
      end
      r.select{|o|o.result.class==Riddl::Wrapper::Description::SSE}.each do |o|
        return IOMessages.new(nil,nil,nil,o.result.interface)
      end
    end
  end
  return nil
  #}}}
end

#load_necessary_handlers!Object



265
266
267
268
269
270
271
272
273
# File 'lib/ruby/riddl/wrapper.rb', line 265

def load_necessary_handlers!
  #{{{
  @doc.find("//des:parameter/@handler").map{|h|h.to_s}.uniq.each do |h|
    if File.exist?(File.dirname(__FILE__) + '/handlers/' + File.basename(h) + ".rb")
      require File.expand_path(File.dirname(__FILE__) + '/handlers/' + File.basename(h))
    end
  end
  #}}}
end

#load_necessary_roles!Object

}}}



274
275
276
277
278
279
280
281
282
283
# File 'lib/ruby/riddl/wrapper.rb', line 274

def load_necessary_roles!
  #{{{
  @doc.find("//des:resource/@role").map{|h|h.to_s}.uniq.each do |h|
    h = Protocols::Utils::escape(h)
    if File.exists?(File.dirname(__FILE__) + '/roles/' + h + '.rb')
      require File.expand_path(File.dirname(__FILE__) + '/roles/' + h)
    end
  end
  #}}}
end

#pathsObject



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/ruby/riddl/wrapper.rb', line 285

def paths
  #{{{
  description
  declaration
  tmp = []

  tmp = @description.paths if @is_description
  tmp = @declaration.paths if @is_declaration
  tmp.map do |t|
    [t[0],Regexp.new("^" + t[1].gsub(/\{\}/,"[^/]+") + (t[2] ? '\/?' : '\/?$'))]
  end
  #}}}
end

#resource_description(path) ⇒ Object

}}}



171
172
173
174
175
# File 'lib/ruby/riddl/wrapper.rb', line 171

def resource_description(path)
  req = @description.get_resource(path).description_xml(@doc.namespaces) if @is_description
  req = @declaration.get_resource(path).description_xml(@doc.namespaces) if @is_declaration
  req.to_s
end

#role(path) ⇒ Object

{{{



163
164
165
166
167
168
169
# File 'lib/ruby/riddl/wrapper.rb', line 163

def role(path) #{{{
  description
  declaration
  return @description.get_resource(path).role if @is_description
  return @declaration.get_resource(path).role if @is_declaration
  nil
end

#validate!Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/ruby/riddl/wrapper.rb', line 247

def validate!
  #{{{
  if @is_description
    xval = @doc.validate_against(XML::Smart.open_unprotected(DESCRIPTION_FILE))
    xchk = ResourceChecker.new(@doc).check
    puts xchk.join("\n") unless xchk.empty?
    return xval && xchk.empty?
  end
  if @is_declaration
    xval = @doc.validate_against(XML::Smart.open_unprotected(DECLARATION_FILE))
    xchk = LayerChecker.new(@doc).check
    puts xchk.join("\n") unless xchk.empty?
    return xval && xchk.empty?
  end
  nil
  #}}}
end