class RallyAPI::RallyQuery

RallyAPI::RallyQuery - A helper class for making queries to Rally’s REST API

Example: new_query = RallyAPI::RallyQuery.new() and set query properties as needed

--- or ---

new_query = RallyAPI::RallyQuery.new(query_hash) with a hash of attributes
query_hash for example can be:
query_hash = {}
query_hash = Defect, Story, etc
query_hash = “(State = "Closed")”
query_hash = “Name,State,etc”
query_hash = workspace json object or ref defaults to workspace passed in RallyRestJson.new if nil
query_hash = project json object or ref defaults to project passed in RallyRestJson.new if nil
query_hash = true/false
query_hash = true/false
query_hash = “ObjectID asc”
query_hash
query_hash

Attributes

fetch[RW]
limit[RW]
order[RW]
page_size[RW]
pagesize[RW]
project[RW]
project_scope_down[RW]
project_scope_up[RW]
query_string[RW]
type[RW]
types[RW]
workspace[RW]

Public Class Methods

new(query_hash = nil) click to toggle source
# File lib/rally_api/rally_query.rb, line 33
def initialize(query_hash = nil)
  parse_query_hash(query_hash) if !query_hash.nil?
  @page_size          = 200 if @page_size.nil?
  @limit              = 99999 if @limit.nil?
  @project_scope_up   = false if @project_scope_up.nil?
  @project_scope_down = false if @project_scope_down.nil?
  @page_size = @limit if @page_size > @limit
  self
end

Public Instance Methods

add_and(current_q, new_conditions) click to toggle source
# File lib/rally_api/rally_query.rb, line 115
def add_and(current_q, new_conditions)
  return current_q if new_conditions.nil? || new_conditions.empty?
  return new_conditions if current_q.nil? || current_q.empty?
  new_conditions = "(#{new_conditions})" if new_conditions[0] != "("
  "(#{current_q} AND #{new_conditions})"
end
add_or(current_q, new_conditions) click to toggle source
# File lib/rally_api/rally_query.rb, line 108
def add_or(current_q, new_conditions)
  return current_q if (new_conditions.nil? || new_conditions.empty?)
  return new_conditions if (current_q.nil? || current_q.empty?)
  new_conditions = "(#{new_conditions})" if new_conditions[0] != "("
  "(#{current_q} OR #{new_conditions})"
end
build_query_segment(condition_array, op) click to toggle source

support the crazy query string structure for the api each condition with an and or an or needs to be wrapped rpn style in ()

# File lib/rally_api/rally_query.rb, line 87
def build_query_segment(condition_array, op)
  return nil if condition_array.length == 0
  return condition_array.first if condition_array.length == 1

  op = op.downcase  #should be or or and

  query_segment = ""
  condition_array.each do |condition|
    q_part = "(#{condition})" if condition[0] != "("
    case op
      when 'or'
        query_segment = add_or(query_segment, q_part)
      when 'and'
        query_segment = add_and(query_segment, q_part)
      else
    end
  end

  return query_segment
end
make_query_params() click to toggle source
# File lib/rally_api/rally_query.rb, line 43
def make_query_params
  query_params = {}
  query_params[:query]            = @query_string         # unless @query_string.nil?
  query_params[:fetch]            = @fetch                unless @fetch.nil?
  query_params[:workspace]        = @workspace["_ref"]    if !@workspace.nil?
  query_params[:project]          = @project["_ref"]      if !@project.nil?
  query_params[:projectScopeUp]   = @project_scope_up     unless @project_scope_up.nil?
  query_params[:projectScopeDown] = @project_scope_down   unless @project_scope_down.nil?
  query_params[:order]            = @order                unless @order.nil?
  query_params[:pagesize]         = @page_size            unless @page_size.nil?
  query_params[:search]           = @search               unless @search.nil?
  query_params[:types]            = @types                unless @types.nil?

  query_params
end
validate() click to toggle source
# File lib/rally_api/rally_query.rb, line 59
def validate()
  errors = []

  if @type.nil? || type == ""
    errors.push("Object type for query cannot be nil")
  end

  if @limit < 0
    errors.push("Stop after - #{@stop_after} - must be a number")
  end

  if @page_size < 0
    errors.push("Page size - #{@page_size} - must be a number")
  end

  if !@workspace.nil?
    errors.push("Workspace - #{@workspace} - must have a ref") if @workspace["_ref"].nil?
  end

  if !@project.nil?
    errors.push("Project - #{@project} - must have a ref") if @project["_ref"].nil?
  end

  errors
end

Private Instance Methods

parse_query_hash(query_hash) click to toggle source
# File lib/rally_api/rally_query.rb, line 124
def parse_query_hash(query_hash)
  @type               = query_hash[:type].to_s
  @query_string       = query_hash[:query_string]
  @fetch              = query_hash[:fetch]
  @project_scope_down = query_hash[:project_scope_down]
  @project_scope_up   = query_hash[:project_scope_up]
  @order              = query_hash[:order]
  @page_size          = query_hash[:page_size]
  @limit              = query_hash[:limit]
  @workspace          = query_hash[:workspace]
  @project            = query_hash[:project]
end