Class: Notion::Query::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/notion/query/builder.rb

Defined Under Namespace

Classes: Filter

Constant Summary collapse

DEFAULT_VALUE =
Object.new.freeze
OPERATORS =
%i[
  equals does_not_equal contains does_not_contain starts_with ends_with greater_than
  less_than greater_than_or_equal_to less_than_or_equal_to before after on_or_before
  on_or_after is_empty is_not_empty past_week past_month past_year next_week next_month next_year
].freeze
EMPTY_OBJECT_OPERATORS =
%i[past_week past_month past_year next_week next_month next_year].freeze

Instance Method Summary collapse

Constructor Details

#initialize(schema: nil, strict: false) ⇒ Builder

Returns a new instance of Builder.



16
17
18
19
20
21
# File 'lib/notion/query/builder.rb', line 16

def initialize(schema: nil, strict: false)
  @filters = []
  @sorts = []
  @schema = schema || {}
  @strict = strict
end

Instance Method Details

#add(property, operator, value, type = nil) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/notion/query/builder.rb', line 27

def add(property, operator, value, type = nil)
  raise ArgumentError, "property is required" if property.nil? || property.to_s.empty?

  definition = @schema[property.to_s] || @schema[property.to_sym]
  raise UnknownPropertyError, "unknown property: #{property}" if @strict && !definition

  property_type = type || schema_type(definition) || :rich_text
  condition = { property: property.to_s, property_type.to_sym => { operator => value } }
  @filters << condition
  self
end

#all_of(*filters) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
# File 'lib/notion/query/builder.rb', line 39

def all_of(*filters)
  raise ArgumentError, "all_of requires a filter" if filters.empty?

  @filters << { and: filters.map { |filter| filter_value(filter) } }
  self
end

#any_of(*filters) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
# File 'lib/notion/query/builder.rb', line 46

def any_of(*filters)
  raise ArgumentError, "any_of requires a filter" if filters.empty?

  @filters << { or: filters.map { |filter| filter_value(filter) } }
  self
end

#order(property = nil, timestamp: nil, direction: :ascending) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
# File 'lib/notion/query/builder.rb', line 65

def order(property = nil, timestamp: nil, direction: :ascending)
  raise ArgumentError, "provide either property or timestamp" if property.nil? == timestamp.nil?
  unless %i[ascending descending].include?(direction)
    raise ArgumentError, "direction must be ascending or descending"
  end

  @sorts << { direction: direction }.merge(property ? { property: property.to_s } : { timestamp: timestamp })
  self
end

#order_by_timestamp(timestamp, direction = :ascending) ⇒ Object



75
76
77
# File 'lib/notion/query/builder.rb', line 75

def order_by_timestamp(timestamp, direction = :ascending)
  order(timestamp: timestamp, direction: direction)
end

#raw(filter) ⇒ Object



79
80
81
82
# File 'lib/notion/query/builder.rb', line 79

def raw(filter)
  @filters << filter
  self
end

#to_hObject



84
85
86
87
88
89
# File 'lib/notion/query/builder.rb', line 84

def to_h
  result = {}
  result[:filter] = @filters.one? ? @filters.first : { and: @filters } unless @filters.empty?
  result[:sorts] = @sorts unless @sorts.empty?
  result
end

#where(property, type: nil) ⇒ Object



23
24
25
# File 'lib/notion/query/builder.rb', line 23

def where(property, type: nil)
  Filter.new(self, property, type)
end