class SparqlDoc::Query

Wrapper for SPARQL query

Constants

ANNOTATIONS

Attributes

package[R]
path[R]
prefixes[R]
query[R]
raw_query[R]
type[R]

Public Class Methods

new(path, query, package={}) click to toggle source
# File lib/sparql-doc/query.rb, line 33
def initialize(path, query, package={})
  ANNOTATIONS.each do |var, config|
    if config[:multi]
      instance_variable_set( "@#{var}", [] )
    else
      instance_variable_set( "@#{var}", "" )
    end
  end
  @path = path
  @query = query
  @raw_query = query
  @title = @path
  @description = ""
  @prefixes = {}

  ["endpoint", "author", "tag"].each do |annotation|
    if package[annotation]
      instance_variable_set( "@#{annotation}", package[annotation])
    end
  end
  parseQuery()
end

Public Instance Methods

description(html=false) click to toggle source
# File lib/sparql-doc/query.rb, line 60
def description(html=false)
  if html
    renderer = Redcarpet::Render::HTML.new({})
    markdown = Redcarpet::Markdown.new(renderer, {})
    return markdown.render(@description)
  end
  @description
end
output_filename() click to toggle source
# File lib/sparql-doc/query.rb, line 56
def output_filename
  return "#{path.gsub(".rq", "")}.html"
end
query_string() click to toggle source
# File lib/sparql-doc/query.rb, line 69
def query_string
  CGI::escape( @query )
end

Private Instance Methods

parseQuery() click to toggle source
# File lib/sparql-doc/query.rb, line 75
def parseQuery
  query_lines = []
  header = true
  description = false
  description_lines = []
  @raw_query.split("\n").each do |line|
    if ( header && line.match(/^#/) )
      if ( matches = line.match(/^# *@([a-zA-Z]+) *(.+)$/i) )
        annotation = matches[1]
        config = ANNOTATIONS[ annotation.intern ]
        if config
          if config[:multi]
            val = instance_variable_get("@#{annotation}")
            val << matches[2].strip
          else
            instance_variable_set("@#{annotation}", matches[2].strip)
          end
          description = true
        else
          $stderr.puts("Ignoring unknown annotation: @#{annotation}")
        end
      else
        if (description == false)
          description_lines << line[1..-1].strip
        end
      end
    else
      header = false
      query_lines << line
    end
    @description = description_lines.join("\n") unless description_lines.empty?
    @query = query_lines.join("\n") unless query_lines.empty?
    @query.strip!
    query_lines.each do |line|
      if (matches = line.match(/^ *PREFIX *([a-zA-Z_-]+) *: *<(.+)>$/i) )
        @prefixes[ matches[1] ] = matches[2]
      end
      if (matches = line.match(/^ *(SELECT|CONSTRUCT|DESCRIBE|ASK) */i) )
        @type = matches[1].upcase
      end
    end
  end
end