Class: RallyAPI::RallyQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/rally_api/rally_query.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_hash = nil) ⇒ RallyQuery

Returns a new instance of RallyQuery.



33
34
35
36
37
38
39
40
41
# 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

Instance Attribute Details

#fetchObject

Returns the value of attribute fetch.



28
29
30
# File 'lib/rally_api/rally_query.rb', line 28

def fetch
  @fetch
end

#limitObject

Returns the value of attribute limit.



29
30
31
# File 'lib/rally_api/rally_query.rb', line 29

def limit
  @limit
end

#orderObject

Returns the value of attribute order.



29
30
31
# File 'lib/rally_api/rally_query.rb', line 29

def order
  @order
end

#page_sizeObject Also known as: pagesize

Returns the value of attribute page_size.



29
30
31
# File 'lib/rally_api/rally_query.rb', line 29

def page_size
  @page_size
end

#projectObject

Returns the value of attribute project.



28
29
30
# File 'lib/rally_api/rally_query.rb', line 28

def project
  @project
end

#project_scope_downObject

Returns the value of attribute project_scope_down.



28
29
30
# File 'lib/rally_api/rally_query.rb', line 28

def project_scope_down
  @project_scope_down
end

#project_scope_upObject

Returns the value of attribute project_scope_up.



28
29
30
# File 'lib/rally_api/rally_query.rb', line 28

def project_scope_up
  @project_scope_up
end

#query_stringObject

Returns the value of attribute query_string.



28
29
30
# File 'lib/rally_api/rally_query.rb', line 28

def query_string
  @query_string
end

#searchObject

Returns the value of attribute search.



28
29
30
# File 'lib/rally_api/rally_query.rb', line 28

def search
  @search
end

#typeObject

Returns the value of attribute type.



28
29
30
# File 'lib/rally_api/rally_query.rb', line 28

def type
  @type
end

#typesObject

Returns the value of attribute types.



28
29
30
# File 'lib/rally_api/rally_query.rb', line 28

def types
  @types
end

#workspaceObject

Returns the value of attribute workspace.



28
29
30
# File 'lib/rally_api/rally_query.rb', line 28

def workspace
  @workspace
end

Instance Method Details

#add_and(current_q, new_conditions) ⇒ Object



115
116
117
118
119
120
# 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) ⇒ Object



108
109
110
111
112
113
# 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) ⇒ Object

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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_paramsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

#validateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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