Class: VersionOne::Query
- Inherits:
-
Object
- Object
- VersionOne::Query
- Defined in:
- lib/version-one/query.rb
Constant Summary collapse
- REQUIRED_FIELDS =
%w{AssetType}.freeze
Class Method Summary collapse
- .issues ⇒ Object
- .primary_work_items ⇒ Object
- .projects ⇒ Object
- .requests ⇒ Object
- .sprints ⇒ Object
- .stories ⇒ Object
Instance Method Summary collapse
- #active ⇒ Object
- #all ⇒ Object
- #asof(date) ⇒ Object
- #asof_query ⇒ Object
- #cache(key, options = {}) ⇒ Object
- #dup(&block) ⇒ Object
- #each(&block) ⇒ Object
- #filter_query ⇒ Object
- #find(what) ⇒ Object
- #find_by_url(url) ⇒ Object
- #first ⇒ Object
- #for_project_and_children(project) ⇒ Object
-
#initialize(_asset_type, _client = nil) ⇒ Query
constructor
A new instance of Query.
- #limit(size) ⇒ Object
- #offset(index) ⇒ Object
- #order(attrib, dir = :asc) ⇒ Object
- #order_query ⇒ Object
- #page_query ⇒ Object
- #select(*fields) ⇒ Object
-
#select_query ⇒ Object
def http_get(uri) xml = nil.
- #to_a ⇒ Object
- #to_url(what = nil) ⇒ Object
- #uri_escape(s) ⇒ Object
- #where(criteria) ⇒ Object
Constructor Details
#initialize(_asset_type, _client = nil) ⇒ 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
.issues ⇒ Object
49 50 51 |
# File 'lib/version-one/query.rb', line 49 def self.issues Query.new 'Issue' end |
.primary_work_items ⇒ Object
33 34 35 |
# File 'lib/version-one/query.rb', line 33 def self.primary_work_items Query.new 'PrimaryWorkitem' end |
.projects ⇒ Object
41 42 43 |
# File 'lib/version-one/query.rb', line 41 def self.projects Query.new 'Scope' end |
.requests ⇒ Object
53 54 55 |
# File 'lib/version-one/query.rb', line 53 def self.requests Query.new 'Request' end |
.sprints ⇒ Object
45 46 47 |
# File 'lib/version-one/query.rb', line 45 def self.sprints Query.new 'Timebox' end |
.stories ⇒ Object
37 38 39 |
# File 'lib/version-one/query.rb', line 37 def self.stories Query.new 'Story' end |
Instance Method Details
#active ⇒ Object
142 143 144 |
# File 'lib/version-one/query.rb', line 142 def active where('IsInactive' => 'false') end |
#all ⇒ Object
170 171 172 |
# File 'lib/version-one/query.rb', line 170 def all find(nil) end |
#asof(date) ⇒ Object
160 161 162 163 164 |
# File 'lib/version-one/query.rb', line 160 def asof(date) dup do @asof = date end end |
#asof_query ⇒ Object
257 258 259 260 261 262 263 |
# File 'lib/version-one/query.rb', line 257 def asof_query if @asof.nil? nil else 'asof=' + @asof.xmlschema end end |
#cache(key, options = {}) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/version-one/query.rb', line 174 def cache(key, ={}) dup do [:namespace] ||= 'VersionOne' @cache = { :key => key, :options => } end 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
188 189 190 |
# File 'lib/version-one/query.rb', line 188 def each(&block) to_a.each(&block) end |
#filter_query ⇒ Object
233 234 235 236 237 238 239 |
# File 'lib/version-one/query.rb', line 233 def filter_query if @filter.empty? nil else 'where=' + uri_escape(@filter.collect{|s| "(#{s})"}.join(';')) end end |
#find(what) ⇒ Object
69 70 71 72 |
# File 'lib/version-one/query.rb', line 69 def find(what) url = to_url(what) find_by_url(url) end |
#find_by_url(url) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/version-one/query.rb', line 57 def find_by_url(url) xml = @client.get(url, cache: @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 |
#first ⇒ Object
166 167 168 |
# File 'lib/version-one/query.rb', line 166 def first limit(1).all.first end |
#for_project_and_children(project) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/version-one/query.rb', line 128 def for_project_and_children(project) project_id = case project when Asset project.id when Integer "Scope:#{project}" when /^(Scope:)?(\d+)(:\d+)?$/ ($1 || 'Scope:') + $2 else raise ArgumentError, 'Invalid project argument' end where('Scope.ParentMeAndUp' => project_id) end |
#limit(size) ⇒ Object
156 157 158 |
# File 'lib/version-one/query.rb', line 156 def limit(size) dup { @limit = size } end |
#offset(index) ⇒ Object
152 153 154 |
# File 'lib/version-one/query.rb', line 152 def offset(index) dup { @offset = index } end |
#order(attrib, dir = :asc) ⇒ Object
146 147 148 149 150 |
# File 'lib/version-one/query.rb', line 146 def order(attrib, dir=:asc) raise ArgumentError unless attrib.is_a? String attrib = '-' + attrib if dir == :desc dup { @order << attrib } end |
#order_query ⇒ Object
249 250 251 252 253 254 255 |
# File 'lib/version-one/query.rb', line 249 def order_query if @order.empty? nil else 'sort=' + uri_escape(@order.join(',')) end end |
#page_query ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/version-one/query.rb', line 241 def page_query if @limit "page=#{@limit},#{@offset || 0}" else nil end end |
#select(*fields) ⇒ Object
108 109 110 111 112 |
# File 'lib/version-one/query.rb', line 108 def select(*fields) dup do @select = @select + fields end end |
#select_query ⇒ Object
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
def cache_store
Rails.cache
end
def can_cache?
@cache && cache_store
end
224 225 226 227 228 229 230 231 |
# File 'lib/version-one/query.rb', line 224 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_a ⇒ Object
184 185 186 |
# File 'lib/version-one/query.rb', line 184 def to_a all end |
#to_url(what = nil) ⇒ Object
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 106 |
# File 'lib/version-one/query.rb', line 74 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
265 266 267 268 |
# File 'lib/version-one/query.rb', line 265 def uri_escape(s) @uri_parser ||= URI::Parser.new @uri_parser.escape(s, /[^A-za-z0-9\-()']/) end |
#where(criteria) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/version-one/query.rb', line 114 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 |