notion_ruby_mapping 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/Guardfile +1 -1
  3. data/README.md +550 -197
  4. data/env.yml.sample +7 -0
  5. data/lib/notion_ruby_mapping/base.rb +45 -25
  6. data/lib/notion_ruby_mapping/checkbox_property.rb +26 -0
  7. data/lib/notion_ruby_mapping/created_by_property.rb +21 -0
  8. data/lib/notion_ruby_mapping/created_time_property.rb +20 -0
  9. data/lib/notion_ruby_mapping/database.rb +8 -8
  10. data/lib/notion_ruby_mapping/date_base_property.rb +40 -38
  11. data/lib/notion_ruby_mapping/date_property.rb +61 -0
  12. data/lib/notion_ruby_mapping/email_property.rb +30 -1
  13. data/lib/notion_ruby_mapping/files_property.rb +40 -0
  14. data/lib/notion_ruby_mapping/formula_property.rb +18 -0
  15. data/lib/notion_ruby_mapping/last_edited_by_property.rb +21 -0
  16. data/lib/notion_ruby_mapping/last_edited_time_property.rb +20 -0
  17. data/lib/notion_ruby_mapping/list.rb +33 -2
  18. data/lib/notion_ruby_mapping/mention_object.rb +49 -0
  19. data/lib/notion_ruby_mapping/multi_select_property.rb +25 -6
  20. data/lib/notion_ruby_mapping/notion_cache.rb +126 -53
  21. data/lib/notion_ruby_mapping/number_property.rb +13 -8
  22. data/lib/notion_ruby_mapping/page.rb +5 -2
  23. data/lib/notion_ruby_mapping/payload.rb +10 -2
  24. data/lib/notion_ruby_mapping/people_property.rb +38 -0
  25. data/lib/notion_ruby_mapping/phone_number_property.rb +30 -1
  26. data/lib/notion_ruby_mapping/property.rb +79 -41
  27. data/lib/notion_ruby_mapping/property_cache.rb +31 -12
  28. data/lib/notion_ruby_mapping/query.rb +5 -2
  29. data/lib/notion_ruby_mapping/relation_property.rb +37 -0
  30. data/lib/notion_ruby_mapping/rich_text_object.rb +74 -0
  31. data/lib/notion_ruby_mapping/rich_text_property.rb +18 -0
  32. data/lib/notion_ruby_mapping/rollup_property.rb +29 -0
  33. data/lib/notion_ruby_mapping/select_property.rb +12 -6
  34. data/lib/notion_ruby_mapping/text_object.rb +89 -0
  35. data/lib/notion_ruby_mapping/text_property.rb +61 -0
  36. data/lib/notion_ruby_mapping/title_property.rb +18 -0
  37. data/lib/notion_ruby_mapping/url_property.rb +30 -1
  38. data/lib/notion_ruby_mapping/user_object.rb +38 -0
  39. data/lib/notion_ruby_mapping/version.rb +2 -1
  40. data/lib/notion_ruby_mapping.rb +3 -3
  41. data/notion_ruby_mapping.gemspec +3 -1
  42. metadata +40 -7
data/env.yml.sample CHANGED
@@ -1,6 +1,13 @@
1
1
  ---
2
2
  notion_token: "secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
3
3
  top_page: "c01166c613ae45cbb96818b4ef2f5a77"
4
+ unpermitted_page: "4a6eb31244204fecb488da11f3caf871"
4
5
  database: "c37a2c66e3aa4a0da44773de3b80c253"
6
+ unpermitted_database: "668d797c76fa49349b05ad288df2d136"
5
7
  h1block: "0250fb6d600142eca4c74efb8794fc6b"
8
+ unpermitted_block: "0c940186ab704351bb342d16f0635d49"
6
9
  db_first_page: "dcdc805c85fa4155a55c20fc28771af7"
10
+ db_second_page: "6601e719a39a460c908e8909467fcccf"
11
+ db_update_page: "206ffaa277744a99baf593e28730240c"
12
+ user_hkob: "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
13
+ parent1_page: "860753bb6d1f48de96211fa6e0e31f82"
@@ -3,12 +3,18 @@
3
3
  module NotionRubyMapping
4
4
  # Notion Base object (Parent of Page / Database / List)
5
5
  class Base
6
- def initialize(json: nil, id: nil)
6
+ # @param [Hash, nil] json
7
+ # @param [String, nil] id
8
+ # @param [Array<Property, Class, String>] assign
9
+ def initialize(json: nil, id: nil, assign: [])
7
10
  @nc = NotionCache.instance
8
11
  @json = json
9
- @id = @nc.hex_id(id || @json["id"])
12
+ @id = @nc.hex_id(id || json && @json["id"])
13
+ raise StandardError, "Unknown id" if !is_a?(List) && @id.nil?
14
+
10
15
  @payload = nil
11
16
  @property_cache = nil
17
+ assign.each_slice(2) { |(klass, key)| assign_property(klass, key) }
12
18
  end
13
19
  attr_reader :json, :id
14
20
 
@@ -25,7 +31,7 @@ module NotionRubyMapping
25
31
  when "block"
26
32
  Block.new json: json
27
33
  else
28
- throw Notion::Api::Errors::ObjectNotFound
34
+ raise StandardError, json.inspect
29
35
  end
30
36
  end
31
37
 
@@ -38,7 +44,7 @@ module NotionRubyMapping
38
44
  def properties
39
45
  unless @property_cache
40
46
  unless @json
41
- return nil if @id.nil?
47
+ raise StandardError, "Unknown id" if @id.nil?
42
48
 
43
49
  reload
44
50
  end
@@ -47,6 +53,11 @@ module NotionRubyMapping
47
53
  @property_cache
48
54
  end
49
55
 
56
+ # @return [String] title
57
+ def title
58
+ properties.select { |p| p.is_a? TitleProperty }.map(&:full_text).join ""
59
+ end
60
+
50
61
  # @return [NotionRubyMapping::Base] reloaded self
51
62
  def reload
52
63
  update_json reload_json
@@ -66,39 +77,44 @@ module NotionRubyMapping
66
77
  # @param [Hash] json
67
78
  # @return [NotionRubyMapping::Base]
68
79
  def update_json(json)
69
- if @json.nil? || @json["type"] == json["type"]
70
- @json = json
71
- @id = @nc.hex_id(@json["id"])
72
- clear_object
73
- end
80
+ raise StandardError, json.inspect unless json["object"] != "error" && @json.nil? || @json["type"] == json["type"]
81
+
82
+ @json = json
83
+ @id = @nc.hex_id(@json["id"])
84
+ restore_from_json
74
85
  self
75
86
  end
76
87
 
77
- # @return [NotionRubyMapping::Base]
78
- def clear_object
79
- @payload = nil
80
- @property_cache = nil
81
- self
88
+ def restore_from_json
89
+ payload.clear
90
+ return if (ps = @json["properties"]).nil?
91
+
92
+ properties.json = json_properties
93
+ return unless is_a? Page
94
+
95
+ ps.each do |key, json|
96
+ properties[key].update_from_json json
97
+ end
82
98
  end
83
99
 
84
100
  # @param [String] emoji
85
101
  # @param [String] url
86
102
  # @return [NotionRubyMapping::Base]
87
103
  def set_icon(emoji: nil, url: nil)
88
- if self.is_a?(Page) || self.is_a?(Database)
104
+ if is_a?(Page) || is_a?(Database)
89
105
  payload.set_icon(emoji: emoji, url: url)
90
106
  update
91
107
  end
92
108
  self
93
109
  end
94
-
110
+
95
111
  # @param [String] key
96
112
  # @return [NotionRubyMapping::PropertyCache, Hash] obtained Page value or PropertyCache
97
113
  def [](key)
98
114
  unless @json
99
- return nil if @id.nil?
115
+ raise StandardError, "Unknown id" if @id.nil?
100
116
 
101
- update_json reload
117
+ reload
102
118
  end
103
119
  case key
104
120
  when "properties"
@@ -113,16 +129,20 @@ module NotionRubyMapping
113
129
  self["icon"]
114
130
  end
115
131
 
116
- # @param [Property] property Property object for udpate or create
117
- # @return [NotionRubyMapping::Base]
118
- def add_property_for_update(property)
119
- properties.add_property property, will_update: true
120
- self
132
+ # @param [Property, Class] klass
133
+ # @param [String] title
134
+ # @return [Property] generated property
135
+ def assign_property(klass, title)
136
+ @property_cache ||= PropertyCache.new {}
137
+
138
+ property = klass.new(title, will_update: true)
139
+ @property_cache.add_property property
140
+ property
121
141
  end
122
142
 
123
143
  # @return [Hash] created json
124
- def create_json
125
- payload.create_json @property_cache&.create_json
144
+ def property_values_json
145
+ payload.property_values_json @property_cache&.property_values_json
126
146
  end
127
147
  end
128
148
  end
@@ -5,5 +5,31 @@ module NotionRubyMapping
5
5
  class CheckboxProperty < Property
6
6
  include EqualsDoesNotEqual
7
7
  TYPE = "checkbox"
8
+
9
+ # @param [String] name Property name
10
+ # @param [Boolean] checkbox Number value (optional)
11
+ def initialize(name, will_update: false, checkbox: false)
12
+ super name, will_update: will_update
13
+ @checkbox = checkbox
14
+ end
15
+ attr_reader :checkbox
16
+
17
+ # @param [Boolean] flag
18
+ # @return [TrueClass, FalseClass] settled value
19
+ def checkbox=(flag)
20
+ @will_update = true
21
+ @checkbox = flag
22
+ end
23
+
24
+ # @param [Hash] json
25
+ def update_from_json(json)
26
+ @will_update = false
27
+ @checkbox = json["checkbox"]
28
+ end
29
+
30
+ # @return [Hash]
31
+ def property_values_json
32
+ {@name => {"checkbox" => @checkbox, "type" => "checkbox"}}
33
+ end
8
34
  end
9
35
  end
@@ -1,7 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
+ # CreatedByProperty
4
5
  class CreatedByProperty < MultiProperty
5
6
  TYPE = "created_by"
7
+
8
+ # @param [String] name Property name
9
+ # @param [String] user_id user_id (optional)
10
+ # @param [Hash] json json (optional)
11
+ def initialize(name, json: nil, user_id: nil)
12
+ super name, will_update: false
13
+ @user = UserObject.new user_id: user_id, json: json
14
+ end
15
+ attr_reader :user
16
+
17
+ # @param [Hash] json
18
+ def update_from_json(json)
19
+ @will_update = false
20
+ @user = UserObject.new json: json["created_by"]
21
+ end
22
+
23
+ # @return [Hash] {} created_time cannot be updated
24
+ def property_values_json
25
+ {}
26
+ end
6
27
  end
7
28
  end
@@ -1,7 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
+ # CreatedTimeProperty
4
5
  class CreatedTimeProperty < DateBaseProperty
5
6
  TYPE = "created_time"
7
+
8
+ # @param [String] name Property name
9
+ # @param [String] created_time created_time value (optional)
10
+ def initialize(name, created_time: nil)
11
+ super name, will_update: false
12
+ @created_time = created_time
13
+ end
14
+ attr_reader :created_time
15
+
16
+ # @param [Hash] json
17
+ def update_from_json(json)
18
+ @will_update = false
19
+ @created_time = json["created_time"]
20
+ end
21
+
22
+ # @return [Hash] {} created_time cannot be updated
23
+ def property_values_json
24
+ {}
25
+ end
6
26
  end
7
27
  end
@@ -7,22 +7,22 @@ module NotionRubyMapping
7
7
  NotionCache.instance.database id
8
8
  end
9
9
 
10
- # @param [String] id database_id (with or without "-")
11
10
  # @param [NotionRubyMapping::Query] query object
12
- def self.query(id, query = nil)
13
- query ||= Query.new
14
- NotionCache.instance.database_query(id, query)
11
+ def query_database(query = Query.new)
12
+ response = @nc.database_query @id, query
13
+ List.new json: response, database: self, query: query
15
14
  end
16
15
 
17
- # @param [String] id database_id (with or without "-")
18
- # @param [Payload] payload
16
+ # @return [NotionRubyMapping::Base]
19
17
  def update
20
- update_json @nc.update_database(@id, create_json)
18
+ update_json @nc.update_database_request(@id, property_values_json)
21
19
  end
22
20
 
21
+ protected
22
+
23
23
  # @return [Hash]
24
24
  def reload_json
25
- @nc.database_json @id
25
+ @nc.database_request @id
26
26
  end
27
27
  end
28
28
  end
@@ -3,71 +3,73 @@
3
3
  require "date"
4
4
 
5
5
  module NotionRubyMapping
6
- # Date base property (date, created_time, last_edited_time)
6
+ # Date base property (date, created_time, last_edited_time, formula)
7
7
  class DateBaseProperty < Property
8
8
  include IsEmptyIsNotEmpty
9
9
 
10
- # @param [Date, Time, DateTime, String] obj
11
- # @return [String] iso8601 format string
12
- def value_str(obj)
13
- case obj
14
- when Date
15
- obj.iso8601
16
- when Time
17
- obj.strftime("%Y-%m-%dT%H:%M:%S%:z")
18
- when DateTime
19
- obj.iso8601
20
- else
21
- obj
22
- end
10
+ def filter_equals(date, rollup = nil, rollup_type = nil)
11
+ make_filter_query "equals", value_str(date), rollup, rollup_type
23
12
  end
24
13
 
25
- def filter_equals(value)
26
- make_filter_query "equals", value_str(value)
14
+ def filter_does_not_equal(date, rollup = nil, rollup_type = nil)
15
+ make_filter_query "does_not_equal", value_str(date), rollup, rollup_type
27
16
  end
28
17
 
29
- def filter_does_not_equal(value)
30
- make_filter_query "does_not_equal", value_str(value)
18
+ def filter_before(date, rollup = nil, rollup_type = nil)
19
+ make_filter_query "before", value_str(date), rollup, rollup_type
31
20
  end
32
21
 
33
- def filter_before(value)
34
- make_filter_query "before", value_str(value)
22
+ def filter_after(date, rollup = nil, rollup_type = nil)
23
+ make_filter_query "after", value_str(date), rollup, rollup_type
35
24
  end
36
25
 
37
- def filter_after(value)
38
- make_filter_query "after", value_str(value)
26
+ def filter_on_or_before(date, rollup = nil, rollup_type = nil)
27
+ make_filter_query "on_or_before", value_str(date), rollup, rollup_type
39
28
  end
40
29
 
41
- def filter_on_or_before(value)
42
- make_filter_query "on_or_before", value_str(value)
30
+ def filter_on_or_after(date, rollup = nil, rollup_type = nil)
31
+ make_filter_query "on_or_after", value_str(date), rollup, rollup_type
43
32
  end
44
33
 
45
- def filter_on_or_after(value)
46
- make_filter_query "on_or_after", value_str(value)
34
+ def filter_past_week(rollup = nil, rollup_type = nil)
35
+ make_filter_query "past_week", {}, rollup, rollup_type
47
36
  end
48
37
 
49
- def filter_past_week
50
- make_filter_query "past_week", {}
38
+ def filter_past_month(rollup = nil, rollup_type = nil)
39
+ make_filter_query "past_month", {}, rollup, rollup_type
51
40
  end
52
41
 
53
- def filter_past_month
54
- make_filter_query "past_month", {}
42
+ def filter_past_year(rollup = nil, rollup_type = nil)
43
+ make_filter_query "past_year", {}, rollup, rollup_type
55
44
  end
56
45
 
57
- def filter_past_year
58
- make_filter_query "past_year", {}
46
+ def filter_next_week(rollup = nil, rollup_type = nil)
47
+ make_filter_query "next_week", {}, rollup, rollup_type
59
48
  end
60
49
 
61
- def filter_next_week
62
- make_filter_query "next_week", {}
50
+ def filter_next_month(rollup = nil, rollup_type = nil)
51
+ make_filter_query "next_month", {}, rollup, rollup_type
63
52
  end
64
53
 
65
- def filter_next_month
66
- make_filter_query "next_month", {}
54
+ def filter_next_year(rollup = nil, rollup_type = nil)
55
+ make_filter_query "next_year", {}, rollup, rollup_type
67
56
  end
68
57
 
69
- def filter_next_year
70
- make_filter_query "next_year", {}
58
+ protected
59
+
60
+ # @param [Date, Time, DateTime, String, nil] obj
61
+ # @return [String] iso8601 format string
62
+ def value_str(obj)
63
+ case obj
64
+ when Date
65
+ obj.iso8601
66
+ when Time
67
+ obj.strftime("%Y-%m-%dT%H:%M:%S%:z")
68
+ when DateTime
69
+ obj.iso8601
70
+ else
71
+ obj
72
+ end
71
73
  end
72
74
  end
73
75
  end
@@ -1,7 +1,68 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
+ # DateProperty
4
5
  class DateProperty < DateBaseProperty
5
6
  TYPE = "date"
7
+
8
+ # @param [String] name
9
+ # @param [Hash] json
10
+ # @param [Date, Time, DateTime, String, nil] start_date
11
+ # @param [Date, Time, DateTime, String, nil] end_date
12
+ # @param [String, nil] time_zone
13
+ def initialize(name, will_update: false, json: nil, start_date: nil, end_date: nil, time_zone: nil)
14
+ super name, will_update: will_update
15
+ @start_date = start_date || json && json["start"]
16
+ @end_date = end_date || json && json["end"]
17
+ @time_zone = time_zone || json && json["time_zone"]
18
+ end
19
+
20
+ # @return [Hash] created json
21
+ def property_values_json
22
+ {
23
+ @name => {
24
+ "type" => "date",
25
+ "date" => {
26
+ "start" => value_str(@start_date),
27
+ "end" => value_str(@end_date),
28
+ "time_zone" => @time_zone,
29
+ },
30
+ },
31
+ }
32
+ end
33
+
34
+ # @param [Date, Time, DateTime, String] start_date
35
+ # @return [Date, Time, DateTime, String]
36
+ def start_date=(start_date)
37
+ @will_update = true
38
+ @start_date = start_date
39
+ @end_date = nil if @start_date.class != @end_date.class || @start_date > @end_date
40
+ @start_date
41
+ end
42
+
43
+ # @param [Date, Time, DateTime, String] end_date
44
+ # @return [Date, Time, DateTime, String]
45
+ def end_date=(end_date)
46
+ @will_update = true
47
+ @end_date = end_date
48
+ @end_date = nil if @start_date.class != @end_date.class || @start_date > @end_date
49
+ @end_date
50
+ end
51
+
52
+ # @param [String] time_zone
53
+ # @return [Array, nil] settled array
54
+ def time_zone=(time_zone)
55
+ @will_update = true
56
+ @time_zone = time_zone
57
+ end
58
+
59
+ # @param [Hash] json
60
+ def update_from_json(json)
61
+ @will_update = false
62
+ jd = json["date"]
63
+ @start_date = jd["start"]
64
+ @end_date = jd["end"]
65
+ @time_zone = jd["time_zone"]
66
+ end
6
67
  end
7
68
  end
@@ -1,7 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
- class EmailProperty < TextProperty
4
+ # EmailProperty
5
+ class EmailProperty < Property
6
+ include EqualsDoesNotEqual
7
+ include ContainsDoesNotContain
8
+ include StartsWithEndsWith
9
+ include IsEmptyIsNotEmpty
5
10
  TYPE = "email"
11
+
12
+ # @param [String] name Property name
13
+ # @param [String] email email value (optional)
14
+ def initialize(name, will_update: false, email: nil)
15
+ super name, will_update: will_update
16
+ @email = email
17
+ end
18
+ attr_reader :email
19
+
20
+ # @param [Hash] json
21
+ def update_from_json(json)
22
+ @will_update = false
23
+ @email = json["email"]
24
+ end
25
+
26
+ # @return [Hash]
27
+ def property_values_json
28
+ {@name => {"email" => @email, "type" => "email"}}
29
+ end
30
+
31
+ def email=(email)
32
+ @will_update = true
33
+ @email = email
34
+ end
6
35
  end
7
36
  end
@@ -5,5 +5,45 @@ module NotionRubyMapping
5
5
  class FilesProperty < Property
6
6
  include IsEmptyIsNotEmpty
7
7
  TYPE = "files"
8
+
9
+ # @param [String] name Property name
10
+ # @param [String] files files value (optional)
11
+ def initialize(name, will_update: false, json: nil, files: [])
12
+ super name, will_update: will_update
13
+ @files = json || Array(files).map { |url| url_to_hash url } || []
14
+ end
15
+ attr_reader :files
16
+
17
+ # @param [String] url
18
+ # @return [Hash]
19
+ def url_to_hash(url)
20
+ {
21
+ "name" => url,
22
+ "type" => "external",
23
+ "external" => {
24
+ "url" => url,
25
+ },
26
+ }
27
+ end
28
+
29
+ # @param [Hash] json
30
+ def update_from_json(json)
31
+ @will_update = false
32
+ @files = json["files"]
33
+ end
34
+
35
+ # @return [Hash]
36
+ def property_values_json
37
+ if @files.map { |f| f["type"] }.include? "file"
38
+ {}
39
+ else
40
+ {@name => {"files" => @files, "type" => "files"}}
41
+ end
42
+ end
43
+
44
+ def files=(files = [])
45
+ @will_update = true
46
+ @files = Array(files).map { |url| url_to_hash url } || []
47
+ end
8
48
  end
9
49
  end
@@ -7,5 +7,23 @@ module NotionRubyMapping
7
7
  include StartsWithEndsWith
8
8
  include GreaterThanLessThan
9
9
  TYPE = "formula"
10
+
11
+ # @param [String] name
12
+ # @param [Hash] json
13
+ def initialize(name, json: nil)
14
+ super name, will_update: false
15
+ @json = json
16
+ end
17
+
18
+ # @param [Hash] json
19
+ def update_from_json(json)
20
+ @will_update = false
21
+ @json = json["formula"]
22
+ end
23
+
24
+ # @return [Hash] {} created_time cannot be updated
25
+ def property_values_json
26
+ {}
27
+ end
10
28
  end
11
29
  end
@@ -1,7 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
+ # LastEditedByProperty
4
5
  class LastEditedByProperty < MultiProperty
5
6
  TYPE = "last_edited_by"
7
+
8
+ # @param [String] name Property name
9
+ # @param [String] user_id user_id (optional)
10
+ # @param [Hash] json json (optional)
11
+ def initialize(name, user_id: nil, json: nil)
12
+ super name, will_update: false
13
+ @user = UserObject.new user_id: user_id, json: json
14
+ end
15
+ attr_reader :user
16
+
17
+ # @param [Hash] json
18
+ def update_from_json(json)
19
+ @will_update = false
20
+ @user = UserObject.new json: json["last_edited_by"]
21
+ end
22
+
23
+ # @return [Hash] {} created_time cannot be updated
24
+ def property_values_json
25
+ {}
26
+ end
6
27
  end
7
28
  end
@@ -1,7 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
+ # LastEditedTimeProperty
4
5
  class LastEditedTimeProperty < DateBaseProperty
5
6
  TYPE = "last_edited_time"
7
+
8
+ # @param [String] name Property name
9
+ # @param [String] last_edited_time last_edited_time value (optional)
10
+ def initialize(name, last_edited_time: nil)
11
+ super name, will_update: false
12
+ @last_edited_time = last_edited_time
13
+ end
14
+ attr_reader :last_edited_time
15
+
16
+ # @param [Hash] json
17
+ def update_from_json(json)
18
+ @will_update = false
19
+ @last_edited_time = json["last_edited_time"]
20
+ end
21
+
22
+ # @return [Hash] {} created_time cannot be updated
23
+ def property_values_json
24
+ {}
25
+ end
6
26
  end
7
27
  end
@@ -5,12 +5,43 @@ module NotionRubyMapping
5
5
  class List < Base
6
6
  include Enumerable
7
7
 
8
+ def initialize(json: nil, id: nil, database: nil, query: nil)
9
+ super(json: json, id: id)
10
+ @has_more = @json["has_more"]
11
+ @database = database
12
+ @query = query
13
+ @index = 0
14
+ @has_content = true
15
+ end
16
+ attr_reader :has_more
17
+
8
18
  def each
9
19
  return enum_for(:each) unless block_given?
10
20
 
11
- json["results"].each do |block_json|
12
- yield Base.create_from_json(block_json)
21
+ while @has_content
22
+ if @index < results.length
23
+ object = Base.create_from_json(results[@index])
24
+ @index += 1
25
+ yield object
26
+ elsif @has_more
27
+ if @database
28
+ @query.start_cursor = @json["next_cursor"]
29
+ @json = @database.query_database @query
30
+ @index = 0
31
+ @has_more = @json["has_more"]
32
+ else
33
+ @has_content = false
34
+ end
35
+ else
36
+ @has_content = false
37
+ end
13
38
  end
14
39
  end
40
+
41
+ private
42
+
43
+ def results
44
+ @json["results"]
45
+ end
15
46
  end
16
47
  end