Class: ZendeskAPI::Collection
- Inherits:
-
Object
- Object
- ZendeskAPI::Collection
- 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
- SPECIALLY_JOINED_PARAMS =
Options passed in that are automatically converted from an array to a comma-separated list.
[:ids, :only]
Instance Attribute Summary (collapse)
-
- (ZendeskAPI::Association) association
readonly
The class association.
-
- (Hash) options
readonly
Query options.
-
- (Faraday::Response) response
readonly
The last response.
Instance Method Summary (collapse)
-
- (Object) <<(item)
Adds an item to this collection.
-
- (Object) all(start_page = @options["page"], &block)
Calls #each on every page with the passed in block.
-
- (Object) all!(start_page = @options["page"], &block)
Calls #each on every page with the passed in block.
-
- (Object) clear_cache
Clears all cached resources and associated values.
-
- (Number) count
The total number of resources server-side (disregarding pagination).
-
- (Number) count!
The total number of resources server-side (disregarding pagination).
- - (Object) each_page(*args, &block)
- - (Object) each_page!(*args, &block)
- - (Object) fetch(*args)
-
- (Object) fetch!(reload = false)
Executes actual GET from API and loads resources into proper class.
- - (Boolean) first_page?
-
- (Object) include(*sideloads)
Adds an item (or items) to the list of side-loaded resources to request.
-
- (Collection) initialize(client, resource, options = {})
constructor
Creates a new Collection instance.
- - (Boolean) last_page?
-
- (Object) method_missing(name, *args, &block)
Sends methods to underlying array of resources.
-
- (Object) next
Find the next page.
-
- (Collection) page(number)
Changes the page option.
-
- (Object) path
The API path to this collection.
-
- (Collection) per_page(count)
Changes the per_page option.
-
- (Object) prev
Find the previous page.
-
- (Object) replace(collection)
Replaces the current (loaded or not) resources with the passed in collection.
-
- (Collection) save
Saves all newly created resources stored in this collection.
-
- (Collection) save!
Saves all newly created resources stored in this collection.
-
- (Object) to_a
Alias for fetch(false).
-
- (Object) to_a!
Alias for fetch!(false).
Constructor Details
- (Collection) initialize(client, resource, options = {})
Creates a new Collection instance. Does not fetch resources. Additional options are: verb (default: GET), path (default: resource param), page, per_page.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/zendesk_api/collection.rb', line 27 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
- (Object) method_missing(name, *args, &block)
Sends methods to underlying array of resources.
245 246 247 248 249 250 251 252 253 |
# File 'lib/zendesk_api/collection.rb', line 245 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
- (ZendeskAPI::Association) association (readonly)
The class association
14 15 16 |
# File 'lib/zendesk_api/collection.rb', line 14 def association @association end |
- (Hash) options (readonly)
Query options
20 21 22 |
# File 'lib/zendesk_api/collection.rb', line 20 def @options end |
- (Faraday::Response) response (readonly)
The last response
17 18 19 |
# File 'lib/zendesk_api/collection.rb', line 17 def response @response end |
Instance Method Details
- (Object) <<(item)
Adds an item to this collection
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/zendesk_api/collection.rb', line 120 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 item.merge!(:association => @association) if item.is_a?(Hash) @resources << @resource_class.new(@client, item) end end |
- (Object) all(start_page = @options["page"], &block)
Calls #each on every page with the passed in block
179 180 181 |
# File 'lib/zendesk_api/collection.rb', line 179 def all(start_page = @options["page"], &block) _all(start_page, &block) end |
- (Object) all!(start_page = @options["page"], &block)
Calls #each on every page with the passed in block
173 174 175 |
# File 'lib/zendesk_api/collection.rb', line 173 def all!(start_page = @options["page"], &block) _all(start_page, :bang, &block) end |
- (Object) clear_cache
Clears all cached resources and associated values.
234 235 236 237 238 239 |
# File 'lib/zendesk_api/collection.rb', line 234 def clear_cache @resources = nil @count = nil @next_page = nil @prev_page = nil end |
- (Number) count
The total number of resources server-side (disregarding pagination).
64 65 66 67 |
# File 'lib/zendesk_api/collection.rb', line 64 def count fetch @count || -1 end |
- (Number) count!
The total number of resources server-side (disregarding pagination).
70 71 72 73 |
# File 'lib/zendesk_api/collection.rb', line 70 def count! fetch! @count || -1 end |
- (Object) each_page(*args, &block)
188 189 190 191 |
# File 'lib/zendesk_api/collection.rb', line 188 def each_page(*args, &block) warn "ZendeskAPI::Collection#each_page is deprecated, please use ZendeskAPI::Collection#all" all(*args, &block) end |
- (Object) each_page!(*args, &block)
183 184 185 186 |
# File 'lib/zendesk_api/collection.rb', line 183 def each_page!(*args, &block) warn "ZendeskAPI::Collection#each_page! is deprecated, please use ZendeskAPI::Collection#all!" all!(*args, &block) end |
- (Object) fetch(*args)
155 156 157 158 159 |
# File 'lib/zendesk_api/collection.rb', line 155 def fetch(*args) fetch!(*args) rescue Faraday::Error::ClientError => e [] end |
- (Object) fetch!(reload = false)
Executes actual GET from API and loads resources into proper class.
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/zendesk_api/collection.rb', line 141 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.dup) @query = nil @resources end |
- (Boolean) first_page?
91 92 93 |
# File 'lib/zendesk_api/collection.rb', line 91 def first_page? !@options["page"] || @options["page"].to_i <= 1 end |
- (Object) include(*sideloads)
Adds an item (or items) to the list of side-loaded resources to request
113 114 115 |
# File 'lib/zendesk_api/collection.rb', line 113 def include(*sideloads) self.tap { @includes.concat(sideloads.map(&:to_s)) } end |
- (Boolean) last_page?
95 96 97 |
# File 'lib/zendesk_api/collection.rb', line 95 def last_page? !@options["page"] || @options["page"].to_i * @options["per_page"].to_i >= count end |
- (Object) next
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.
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/zendesk_api/collection.rb', line 205 def next if @options["page"] clear_cache @options["page"] += 1 elsif @query = @next_page fetch(true) else clear_cache @resources = [] end end |
- (Collection) page(number)
Changes the page option. Returns self, so it can be chained. No execution.
85 86 87 88 89 |
# File 'lib/zendesk_api/collection.rb', line 85 def page(number) clear_cache if number @options["page"] = number self end |
- (Object) path
The API path to this collection
135 136 137 |
# File 'lib/zendesk_api/collection.rb', line 135 def path @association.generate_path(:with_parent => true) end |
- (Collection) per_page(count)
Changes the per_page option. Returns self, so it can be chained. No execution.
77 78 79 80 81 |
# File 'lib/zendesk_api/collection.rb', line 77 def per_page(count) clear_cache if count @options["per_page"] = count self end |
- (Object) prev
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.
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/zendesk_api/collection.rb', line 221 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 |
- (Object) replace(collection)
Replaces the current (loaded or not) resources with the passed in collection
196 197 198 199 |
# File 'lib/zendesk_api/collection.rb', line 196 def replace(collection) raise "this collection is for #{@resource_class}" if collection.any?{|r| !r.is_a?(@resource_class) } @resources = collection end |
- (Collection) save
Saves all newly created resources stored in this collection.
101 102 103 |
# File 'lib/zendesk_api/collection.rb', line 101 def save _save end |
- (Collection) save!
Saves all newly created resources stored in this collection.
107 108 109 |
# File 'lib/zendesk_api/collection.rb', line 107 def save! _save(:save!) end |
- (Object) to_a
Alias for fetch(false)
162 163 164 |
# File 'lib/zendesk_api/collection.rb', line 162 def to_a fetch end |
- (Object) to_a!
Alias for fetch!(false)
167 168 169 |
# File 'lib/zendesk_api/collection.rb', line 167 def to_a! fetch! end |