Class: Nylas::Constraints
- Inherits:
-
Object
- Object
- Nylas::Constraints
- Defined in:
- lib/nylas/constraints.rb
Overview
The constraints a particular GET request will include in their query params
Instance Attribute Summary collapse
-
#accept ⇒ Object
Returns the value of attribute accept.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#view ⇒ Object
Returns the value of attribute view.
-
#where ⇒ Object
Returns the value of attribute where.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(where: {}, limit: nil, offset: 0, view: nil, per_page: 100, accept: "application/json") ⇒ Constraints
constructor
A new instance of Constraints.
- #limit_for_query ⇒ Object
- #merge(where: {}, limit: nil, offset: nil, view: nil, per_page: nil, accept: nil) ⇒ Object
- #next_page ⇒ Object
- #to_headers ⇒ Object
- #to_query ⇒ Object
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
#accept ⇒ Object
Returns the value of attribute accept.
6 7 8 |
# File 'lib/nylas/constraints.rb', line 6 def accept @accept end |
#limit ⇒ Object
Returns the value of attribute limit.
6 7 8 |
# File 'lib/nylas/constraints.rb', line 6 def limit @limit end |
#offset ⇒ Object
Returns the value of attribute offset.
6 7 8 |
# File 'lib/nylas/constraints.rb', line 6 def offset @offset end |
#per_page ⇒ Object
Returns the value of attribute per_page.
6 7 8 |
# File 'lib/nylas/constraints.rb', line 6 def per_page @per_page end |
#view ⇒ Object
Returns the value of attribute view.
6 7 8 |
# File 'lib/nylas/constraints.rb', line 6 def view @view end |
#where ⇒ Object
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
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_query ⇒ Object
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_page ⇒ Object
26 27 28 |
# File 'lib/nylas/constraints.rb', line 26 def next_page merge(offset: offset + per_page) end |
#to_headers ⇒ Object
40 41 42 |
# File 'lib/nylas/constraints.rb', line 40 def to_headers accept == "application/json" ? {} : { "Accept" => accept, "Content-types" => accept } end |
#to_query ⇒ Object
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 |