Sha256: 8874af17c7837fd87afe61054120d7699b75fcd08206da182adedbf66d2bc8e2

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module Nylas
  # The constraints a particular GET request will include in their query params
  class Constraints
    attr_accessor :where, :limit, :offset, :view, :per_page
    def initialize(where: {}, limit: nil, offset: 0, view: nil, per_page: 100)
      self.where = where
      self.limit = limit
      self.offset = offset
      self.view = view
      self.per_page = per_page
    end

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

    def next_page
      merge(offset: offset + per_page)
    end

    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

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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nylas-4.0.0.rc2 lib/nylas/constraints.rb