Class: Nylas::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/nylas/collection.rb

Overview

An enumerable for working with index and search endpoints

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#apiObject

Returns the value of attribute api.



6
7
8
# File 'lib/nylas/collection.rb', line 6

def api
  @api
end

#constraintsObject

Returns the value of attribute constraints.



6
7
8
# File 'lib/nylas/collection.rb', line 6

def constraints
  @constraints
end

#modelObject

Returns the value of attribute model.



6
7
8
# File 'lib/nylas/collection.rb', line 6

def model
  @model
end

Instance Method Details

#countInteger

Returns:

  • (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

#eachObject

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

#executeHash, Array

Retrieves the data from the API for the particular constraints

Returns:

  • (Hash, Array)


161
162
163
# File 'lib/nylas/collection.rb', line 161

def execute
  api.execute(**to_be_executed)
end

#expandedCollection<Model>

Returns:



65
66
67
# File 'lib/nylas/collection.rb', line 65

def expanded
  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_eachObject

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

#idsArray<String>

Returns:

  • (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

Returns:

  • (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

#rawCollection<String>

The collection now returns a string representation of the model in a particular mime type instead of Model objects

Returns:

Raises:



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_pathObject



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_executedHash

Returns Specification for request to be passed to API#execute.

Returns:

  • (Hash)

    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

Returns:

Raises:



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