notion_ruby_mapping 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +176 -72
  3. data/env.yml.sample +6 -0
  4. data/images/post_set_icon.png +0 -0
  5. data/images/pre_set_icon.png +0 -0
  6. data/lib/notion_ruby_mapping/base.rb +76 -3
  7. data/lib/notion_ruby_mapping/checkbox_property.rb +1 -1
  8. data/lib/notion_ruby_mapping/created_by_property.rb +1 -1
  9. data/lib/notion_ruby_mapping/created_time_property.rb +1 -1
  10. data/lib/notion_ruby_mapping/database.rb +11 -2
  11. data/lib/notion_ruby_mapping/date_base_property.rb +12 -12
  12. data/lib/notion_ruby_mapping/date_property.rb +1 -1
  13. data/lib/notion_ruby_mapping/email_property.rb +1 -1
  14. data/lib/notion_ruby_mapping/files_property.rb +1 -1
  15. data/lib/notion_ruby_mapping/formula_property.rb +1 -1
  16. data/lib/notion_ruby_mapping/last_edited_by_property.rb +1 -1
  17. data/lib/notion_ruby_mapping/last_edited_time_property.rb +1 -1
  18. data/lib/notion_ruby_mapping/multi_select_property.rb +1 -1
  19. data/lib/notion_ruby_mapping/notion_cache.rb +44 -19
  20. data/lib/notion_ruby_mapping/number_property.rb +18 -1
  21. data/lib/notion_ruby_mapping/page.rb +9 -4
  22. data/lib/notion_ruby_mapping/payload.rb +28 -0
  23. data/lib/notion_ruby_mapping/people_property.rb +1 -1
  24. data/lib/notion_ruby_mapping/phone_number_property.rb +1 -1
  25. data/lib/notion_ruby_mapping/property.rb +52 -16
  26. data/lib/notion_ruby_mapping/property_cache.rb +38 -0
  27. data/lib/notion_ruby_mapping/query.rb +10 -10
  28. data/lib/notion_ruby_mapping/relation_property.rb +1 -1
  29. data/lib/notion_ruby_mapping/rich_text_property.rb +1 -1
  30. data/lib/notion_ruby_mapping/select_property.rb +1 -1
  31. data/lib/notion_ruby_mapping/title_property.rb +1 -1
  32. data/lib/notion_ruby_mapping/url_property.rb +1 -1
  33. data/lib/notion_ruby_mapping/version.rb +1 -1
  34. data/lib/notion_ruby_mapping.rb +3 -1
  35. metadata +7 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0262f773e54fec1b483554fb0d616706725ad422c10ce0830deeaf610ada1915
4
- data.tar.gz: 554e4b2e4750f2b574fbfc73b77d23825a57ae92bc715fe1e977fcf56882efb8
3
+ metadata.gz: c557600e1d6f89386696ffeadd7b3169fc0f3bfd19cc9aab29ce4e401d5b8969
4
+ data.tar.gz: 3dc90d601a14e96bc6c65b44a75231095cc52a7efc89067b5e37653133e546cf
5
5
  SHA512:
6
- metadata.gz: 504081f42de292dd7b462ed4565cfbc5c39ed6197c76d743081ffd847f5917328f5dbf63b50c1d7524f9fedc9457a12518a90422803996760256a4ac7f0aa549
7
- data.tar.gz: cd069974312f74381d12605632e9782705c859e6f698fe76641f8917fbe259a758cdfac748dbde23888e8b6cdb0a472b32c68ccce9678598c6768f47a48b5a61
6
+ metadata.gz: 3177e1c978dfda8968986afc96c4456a3842112d5de4d8b661074be3aaf3eb3a75b87297ac3b5701874091ac3444e8d88782747efa4ffbab02f3299875363d47
7
+ data.tar.gz: 3ad09dca8a9f818e35b8f8b1c611f89273efc4c9b34a78daf5fb34232372f5827afe5e307011380fa47263bc2de694461339d4b27793e4b2cb1af0fecdf75f6f
data/README.md CHANGED
@@ -1,8 +1,28 @@
1
1
  # NotionRubyMapping
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/notion_ruby_mapping`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Notion Ruby mapping is currently under development.
4
+
5
+ Development note is here. [Idea note of "notion_ruby_mapping"](https://www.notion.so/hkob/Idea-note-of-notion_ruby_mapping-3b0a3bb3c171438a830f9579d41df501)
6
+
7
+ ## Table of Contents
8
+ - [NotionRubyMapping](#notionrubymapping)
9
+ - [Installation](#installation)
10
+ - [Example code](#example-code)
11
+ - [Usage](#usage)
12
+ - [Create a New Integration](#create-a-new-integration)
13
+ - [Create client](#create-client)
14
+ - [Classes](#classes)
15
+ - [Base class](#base-class)
16
+ - [Database class](#database-class)
17
+ - [Query class](#query-class)
18
+ - [Page class](#page-class)
19
+ - [List class](#list-class)
20
+ - [Block class](#block-class)
21
+ - [ChangeLog](#changelog)
22
+ - [Contributing](#contributing)
23
+ - [License](#license)
24
+ - [Code of Conduct](#code-of-conduct)
25
+ - [Acknowledgements](#acknowledgements)
6
26
 
7
27
  ## Installation
8
28
 
@@ -20,6 +40,38 @@ Or install it yourself as:
20
40
 
21
41
  $ gem install notion_ruby_mapping
22
42
 
43
+ ## Example code
44
+
45
+ The following code sets a "💿" icon on all unset pages in the database.
46
+ ```Ruby
47
+ require "notion_ruby_mapping"
48
+
49
+ include NotionRubyMapping
50
+
51
+ token = ENV["NOTION_API_TOKEN"]
52
+ database_id = ENV["DATABASE_ID"]
53
+
54
+ NotionCache.instance.create_client token
55
+
56
+ Database.query(database_id).each do |page|
57
+ p page.set_icon(emoji: "💿").id unless page.icon
58
+ end
59
+ ```
60
+
61
+ The following code sets serial numbers to the pages whose title is not empty in ascending order of titles.
62
+ ```Ruby
63
+ tp = RichTextProperty.new("TextTitle")
64
+ Database.query(database_id, tp.filter_is_not_empty.ascending(tp)).each.with_index(1) do |page, index|
65
+ page.properties["NumberTitle"].number = index
66
+ page.update
67
+ end
68
+
69
+ ```
70
+
71
+ |Before execution|After execution|
72
+ |---|---|
73
+ |![Before execution](images/pre_set_icon.png)|![After execution](images/post_set_icon.png)|
74
+
23
75
  ## Usage
24
76
 
25
77
  ### Create a New Integration
@@ -35,6 +87,23 @@ Please create a client (notion-ruby-client) before you use the following class.
35
87
 
36
88
  ### Classes
37
89
 
90
+ #### Base class (Abstract class for Database / Page / Block / List)
91
+
92
+ - Set icon (only Database / Page)
93
+
94
+ ```Ruby
95
+ obj.set_icon emoji: "💿" # set emoji
96
+ obj.set_icon url: "https://cdn.profile-image.st-hatena.com/users/hkob/profile.png" # set external url
97
+ ```
98
+
99
+ - Get values and properties
100
+
101
+ ```Ruby
102
+ obj.icon # obtain icon json
103
+ obj["icon"] # same as obj.icon
104
+ obj.properties["NumberTitle"]
105
+ ```
106
+
38
107
  #### Database class
39
108
 
40
109
  - Retrieve a database
@@ -50,7 +119,7 @@ Database.query("c37a2c66-e3aa-4a0d-a447-73de3b80c253") # retrieves all pages
50
119
  Database.query("c37a2c66-e3aa-4a0d-a447-73de3b80c253", query) # retrieves using query
51
120
  ```
52
121
 
53
- #### Query class
122
+ #### Query class and related *Property class
54
123
 
55
124
  Query object can be generated from the following Property objects.
56
125
  For example, in order to obtain the pages whose title starts with "A" and ordered by ascending,
@@ -149,14 +218,14 @@ query1 = tp.filter_starts_with("start")
149
218
 
150
219
  # Result of query1.filter
151
220
  {
152
- and: [
221
+ "and" => [
153
222
  {
154
- property: "tp",
155
- title: {starts_with: "start"},
223
+ "property" => "tp",
224
+ "title" => {"starts_with" => "start"},
156
225
  },
157
226
  {
158
- property: "np",
159
- number: {greater_than: 100},
227
+ "property" => "np",
228
+ "number" => {"greater_than" => 100},
160
229
  },
161
230
  ],
162
231
  }
@@ -170,18 +239,18 @@ query2 = tp.filter_starts_with("start")
170
239
 
171
240
  # Result of query2.filter
172
241
  {
173
- and: [
242
+ "and" => [
174
243
  {
175
- property: "tp",
176
- title: {starts_with: "start"},
244
+ "property" => "tp",
245
+ "title" => {"starts_with" => "start"},
177
246
  },
178
247
  {
179
- property: "np",
180
- number: {greater_than: 100},
248
+ "property" => "np",
249
+ "number" => {"greater_than" => 100},
181
250
  },
182
251
  {
183
- property: "cp",
184
- checkbox: {equals: true},
252
+ "property" => "cp",
253
+ "checkbox" => {"equals" => true},
185
254
  },
186
255
  ],
187
256
  }
@@ -194,14 +263,14 @@ query3 = tp.filter_starts_with("start")
194
263
 
195
264
  # Result of query3.filter
196
265
  {
197
- or: [
266
+ "or" => [
198
267
  {
199
- property: "tp",
200
- title: {starts_with: "start"},
268
+ "property" => "tp",
269
+ "title" => {"starts_with" => "start"},
201
270
  },
202
271
  {
203
- property: "np",
204
- number: {greater_than: 100},
272
+ "property" => "np",
273
+ "number" => {"greater_than" => 100},
205
274
  },
206
275
  ],
207
276
  }
@@ -215,18 +284,18 @@ query4 = tp.filter_starts_with("start")
215
284
 
216
285
  # Result of query4.filter
217
286
  {
218
- or: [
287
+ "or" => [
219
288
  {
220
- property: "tp",
221
- title: {starts_with: "start"},
289
+ "property" => "tp",
290
+ "title" => {"starts_with" => "start"},
222
291
  },
223
292
  {
224
- property: "np",
225
- number: {greater_than: 100},
293
+ "property" => "np",
294
+ "number" => {"greater_than" => 100},
226
295
  },
227
296
  {
228
- property: "cp",
229
- checkbox: {equals: true},
297
+ "property" => "cp",
298
+ "checkbox" => {"equals" => true},
230
299
  },
231
300
  ],
232
301
  }
@@ -240,22 +309,22 @@ query5 = tp.filter_starts_with("start")
240
309
 
241
310
  # Result of query5.filter
242
311
  {
243
- or: [
312
+ "or" => [
244
313
  {
245
- and: [
314
+ "and" => [
246
315
  {
247
- property: "tp",
248
- title: {starts_with: "start"},
316
+ "property" => "tp",
317
+ "title" => {"starts_with" => "start"},
249
318
  },
250
319
  {
251
- property: "np",
252
- number: {greater_than: 100},
320
+ "property" => "np",
321
+ "number" => {"greater_than" => 100},
253
322
  },
254
323
  ],
255
324
  },
256
325
  {
257
- property: "cp",
258
- checkbox: {equals: true},
326
+ "property" => "cp",
327
+ "checkbox" => {"equals" => true},
259
328
  },
260
329
  ],
261
330
  }
@@ -269,22 +338,22 @@ query6 = tp.filter_starts_with("start")
269
338
 
270
339
  # Result of query6.filter
271
340
  {
272
- and: [
341
+ "and" => [
273
342
  {
274
- or: [
343
+ "or" => [
275
344
  {
276
- property: "tp",
277
- title: {starts_with: "start"},
345
+ "property" => "tp",
346
+ "title" => {"starts_with" => "start"},
278
347
  },
279
348
  {
280
- property: "np",
281
- number: {greater_than: 100},
349
+ "property" => "np",
350
+ "number" => {"greater_than" => 100},
282
351
  },
283
352
  ],
284
353
  },
285
354
  {
286
- property: "cp",
287
- checkbox: {equals: true},
355
+ "property" => "cp",
356
+ "checkbox" => {"equals" => true},
288
357
  },
289
358
  ],
290
359
  }
@@ -297,28 +366,28 @@ query7 = np.filter_greater_than(100).and(np.filter_less_than(200))
297
366
 
298
367
  # Result of query7.filter
299
368
  {
300
- or: [
369
+ "or" => [
301
370
  {
302
- and: [
371
+ "and" => [
303
372
  {
304
- property: "np",
305
- number: {greater_than: 100},
373
+ "property" => "np",
374
+ "number" => {"greater_than" => 100},
306
375
  },
307
376
  {
308
- property: "np",
309
- number: {less_than: 200},
377
+ "property" => "np",
378
+ "number" => {"less_than" => 200},
310
379
  },
311
380
  ],
312
381
  },
313
382
  {
314
- and: [
383
+ "and" => [
315
384
  {
316
- property: "np",
317
- number: {greater_than: 300},
385
+ "property" => "np",
386
+ "number" => {"greater_than" => 300},
318
387
  },
319
388
  {
320
- property: "np",
321
- number: {less_than: 400},
389
+ "property" => "np",
390
+ "number" => {"less_than" => 400},
322
391
  },
323
392
  ],
324
393
  },
@@ -335,24 +404,35 @@ query11 = Query.new.descending letp
335
404
  query12 = Query.new.ascending(tp).descending letp
336
405
 
337
406
  # Result of query8.sort
338
- [{property: "tp", direction: "ascending"}]
407
+ [{"property" => "tp", "direction" => "ascending"}]
339
408
 
340
409
  # Result of query9.sort
341
- [{timestamp: "letp", direction: "ascending"}]
410
+ [{"timestamp" => "letp", "direction" => "ascending"}]
342
411
 
343
412
  # Result of query10.sort
344
- [{property: "tp", direction: "descending"}]
413
+ [{"property" => "tp", "direction" => "descending"}]
345
414
 
346
415
  # Result of query11.sort
347
- [{timestamp: "letp", direction: "descending"}]
416
+ [{"timestamp" => "letp", "direction" => "descending"}]
348
417
 
349
418
  # Result of query12.sort
350
419
  [
351
- {property: "tp", direction: "ascending"},
352
- {timestamp: "letp", direction: "descending"},
420
+ {"property" => "tp", "direction" => "ascending"},
421
+ {"timestamp" => "letp", "direction" => "descending"},
353
422
  ]
354
423
  ```
355
424
 
425
+ - filter with sort
426
+ ```Ruby
427
+ query13 = tp.filter_starts_with("A").ascending(tp)
428
+
429
+ # Result of query13.filter
430
+ {"property" => "tp", "title" => {"starts_with" => "start"}},
431
+
432
+ # Result of query13.sort
433
+ [{"property" => "tp", "direction" => "ascending"}]
434
+ ```
435
+
356
436
  #### Page class
357
437
 
358
438
  - Retrieve a page
@@ -360,16 +440,38 @@ query12 = Query.new.ascending(tp).descending letp
360
440
  page = Page.find("c01166c6-13ae-45cb-b968-18b4ef2f5a77")
361
441
  ```
362
442
 
363
- - Retrieve block children (List object)
443
+ - Update values and properties
444
+
445
+ Page properties can update in the following three ways.
446
+
447
+ 1. update the property directory (fastest: one API call only)
364
448
  ```Ruby
365
- children = page.children
449
+ page = Page.new id: page_id
450
+ np = NumberProperty.new "NumberTitle", number: 3.14
451
+ page.add_property_for_update np
452
+ page.update # update page API call
453
+ print page
454
+ ```
455
+
456
+ 2. update the loaded page (easy but slow: two API call)
457
+ ```Ruby
458
+ page = Page.find first_page_id # retrieve page API call
459
+ page.properties["NumberTitle"].number = 2022
460
+ page.update # update page API call
461
+ print page
366
462
  ```
367
463
 
368
- - Set icon
464
+ 3. update the unloaded page using autoload (easy but slow: two API call)
465
+ ```Ruby
466
+ page = Page.new id: first_page_id
467
+ page.properties["NumberTitle"].number = 12345 # retrieve page API call (autoload)
468
+ page.update # update page API call
469
+ print page
470
+ ```
369
471
 
472
+ - Retrieve block children (List object)
370
473
  ```Ruby
371
- page.set_icon emoji: "💿"
372
- page.set_icon url: "https://cdn.profile-image.st-hatena.com/users/hkob/profile.png"
474
+ children = page.children
373
475
  ```
374
476
 
375
477
  #### List class
@@ -381,19 +483,21 @@ list.each do |obj| # obj's class is Page or Block
381
483
  end
382
484
  ```
383
485
 
384
- ### Block class
486
+ #### Block class
385
487
 
386
488
  Not implemented
387
489
 
388
- ## Development
389
-
390
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
490
+ ## ChangeLog
391
491
 
392
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
492
+ - 2022/2/17 added Page#properties, Page#add_property_for_update, Page#update
493
+ - 2022/2/16 added PropertyCache and Payload class
494
+ - 2022/2/14 added Database#set_icon
495
+ - 2022/2/13 added Page#set_icon
496
+ - 2022/2/13 First commit
393
497
 
394
498
  ## Contributing
395
499
 
396
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/notion_ruby_mapping. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/notion_ruby_mapping/blob/main/CODE_OF_CONDUCT.md).
500
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hkob/notion_ruby_mapping. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/notion_ruby_mapping/blob/main/CODE_OF_CONDUCT.md).
397
501
 
398
502
  ## License
399
503
 
data/env.yml.sample ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ notion_token: "secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
3
+ top_page: "c01166c613ae45cbb96818b4ef2f5a77"
4
+ database: "c37a2c66e3aa4a0da44773de3b80c253"
5
+ h1block: "0250fb6d600142eca4c74efb8794fc6b"
6
+ db_first_page: "dcdc805c85fa4155a55c20fc28771af7"
Binary file
Binary file
@@ -7,11 +7,13 @@ module NotionRubyMapping
7
7
  @nc = NotionCache.instance
8
8
  @json = json
9
9
  @id = @nc.hex_id(id || @json["id"])
10
+ @payload = nil
11
+ @property_cache = nil
10
12
  end
11
13
  attr_reader :json, :id
12
14
 
13
- # @param [Object] json
14
- # @return [NotionRubyMapping::Block, NotionRubyMapping::List, NotionRubyMapping::Database, NotionRubyMapping::Page]
15
+ # @param [Hash, Notion::Messages] json
16
+ # @return [NotionRubyMapping::Base]
15
17
  def self.create_from_json(json)
16
18
  case json["object"]
17
19
  when "page"
@@ -27,23 +29,94 @@ module NotionRubyMapping
27
29
  end
28
30
  end
29
31
 
32
+ # @return [NotionRubyMapping::Payload] get or created Payload object
33
+ def payload
34
+ @payload ||= Payload.new
35
+ end
36
+
37
+ # @return [NotionRubyMapping::PropertyCache] get or created PropertyCache object
38
+ def properties
39
+ unless @property_cache
40
+ unless @json
41
+ return nil if @id.nil?
42
+
43
+ update_json reload
44
+ end
45
+ @property_cache = PropertyCache.new json_properties
46
+ end
47
+ @property_cache
48
+ end
49
+
50
+ # @return [Hash] json properties
51
+ def json_properties
52
+ @json && @json["properties"]
53
+ end
54
+
30
55
  # @return [NotionRubyMapping::List]
31
56
  def children
32
57
  @children ||= @nc.block_children(id)
33
58
  end
34
59
 
60
+ # @param [Hash] json
61
+ # @return [NotionRubyMapping::Base]
35
62
  def update_json(json)
36
63
  if @json.nil? || @json["type"] == json["type"]
37
64
  @json = json
65
+ @id = @nc.hex_id(@json["id"])
38
66
  clear_object
39
67
  end
68
+ self
40
69
  end
41
70
 
71
+ # @return [NotionRubyMapping::Base]
42
72
  def clear_object
73
+ @payload = nil
74
+ @property_cache = nil
75
+ self
76
+ end
77
+
78
+ # @param [String] emoji
79
+ # @param [String] url
80
+ # @return [NotionRubyMapping::Base]
81
+ def set_icon(emoji: nil, url: nil)
82
+ if self.is_a?(Page) || self.is_a?(Database)
83
+ payload.set_icon(emoji: emoji, url: url)
84
+ update
85
+ end
86
+ self
43
87
  end
88
+
89
+ # @param [String] key
90
+ # @return [NotionRubyMapping::PropertyCache, Hash] obtained Page value or PropertyCache
91
+ def [](key)
92
+ unless @json
93
+ return nil if @id.nil?
44
94
 
95
+ update_json reload
96
+ end
97
+ case key
98
+ when "properties"
99
+ properties
100
+ else
101
+ @json[key]
102
+ end
103
+ end
104
+
105
+ # @return [Hash, nil] obtained Hash
45
106
  def icon
46
- @json["icon"]
107
+ self["icon"]
108
+ end
109
+
110
+ # @param [Property] property Property object for udpate or create
111
+ # @return [NotionRubyMapping::Base]
112
+ def add_property_for_update(property)
113
+ properties.add_property property, will_update: true
114
+ self
115
+ end
116
+
117
+ # @return [Hash] created json
118
+ def create_json
119
+ payload.create_json @property_cache&.create_json
47
120
  end
48
121
  end
49
122
  end
@@ -4,6 +4,6 @@ module NotionRubyMapping
4
4
  # Checkbox property
5
5
  class CheckboxProperty < Property
6
6
  include EqualsDoesNotEqual
7
- TYPE = :checkbox
7
+ TYPE = "checkbox"
8
8
  end
9
9
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class CreatedByProperty < MultiProperty
5
- TYPE = :created_by
5
+ TYPE = "created_by"
6
6
  end
7
7
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class CreatedTimeProperty < DateBaseProperty
5
- TYPE = :created_time
5
+ TYPE = "created_time"
6
6
  end
7
7
  end
@@ -3,13 +3,22 @@
3
3
  module NotionRubyMapping
4
4
  # Notion database
5
5
  class Database < Base
6
- def self.find(key)
7
- NotionCache.instance.database key
6
+ def self.find(id)
7
+ NotionCache.instance.database id
8
8
  end
9
9
 
10
+ # @param [String] id database_id (with or without "-")
11
+ # @param [NotionRubyMapping::Query] query object
10
12
  def self.query(id, query = nil)
11
13
  query ||= Query.new
12
14
  NotionCache.instance.database_query(id, query)
13
15
  end
16
+
17
+ # @param [String] id database_id (with or without "-")
18
+ # @param [Payload] payload
19
+ def update
20
+ update_json @nc.update_database @id, @payload.create_json
21
+ end
14
22
  end
15
23
  end
24
+
@@ -23,51 +23,51 @@ module NotionRubyMapping
23
23
  end
24
24
 
25
25
  def filter_equals(value)
26
- make_filter_query :equals, value_str(value)
26
+ make_filter_query "equals", value_str(value)
27
27
  end
28
28
 
29
29
  def filter_does_not_equal(value)
30
- make_filter_query :does_not_equal, value_str(value)
30
+ make_filter_query "does_not_equal", value_str(value)
31
31
  end
32
32
 
33
33
  def filter_before(value)
34
- make_filter_query :before, value_str(value)
34
+ make_filter_query "before", value_str(value)
35
35
  end
36
36
 
37
37
  def filter_after(value)
38
- make_filter_query :after, value_str(value)
38
+ make_filter_query "after", value_str(value)
39
39
  end
40
40
 
41
41
  def filter_on_or_before(value)
42
- make_filter_query :on_or_before, value_str(value)
42
+ make_filter_query "on_or_before", value_str(value)
43
43
  end
44
44
 
45
45
  def filter_on_or_after(value)
46
- make_filter_query :on_or_after, value_str(value)
46
+ make_filter_query "on_or_after", value_str(value)
47
47
  end
48
48
 
49
49
  def filter_past_week
50
- make_filter_query :past_week, {}
50
+ make_filter_query "past_week", {}
51
51
  end
52
52
 
53
53
  def filter_past_month
54
- make_filter_query :past_month, {}
54
+ make_filter_query "past_month", {}
55
55
  end
56
56
 
57
57
  def filter_past_year
58
- make_filter_query :past_year, {}
58
+ make_filter_query "past_year", {}
59
59
  end
60
60
 
61
61
  def filter_next_week
62
- make_filter_query :next_week, {}
62
+ make_filter_query "next_week", {}
63
63
  end
64
64
 
65
65
  def filter_next_month
66
- make_filter_query :next_month, {}
66
+ make_filter_query "next_month", {}
67
67
  end
68
68
 
69
69
  def filter_next_year
70
- make_filter_query :next_year, {}
70
+ make_filter_query "next_year", {}
71
71
  end
72
72
  end
73
73
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class DateProperty < DateBaseProperty
5
- TYPE = :date
5
+ TYPE = "date"
6
6
  end
7
7
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class EmailProperty < TextProperty
5
- TYPE = :email
5
+ TYPE = "email"
6
6
  end
7
7
  end
@@ -4,6 +4,6 @@ module NotionRubyMapping
4
4
  # Select property
5
5
  class FilesProperty < Property
6
6
  include IsEmptyIsNotEmpty
7
- TYPE = :files
7
+ TYPE = "files"
8
8
  end
9
9
  end
@@ -6,6 +6,6 @@ module NotionRubyMapping
6
6
  include ContainsDoesNotContain
7
7
  include StartsWithEndsWith
8
8
  include GreaterThanLessThan
9
- TYPE = :formula
9
+ TYPE = "formula"
10
10
  end
11
11
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class LastEditedByProperty < MultiProperty
5
- TYPE = :last_edited_by
5
+ TYPE = "last_edited_by"
6
6
  end
7
7
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class LastEditedTimeProperty < DateBaseProperty
5
- TYPE = :last_edited_time
5
+ TYPE = "last_edited_time"
6
6
  end
7
7
  end
@@ -3,6 +3,6 @@
3
3
  module NotionRubyMapping
4
4
  # MultiSelect property
5
5
  class MultiSelectProperty < MultiProperty
6
- TYPE = :multi_select
6
+ TYPE = "multi_select"
7
7
  end
8
8
  end
@@ -38,40 +38,52 @@ module NotionRubyMapping
38
38
 
39
39
  begin
40
40
  json = yield(@client)
41
- p json
42
41
  @object_hash[key] = Base.create_from_json json
43
42
  rescue StandardError
44
43
  nil
45
44
  end
46
45
  end
47
46
 
48
- # @param [String] id
47
+ # @param [String] id page_id (with or without "-")
48
+ # @return [Hash] obtained json
49
+ def page_json(id)
50
+ sleep @wait
51
+ @client.page page_id: id
52
+ end
53
+
54
+ # @param [String] id page_id (with or without "-")
49
55
  # @return [NotionRubyMapping::Page, nil] Page object or nil
50
56
  def page(id)
51
- object_for_key(id) do
52
- sleep @wait
53
- @client.page page_id: id
54
- end
57
+ object_for_key(id) { page_json id }
55
58
  end
56
59
 
57
- # @param [String] id
60
+ # @param [String] id database_id (with or without "-")
61
+ # @return [Hash] obtained json
62
+ def database_json(id)
63
+ sleep @wait
64
+ @client.database database_id: id
65
+ end
66
+
67
+ # @param [String] id database_id (with or without "-")
58
68
  # @return [NotionRubyMapping::Database, nil] Database object or nil
59
69
  def database(id)
60
- object_for_key(id) do
61
- sleep @wait
62
- @client.database database_id: id
63
- end
70
+ object_for_key(id) { database_json id }
64
71
  end
65
72
 
66
- # @param [String] id
67
- # @return [NotionRubyMapping::Block, nil]
73
+ # @param [String] id block_id (with or without "-")
74
+ # @return [Hash] obtained json
75
+ def block_json(id)
76
+ sleep @wait
77
+ @client.block block_id: id
78
+ end
79
+ # @param [String] id block_id (with or without "-")
80
+ # @return [NotionRubyMapping::Block, nil] Block object or nil
68
81
  def block(id)
69
- object_for_key(id) do
70
- sleep @wait
71
- @client.block block_id: id
72
- end
82
+ object_for_key(id) { block_json id }
73
83
  end
74
84
 
85
+ # @param [String] id page_id / block_id (with or without "-")
86
+ # @return [NotionRubyMapping::List] List object
75
87
  def block_children(id)
76
88
  array = []
77
89
  sleep @wait
@@ -81,20 +93,33 @@ module NotionRubyMapping
81
93
  Base.create_from_json({"object" => "list", "results" => array})
82
94
  end
83
95
 
96
+ # @param [String] id page_id / block_id (with or without "-")
97
+ # @param [NotionRubyMapping::Query] query query object
98
+ # @return [NotionRubyMapping::List] List object
84
99
  def database_query(id, query)
85
100
  array = []
86
101
  parameters = {database_id: id, sleep_interval: @wait, max_retries: 20}
87
102
  parameters[:filter] = query.filter unless query.filter.empty?
88
- parameters[:sort] = query.sort unless query.sort.empty?
89
- @client.database_query(**parameters) do |page|
103
+ parameters[:sorts] = query.sort unless query.sort.empty?
104
+
105
+ @client.database_query(parameters) do |page|
90
106
  array.concat page.results
91
107
  end
92
108
  Base.create_from_json({"object" => "list", "results" => array})
93
109
  end
94
110
 
111
+ # @param [String] id page_id (with or without "-")
112
+ # @param [Hash] payload
95
113
  def update_page(id, payload)
96
114
  sleep @wait
97
115
  @client.update_page payload.merge({page_id: id})
98
116
  end
117
+
118
+ # @param [String] id page_id (with or without "-")
119
+ # @param [Hash] payload
120
+ def update_database(id, payload)
121
+ sleep @wait
122
+ @client.update_database payload.merge({database_id: id})
123
+ end
99
124
  end
100
125
  end
@@ -6,6 +6,23 @@ module NotionRubyMapping
6
6
  include EqualsDoesNotEqual
7
7
  include GreaterThanLessThan
8
8
  include IsEmptyIsNotEmpty
9
- TYPE = :number
9
+ TYPE = "number"
10
+
11
+ # @param [String] name Property name
12
+ # @param [Number, Fixnum] number Number value (optional)
13
+ # @param [String] format Format string (optional, default: "number")
14
+ def initialize(name, number: nil)
15
+ super(name)
16
+ @number = number
17
+ end
18
+
19
+ def create_json
20
+ {"type" => "number", "number" => @number}
21
+ end
22
+
23
+ def number=(n)
24
+ @will_update = true
25
+ @number = n
26
+ end
10
27
  end
11
28
  end
@@ -7,10 +7,15 @@ module NotionRubyMapping
7
7
  NotionCache.instance.page key
8
8
  end
9
9
 
10
- def set_icon(emoji: nil, url: nil)
11
- payload = emoji ? {type: :emoji, emoji: emoji} : {type: :external, external: {url: url}}
12
- update_json @nc.update_page(id, {icon: payload})
13
- self
10
+ # @param [String] id page_id (with or without "-")
11
+ # @param [Payload] payload
12
+ def update
13
+ update_json @nc.update_page(@id, create_json)
14
+ end
15
+
16
+ # @return [Hash]
17
+ def reload
18
+ @nc.page_json @id
14
19
  end
15
20
  end
16
21
  end
@@ -0,0 +1,28 @@
1
+ module NotionRubyMapping
2
+ class Payload
3
+ def initialize
4
+ @json = {}
5
+ end
6
+
7
+ # @param [String] emoji
8
+ # @param [String] url
9
+ # @return [NotionRubyMapping::Payload] updated Payload
10
+ def set_icon(emoji: nil, url: nil)
11
+ payload = if emoji
12
+ {"type" => "emoji", "emoji" => emoji}
13
+ elsif url
14
+ {"type" => "external", "external" => {"url" => url}}
15
+ else
16
+ {}
17
+ end
18
+ @json["icon"] = payload
19
+ self
20
+ end
21
+
22
+ # @return [Hash] created json
23
+ # @param optional [Hash] optional_json
24
+ def create_json(optional_json = nil)
25
+ @json.merge(optional_json || {})
26
+ end
27
+ end
28
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class PeopleProperty < MultiProperty
5
- TYPE = :people
5
+ TYPE = "people"
6
6
  end
7
7
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class PhoneNumberProperty < TextProperty
5
- TYPE = :phone_number
5
+ TYPE = "phone_number"
6
6
  end
7
7
  end
@@ -3,86 +3,122 @@
3
3
  module NotionRubyMapping
4
4
  # abstract class for property
5
5
  class Property
6
+ # @param [String] name Property name
7
+ # @return [Property] generated Property object
6
8
  def initialize(name)
7
9
  @name = name
10
+ @will_update = false
8
11
  end
9
12
  attr_reader :name
13
+ attr_accessor :will_update
10
14
 
11
- # @param [Symbol] key query parameter
15
+ # @param [String] key query parameter
12
16
  # @param [Object] value query value
13
17
  # @return [NotionRubyMapping::Query] generated Query object
14
18
  def make_filter_query(key, value)
15
- Query.new(filter: {property: @name, type => {key => value}})
19
+ Query.new(filter: {"property" => @name, type => {key => value}})
16
20
  end
17
21
 
18
22
  # @return [Symbol] property type
19
23
  def type
20
24
  self.class::TYPE
21
25
  end
26
+
27
+ # @param [String] key
28
+ # @param [Hash] json
29
+ # @return [NotionRubyMapping::NumberProperty, nil] generated Property object
30
+ def self.create_from_json(key, json)
31
+ case json["type"]
32
+ when "number"
33
+ NumberProperty.new key, number: json["number"]
34
+ else
35
+ nil
36
+ end
37
+ end
22
38
  end
23
39
 
24
40
  # module for make query of equals and does_not_equal
25
41
  module EqualsDoesNotEqual
26
- # @param [String, Number] value
27
- # @return [Object]
42
+ # @param [String, Number] value Query value
43
+ # @return [NotionRubyMapping::Query] generated Query object
28
44
  def filter_equals(value)
29
- make_filter_query :equals, value
45
+ make_filter_query "equals", value
30
46
  end
31
47
 
48
+ # @param [String, Number] value Query value
49
+ # @return [NotionRubyMapping::Query] generated Query object
32
50
  def filter_does_not_equal(value)
33
- make_filter_query :does_not_equal, value
51
+ make_filter_query "does_not_equal", value
34
52
  end
35
53
  end
36
54
 
37
55
  # module for make query of contains and does_not_contain
38
56
  module ContainsDoesNotContain
57
+ # @param [String] value Query value
58
+ # @return [NotionRubyMapping::Query] generated Query object
39
59
  def filter_contains(value)
40
- make_filter_query :contains, value
60
+ make_filter_query "contains", value
41
61
  end
42
62
 
63
+ # @param [String] value Query value
64
+ # @return [NotionRubyMapping::Query] generated Query object
43
65
  def filter_does_not_contain(value)
44
- make_filter_query :does_not_contain, value
66
+ make_filter_query "does_not_contain", value
45
67
  end
46
68
  end
47
69
 
48
70
  # module for make query of starts_with and ends_with
49
71
  module StartsWithEndsWith
72
+ # @param [String] value Query value
73
+ # @return [NotionRubyMapping::Query] generated Query object
50
74
  def filter_starts_with(value)
51
- make_filter_query :starts_with, value
75
+ make_filter_query "starts_with", value
52
76
  end
53
77
 
78
+ # @param [String] value Query value
79
+ # @return [NotionRubyMapping::Query] generated Query object
54
80
  def filter_ends_with(value)
55
- make_filter_query :ends_with, value
81
+ make_filter_query "ends_with", value
56
82
  end
57
83
  end
58
84
 
59
85
  # module for make query of is_empty and is_not_empty
60
86
  module IsEmptyIsNotEmpty
87
+ # @return [NotionRubyMapping::Query] generated Query object
61
88
  def filter_is_empty
62
- make_filter_query :is_empty, true
89
+ make_filter_query "is_empty", true
63
90
  end
64
91
 
92
+ # @return [NotionRubyMapping::Query] generated Query object
65
93
  def filter_is_not_empty
66
- make_filter_query :is_not_empty, true
94
+ make_filter_query "is_not_empty", true
67
95
  end
68
96
  end
69
97
 
70
98
  # module for make query of starts_with and ends_with
71
99
  module GreaterThanLessThan
100
+ # @param [Number] value Query value
101
+ # @return [NotionRubyMapping::Query] generated Query object
72
102
  def filter_greater_than(value)
73
- make_filter_query :greater_than, value
103
+ make_filter_query "greater_than", value
74
104
  end
75
105
 
106
+ # @param [Number] value Query value
107
+ # @return [NotionRubyMapping::Query] generated Query object
76
108
  def filter_less_than(value)
77
- make_filter_query :less_than, value
109
+ make_filter_query "less_than", value
78
110
  end
79
111
 
112
+ # @param [Number] value Query value
113
+ # @return [NotionRubyMapping::Query] generated Query object
80
114
  def filter_greater_than_or_equal_to(value)
81
- make_filter_query :greater_than_or_equal_to, value
115
+ make_filter_query "greater_than_or_equal_to", value
82
116
  end
83
117
 
118
+ # @param [Number] value Query value
119
+ # @return [NotionRubyMapping::Query] generated Query object
84
120
  def filter_less_than_or_equal_to(value)
85
- make_filter_query :less_than_or_equal_to, value
121
+ make_filter_query "less_than_or_equal_to", value
86
122
  end
87
123
  end
88
124
  end
@@ -0,0 +1,38 @@
1
+ module NotionRubyMapping
2
+ class PropertyCache
3
+ def initialize(json = {})
4
+ @properties = {}
5
+ @json = json
6
+ end
7
+
8
+ # @param [String] key
9
+ # @return [Property] Property for key
10
+ def [](key)
11
+ ans = @properties[key]
12
+ unless ans
13
+ if @json && @json[key]
14
+ @properties[key] = Property.create_from_json key, @json[key]
15
+ end
16
+ end
17
+ @properties[key]
18
+ end
19
+
20
+ # @param [Property] property added Property
21
+ # @param [FalseClass] will_update true if the property value will update to Notion
22
+ def add_property(property, will_update: false)
23
+ @properties[property.name] = property
24
+ property.will_update = true if will_update
25
+ self
26
+ end
27
+
28
+ # @return [Hash] created json
29
+ def create_json
30
+ @properties.each_with_object({}) do |(key, property), ans|
31
+ if property.will_update
32
+ ans["properties"] ||= {}
33
+ ans["properties"][key] = property.create_json
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -12,10 +12,10 @@ module NotionRubyMapping
12
12
  # @param [Query] other_query other query
13
13
  # @return [NotionRubyMapping::Query] updated self (Query object)
14
14
  def and(other_query)
15
- if @filter.key? :and
16
- @filter[:and] << other_query.filter
15
+ if @filter.key? "and"
16
+ @filter["and"] << other_query.filter
17
17
  else
18
- @filter = {and: [@filter, other_query.filter]}
18
+ @filter = {"and" => [@filter, other_query.filter]}
19
19
  end
20
20
  self
21
21
  end
@@ -23,10 +23,10 @@ module NotionRubyMapping
23
23
  # @param [Query] other_query other query
24
24
  # @return [NotionRubyMapping::Query] updated self (Query object)
25
25
  def or(other_query)
26
- if @filter.key? :or
27
- @filter[:or] << other_query.filter
26
+ if @filter.key? "or"
27
+ @filter["or"] << other_query.filter
28
28
  else
29
- @filter = {or: [@filter, other_query.filter]}
29
+ @filter = {"or" => [@filter, other_query.filter]}
30
30
  end
31
31
  self
32
32
  end
@@ -34,16 +34,16 @@ module NotionRubyMapping
34
34
  # @param [NotionRubyMapping::Property] property
35
35
  # @return [NotionRubyMapping::Query] updated self (Query object)
36
36
  def ascending(property)
37
- key = property.is_a?(LastEditedTimeProperty) || property.is_a?(CreatedTimeProperty) ? :timestamp : :property
38
- @sort << {key => property.name, direction: "ascending"}
37
+ key = property.is_a?(LastEditedTimeProperty) || property.is_a?(CreatedTimeProperty) ? "timestamp" : "property"
38
+ @sort << {key => property.name, "direction" => "ascending"}
39
39
  self
40
40
  end
41
41
 
42
42
  # @param [NotionRubyMapping::Property] property
43
43
  # @return [NotionRubyMapping::Query] updated self (Query object)
44
44
  def descending(property)
45
- key = property.is_a?(LastEditedTimeProperty) || property.is_a?(CreatedTimeProperty) ? :timestamp : :property
46
- @sort << {key => property.name, direction: "descending"}
45
+ key = property.is_a?(LastEditedTimeProperty) || property.is_a?(CreatedTimeProperty) ? "timestamp" : "property"
46
+ @sort << {key => property.name, "direction" => "descending"}
47
47
  self
48
48
  end
49
49
  end
@@ -3,6 +3,6 @@
3
3
  module NotionRubyMapping
4
4
  # MultiSelect property
5
5
  class RelationProperty < MultiProperty
6
- TYPE = :relation
6
+ TYPE = "relation"
7
7
  end
8
8
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class RichTextProperty < TextProperty
5
- TYPE = :rich_text
5
+ TYPE = "rich_text"
6
6
  end
7
7
  end
@@ -5,6 +5,6 @@ module NotionRubyMapping
5
5
  class SelectProperty < Property
6
6
  include EqualsDoesNotEqual
7
7
  include IsEmptyIsNotEmpty
8
- TYPE = :select
8
+ TYPE = "select"
9
9
  end
10
10
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class TitleProperty < TextProperty
5
- TYPE = :title
5
+ TYPE = "title"
6
6
  end
7
7
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module NotionRubyMapping
4
4
  class UrlProperty < TextProperty
5
- TYPE = :url
5
+ TYPE = "url"
6
6
  end
7
7
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "yaml"
4
+
3
5
  %w[version notion_cache base page database list block property text_property title_property rich_text_property
4
6
  url_property email_property phone_number_property number_property checkbox_property select_property
5
7
  multi_property multi_select_property date_base_property date_property created_time_property last_edited_time_property
6
8
  people_property created_by_property last_edited_by_property files_property relation_property formula_property
7
- query].each do |k|
9
+ query payload property_cache.rb].each do |k|
8
10
  require_relative "notion_ruby_mapping/#{k}"
9
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notion_ruby_mapping
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroyuki KOBAYASHI
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-14 00:00:00.000000000 Z
11
+ date: 2022-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: notion-ruby-client
@@ -112,6 +112,9 @@ files:
112
112
  - Rakefile
113
113
  - bin/console
114
114
  - bin/setup
115
+ - env.yml.sample
116
+ - images/post_set_icon.png
117
+ - images/pre_set_icon.png
115
118
  - lib/notion_ruby_mapping.rb
116
119
  - lib/notion_ruby_mapping/base.rb
117
120
  - lib/notion_ruby_mapping/block.rb
@@ -132,9 +135,11 @@ files:
132
135
  - lib/notion_ruby_mapping/notion_cache.rb
133
136
  - lib/notion_ruby_mapping/number_property.rb
134
137
  - lib/notion_ruby_mapping/page.rb
138
+ - lib/notion_ruby_mapping/payload.rb
135
139
  - lib/notion_ruby_mapping/people_property.rb
136
140
  - lib/notion_ruby_mapping/phone_number_property.rb
137
141
  - lib/notion_ruby_mapping/property.rb
142
+ - lib/notion_ruby_mapping/property_cache.rb
138
143
  - lib/notion_ruby_mapping/query.rb
139
144
  - lib/notion_ruby_mapping/relation_property.rb
140
145
  - lib/notion_ruby_mapping/rich_text_property.rb