Class: VersionOne::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/version-one/query.rb

Constant Summary collapse

REQUIRED_FIELDS =
%w{AssetType}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_asset_type, _client = nil) ⇒ Query

Returns a new instance of Query.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/version-one/query.rb', line 6

def initialize(_asset_type, _client=nil)
  @asset_type = _asset_type
  @select = []
  @filter = []
  @order = []
  @cache = nil
  @asof = nil
  @limit = nil
  @offset = nil
  @client = VersionOne::Client.new
end

Class Method Details

.issuesObject



49
50
51
# File 'lib/version-one/query.rb', line 49

def self.issues
  Query.new 'Issue'
end

.primary_work_itemsObject



33
34
35
# File 'lib/version-one/query.rb', line 33

def self.primary_work_items
  Query.new 'PrimaryWorkitem'
end

.projectsObject



41
42
43
# File 'lib/version-one/query.rb', line 41

def self.projects
  Query.new 'Scope'
end

.sprintsObject



45
46
47
# File 'lib/version-one/query.rb', line 45

def self.sprints
  Query.new 'Timebox'
end

.storiesObject



37
38
39
# File 'lib/version-one/query.rb', line 37

def self.stories
  Query.new 'Story'
end

Instance Method Details

#activeObject



131
132
133
# File 'lib/version-one/query.rb', line 131

def active
  where("IsInactive='false'")
end

#allObject



159
160
161
# File 'lib/version-one/query.rb', line 159

def all
  find(nil)
end

#asof(date) ⇒ Object



149
150
151
152
153
# File 'lib/version-one/query.rb', line 149

def asof(date)
  dup do
    @asof = date
  end
end

#asof_queryObject



245
246
247
248
249
250
251
# File 'lib/version-one/query.rb', line 245

def asof_query
  if @asof.nil?
    nil
  else
    'asof=' + @asof.xmlschema
  end
end

#cache(key, options = {}) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/version-one/query.rb', line 163

def cache(key, options={})
  dup do
    @cache = {
        :key => key,
        :options => options
    }
  end
end

#cache_storeObject



204
205
206
# File 'lib/version-one/query.rb', line 204

def cache_store
  Rails.cache
end

#can_cache?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/version-one/query.rb', line 208

def can_cache?
  @cache && cache_store
end

#dup(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/version-one/query.rb', line 18

def dup(&block)
  q = Query.new(@asset_type)

  [:@select, :@filter, :@order, :@asof, :@limit, :@offset, :@cache, :@client].each do |sym|
    val = instance_variable_get(sym)
    unless val.nil?
      val = val.dup if val.is_a? Array
      q.instance_variable_set(sym,  val)
    end
  end

  q.instance_eval &block
  q
end

#each(&block) ⇒ Object



176
177
178
# File 'lib/version-one/query.rb', line 176

def each(&block)
  to_a.each(&block)
end

#filter_queryObject



221
222
223
224
225
226
227
# File 'lib/version-one/query.rb', line 221

def filter_query
  if @filter.empty?
    nil
  else
    'where=' + uri_escape(@filter.collect{|s| "(#{s})"}.join(';'))
  end
end

#find(what) ⇒ Object



68
69
70
71
# File 'lib/version-one/query.rb', line 68

def find(what)
  url = to_url(what)
  find_by_url(url)
end

#find_by_url(url) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/version-one/query.rb', line 53

def find_by_url(url)
  xml = nil
  #xml = cache_store.read(@cache[:key]) if can_cache?
  xml ||= @client.get(url)
  #cache_store.write(@cache[:key], xml, @cache[:options]) if can_cache?

  if xml.name == 'Error'
    msg = 'VersionOne Error: %s (%s)' % [xml.find_first('Message').content, xml.attributes['href']]
    raise msg
  else
    VersionOne::Asset.from_xml(xml)
  end

end

#firstObject



155
156
157
# File 'lib/version-one/query.rb', line 155

def first
  limit(1).all.first
end

#for_project_and_children(project_id) ⇒ Object



127
128
129
# File 'lib/version-one/query.rb', line 127

def for_project_and_children(project_id)
  where("Scope.ParentMeAndUp='Scope:#{project_id}'")
end

#http_get(uri) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/version-one/query.rb', line 182

def http_get(uri)
  xml = nil

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request_path = uri.path
  request_path += '?' + uri.query unless uri.query.blank?
  Rails.logger.debug("Uri path = #{request_path}")

  http.start do
    request = Net::HTTP::Get.new(request_path)
    request.basic_auth(VersionOne.user, VersionOne.password)
    response = http.request(request)

    xml = response.body
    Rails.logger.debug(xml)
  end

  xml
end

#limit(size) ⇒ Object



145
146
147
# File 'lib/version-one/query.rb', line 145

def limit(size)
  dup { @limit = size }
end

#offset(index) ⇒ Object



141
142
143
# File 'lib/version-one/query.rb', line 141

def offset(index)
  dup { @offset = index }
end

#order(attrib, dir = :asc) ⇒ Object

Raises:

  • (ArgumentError)


135
136
137
138
139
# File 'lib/version-one/query.rb', line 135

def order(attrib, dir=:asc)
  raise ArgumentError unless attrib.is_a? String
  attrib = '-' + attrib if dir == :desc
  dup { @order << attrib }
end

#order_queryObject



237
238
239
240
241
242
243
# File 'lib/version-one/query.rb', line 237

def order_query
  if @order.empty?
    nil
  else
    'sort=' + uri_escape(@order.join(','))
  end
end

#page_queryObject



229
230
231
232
233
234
235
# File 'lib/version-one/query.rb', line 229

def page_query
  if @limit
    "page=#{@limit},#{@offset || 0}"
  else
    nil
  end
end

#select(*fields) ⇒ Object



107
108
109
110
111
# File 'lib/version-one/query.rb', line 107

def select(*fields)
  dup do
    @select = @select + fields
  end
end

#select_queryObject



212
213
214
215
216
217
218
219
# File 'lib/version-one/query.rb', line 212

def select_query
  if @select.empty?
    nil
  else
    REQUIRED_FIELDS.each {|f| @select << f unless @select.include?(f) }
    'sel=' + uri_escape(@select.join(','))
  end
end

#to_aObject



172
173
174
# File 'lib/version-one/query.rb', line 172

def to_a
  all
end

#to_url(what = nil) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/version-one/query.rb', line 73

def to_url(what=nil)
  what = what.id.to_s if what.is_a?(Asset)

  what = case what
           when NilClass
             what
           when Integer
             what.to_s
           when /^[A-Z]+((?::\d+)+)$/i
             $1.gsub(':', '/')
           else
             :bad
         end

  raise ArgumentError, 'Invalid parameter type' if what == :bad

  url = ['rest-1.v1/Data', @asset_type, what].compact.join('/')

  query = [
      select_query,
      filter_query,
      page_query,
      order_query,
      asof_query
  ].compact.join('&')

  if query && !query.empty?
    url << '?'
    url << query
  end

  url
end

#uri_escape(s) ⇒ Object



253
254
255
256
# File 'lib/version-one/query.rb', line 253

def uri_escape(s)
  @uri_parser ||= URI::Parser.new
  @uri_parser.escape(s, /[^A-za-z0-9\-()']/)
end

#where(criteria) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/version-one/query.rb', line 113

def where(criteria)
  criteria = case criteria
              when String
                [criteria]
              when Hash
                criteria.map{|k,v| "#{k}='#{v.to_s}'"}
              else
                raise ArgumentError
  end
  dup do
    @filter.concat criteria
  end
end