class Razor::CLI::Query

Public Class Methods

new(parse, navigate, collections, segments) click to toggle source
# File lib/razor/cli/query.rb, line 2
def initialize(parse, navigate, collections, segments)
  @parse = parse
  @navigate = navigate
  @collections = collections
  @segments = segments
  @stripped_segments = []
  @options = {}
end

Public Instance Methods

get_optparse(doc, nav) click to toggle source
# File lib/razor/cli/query.rb, line 15
def get_optparse(doc, nav)
  # If the last document is an Array, we need to find
  # which element matches the given query. Once found,
  # return the 'params' section, if it has one.
  if doc.is_a?(Array)
    query = doc.find {|coll| coll['name'] == nav}
    params = (query && query['params']) || {}
  elsif doc.is_a?(Hash)
    params = (doc[nav].is_a?(Hash) && doc[nav].has_key?('params') &&
        doc[nav]['params'].is_a?(Hash) && doc[nav]['params']) || {}
  end
  @queryoptparse = OptionParser.new do |opts|
    opts.on "-f", "--full", _("Show full details when viewing entities") do
      @parse.format = 'full'
    end

    opts.on "-s", "--short", _("Show shortened details when viewing entities") do
      @parse.format = 'short'
    end

    params.each do |param, args|
      if param == 'depth'
        # Set the depth parameter if the server offers it
        @options[param] = 1
        next
      end

      if args['type'] == 'boolean'
        opts.on "--#{param}" do
          @options[param] = true
        end
      else
        opts.on "--#{param} VALUE" do |value|
          @options[param] = value
        end
      end
    end
  end
end
run() click to toggle source
# File lib/razor/cli/query.rb, line 55
def run
  @doc = @collections
  while @segments.any?
    nav = @segments.shift
    @parse.stripped_args << nav

    @options = {}
    @segments = get_optparse(@doc, nav).order(@segments)

    # Adding the depth parameter to an intermediate request is an
    # inefficiency, so we should delete it.
    @options.delete('depth') if @segments.any?

    @doc = @navigate.move_to nav, @doc, @options
  end

  # Get the next level if it's a list of objects.
  if @doc.is_a?(Hash) and @doc['items'].is_a?(Array)
    # If our nav endpoint had a depth parameter, then we will already have
    # the next level.
    return @doc if @options['depth']

    # Cache doc_resource since these queries are just for extra detail.
    temp_doc_resource = @navigate.doc_resource
    @doc['items'] = @doc['items'].map do |item|
      item.is_a?(Hash) && item.has_key?('id') ? @navigate.json_get(URI.parse(item['id'])) : item
    end
    @navigate.doc_resource = temp_doc_resource
  end
  @doc
end
stripped_segments() click to toggle source
# File lib/razor/cli/query.rb, line 11
def stripped_segments
  @stripped_segments.join
end