Class: Nylas::Collection
- Inherits:
-
Object
- Object
- Nylas::Collection
- Extended by:
- Forwardable
- Defined in:
- lib/nylas/collection.rb
Overview
An enumerable for working with index and search endpoints
Direct Known Subclasses
CalendarCollection, ComponentCollection, DeltasCollection, EventCollection, JobStatusCollection, SchedulerCollection, SearchCollection
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#constraints ⇒ Object
Returns the value of attribute constraints.
-
#model ⇒ Object
Returns the value of attribute model.
Instance Method Summary collapse
- #count ⇒ Integer
- #create(**attributes) ⇒ Object
-
#each ⇒ Object
Iterates over a single page of results based upon current pagination settings.
-
#execute ⇒ Hash, Array
Retrieves the data from the API for the particular constraints.
- #expanded ⇒ Collection<Model>
-
#find(id) ⇒ Object
Retrieves a record.
-
#find_each ⇒ Object
Iterates over every result that meets the filters, retrieving a page at a time.
- #find_model(id) ⇒ Object
- #find_raw(id) ⇒ Object
- #ids ⇒ Array<String>
-
#initialize(model:, api:, constraints: nil) ⇒ Collection
constructor
A new instance of Collection.
- #limit(quantity) ⇒ Object
- #more_pages?(accumulated, current_page) ⇒ Boolean
-
#new(**attributes) ⇒ Object
Instantiates a new model.
- #next_page(accumulated:, current_page:) ⇒ Object
- #offset(start) ⇒ Object
-
#raw ⇒ Collection<String>
The collection now returns a string representation of the model in a particular mime type instead of Model objects.
- #resources_path ⇒ Object
- #search(query) ⇒ Object
-
#to_be_executed ⇒ Hash
Specification for request to be passed to API#execute.
-
#where(filters) ⇒ Collection<Model>
Merges in additional filters when querying the collection.
Constructor Details
#initialize(model:, api:, constraints: nil) ⇒ Collection
Returns a new instance of Collection.
12 13 14 15 16 |
# File 'lib/nylas/collection.rb', line 12 def initialize(model:, api:, constraints: nil) self.constraints = Constraints.from_constraints(constraints) self.model = model self.api = api end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
6 7 8 |
# File 'lib/nylas/collection.rb', line 6 def api @api end |
#constraints ⇒ Object
Returns the value of attribute constraints.
6 7 8 |
# File 'lib/nylas/collection.rb', line 6 def constraints @constraints end |
#model ⇒ Object
Returns the value of attribute model.
6 7 8 |
# File 'lib/nylas/collection.rb', line 6 def model @model end |
Instance Method Details
#count ⇒ Integer
53 54 55 |
# File 'lib/nylas/collection.rb', line 53 def count self.class.new(model: model, api: api, constraints: constraints.merge(view: "count")).execute[:count] end |
#create(**attributes) ⇒ Object
23 24 25 26 27 |
# File 'lib/nylas/collection.rb', line 23 def create(**attributes) instance = model.new(**attributes.merge(api: api)) instance.save instance end |
#each ⇒ Object
Iterates over a single page of results based upon current pagination settings
68 69 70 71 72 73 74 |
# File 'lib/nylas/collection.rb', line 68 def each return enum_for(:each) unless block_given? execute.each do |result| yield(model.new(**result.merge(api: api))) end end |
#execute ⇒ Hash, Array
Retrieves the data from the API for the particular constraints
147 148 149 |
# File 'lib/nylas/collection.rb', line 147 def execute api.execute(**to_be_executed) end |
#expanded ⇒ Collection<Model>
58 59 60 |
# File 'lib/nylas/collection.rb', line 58 def self.class.new(model: model, api: api, constraints: constraints.merge(view: "expanded")) end |
#find(id) ⇒ Object
Retrieves a record. Nylas doesn't support where filters on GET so this will not take into consideration other query constraints, such as where clauses.
117 118 119 |
# File 'lib/nylas/collection.rb', line 117 def find(id) constraints.accept == "application/json" ? find_model(id) : find_raw(id) end |
#find_each ⇒ Object
Iterates over every result that meets the filters, retrieving a page at a time
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/nylas/collection.rb', line 85 def find_each return enum_for(:find_each) unless block_given? query = self accumulated = 0 while query results = query.each do |instance| yield(instance) end accumulated += results.length query = query.next_page(accumulated: accumulated, current_page: results) end end |
#find_model(id) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/nylas/collection.rb', line 129 def find_model(id) response = api.execute( **to_be_executed.merge( path: "#{resources_path}/#{id}", query: view_query ) ) model.from_hash(response, api: api) end |
#find_raw(id) ⇒ Object
121 122 123 |
# File 'lib/nylas/collection.rb', line 121 def find_raw(id) api.execute(**to_be_executed.merge(path: "#{resources_path}/#{id}")).to_s end |
#ids ⇒ Array<String>
63 64 65 |
# File 'lib/nylas/collection.rb', line 63 def ids self.class.new(model: model, api: api, constraints: constraints.merge(view: "ids")).execute end |
#limit(quantity) ⇒ Object
76 77 78 |
# File 'lib/nylas/collection.rb', line 76 def limit(quantity) self.class.new(model: model, api: api, constraints: constraints.merge(limit: quantity)) end |
#more_pages?(accumulated, current_page) ⇒ Boolean
107 108 109 110 111 112 113 |
# File 'lib/nylas/collection.rb', line 107 def more_pages?(accumulated, current_page) return false if current_page.empty? return false if constraints.limit && accumulated >= constraints.limit return false if constraints.per_page && current_page.length < constraints.per_page true end |
#new(**attributes) ⇒ Object
Instantiates a new model
19 20 21 |
# File 'lib/nylas/collection.rb', line 19 def new(**attributes) model.new(attributes.merge(api: api)) end |
#next_page(accumulated:, current_page:) ⇒ Object
101 102 103 104 105 |
# File 'lib/nylas/collection.rb', line 101 def next_page(accumulated:, current_page:) return nil unless more_pages?(accumulated, current_page) self.class.new(model: model, api: api, constraints: constraints.next_page) end |
#offset(start) ⇒ Object
80 81 82 |
# File 'lib/nylas/collection.rb', line 80 def offset(start) self.class.new(model: model, api: api, constraints: constraints.merge(offset: start)) end |
#raw ⇒ Collection<String>
The collection now returns a string representation of the model in a particular mime type instead of Model objects
46 47 48 49 50 |
# File 'lib/nylas/collection.rb', line 46 def raw raise ModelNotAvailableAsRawError, model unless model.exposable_as_raw? self.class.new(model: model, api: api, constraints: constraints.merge(accept: model.raw_mime_type)) end |
#resources_path ⇒ Object
125 126 127 |
# File 'lib/nylas/collection.rb', line 125 def resources_path model.resources_path(api: api) end |
#search(query) ⇒ Object
37 38 39 40 41 |
# File 'lib/nylas/collection.rb', line 37 def search(query) raise ModelNotSearchableError, model unless model.searchable? SearchCollection.new(model: model, api: api, constraints: constraints.merge(where: { q: query })) end |
#to_be_executed ⇒ Hash
Returns Specification for request to be passed to API#execute.
140 141 142 143 |
# File 'lib/nylas/collection.rb', line 140 def to_be_executed { method: :get, path: resources_path, query: constraints.to_query, headers: constraints.to_headers, auth_method: model.auth_method } end |
#where(filters) ⇒ Collection<Model>
Merges in additional filters when querying the collection
31 32 33 34 35 |
# File 'lib/nylas/collection.rb', line 31 def where(filters) raise ModelNotFilterableError, model unless model.filterable? self.class.new(model: model, api: api, constraints: constraints.merge(where: filters)) end |