Class: NotionRubyMapping::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_ruby_mapping/controllers/query.rb

Overview

Query object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter: {}, sort: [], page_size: 100, start_cursor: nil, filter_properties: []) ⇒ Query

Returns a new instance of Query.



6
7
8
9
10
11
12
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 6

def initialize(filter: {}, sort: [], page_size: 100, start_cursor: nil, filter_properties: [])
  @filter = filter
  @sort = sort
  @page_size = page_size
  @start_cursor = start_cursor
  @filter_properties = Array(filter_properties)
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



13
14
15
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 13

def filter
  @filter
end

#filter_propertiesObject

Returns the value of attribute filter_properties.



14
15
16
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 14

def filter_properties
  @filter_properties
end

#page_sizeObject

Returns the value of attribute page_size.



14
15
16
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 14

def page_size
  @page_size
end

#sortObject (readonly)

Returns the value of attribute sort.



13
14
15
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 13

def sort
  @sort
end

#start_cursorObject

Returns the value of attribute start_cursor.



14
15
16
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 14

def start_cursor
  @start_cursor
end

Instance Method Details

#and(another_query) ⇒ NotionRubyMapping::Query

Returns updated self (Query object).

Parameters:

  • another_query (Query)

    other query

Returns:



18
19
20
21
22
23
24
25
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 18

def and(another_query)
  if @filter.key? "and"
    @filter["and"] << another_query.filter
  else
    @filter = {"and" => [@filter, another_query.filter]}
  end
  self
end

#ascending(property) ⇒ NotionRubyMapping::Query

Returns updated self (Query object).

Parameters:

Returns:



29
30
31
32
33
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 29

def ascending(property)
  key = property.is_a?(LastEditedTimeProperty) || property.is_a?(CreatedTimeProperty) ? "timestamp" : "property"
  @sort << {key => property.name, "direction" => "ascending"}
  self
end

#database_query_stringObject



72
73
74
75
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 72

def database_query_string
  ans = Array(@filter_properties).map { |p| "filter_properties=#{p.property_id}" }
  ans.empty? ? "" : "?#{ans.join("&")}"
end

#descending(property) ⇒ NotionRubyMapping::Query

Returns updated self (Query object).

Parameters:

Returns:



37
38
39
40
41
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 37

def descending(property)
  key = property.is_a?(LastEditedTimeProperty) || property.is_a?(CreatedTimeProperty) ? "timestamp" : "property"
  @sort << {key => property.name, "direction" => "descending"}
  self
end

#or(other_query) ⇒ NotionRubyMapping::Query

Returns updated self (Query object).

Parameters:

  • other_query (Query)

    other query

Returns:



45
46
47
48
49
50
51
52
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 45

def or(other_query)
  if @filter.key? "or"
    @filter["or"] << other_query.filter
  else
    @filter = {"or" => [@filter, other_query.filter]}
  end
  self
end

#query_jsonHash

Returns:

  • (Hash)


55
56
57
58
59
60
61
62
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 55

def query_json
  parameters = {}
  parameters["filter"] = @filter unless @filter.empty?
  parameters["sorts"] = @sort unless @sort.empty?
  parameters["start_cursor"] = @start_cursor if @start_cursor
  parameters["page_size"] = @page_size if @page_size
  parameters
end

#query_stringString (frozen)

Returns:

  • (String (frozen))


65
66
67
68
69
70
# File 'lib/notion_ruby_mapping/controllers/query.rb', line 65

def query_string
  ans = []
  ans << "page_size=#{@page_size}" if @page_size
  ans << "start_cursor=#{@start_cursor}" if @start_cursor
  ans.empty? ? "" : "?#{ans.join("&")}"
end