Class: Nylas::Constraints

Inherits:
Object
  • Object
show all
Defined in:
lib/nylas/constraints.rb

Overview

The constraints a particular GET request will include in their query params

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(where: {}, limit: nil, offset: 0, view: nil, per_page: 100, accept: "application/json") ⇒ Constraints

Returns a new instance of Constraints.



8
9
10
11
12
13
14
15
# File 'lib/nylas/constraints.rb', line 8

def initialize(where: {}, limit: nil, offset: 0, view: nil, per_page: 100, accept: "application/json")
  self.where = where
  self.limit = limit
  self.offset = offset
  self.view = view
  self.per_page = per_page
  self.accept = accept
end

Instance Attribute Details

#acceptObject

Returns the value of attribute accept.



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

def accept
  @accept
end

#limitObject

Returns the value of attribute limit.



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

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

#per_pageObject

Returns the value of attribute per_page.



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

def per_page
  @per_page
end

#viewObject

Returns the value of attribute view.



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

def view
  @view
end

#whereObject

Returns the value of attribute where.



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

def where
  @where
end

Class Method Details

.from_constraints(constraints = Constraints.new) ⇒ Object

Raises:

  • (TypeError)


48
49
50
51
52
53
54
# File 'lib/nylas/constraints.rb', line 48

def self.from_constraints(constraints = Constraints.new)
  return constraints if constraints.is_a?(Constraints)
  return new(**constraints) if constraints.respond_to?(:key?)
  return new if constraints.nil?

  raise TypeError, "passed in constraints #{constraints} cannot be cast to a set of constraints"
end

Instance Method Details

#limit_for_queryObject



44
45
46
# File 'lib/nylas/constraints.rb', line 44

def limit_for_query
  !limit.nil? && limit < per_page ? limit : per_page
end

#merge(where: {}, limit: nil, offset: nil, view: nil, per_page: nil, accept: nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/nylas/constraints.rb', line 17

def merge(where: {}, limit: nil, offset: nil, view: nil, per_page: nil, accept: nil)
  Constraints.new(where: self.where.merge(where),
                  limit: limit || self.limit,
                  per_page: per_page || self.per_page,
                  offset: offset || self.offset,
                  view: view || self.view,
                  accept: accept || self.accept)
end

#next_pageObject



26
27
28
# File 'lib/nylas/constraints.rb', line 26

def next_page
  merge(offset: offset + per_page)
end

#to_headersObject



40
41
42
# File 'lib/nylas/constraints.rb', line 40

def to_headers
  accept == "application/json" ? {} : { "Accept" => accept, "Content-types" => accept }
end

#to_queryObject



30
31
32
33
34
35
36
37
38
# File 'lib/nylas/constraints.rb', line 30

def to_query
  query = where.each_with_object({}) do |(name, value), result|
    result[name] = value
  end
  query[:limit] = limit_for_query
  query[:offset] = offset unless offset.nil?
  query[:view] = view unless view.nil?
  query
end