Class: ZendeskAPI::Collection
- Inherits:
-
Object
- Object
- ZendeskAPI::Collection
- Includes:
- Sideloading
- Defined in:
- lib/zendesk_api/collection.rb
Overview
Represents a collection of resources. Lazily loaded, resources aren’t actually fetched until explicitly needed (e.g. #each, #fetch).
Constant Summary collapse
- SPECIALLY_JOINED_PARAMS =
Options passed in that are automatically converted from an array to a comma-separated list.
[:ids, :only]
Instance Attribute Summary collapse
-
#association ⇒ ZendeskAPI::Association
readonly
The class association.
-
#error ⇒ ZendeskAPI::ClientError
readonly
The last response error.
-
#options ⇒ Hash
readonly
Query options.
-
#response ⇒ Faraday::Response
readonly
The last response.
Instance Method Summary collapse
-
#<<(item) ⇒ Object
Adds an item to this collection.
-
#all(start_page = , &block) ⇒ Object
Calls #each on every page with the passed in block.
-
#all!(start_page = , &block) ⇒ Object
Calls #each on every page with the passed in block.
-
#build(opts = {}) ⇒ Object
Convenience method to build a new resource and add it to the collection.
-
#build!(opts = {}) ⇒ Object
Convenience method to build a new resource and add it to the collection.
-
#clear_cache ⇒ Object
Clears all cached resources and associated values.
-
#count ⇒ Number
The total number of resources server-side (disregarding pagination).
-
#count! ⇒ Number
The total number of resources server-side (disregarding pagination).
- #each_page(*args, &block) ⇒ Object
- #each_page!(*args, &block) ⇒ Object
- #fetch(*args) ⇒ Object
-
#fetch!(reload = false) ⇒ Object
Executes actual GET from API and loads resources into proper class.
- #first_page? ⇒ Boolean
-
#include(*sideloads) ⇒ Object
Adds an item (or items) to the list of side-loaded resources to request.
-
#initialize(client, resource, options = {}) ⇒ Collection
constructor
Creates a new Collection instance.
- #last_page? ⇒ Boolean
-
#method_missing(name, *args, &block) ⇒ Object
Sends methods to underlying array of resources.
-
#next ⇒ Object
Find the next page.
-
#page(number) ⇒ Collection
Changes the page option.
-
#path ⇒ Object
The API path to this collection.
-
#per_page(count) ⇒ Collection
Changes the per_page option.
-
#prev ⇒ Object
Find the previous page.
-
#replace(collection) ⇒ Object
Replaces the current (loaded or not) resources with the passed in collection.
- #respond_to?(name) ⇒ Boolean
-
#save ⇒ Collection
Saves all newly created resources stored in this collection.
-
#save! ⇒ Collection
Saves all newly created resources stored in this collection.
-
#to_a ⇒ Object
Alias for fetch(false).
-
#to_a! ⇒ Object
Alias for fetch!(false).
- #to_ary ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Methods included from Sideloading
Constructor Details
#initialize(client, resource, options = {}) ⇒ Collection
Creates a new Collection instance. Does not fetch resources. Additional options are: verb (default: GET), path (default: resource param), page, per_page.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/zendesk_api/collection.rb', line 30 def initialize(client, resource, = {}) @client, @resource_class, @resource = client, resource, resource.resource_name @options = Hashie::Mash.new() join_special_params @verb = @options.delete(:verb) @includes = Array(@options.delete(:include)) # Used for Attachments, TicketComment if @resource_class.is_a?(Class) && @resource_class.superclass == ZendeskAPI::Data @resources = [] @fetchable = false else @fetchable = true end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Sends methods to underlying array of resources.
292 293 294 295 296 297 298 299 300 |
# File 'lib/zendesk_api/collection.rb', line 292 def method_missing(name, *args, &block) if resource_methods.include?(name) collection_method(name, *args, &block) elsif Array.new.respond_to?(name) array_method(name, *args, &block) else next_collection(name, *args, &block) end end |
Instance Attribute Details
#association ⇒ ZendeskAPI::Association (readonly)
Returns The class association.
14 15 16 |
# File 'lib/zendesk_api/collection.rb', line 14 def association @association end |
#error ⇒ ZendeskAPI::ClientError (readonly)
Returns The last response error.
23 24 25 |
# File 'lib/zendesk_api/collection.rb', line 23 def error @error end |
#options ⇒ Hash (readonly)
Returns query options.
20 21 22 |
# File 'lib/zendesk_api/collection.rb', line 20 def @options end |
#response ⇒ Faraday::Response (readonly)
Returns The last response.
17 18 19 |
# File 'lib/zendesk_api/collection.rb', line 17 def response @response end |
Instance Method Details
#<<(item) ⇒ Object
Adds an item to this collection
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/zendesk_api/collection.rb', line 161 def <<(item) fetch if item.is_a?(Resource) if item.is_a?(@resource_class) @resources << item else raise "this collection is for #{@resource_class}" end else @resources << wrap_resource(item, true) end end |
#all(start_page = , &block) ⇒ Object
Calls #each on every page with the passed in block
221 222 223 |
# File 'lib/zendesk_api/collection.rb', line 221 def all(start_page = @options["page"], &block) _all(start_page, &block) end |
#all!(start_page = , &block) ⇒ Object
Calls #each on every page with the passed in block
215 216 217 |
# File 'lib/zendesk_api/collection.rb', line 215 def all!(start_page = @options["page"], &block) _all(start_page, :bang, &block) end |
#build(opts = {}) ⇒ Object
Convenience method to build a new resource and add it to the collection. Fetches the collection as well.
86 87 88 89 90 |
# File 'lib/zendesk_api/collection.rb', line 86 def build(opts = {}) wrap_resource(opts, true).tap do |res| self << res end end |
#build!(opts = {}) ⇒ Object
Convenience method to build a new resource and add it to the collection. Fetches the collection as well.
95 96 97 98 99 100 101 102 |
# File 'lib/zendesk_api/collection.rb', line 95 def build!(opts = {}) wrap_resource(opts, true).tap do |res| fetch! # << does a fetch too self << res end end |
#clear_cache ⇒ Object
Clears all cached resources and associated values.
276 277 278 279 280 281 282 |
# File 'lib/zendesk_api/collection.rb', line 276 def clear_cache @resources = nil @count = nil @next_page = nil @prev_page = nil @query = nil end |
#count ⇒ Number
Returns The total number of resources server-side (disregarding pagination).
105 106 107 108 |
# File 'lib/zendesk_api/collection.rb', line 105 def count fetch @count || -1 end |
#count! ⇒ Number
Returns The total number of resources server-side (disregarding pagination).
111 112 113 114 |
# File 'lib/zendesk_api/collection.rb', line 111 def count! fetch! @count || -1 end |
#each_page(*args, &block) ⇒ Object
230 231 232 233 |
# File 'lib/zendesk_api/collection.rb', line 230 def each_page(*args, &block) warn "ZendeskAPI::Collection#each_page is deprecated, please use ZendeskAPI::Collection#all" all(*args, &block) end |
#each_page!(*args, &block) ⇒ Object
225 226 227 228 |
# File 'lib/zendesk_api/collection.rb', line 225 def each_page!(*args, &block) warn "ZendeskAPI::Collection#each_page! is deprecated, please use ZendeskAPI::Collection#all!" all!(*args, &block) end |
#fetch(*args) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/zendesk_api/collection.rb', line 195 def fetch(*args) fetch!(*args) rescue Faraday::Error::ClientError => e @error = e [] end |
#fetch!(reload = false) ⇒ Object
Executes actual GET from API and loads resources into proper class.
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/zendesk_api/collection.rb', line 182 def fetch!(reload = false) if @resources && (!@fetchable || !reload) return @resources elsif association && association..parent && association..parent.new_record? return (@resources = []) end @response = get_response(@query || self.path) handle_response(@response.body) @resources end |
#first_page? ⇒ Boolean
132 133 134 |
# File 'lib/zendesk_api/collection.rb', line 132 def first_page? !@prev_page end |
#include(*sideloads) ⇒ Object
Adds an item (or items) to the list of side-loaded resources to request
154 155 156 |
# File 'lib/zendesk_api/collection.rb', line 154 def include(*sideloads) self.tap { @includes.concat(sideloads.map(&:to_s)) } end |
#last_page? ⇒ Boolean
136 137 138 |
# File 'lib/zendesk_api/collection.rb', line 136 def last_page? !@next_page || @next_page == @query end |
#next ⇒ Object
Find the next page. Does one of three things:
-
If there is already a page number in the options hash, it increases it and invalidates the cache, returning the new page number.
-
If there is a next_page url cached, it executes a fetch on that url and returns the results.
-
Otherwise, returns an empty array.
247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/zendesk_api/collection.rb', line 247 def next if @options["page"] clear_cache @options["page"] += 1 elsif @query = @next_page fetch(true) else clear_cache @resources = [] end end |
#page(number) ⇒ Collection
Changes the page option. Returns self, so it can be chained. No execution.
126 127 128 129 130 |
# File 'lib/zendesk_api/collection.rb', line 126 def page(number) clear_cache if number @options["page"] = number self end |
#path ⇒ Object
The API path to this collection
176 177 178 |
# File 'lib/zendesk_api/collection.rb', line 176 def path @association.generate_path(:with_parent => true) end |
#per_page(count) ⇒ Collection
Changes the per_page option. Returns self, so it can be chained. No execution.
118 119 120 121 122 |
# File 'lib/zendesk_api/collection.rb', line 118 def per_page(count) clear_cache if count @options["per_page"] = count self end |
#prev ⇒ Object
Find the previous page. Does one of three things:
-
If there is already a page number in the options hash, it increases it and invalidates the cache, returning the new page number.
-
If there is a prev_page url cached, it executes a fetch on that url and returns the results.
-
Otherwise, returns an empty array.
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/zendesk_api/collection.rb', line 263 def prev if @options["page"] && @options["page"] > 1 clear_cache @options["page"] -= 1 elsif @query = @prev_page fetch(true) else clear_cache @resources = [] end end |
#replace(collection) ⇒ Object
Replaces the current (loaded or not) resources with the passed in collection
238 239 240 241 |
# File 'lib/zendesk_api/collection.rb', line 238 def replace(collection) raise "this collection is for #{@resource_class}" if collection.any?{|r| !r.is_a?(@resource_class) } @resources = collection end |
#respond_to?(name) ⇒ Boolean
287 288 289 |
# File 'lib/zendesk_api/collection.rb', line 287 def respond_to?(name) super || Array.new.respond_to?(name) end |
#save ⇒ Collection
Saves all newly created resources stored in this collection.
142 143 144 |
# File 'lib/zendesk_api/collection.rb', line 142 def save _save end |
#save! ⇒ Collection
Saves all newly created resources stored in this collection.
148 149 150 |
# File 'lib/zendesk_api/collection.rb', line 148 def save! _save(:save!) end |
#to_a ⇒ Object
Alias for fetch(false)
204 205 206 |
# File 'lib/zendesk_api/collection.rb', line 204 def to_a fetch end |
#to_a! ⇒ Object
Alias for fetch!(false)
209 210 211 |
# File 'lib/zendesk_api/collection.rb', line 209 def to_a! fetch! end |
#to_ary ⇒ Object
285 |
# File 'lib/zendesk_api/collection.rb', line 285 def to_ary; nil; end |
#to_param ⇒ Object
316 317 318 |
# File 'lib/zendesk_api/collection.rb', line 316 def to_param map(&:to_param) end |
#to_s ⇒ Object Also known as: to_str
303 304 305 306 307 308 309 310 311 312 |
# File 'lib/zendesk_api/collection.rb', line 303 def to_s if @resources @resources.inspect else inspect = [] inspect << "options=#{@options.inspect}" if @options.any? inspect << "path=#{path}" "#{Inflection.singular(@resource)} collection [#{inspect.join(",")}]" end end |