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 56 57 58 59 60 61 62 |
# File 'lib/nylas/collection.rb', line 53 def count collection = self.class.new(model: model, api: api, constraints: constraints) if model.countable collection.constraints = collection.constraints.merge(view: "count") collection.execute[:count] else collection.find_each.map.count end 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
82 83 84 85 86 87 88 |
# File 'lib/nylas/collection.rb', line 82 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
161 162 163 |
# File 'lib/nylas/collection.rb', line 161 def execute api.execute(**to_be_executed) end |
#expanded ⇒ Collection<Model>
65 66 67 |
# File 'lib/nylas/collection.rb', line 65 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.
131 132 133 |
# File 'lib/nylas/collection.rb', line 131 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/nylas/collection.rb', line 99 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
143 144 145 146 147 148 149 150 151 |
# File 'lib/nylas/collection.rb', line 143 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
135 136 137 |
# File 'lib/nylas/collection.rb', line 135 def find_raw(id) api.execute(**to_be_executed.merge(path: "#{resources_path}/#{id}")).to_s end |
#ids ⇒ Array<String>
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/nylas/collection.rb', line 70 def ids collection = self.class.new(model: model, api: api, constraints: constraints) if model.id_listable collection.constraints = collection.constraints.merge(view: "ids") collection.execute else collection.find_each.map(&:id) end end |
#limit(quantity) ⇒ Object
90 91 92 |
# File 'lib/nylas/collection.rb', line 90 def limit(quantity) self.class.new(model: model, api: api, constraints: constraints.merge(limit: quantity)) end |
#more_pages?(accumulated, current_page) ⇒ Boolean
121 122 123 124 125 126 127 |
# File 'lib/nylas/collection.rb', line 121 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
115 116 117 118 119 |
# File 'lib/nylas/collection.rb', line 115 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
94 95 96 |
# File 'lib/nylas/collection.rb', line 94 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
139 140 141 |
# File 'lib/nylas/collection.rb', line 139 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.
154 155 156 157 |
# File 'lib/nylas/collection.rb', line 154 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 |