notion_ruby_mapping 0.3.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +44 -1237
  3. data/env.yml.sample +0 -13
  4. data/examples/change_title.md +1 -1
  5. data/examples/renumbering_pages.md +3 -3
  6. data/examples/set_icon_to_all_icon_unsettled_pages.md +4 -4
  7. data/lib/notion_ruby_mapping/{base.rb → blocks/base.rb} +163 -92
  8. data/lib/notion_ruby_mapping/blocks/block.rb +145 -0
  9. data/lib/notion_ruby_mapping/blocks/bookmark_block.rb +11 -0
  10. data/lib/notion_ruby_mapping/blocks/breadcrumb_block.rb +19 -0
  11. data/lib/notion_ruby_mapping/blocks/bulleted_list_item_block.rb +11 -0
  12. data/lib/notion_ruby_mapping/blocks/callout_block.rb +82 -0
  13. data/lib/notion_ruby_mapping/blocks/child_base_block.rb +21 -0
  14. data/lib/notion_ruby_mapping/blocks/child_database_block.rb +11 -0
  15. data/lib/notion_ruby_mapping/blocks/child_page_block.rb +11 -0
  16. data/lib/notion_ruby_mapping/blocks/code_block.rb +50 -0
  17. data/lib/notion_ruby_mapping/blocks/column_block.rb +29 -0
  18. data/lib/notion_ruby_mapping/blocks/column_list_block.rb +33 -0
  19. data/lib/notion_ruby_mapping/{database.rb → blocks/database.rb} +41 -8
  20. data/lib/notion_ruby_mapping/blocks/divider_block.rb +19 -0
  21. data/lib/notion_ruby_mapping/blocks/embed_block.rb +11 -0
  22. data/lib/notion_ruby_mapping/blocks/equation_block.rb +43 -0
  23. data/lib/notion_ruby_mapping/blocks/file_base_block.rb +50 -0
  24. data/lib/notion_ruby_mapping/blocks/file_block.rb +11 -0
  25. data/lib/notion_ruby_mapping/blocks/heading1_block.rb +18 -0
  26. data/lib/notion_ruby_mapping/blocks/heading2_block.rb +18 -0
  27. data/lib/notion_ruby_mapping/blocks/heading3_block.rb +18 -0
  28. data/lib/notion_ruby_mapping/blocks/image_block.rb +11 -0
  29. data/lib/notion_ruby_mapping/blocks/link_preview_block.rb +16 -0
  30. data/lib/notion_ruby_mapping/blocks/link_to_page_block.rb +40 -0
  31. data/lib/notion_ruby_mapping/{list.rb → blocks/list.rb} +37 -6
  32. data/lib/notion_ruby_mapping/blocks/numbered_list_item_block.rb +11 -0
  33. data/lib/notion_ruby_mapping/blocks/page.rb +77 -0
  34. data/lib/notion_ruby_mapping/blocks/paragraph_block.rb +11 -0
  35. data/lib/notion_ruby_mapping/{block.rb → blocks/pdf_block.rb} +4 -3
  36. data/lib/notion_ruby_mapping/blocks/quote_block.rb +11 -0
  37. data/lib/notion_ruby_mapping/blocks/synced_block.rb +44 -0
  38. data/lib/notion_ruby_mapping/blocks/table_block.rb +50 -0
  39. data/lib/notion_ruby_mapping/blocks/table_of_contents_block.rb +34 -0
  40. data/lib/notion_ruby_mapping/blocks/table_row_block.rb +34 -0
  41. data/lib/notion_ruby_mapping/blocks/template_block.rb +34 -0
  42. data/lib/notion_ruby_mapping/blocks/text_sub_block_color_base_block.rb +45 -0
  43. data/lib/notion_ruby_mapping/blocks/to_do_block.rb +42 -0
  44. data/lib/notion_ruby_mapping/blocks/toggle_block.rb +11 -0
  45. data/lib/notion_ruby_mapping/blocks/toggle_heading1_block.rb +11 -0
  46. data/lib/notion_ruby_mapping/blocks/toggle_heading2_block.rb +11 -0
  47. data/lib/notion_ruby_mapping/blocks/toggle_heading3_block.rb +11 -0
  48. data/lib/notion_ruby_mapping/blocks/url_base_block.rb +34 -0
  49. data/lib/notion_ruby_mapping/blocks/url_caption_base_block.rb +46 -0
  50. data/lib/notion_ruby_mapping/blocks/video_block.rb +11 -0
  51. data/lib/notion_ruby_mapping/{notion_cache.rb → controllers/notion_cache.rb} +144 -102
  52. data/lib/notion_ruby_mapping/{payload.rb → controllers/payload.rb} +32 -18
  53. data/lib/notion_ruby_mapping/{property_cache.rb → controllers/property_cache.rb} +21 -20
  54. data/lib/notion_ruby_mapping/{query.rb → controllers/query.rb} +27 -19
  55. data/lib/notion_ruby_mapping/{rich_text_array.rb → controllers/rich_text_array.rb} +39 -17
  56. data/lib/notion_ruby_mapping/objects/emoji_object.rb +40 -0
  57. data/lib/notion_ruby_mapping/objects/equation_object.rb +43 -0
  58. data/lib/notion_ruby_mapping/objects/file_object.rb +60 -0
  59. data/lib/notion_ruby_mapping/{mention_object.rb → objects/mention_object.rb} +11 -0
  60. data/lib/notion_ruby_mapping/{rich_text_object.rb → objects/rich_text_object.rb} +12 -4
  61. data/lib/notion_ruby_mapping/{text_object.rb → objects/text_object.rb} +0 -1
  62. data/lib/notion_ruby_mapping/{user_object.rb → objects/user_object.rb} +9 -4
  63. data/lib/notion_ruby_mapping/{checkbox_property.rb → properties/checkbox_property.rb} +3 -0
  64. data/lib/notion_ruby_mapping/{created_by_property.rb → properties/created_by_property.rb} +1 -0
  65. data/lib/notion_ruby_mapping/{created_time_property.rb → properties/created_time_property.rb} +1 -0
  66. data/lib/notion_ruby_mapping/properties/date_base_property.rb +135 -0
  67. data/lib/notion_ruby_mapping/{date_property.rb → properties/date_property.rb} +13 -0
  68. data/lib/notion_ruby_mapping/{email_property.rb → properties/email_property.rb} +2 -2
  69. data/lib/notion_ruby_mapping/{files_property.rb → properties/files_property.rb} +26 -13
  70. data/lib/notion_ruby_mapping/{formula_property.rb → properties/formula_property.rb} +5 -1
  71. data/lib/notion_ruby_mapping/{last_edited_by_property.rb → properties/last_edited_by_property.rb} +1 -0
  72. data/lib/notion_ruby_mapping/{last_edited_time_property.rb → properties/last_edited_time_property.rb} +1 -0
  73. data/lib/notion_ruby_mapping/{multi_property.rb → properties/multi_property.rb} +0 -0
  74. data/lib/notion_ruby_mapping/{multi_select_property.rb → properties/multi_select_property.rb} +4 -3
  75. data/lib/notion_ruby_mapping/{number_property.rb → properties/number_property.rb} +4 -0
  76. data/lib/notion_ruby_mapping/{people_property.rb → properties/people_property.rb} +8 -3
  77. data/lib/notion_ruby_mapping/{phone_number_property.rb → properties/phone_number_property.rb} +4 -1
  78. data/lib/notion_ruby_mapping/{property.rb → properties/property.rb} +33 -0
  79. data/lib/notion_ruby_mapping/{relation_property.rb → properties/relation_property.rb} +30 -10
  80. data/lib/notion_ruby_mapping/{rich_text_property.rb → properties/rich_text_property.rb} +0 -0
  81. data/lib/notion_ruby_mapping/{rollup_property.rb → properties/rollup_property.rb} +7 -0
  82. data/lib/notion_ruby_mapping/{select_property.rb → properties/select_property.rb} +8 -2
  83. data/lib/notion_ruby_mapping/{text_property.rb → properties/text_property.rb} +0 -0
  84. data/lib/notion_ruby_mapping/{title_property.rb → properties/title_property.rb} +0 -0
  85. data/lib/notion_ruby_mapping/{url_property.rb → properties/url_property.rb} +4 -1
  86. data/lib/notion_ruby_mapping/version.rb +1 -1
  87. data/lib/notion_ruby_mapping.rb +21 -7
  88. metadata +82 -40
  89. data/lib/notion_ruby_mapping/date_base_property.rb +0 -75
  90. data/lib/notion_ruby_mapping/page.rb +0 -50
@@ -11,6 +11,7 @@ module NotionRubyMapping
11
11
 
12
12
  ### initialize
13
13
 
14
+ # @see https://www.notion.so/hkob/NotionCache-65e1599864d6425686d495a5a4b3a623#dca210788f114cf59464090782c073bf
14
15
  def initialize
15
16
  @object_hash = {}
16
17
  @client = Faraday::Connection.new "https://api.notion.com" do |builder|
@@ -25,38 +26,42 @@ module NotionRubyMapping
25
26
  attr_reader :object_hash
26
27
  attr_writer :client # for test only
27
28
 
28
- # @param [String] notion_token
29
- # @return [NotionRubyMapping::NotionCache] self (NotionCache.instance)
30
- def create_client(notion_token, wait: 0.3333, debug: false)
31
- @notion_token = notion_token
32
- @wait = wait
33
- @debug = debug
34
- self
29
+ # @param [String] block_id
30
+ # @return [String (frozen)] block_path
31
+ def append_block_children_block_path(block_id)
32
+ "v1/blocks/#{block_id}/children"
35
33
  end
36
34
 
37
- ### create path methods
38
-
39
- # @param [String] database_id
35
+ # @param [String] page_id
40
36
  # @return [String (frozen)] page_path
41
- def database_path(database_id)
42
- "v1/databases/#{database_id}"
37
+ def append_block_children_page_path(page_id)
38
+ "v1/blocks/#{page_id}/children"
43
39
  end
44
40
 
45
- # @param [String] database_id
46
- # @return [String (frozen)] page_path
47
- def databases_path
48
- "v1/databases"
41
+ # @param [String] id
42
+ # @param [Hash] payload
43
+ # @return [Hash]
44
+ def append_block_children_request(id, payload)
45
+ request :patch, append_block_children_block_path(id), payload
46
+ end
47
+
48
+ # @param [String] id block_id (with or without "-")
49
+ # @return [NotionRubyMapping::Base] Block object or nil
50
+ def block(id)
51
+ object_for_key(id) { block_request id }
49
52
  end
50
53
 
51
54
  # @param [String] page_id
52
55
  # @return [String (frozen)] page_path
53
- def page_path(page_id)
54
- "v1/pages/#{page_id}"
56
+ def block_children_page_path(page_id, query_string = "")
57
+ "v1/blocks/#{page_id}/children#{query_string}"
55
58
  end
56
59
 
57
- # @return [String (frozen)] page_path
58
- def pages_path
59
- "v1/pages"
60
+ # @param [String] id
61
+ # @param [String] query_string
62
+ # @return [Hash]
63
+ def block_children_request(id, query_string)
64
+ request :get, block_children_page_path(id, query_string)
60
65
  end
61
66
 
62
67
  # @param [String] block_id
@@ -65,92 +70,97 @@ module NotionRubyMapping
65
70
  "v1/blocks/#{block_id}"
66
71
  end
67
72
 
68
- # @param [String] database_id
69
- # @return [String (frozen)] page_path
70
- def query_database_path(database_id)
71
- "v1/databases/#{database_id}/query"
73
+ # @param [String] block_id
74
+ # @return [Hash] response
75
+ def block_request(block_id)
76
+ request :get, block_path(block_id)
72
77
  end
73
78
 
74
- ### Notion API call
75
-
76
- # @param [Symbol] method
77
- # @param [String] path
78
- # @param [Hash] options
79
- # @return [Hash] response hash
80
- def request(method, path, options = {})
81
- raise "Please call `NotionCache.create_client' before using other methods" unless @notion_token
79
+ # @return [Hash]
80
+ def clear_object_hash
81
+ @object_hash = {}
82
+ end
82
83
 
83
- sleep @wait
84
- response = @client.send(method) do |request|
85
- request.headers["Authorization"] = "Bearer #{@notion_token}"
86
- request.headers["Notion-Version"] = NotionRubyMapping::NOTION_VERSION
87
- case method
88
- when :get, :delete
89
- request.url path, options
90
- when :post, :put, :patch
91
- request.headers["Content-Type"] = "application/json"
92
- request.path = path
93
- request.body = options.to_json unless options.empty?
94
- else
95
- raise StandardError, "Unknown method: #{method}"
96
- end
97
- request.options.merge!(options.delete(:request)) if options.key? :request
98
- end
99
- p response.body if @debug
100
- response.body
84
+ # @param [String] notion_token
85
+ # @return [NotionRubyMapping::NotionCache] self (NotionCache.instance)
86
+ def create_client(notion_token, wait: 0.3333, debug: false)
87
+ @notion_token = notion_token
88
+ @wait = wait
89
+ @debug = debug
90
+ self
101
91
  end
102
92
 
103
- # @param [String] page_id
93
+ # @param [Hash] payload
104
94
  # @return [Hash] response
105
- def page_request(page_id)
106
- request :get, page_path(page_id)
95
+ def create_database_request(payload)
96
+ request :post, databases_path, payload
107
97
  end
108
98
 
109
- # @param [String] database_id
99
+ # @param [Hash] payload
110
100
  # @return [Hash] response
111
- def database_request(database_id)
112
- request :get, database_path(database_id)
101
+ def create_page_request(payload)
102
+ request :post, "v1/pages", payload
113
103
  end
114
104
 
115
- def database_query_request(database_id, payload)
116
- request :post, "v1/databases/#{database_id}/query", payload
105
+ # @param [String] id database_id (with or without "-")
106
+ # @return [NotionRubyMapping::Base] Database object or nil
107
+ def database(id)
108
+ object_for_key(id) { database_request id }
117
109
  end
118
110
 
119
- # @param [String] block_id
120
- # @return [Hash] response
121
- def block_request(block_id)
122
- request :get, block_path(block_id)
111
+ # @param [String] database_id
112
+ # @return [String (frozen)] page_path
113
+ def database_path(database_id)
114
+ "v1/databases/#{database_id}"
115
+ end
116
+
117
+ # @param [String] id page_id / block_id (with or without "-")
118
+ # @param [NotionRubyMapping::Query] query query object
119
+ # @return [NotionRubyMapping::Base] List object
120
+ # def database_query(id, query)
121
+ # Base.create_from_json database_query_request(id, query.query_json)
122
+ # end
123
+
124
+ # @param [String] database_id (with or without "-")
125
+ # @param [NotionRubyMapping::Query] query query object
126
+ def database_query_request(database_id, query)
127
+ request :post, "v1/databases/#{database_id}/query", query.query_json
123
128
  end
124
129
 
125
130
  # @param [String] database_id
126
131
  # @return [Hash] response
127
- def update_database_request(database_id, payload)
128
- request :patch, "v1/databases/#{database_id}", payload
132
+ def database_request(database_id)
133
+ request :get, database_path(database_id)
129
134
  end
130
135
 
131
- # @param [String] page_id
132
- # @param [Hash] payload
133
- # @return [Hash] response
134
- def update_page_request(page_id, payload)
135
- request :patch, "v1/pages/#{page_id}", payload
136
+ # @return [String (frozen)] page_path
137
+ def databases_path
138
+ "v1/databases"
136
139
  end
137
140
 
138
- # @param [Hash] payload
139
- # @return [Hash] response
140
- def create_page_request(payload)
141
- request :post, "v1/pages", payload
141
+ # @param [String] id
142
+ # @return [NotionRubyMapping::Base]
143
+ def destroy_block(id)
144
+ Base.create_from_json destroy_block_request(id)
142
145
  end
143
146
 
144
- def create_database_request(payload)
145
- request :post, databases_path, payload
147
+ # @param [String] id
148
+ # @return [Hash]
149
+ def destroy_block_request(id)
150
+ request :delete, block_path(id)
146
151
  end
147
152
 
148
153
  # @param [String] id id string with "-"
149
154
  # @return [String] id without "-"
155
+ # @see https://www.notion.so/hkob/NotionCache-65e1599864d6425686d495a5a4b3a623#a2d70a2e019c4c17898aaa1a36580f1d
150
156
  def hex_id(id)
151
157
  id&.gsub "-", ""
152
158
  end
153
159
 
160
+ def inspect
161
+ "NotionCache"
162
+ end
163
+
154
164
  # @param [String] id id (with or without "-")
155
165
  # @return [NotionRubyMapping::Base]
156
166
  def object_for_key(id)
@@ -167,46 +177,78 @@ module NotionRubyMapping
167
177
  object_for_key(id) { page_request id }
168
178
  end
169
179
 
170
- # @param [String] id database_id (with or without "-")
171
- # @return [NotionRubyMapping::Base] Database object or nil
172
- def database(id)
173
- object_for_key(id) { database_request id }
180
+ # @param [String] page_id
181
+ # @return [String (frozen)] page_path
182
+ def page_path(page_id)
183
+ "v1/pages/#{page_id}"
174
184
  end
175
185
 
176
- # @param [String] id block_id (with or without "-")
177
- # @return [NotionRubyMapping::Base] Block object or nil
178
- def block(id)
179
- object_for_key(id) { block_request id }
186
+ # @param [String] page_id
187
+ # @return [Hash] response
188
+ def page_request(page_id)
189
+ request :get, page_path(page_id)
180
190
  end
181
191
 
182
- # @param [String] id page_id / block_id (with or without "-")
183
- # @return [NotionRubyMapping::Base] List object
184
- def block_children(id)
185
- array = []
192
+ # @return [String (frozen)] page_path
193
+ def pages_path
194
+ "v1/pages"
195
+ end
196
+
197
+ # @param [String] database_id
198
+ # @return [String (frozen)] page_path
199
+ def query_database_path(database_id)
200
+ "v1/databases/#{database_id}/query"
201
+ end
202
+
203
+ # @param [Symbol] method
204
+ # @param [String] path
205
+ # @param [Hash] options
206
+ # @return [Hash] response hash
207
+ def request(method, path, options = {})
208
+ raise "Please call `NotionCache.create_client' before using other methods" unless @notion_token
209
+
186
210
  sleep @wait
187
- @client.block_children(block_id: id, sleep_interval: @wait, max_retries: 20) do |page|
188
- array.concat page.results
211
+ response = @client.send(method) do |request|
212
+ request.headers["Authorization"] = "Bearer #{@notion_token}"
213
+ request.headers["Notion-Version"] = NotionRubyMapping::NOTION_VERSION
214
+ case method
215
+ when :get, :delete
216
+ request.url path, options
217
+ when :post, :put, :patch
218
+ request.headers["Content-Type"] = "application/json"
219
+ request.path = path
220
+ request.body = options.to_json unless options.empty?
221
+ else
222
+ raise StandardError, "Unknown method: #{method}"
223
+ end
224
+ request.options.merge!(options.delete(:request)) if options.key? :request
189
225
  end
190
- Base.create_from_json({"object" => "list", "results" => array})
226
+ p response.body if @debug
227
+ response.body
191
228
  end
192
229
 
193
- # @param [String] id page_id / block_id (with or without "-")
194
- # @param [NotionRubyMapping::Query] query query object
195
- # @return [NotionRubyMapping::Base] List object
196
- def database_query(id, query)
197
- Base.create_from_json database_query_request(id, query.query_json)
230
+ def update_block_request(block_id, payload)
231
+ request :patch, block_path(block_id), payload
198
232
  end
199
233
 
200
234
  # @param [String] id page_id (with or without "-")
201
235
  # @param [Hash] payload
202
- def update_database(id, payload)
203
- sleep @wait
204
- @client.update_database payload.merge({database_id: id})
236
+ # def update_database(id, payload)
237
+ # sleep @wait
238
+ # @client.update_database payload.merge({database_id: id})
239
+ # end
240
+
241
+ # @param [String] database_id
242
+ # @return [Hash] response
243
+ def update_database_request(database_id, payload)
244
+ request :patch, database_path(database_id), payload
205
245
  end
206
246
 
207
- # @return [Hash]
208
- def clear_object_hash
209
- @object_hash = {}
247
+ # @param [String] page_id
248
+ # @param [Hash] payload
249
+ # @return [Hash] response
250
+ def update_page_request(page_id, payload)
251
+ request :patch, page_path(page_id), payload
210
252
  end
211
253
  end
212
254
  end
@@ -5,21 +5,17 @@ module NotionRubyMapping
5
5
  class Payload
6
6
  def initialize(json)
7
7
  @json = json || {}
8
+ @update_block_key = []
8
9
  end
9
10
 
10
- # @param [String] emoji
11
- # @param [String] url
12
- # @return [NotionRubyMapping::Payload] updated Payload
13
- def set_icon(emoji: nil, url: nil)
14
- payload = if emoji
15
- {"type" => "emoji", "emoji" => emoji}
16
- elsif url
17
- {"type" => "external", "external" => {"url" => url}}
18
- else
19
- {}
20
- end
21
- @json["icon"] = payload
22
- self
11
+ # @param [String] key
12
+ def add_update_block_key(key)
13
+ @update_block_key << key
14
+ end
15
+
16
+ # @return [Hash] {}
17
+ def clear
18
+ @json = {}
23
19
  end
24
20
 
25
21
  # @param [Hash] json
@@ -40,15 +36,33 @@ module NotionRubyMapping
40
36
  others.compact.reduce({}) { |hash, o| hash.merge o.property_schema_json }.merge @json
41
37
  end
42
38
 
39
+ # @param [String] emoji
40
+ # @param [String] url
41
+ # @return [NotionRubyMapping::Payload] updated Payload
42
+ def set_icon(emoji: nil, url: nil)
43
+ payload = if emoji
44
+ {"type" => "emoji", "emoji" => emoji}
45
+ elsif url
46
+ {"type" => "external", "external" => {"url" => url}}
47
+ else
48
+ {}
49
+ end
50
+ @json["icon"] = payload
51
+ self
52
+ end
53
+
54
+ def update_block_json(type, json)
55
+ sub_json = json[type]
56
+ ans = {type => sub_json.slice(*@update_block_key)}
57
+ ans[type]["caption"] = sub_json["caption"] if sub_json["caption"]
58
+ ans[type]["rich_text"] = sub_json["rich_text"] if sub_json["rich_text"]
59
+ ans
60
+ end
61
+
43
62
  # @return [Hash] created json
44
63
  # @param [Object] others
45
64
  def update_property_schema_json(*others)
46
65
  others.compact.reduce({}) { |hash, o| hash.merge o.update_property_schema_json }.merge @json
47
66
  end
48
-
49
- # @return [Hash] {}
50
- def clear
51
- @json = {}
52
- end
53
67
  end
54
68
  end
@@ -13,22 +13,20 @@ module NotionRubyMapping
13
13
 
14
14
  # @param [String] key
15
15
  # @return [Property] Property for key
16
+ # @see https://www.notion.so/hkob/PropertyCache-2451fa64a814432db4809831cc77ba25#9709e2b2a7a0479f9951291a501f65c8
16
17
  def [](key)
17
18
  @properties[key] ||= Property.create_from_json key, @json[key], @base_type
18
19
  end
19
20
 
20
- # @param [Array] key
21
- # @return [Array]
22
- def values_at(*key)
23
- generate_all_properties
24
- @properties.values_at(*key)
21
+ # @param [Property] property added Property
22
+ def add_property(property)
23
+ @properties[property.name] = property
24
+ self
25
25
  end
26
26
 
27
- def generate_all_properties
28
- if @json.empty?
29
- @properties.values
30
- else
31
- @json.keys.map { |key| self[key] }
27
+ def clear_will_update
28
+ @properties.each do |_, property|
29
+ property.clear_will_update
32
30
  end
33
31
  end
34
32
 
@@ -39,16 +37,11 @@ module NotionRubyMapping
39
37
  generate_all_properties.each(&block)
40
38
  end
41
39
 
42
- # @param [Property] property added Property
43
- def add_property(property)
44
- @properties[property.name] = property
45
- self
46
- end
47
-
48
-
49
- def clear_will_update
50
- @properties.each do |_, property|
51
- property.clear_will_update
40
+ def generate_all_properties
41
+ if @json.empty?
42
+ @properties.values
43
+ else
44
+ @json.keys.map { |key| self[key] }
52
45
  end
53
46
  end
54
47
 
@@ -81,5 +74,13 @@ module NotionRubyMapping
81
74
  end
82
75
  end
83
76
  end
77
+
78
+ # @param [Array] key
79
+ # @return [Array]
80
+ # @see https://www.notion.so/hkob/PropertyCache-2451fa64a814432db4809831cc77ba25#6eb10d1d85784063a30feb225f47ede3
81
+ def values_at(*key)
82
+ generate_all_properties
83
+ @properties.values_at(*key)
84
+ end
84
85
  end
85
86
  end
@@ -12,24 +12,13 @@ module NotionRubyMapping
12
12
  attr_reader :filter, :sort, :page_size
13
13
  attr_accessor :start_cursor
14
14
 
15
- # @param [Query] other_query other query
15
+ # @param [Query] another_query other query
16
16
  # @return [NotionRubyMapping::Query] updated self (Query object)
17
- def and(other_query)
17
+ def and(another_query)
18
18
  if @filter.key? "and"
19
- @filter["and"] << other_query.filter
19
+ @filter["and"] << another_query.filter
20
20
  else
21
- @filter = {"and" => [@filter, other_query.filter]}
22
- end
23
- self
24
- end
25
-
26
- # @param [Query] other_query other query
27
- # @return [NotionRubyMapping::Query] updated self (Query object)
28
- def or(other_query)
29
- if @filter.key? "or"
30
- @filter["or"] << other_query.filter
31
- else
32
- @filter = {"or" => [@filter, other_query.filter]}
21
+ @filter = {"and" => [@filter, another_query.filter]}
33
22
  end
34
23
  self
35
24
  end
@@ -50,14 +39,33 @@ module NotionRubyMapping
50
39
  self
51
40
  end
52
41
 
42
+ # @param [Query] other_query other query
43
+ # @return [NotionRubyMapping::Query] updated self (Query object)
44
+ def or(other_query)
45
+ if @filter.key? "or"
46
+ @filter["or"] << other_query.filter
47
+ else
48
+ @filter = {"or" => [@filter, other_query.filter]}
49
+ end
50
+ self
51
+ end
52
+
53
53
  # @return [Hash]
54
54
  def query_json
55
55
  parameters = {}
56
- parameters[:filter] = filter unless filter.empty?
57
- parameters[:sorts] = sort unless sort.empty?
58
- parameters[:start_cursor] = start_cursor if start_cursor
59
- parameters[:page_size] = page_size if page_size
56
+ parameters[:filter] = @filter unless @filter.empty?
57
+ parameters[:sorts] = @sort unless @sort.empty?
58
+ parameters[:start_cursor] = @start_cursor if @start_cursor
59
+ parameters[:page_size] = @page_size if @page_size
60
60
  parameters
61
61
  end
62
+
63
+ # @return [String (frozen)]
64
+ def query_string
65
+ ans = []
66
+ ans << "page_size=#{@page_size}" if @page_size
67
+ ans << "start_cursor=#{@start_cursor}" if @start_cursor
68
+ ans.empty? ? "" : "?#{ans.join("&")}"
69
+ end
62
70
  end
63
71
  end
@@ -19,6 +19,32 @@ module NotionRubyMapping
19
19
  end
20
20
  @will_update = will_update
21
21
  end
22
+ attr_writer :will_update
23
+
24
+ def self.rich_text_array(key, text_objects = nil)
25
+ if text_objects.nil?
26
+ RichTextArray.new key
27
+ elsif text_objects.is_a? RichTextArray
28
+ text_objects
29
+ else
30
+ RichTextArray.new key, text_objects: text_objects
31
+ end
32
+ end
33
+
34
+ # @param [String, RichTextObject] to
35
+ # @return [NotionRubyMapping::RichTextObject] added RichTextObject
36
+ def <<(to)
37
+ @will_update = true
38
+ rto = RichTextObject.text_object(to)
39
+ @rich_text_objects << rto
40
+ rto
41
+ end
42
+
43
+ # @param [Numeric] index index number
44
+ # @return [RichTextObject] selected RichTextObject
45
+ def [](index)
46
+ @rich_text_objects[index]
47
+ end
22
48
 
23
49
  # @param [Array] json
24
50
  # @return [Array] RichTextArray
@@ -46,6 +72,17 @@ module NotionRubyMapping
46
72
  map(&:text).join ""
47
73
  end
48
74
 
75
+ def rich_text_objects=(text_info)
76
+ @will_update = true
77
+ @rich_text_objects = if text_info.is_a? RichTextArray
78
+ text_info.filter { true }
79
+ else
80
+ Array(text_info).map do |to|
81
+ RichTextObject.text_object to
82
+ end
83
+ end
84
+ end
85
+
49
86
  def property_values_json
50
87
  will_update ? @rich_text_objects.map(&:property_values_json) : []
51
88
  end
@@ -54,28 +91,13 @@ module NotionRubyMapping
54
91
  will_update ? {@key => @rich_text_objects.map(&:property_values_json)} : {}
55
92
  end
56
93
 
57
- def update_property_schema_json
58
- will_update ? {@key => @rich_text_objects.map(&:property_values_json)} : {}
94
+ def update_property_schema_json(flag = false)
95
+ flag || will_update ? {@key => @rich_text_objects.map(&:property_values_json)} : {}
59
96
  end
60
97
 
61
98
  # @return [TrueClass, FalseClass] true if it will update
62
99
  def will_update
63
100
  @will_update || @rich_text_objects.map(&:will_update).any?
64
101
  end
65
-
66
- # @param [String, RichTextObject] to
67
- # @return [NotionRubyMapping::RichTextObject] added RichTextObject
68
- def <<(to)
69
- @will_update = true
70
- rto = RichTextObject.text_object(to)
71
- @rich_text_objects << rto
72
- rto
73
- end
74
-
75
- # @param [Numeric] index index number
76
- # @return [RichTextObject] selected RichTextObject
77
- def [](index)
78
- @rich_text_objects[index]
79
- end
80
102
  end
81
103
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # TextObject
5
+ class EmojiObject
6
+ # @param [String] emoji
7
+ # @return [TextObject]
8
+ def initialize(emoji: nil, json: {})
9
+ @emoji = emoji || json && json["emoji"]
10
+ @will_update = false
11
+ end
12
+ attr_reader :will_update, :emoji
13
+
14
+ # @param [EmojiObject, String] uo
15
+ # @return [EmojiObject] self or created EmojiObject
16
+ # @see https://www.notion.so/hkob/EmojiObject-4a0d41a10a81490f82471059c5b39b1b#959b7d74c1a54e488b39a96ac8f634e5
17
+ def self.emoji_object(emoji_or_eo)
18
+ if emoji_or_eo.is_a? EmojiObject
19
+ emoji_or_eo
20
+ else
21
+ EmojiObject.new emoji: emoji_or_eo
22
+ end
23
+ end
24
+
25
+ # @param [String] emoji
26
+ # @see https://www.notion.so/hkob/EmojiObject-4a0d41a10a81490f82471059c5b39b1b#b0520d80de084c1c99e8595db9d36542
27
+ def emoji=(emoji)
28
+ @emoji = emoji
29
+ @will_update = true
30
+ end
31
+
32
+ # @return [Hash]
33
+ def property_values_json
34
+ {
35
+ "type" => "emoji",
36
+ "emoji" => @emoji,
37
+ }
38
+ end
39
+ end
40
+ end