ecoportal-api-v2 0.8.30 → 0.8.32

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbeee3d71e92e44bd48070ae3cafcbc0d1422f7e1a266ae7a36cfd241e4674f1
4
- data.tar.gz: 3a6ba98734b9fae590adee2ed5b83394d0fe4389d919f0a7e6287e683efa9220
3
+ metadata.gz: 61f3db1a50844d2f5ca194ff29939ed8daa393a6d3596e50a670ebc0f99e58db
4
+ data.tar.gz: b39bd03de67e23d12a48e12fdc19de1b33d131a1c4bec1aa193bf35fcbdc1035
5
5
  SHA512:
6
- metadata.gz: 178fb29cef779e7a804e12d04098b00baf862a00a53947f7c00783a2b19884bf377b699d9ffd5ddaa0cf7dec2b3d21379e6e79bc1cc90a23fb27c075c0a2b860
7
- data.tar.gz: 2c10eb056a68dfaad088420680f32f1ad2b5945c6603f0f3f786bf3ec0581b08cc1a5b073d43a4cc47024ed7f9b691b9ebb78f85b6582c8f89dfa43bc7cf1c3c
6
+ metadata.gz: fba38418ad4ed3febd3859f0d4d096d75f079aa1e7abc745ca76a7deb5479de93ab5fecede2954646691e1aa17ee66348e04a8f349d2f8800f06ca36e76efa28
7
+ data.tar.gz: dcfad6f62286db5b028d3ab1b29b29a0f34c04c4a8d73d2cdc01c060601d92e678d6e51f96a4ce6caa22e10d9f3bb36ec97dc8f9fef4b894c5c52ba5332e388a
data/CHANGELOG.md CHANGED
@@ -1,7 +1,48 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [0.8.30] - 2022-07-xx
4
+ ## [0.8.33] - 2022-09-xx
5
+
6
+ ### Added
7
+ ### Changed
8
+ ### Fixed
9
+
10
+
11
+ ## [0.8.32] - 2022-09-19
12
+
13
+ ### Added
14
+ - `Ecoportal::API::V2::Pages::PageCreateResponse` to wrap the response on creation
15
+
16
+ ### Changed
17
+ - `Ecoportal::API::V2::Pages#create` to rather return a `PageCreateResponse`
18
+
19
+ ### Fixed
20
+ - `Ecoportal::API::V2::Pages#create` to make sure new page with no changes can be created.
21
+
22
+
23
+ ## [0.8.31] - 2022-09-15
24
+
25
+ ### Added
26
+ - `Ecoportal::API::Common::Content::ClassHelpers.uid` to generate a random uid
27
+
28
+ ### Changed
29
+ - added **inheritable attributes** to
30
+ - `Ecoportal::API::Common::Content::ArrayModel` (:order_matteres, :uniq)
31
+ - `Ecoportal::API::Common::Content::CollectionModel` (klass, :order_matters, :order_key, :items_key, :new_item)
32
+ - `Ecoportal::API::Common::Content::DoubleModel` (:key)
33
+ - moved `SecureRandom` dependency to `Ecoportal::API::Common::Content::ClassHelpers`
34
+
35
+ ### Fixed
36
+ - `Ecoportal::API::V2::Page::Component::SelectionOption#numeric!` fix conversion
37
+ - `Ecoportal::API::Common::Content::ClassHelpers`, `new_class`
38
+ - **Added** parameter `namespace` (default: the class `inherits` parameter): it's purpose is to define the namespace where the new class will sit
39
+ - **Fixed** also the namespace where the new class constant will sit (it was using always the source class `self`)
40
+ - **Improved** `name` parameter to have a default value that is randomized. This is useful when we just want to do something like `SomeClass.new_class`, where the name of the new class doesn't really matter.
41
+ - **Fixed** lost reference of class resolvers when using `embeds_many`
42
+ - `Ecoportal::API::Common::Content::ClassHelpers`, `resolve_class` accepts now a `Hash` with `key` referrer class, and `value` any of the previously accepted ones (i.e. `Symbol` or `String`). To this purpose, `source_class` parameter has been added.
43
+ - This has been natively wrapped in `Ecoportal::API::Common::Content::DoubleModel.embeds_many`, as it naturally shares the `class_resolver` with a third party class that loses the reference.
44
+
45
+ ## [0.8.30] - 2022-07-11
5
46
 
6
47
  ### Added
7
48
  - `Ecoportal::API::V2::Page::MoultCounter`
@@ -6,7 +6,7 @@ module Ecoportal
6
6
  # @note
7
7
  # - Its purpose is to handle an Array of basic objects (i.e. `Date`, `String`, `Number`)
8
8
  class ArrayModel < Content::DoubleModel
9
- class TypeMismatchedComparison < Exception
9
+ class TypeMismatchedComparison < StandardError
10
10
  def initialize (this: nil, that: msg = "Trying to compare objects with different behavior.")
11
11
  if this
12
12
  msg += " From object with 'order_matters: #{this.order_matters?}' and 'uniq: #{this.uniq?}'."
@@ -30,9 +30,10 @@ module Ecoportal
30
30
  raise "To use this comparison both objects should be `ArrayModel`" unless a.is_a?(ArrayModel) && b.is_a?(ArrayModel)
31
31
  (a.order_matters? == b.order_matters?) && (a.uniq? == b.uniq?)
32
32
  end
33
-
34
33
  end
35
34
 
35
+ inheritable_class_vars :order_matteres, :uniq
36
+
36
37
  def initialize(doc = [], parent: self, key: nil)
37
38
  super(doc, parent: parent, key: key)
38
39
  end
@@ -1,3 +1,4 @@
1
+ require 'securerandom'
1
2
  module Ecoportal
2
3
  module API
3
4
  module Common
@@ -10,9 +11,10 @@ module Ecoportal
10
11
  # @note it caches the resolved `klass`es
11
12
  # @raise [Exception] when could not resolve if `exception` is `true`
12
13
  # @param klass [Class, String, Symbol] the class to resolve
14
+ # @param source_class [Class] when the reference to `klass` belongs to a different inheritance chain.
13
15
  # @param exception [Boolean] if it should raise exception when could not resolve
14
16
  # @return [Class] the `Class` constant
15
- def resolve_class(klass, exception: true)
17
+ def resolve_class(klass, source_class: self, exception: true)
16
18
  @resolved ||= {}
17
19
  @resolved[klass] ||=
18
20
  case klass
@@ -25,18 +27,24 @@ module Ecoportal
25
27
  raise if exception
26
28
  end
27
29
  when Symbol
28
- resolve_class(self.send(klass))
30
+ source_class.resolve_class(source_class.send(klass))
31
+ when Hash
32
+ referrer, referred = klass.first
33
+ resolve_class(referred, source_class: referrer, exception: exception)
29
34
  else
30
35
  raise "Unknown class: #{klass}" if exception
31
36
  end
32
37
  end
33
38
 
34
39
  # Helper to normalize `key` into a correct `ruby` **constant name**
40
+ # @note it removes namespace syntax `::`
35
41
  # @param key [String, Symbol] to be normalized
36
42
  # @return [String] a correct constant name
37
43
  def to_constant(key)
38
- str_name = key.to_s.strip.split(/[\-\_ ]/i).compact.map do |str|
39
- str.slice(0).upcase + str.slice(1..-1).downcase
44
+ str_name = key.to_s.strip.split(/::/).compact.map do |str|
45
+ str.slice(0).upcase + str.slice(1..-1)
46
+ end.join("").split(/[\-\_ :]+/i).compact.map do |str|
47
+ str.slice(0).upcase + str.slice(1..-1)
40
48
  end.join("")
41
49
  end
42
50
 
@@ -49,20 +57,27 @@ module Ecoportal
49
57
  str
50
58
  end
51
59
 
60
+ # Generates random ids in hexadecimal to use in class name generation.
61
+ # @param len [Integeter] length of the `uid`
62
+ # @return [String] a random unique id of length `len`
63
+ def uid(len = 8);
64
+ SecureRandom.hex(len/2)
65
+ end
66
+
52
67
  # If the class for `name` exists, it returns it. Otherwise it generates it.
53
68
  # @param name [String, Symbol] the name of the new class
54
69
  # @param inherits [Class] the parent class to _inherit_ from
70
+ # @param namespace [Class, String] an existing `constant` (class or module) the new class will be namespaced on
55
71
  # @yield [child_class] configure the new class
56
72
  # @yieldparam child_class [Class] the new class
57
73
  # @return [Class] the new generated class
58
- def new_class(name, inherits:)
74
+ def new_class(name = "Child#{uid}", inherits: self, namespace: inherits)
59
75
  name = name.to_sym.freeze
60
76
  class_name = to_constant(name)
61
- full_class_name = "#{inherits}::#{class_name}"
62
77
 
63
- unless target_class = resolve_class(full_class_name, exception: false)
78
+ unless target_class = resolve_class("#{namespace}::#{class_name}", exception: false)
64
79
  target_class = Class.new(inherits)
65
- self.const_set class_name, target_class
80
+ Kernel.const_get(namespace.to_s).const_set class_name, target_class
66
81
  end
67
82
 
68
83
  target_class.tap do |klass|
@@ -138,7 +153,6 @@ module Ecoportal
138
153
  subclass.instance_variable_set(instance_var, value.freeze)
139
154
  end
140
155
  end
141
-
142
156
  end
143
157
  end
144
158
  end
@@ -82,11 +82,12 @@ module Ecoportal
82
82
  klass.uniq = uniq
83
83
  end
84
84
  end
85
-
86
85
  end
87
86
 
88
87
  include Enumerable
89
88
 
89
+ inheritable_class_vars :klass, :order_matters, :order_key, :items_key, :new_item
90
+
90
91
  def initialize(ini_doc = [], parent: self, key: nil)
91
92
  unless self.class.klass?
92
93
  raise "Undefined base 'klass' or 'new_item' callback for #{self.class}"
@@ -1,5 +1,3 @@
1
- require 'securerandom'
2
-
3
1
  module Ecoportal
4
2
  module API
5
3
  module Common
@@ -12,7 +10,7 @@ module Ecoportal
12
10
  extend Common::Content::ClassHelpers
13
11
  include Common::Content::ModelHelpers
14
12
 
15
- class UnlinkedModel < Exception
13
+ class UnlinkedModel < StandardError
16
14
  def initialize (msg = "Something went wrong when linking the document.", from: nil, key: nil)
17
15
  msg += " From: #{from}." if from
18
16
  msg += " key: #{key}." if key
@@ -31,8 +29,8 @@ module Ecoportal
31
29
  @key = value.to_s.freeze
32
30
  end
33
31
 
34
- def new_uuid(length: 12)
35
- SecureRandom.hex(length)
32
+ def new_uuid(length: 24)
33
+ uid(length)
36
34
  end
37
35
 
38
36
  # Same as `attr_reader` but links to a subjacent `Hash` model property
@@ -208,18 +206,24 @@ module Ecoportal
208
206
  else
209
207
  raise "You should either specify the 'klass' of the elements or the 'enum_class'"
210
208
  end
211
- embed(method, key: key, multiple: true, klass: eclass)
209
+ embed(method, key: key, multiple: true, klass: eclass) do |instance_with_called_method|
210
+ # keep reference to the original class to resolve the `klass` dependency
211
+ # See stackoverflow: https://stackoverflow.com/a/73709529/4352306
212
+ referrer_class = instance_with_called_method.class
213
+ eclass.klass = {referrer_class => klass} if klass
214
+ end
212
215
  end
213
216
 
214
217
  private
215
218
 
216
- def embed(method, key: method, nullable: false, multiple: false, klass:)
219
+ def embed(method, key: method, nullable: false, multiple: false, klass:, &block)
217
220
  method = method.to_s.freeze
218
221
  var = instance_variable_name(method).freeze
219
222
  k = key.to_s.freeze
220
223
 
221
224
  # retrieving method (getter)
222
225
  define_method(method) do
226
+ yield(self) if block_given?
223
227
  return instance_variable_get(var) if instance_variable_defined?(var)
224
228
  unless nullable
225
229
  doc[k] ||= multiple ? [] : {}
@@ -239,7 +243,7 @@ module Ecoportal
239
243
 
240
244
  end
241
245
 
242
- inheritable_class_vars :forced_model_keys
246
+ inheritable_class_vars :forced_model_keys, :key
243
247
 
244
248
  attr_reader :_parent, :_key
245
249
 
@@ -30,7 +30,7 @@ module Ecoportal
30
30
 
31
31
  private
32
32
 
33
- def to_i
33
+ def to_i(value)
34
34
  Float(value).to_i
35
35
  end
36
36
 
@@ -0,0 +1,13 @@
1
+ module Ecoportal
2
+ module API
3
+ class V2
4
+ class Pages
5
+ # @attr_reader page_id [String] the `id` of the new ooze.
6
+ # @attr_reader active_stage_id [String] the active stage `id` of the new ooze.
7
+ class PageCreateResponse < Common::Content::DoubleModel
8
+ passthrough :page_id, :active_stage_id
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -9,6 +9,7 @@ module Ecoportal
9
9
 
10
10
  class_resolver :stages_class, "Ecoportal::API::V2::Pages::Stages"
11
11
  class_resolver :page_class, "Ecoportal::API::V2::Page"
12
+ class_resolver :create_page_response_class, "Ecoportal::API::V2::Pages::PageCreateResponse"
12
13
 
13
14
  attr_reader :client
14
15
 
@@ -75,9 +76,20 @@ module Ecoportal
75
76
  # @param from [String, Hash, Page] template or `id` of the template
76
77
  # @return [Response] an object with the api response.
77
78
  def create(doc, from:)
78
- body = get_body(doc)
79
79
  id = get_id(from)
80
- client.post("/pages", data: body, params: {template_id: id})
80
+ body = get_body(doc).tap do |hash|
81
+ unless hash["page"]
82
+ hash["page"] = {
83
+ "id" => "111111111111111111111111",
84
+ "operation" => "changed",
85
+ "data" => {"patch_ver" => 0}
86
+ }
87
+ end
88
+ end
89
+ response = client.post("/pages", data: body, params: {template_id: id})
90
+ wrapped = Common::Content::WrappedResponse.new(response, create_page_response_class)
91
+ return wrapped.result if wrapped.success?
92
+ raise "Could not create page from template #{id} - Error #{response.status}: #{response.body}"
81
93
  end
82
94
 
83
95
  private
@@ -93,3 +105,4 @@ end
93
105
 
94
106
  require 'ecoportal/api/v2/page'
95
107
  require 'ecoportal/api/v2/pages/stages'
108
+ require 'ecoportal/api/v2/pages/page_create_response'
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- GEM2_VERSION = "0.8.30"
3
+ GEM2_VERSION = "0.8.32"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecoportal-api-v2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.30
4
+ version: 0.8.32
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-11 00:00:00.000000000 Z
11
+ date: 2022-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -231,6 +231,7 @@ files:
231
231
  - lib/ecoportal/api/v2/page/stage.rb
232
232
  - lib/ecoportal/api/v2/page/stages.rb
233
233
  - lib/ecoportal/api/v2/pages.rb
234
+ - lib/ecoportal/api/v2/pages/page_create_response.rb
234
235
  - lib/ecoportal/api/v2/pages/page_stage.rb
235
236
  - lib/ecoportal/api/v2/pages/stages.rb
236
237
  - lib/ecoportal/api/v2/people.rb