class Riddl::Wrapper

Constants

CHECK_CHOICE
CHECK_DATA
COMMON
DECLARATION
DECLARATION_FILE
DESCRIPTION
DESCRIPTION_FILE
RIDDL_DESCRIPTION_RESOURCE_SHOW
RIDDL_DESCRIPTION_SHOW
VERSION
VERSION_MAJOR

{{{

VERSION_MINOR
XINCLUDE

Public Class Methods

new(name,get_description=false) click to toggle source

}}}

# 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.exist?(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

Public Instance Methods

check_message(params,headers,message) click to toggle source
# 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
declaration() click to toggle source
# 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?() click to toggle source
# File lib/ruby/riddl/wrapper.rb, line 331
def declaration?; @is_declaration; end
description() click to toggle source
# 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?() click to toggle source
# File lib/ruby/riddl/wrapper.rb, line 332
def description?; @is_description; end
io_messages(path,operation,params,headers) click to toggle source
# 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!() click to toggle source
# 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!() click to toggle source
# 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.exist?(File.dirname(__FILE__) + '/roles/' + h + '.rb')
      require File.expand_path(File.dirname(__FILE__) + '/roles/' + h)
    end
  end
  #}}}
end
paths() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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!() click to toggle source
# 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