ecoportal-api 0.3.8 → 0.4.1

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 (38) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +11 -3
  3. data/.yardopts +10 -0
  4. data/Gemfile.lock +10 -5
  5. data/Rakefile +22 -1
  6. data/ecoportal-api.gemspec +6 -4
  7. data/lib/ecoportal/api/common.rb +7 -4
  8. data/lib/ecoportal/api/common/base_class.rb +29 -0
  9. data/lib/ecoportal/api/common/base_model.rb +89 -20
  10. data/lib/ecoportal/api/common/batch_operation.rb +0 -1
  11. data/lib/ecoportal/api/common/batch_response.rb +5 -0
  12. data/lib/ecoportal/api/common/client.rb +61 -1
  13. data/lib/ecoportal/api/common/doc_helpers.rb +2 -0
  14. data/lib/ecoportal/api/common/hash_diff.rb +25 -23
  15. data/lib/ecoportal/api/common/response.rb +4 -0
  16. data/lib/ecoportal/api/common/wrapped_response.rb +19 -10
  17. data/lib/ecoportal/api/internal.rb +17 -20
  18. data/lib/ecoportal/api/internal/account.rb +23 -16
  19. data/lib/ecoportal/api/internal/login_provider.rb +1 -1
  20. data/lib/ecoportal/api/internal/login_providers.rb +10 -0
  21. data/lib/ecoportal/api/internal/people.rb +3 -8
  22. data/lib/ecoportal/api/internal/permissions.rb +1 -1
  23. data/lib/ecoportal/api/internal/person.rb +18 -16
  24. data/lib/ecoportal/api/internal/person_details.rb +1 -5
  25. data/lib/ecoportal/api/internal/person_schema.rb +1 -5
  26. data/lib/ecoportal/api/internal/person_schemas.rb +2 -6
  27. data/lib/ecoportal/api/internal/policy_group.rb +1 -1
  28. data/lib/ecoportal/api/internal/policy_groups.rb +10 -1
  29. data/lib/ecoportal/api/v1.rb +27 -4
  30. data/lib/ecoportal/api/v1/people.rb +62 -25
  31. data/lib/ecoportal/api/v1/person.rb +47 -28
  32. data/lib/ecoportal/api/v1/person_details.rb +27 -13
  33. data/lib/ecoportal/api/v1/person_schema.rb +3 -6
  34. data/lib/ecoportal/api/v1/person_schemas.rb +26 -9
  35. data/lib/ecoportal/api/v1/schema_field.rb +1 -1
  36. data/lib/ecoportal/api/v1/schema_field_value.rb +2 -1
  37. data/lib/ecoportal/api/version.rb +1 -1
  38. metadata +45 -3
@@ -2,6 +2,7 @@ module Ecoportal
2
2
  module API
3
3
  module Common
4
4
  module DocHelpers
5
+
5
6
  def get_body(doc)
6
7
  if doc.respond_to?(:as_update)
7
8
  doc.as_update
@@ -11,6 +12,7 @@ module Ecoportal
11
12
  doc
12
13
  end
13
14
  end
15
+
14
16
  def get_id(doc)
15
17
  id = nil
16
18
  id ||= doc.id if doc.respond_to?(:id)
@@ -4,32 +4,34 @@ module Ecoportal
4
4
  module HashDiff
5
5
  ID_KEYS = %w[id]
6
6
 
7
- def self.diff(a, b)
8
- return a if a.class != b.class
9
- case a
10
- when Hash
11
- {}.tap do |diffed|
12
- a.each do |key, a_value|
13
- b_value = b[key]
14
- next if a_value == b_value && !ID_KEYS.include?(key)
15
- diffed[key] = diff(a_value, b_value)
16
- diffed.delete(key) if diffed[key] == {}
7
+ class << self
8
+ def diff(a, b)
9
+ return a if a.class != b.class
10
+ case a
11
+ when Hash
12
+ {}.tap do |diffed|
13
+ a.each do |key, a_value|
14
+ b_value = b[key]
15
+ next if a_value == b_value && !ID_KEYS.include?(key)
16
+ diffed[key] = diff(a_value, b_value)
17
+ diffed.delete(key) if diffed[key] == {}
18
+ end
19
+ # All keys are IDs, so it's actually blank
20
+ if (diffed.keys - ID_KEYS).empty?
21
+ return {}
22
+ end
17
23
  end
18
- # All keys are IDs, so it's actually blank
19
- if (diffed.keys - ID_KEYS).empty?
20
- return {}
24
+ when Array
25
+ return a unless a.length == b.length
26
+ a.map.with_index do |a_value, idx|
27
+ b_value = b[idx]
28
+ diff(a_value, b_value)
29
+ end.reject do |el|
30
+ el == {}
21
31
  end
32
+ else
33
+ a
22
34
  end
23
- when Array
24
- return a unless a.length == b.length
25
- a.map.with_index do |a_value, idx|
26
- b_value = b[idx]
27
- diff(a_value, b_value)
28
- end.reject do |el|
29
- el == {}
30
- end
31
- else
32
- a
33
35
  end
34
36
  end
35
37
  end
@@ -2,7 +2,9 @@ module Ecoportal
2
2
  module API
3
3
  module Common
4
4
  class Response
5
+
5
6
  attr_reader :status, :body
7
+
6
8
  def initialize(response)
7
9
  @status = response.status
8
10
  @body = [].tap do |body_data|
@@ -13,9 +15,11 @@ module Ecoportal
13
15
  @body = JSON.parse(@body.to_s) rescue nil
14
16
  response
15
17
  end
18
+
16
19
  def success?
17
20
  @status.success?
18
21
  end
22
+
19
23
  def print
20
24
  puts "Status: #{@status.code}"
21
25
  puts "Body:"
@@ -3,35 +3,43 @@ module Ecoportal
3
3
  module Common
4
4
  class WrappedResponse
5
5
  include Enumerable
6
+
6
7
  attr_reader :response, :result
8
+
7
9
  def initialize(response, klass)
8
10
  @response = response
9
11
  @klass = klass
10
12
 
11
13
  if @response.success?
12
- @result = if @response.body.is_a?(Array)
13
- @response.body.map do |doc|
14
- @klass.new(doc)
15
- end
16
- else
17
- @klass.new(@response.body)
18
- end
14
+ @result =
15
+ if @response.body.is_a?(Array)
16
+ @response.body.map do |doc|
17
+ @klass.new(doc)
18
+ end
19
+ else
20
+ @klass.new(@response.body)
21
+ end
19
22
  end
20
23
  end
24
+
21
25
  def body
22
26
  response.body.to_s
23
27
  end
28
+
24
29
  def each
25
- [*@result].each do |doc|
30
+ [*result].each do |doc|
26
31
  yield doc
27
32
  end
28
33
  end
34
+
29
35
  def status
30
- @response.status.code
36
+ response.status.code
31
37
  end
38
+
32
39
  def success?
33
- @response.success?
40
+ response.success?
34
41
  end
42
+
35
43
  def print
36
44
  if success?
37
45
  each(&:print)
@@ -39,6 +47,7 @@ module Ecoportal
39
47
  puts "Request failed."
40
48
  end
41
49
  end
50
+
42
51
  end
43
52
  end
44
53
  end
@@ -1,34 +1,31 @@
1
1
  module Ecoportal
2
2
  module API
3
- class Internal
3
+ class Internal < V1
4
4
  include Common::Logging
5
- attr_reader :client, :logger
6
- def initialize(api_key, host: "live.ecoportal.com", logger: default_logger)
7
- @logger = logger
8
- @client = Common::Client.new(
9
- api_key: api_key,
10
- host: host,
11
- version: "v0",
12
- logger: @logger
13
- )
14
- end
15
- def people
16
- Internal::People.new(client)
17
- end
18
- def person_schemas
19
- Internal::PersonSchemas.new(client)
20
- end
5
+
6
+ VERSION = "v0"
7
+ class_resolver :people_class, "Ecoportal::API::Internal::People"
8
+ class_resolver :person_schemas_class, "Ecoportal::API::Internal::PersonSchemas"
9
+
10
+ class_resolver :policy_groups_class, "Ecoportal::API::Internal::PolicyGroups"
11
+ class_resolver :login_providers_class, "Ecoportal::API::Internal::LoginProviders"
12
+
13
+ # Obtain specific object for policy groups api requests.
14
+ # @return [PolicyGroups] an instance object ready to make policy groups api requests.
21
15
  def policy_groups
22
- Internal::PolicyGroups.new(client)
16
+ policy_groups_class.new(client)
23
17
  end
18
+
19
+ # Obtain specific object for login providers api requests.
20
+ # @return [LoginProviders] an instance object ready to make login providers api requests.
24
21
  def login_providers
25
- Internal::LoginProviders.new(client)
22
+ login_providers_class.new(client)
26
23
  end
27
24
  end
28
25
  end
29
26
  end
30
27
 
31
- require 'ecoportal/api/internal/people'
32
28
  require 'ecoportal/api/internal/person_schemas'
33
29
  require 'ecoportal/api/internal/policy_groups'
34
30
  require 'ecoportal/api/internal/login_providers'
31
+ require 'ecoportal/api/internal/people'
@@ -2,14 +2,36 @@ module Ecoportal
2
2
  module API
3
3
  class Internal
4
4
  class Account < Common::BaseModel
5
- passthrough :policy_group_ids, :landing_page_id, :permissions_preset, :permissions_custom, :preferences, :prefilter, :filter_tags, :login_provider_ids, :starred_ids, :accept_eula, :send_invites, :default_tag, to: :doc
5
+ passthrough :policy_group_ids, :landing_page_id, :permissions_preset, :permissions_custom,
6
+ :preferences, :prefilter, :filter_tags, :login_provider_ids, :starred_ids, :accept_eula,
7
+ :send_invites, :default_tag
8
+
9
+ class_resolver :preferences_class, "Ecoportal::API::Internal::Preferences"
10
+ class_resolver :permissions_class, "Ecoportal::API::Internal::Permissions"
11
+
12
+ embeds_one :permissions, key: "permissions_custom", klass: :permissions_class
13
+ embeds_one :preferences, klass: :preferences_class
6
14
 
7
15
  VALID_TAG_REGEX = /^[A-Za-z0-9 &_'\/-]+$/
8
16
 
17
+ # Sets the `permissions_preset`.
18
+ # @note basically the same as `permissions_preset=` but when `"custom"`, it's changed to `nil`
19
+ # @param value [nil, String] preset name.
9
20
  def preset=(value)
10
21
  self.permissions_preset = value == "custom" ? nil : value
11
22
  end
12
23
 
24
+ # Gets the `permissions_preset`.
25
+ # @note basically the same as `permissions_preset` but when 'nil', it returns `"custom"` instead
26
+ # @return [nil, String] preset name.
27
+ def preset
28
+ self.permissions_preset.nil? ? "custom" : self.permissions_preset
29
+ end
30
+
31
+ # Validates the string tags of the array, and sets the `filter_tags` property of the account.
32
+ # @note all is set in upper case.
33
+ # @raise [Exception] if there was any invalid string tag.
34
+ # @param value [Array<String>] array of tags.
13
35
  def filter_tags=(value)
14
36
  unless value.is_a?(Array)
15
37
  raise "filter_tags= needs to be passed an Array, got #{value.class}"
@@ -22,10 +44,6 @@ module Ecoportal
22
44
  end
23
45
  end
24
46
 
25
- def preset
26
- self.permissions_preset.nil? ? "custom" : self.permissions_preset
27
- end
28
-
29
47
  def as_json
30
48
  super.tap do |hash|
31
49
  if preset == "custom"
@@ -37,17 +55,6 @@ module Ecoportal
37
55
  end
38
56
  end
39
57
 
40
- def permissions
41
- return @permissions if defined?(@permissions)
42
- doc["permissions_custom"] ||= {}
43
- @permissions = Internal::Permissions.new(doc["permissions_custom"])
44
- end
45
-
46
- def preferences
47
- return @preferences if defined?(@preferences)
48
- doc["preferences"] ||= {}
49
- @preferences = Internal::Preferences.new(doc["preferences"])
50
- end
51
58
  end
52
59
  end
53
60
  end
@@ -2,7 +2,7 @@ module Ecoportal
2
2
  module API
3
3
  class Internal
4
4
  class LoginProvider < Common::BaseModel
5
- passthrough :id, :name, :type, :enabled_for, to: :doc
5
+ passthrough :id, :name, :type, :enabled_for
6
6
  end
7
7
  end
8
8
  end
@@ -1,20 +1,30 @@
1
1
  module Ecoportal
2
2
  module API
3
3
  class Internal
4
+ # @attr_reader client [Common::Client] a `Common::Client` object that holds the configuration of the api connection.
4
5
  class LoginProviders
5
6
  include Enumerable
7
+
6
8
  attr_reader :client
9
+
10
+ # @param client [Common::Client] a `Common::Client` object that holds the configuration of the api connection.
11
+ # @return [LoginProviders] an instance object ready to make login providers api requests.
7
12
  def initialize(client)
8
13
  @client = client
9
14
  end
15
+
16
+ # Gets all the login providers via api request.
17
+ # @return [Enumerable<LoginProvider>] an `Enumerable` with all login providers already wrapped as `LoginProvider` objects.
10
18
  def get_all
11
19
  response = @client.get("/login_providers")
12
20
  Common::WrappedResponse.new(response, Internal::LoginProvider)
13
21
  end
22
+
14
23
  def each(&block)
15
24
  return to_enum(:each) unless block
16
25
  get_all.each(&block)
17
26
  end
27
+
18
28
  end
19
29
  end
20
30
  end
@@ -1,19 +1,14 @@
1
- require 'ecoportal/api/common/doc_helpers'
2
1
  module Ecoportal
3
2
  module API
4
3
  class Internal
5
4
  class People < V1::People
6
- private
7
-
8
- def person_class
9
- Internal::Person
10
- end
5
+ class_resolver :person_class, "Ecoportal::API::Internal::Person"
11
6
  end
12
7
  end
13
8
  end
14
9
  end
15
10
 
16
- require 'ecoportal/api/internal/person'
17
- require 'ecoportal/api/internal/person_details'
18
11
  require 'ecoportal/api/internal/schema_field_value'
12
+ require 'ecoportal/api/internal/person_details'
19
13
  require 'ecoportal/api/internal/account'
14
+ require 'ecoportal/api/internal/person'
@@ -2,7 +2,7 @@ module Ecoportal
2
2
  module API
3
3
  class Internal
4
4
  class Permissions < Common::BaseModel
5
- passthrough :files, :data, :reports, :organization, :people, :pages, :page_editor, :registers, to: :doc
5
+ passthrough :files, :data, :reports, :organization, :people, :pages, :page_editor, :registers
6
6
  end
7
7
  end
8
8
  end
@@ -1,17 +1,23 @@
1
1
  module Ecoportal
2
2
  module API
3
3
  class Internal
4
+ # @attr account [Account, nil] the account of the person or `nil` if missing.
4
5
  class Person < V1::Person
6
+ class_resolver :person_details_class, "Ecoportal::API::Internal::PersonDetails"
7
+ class_resolver :person_account_class, "Ecoportal::API::Internal::Account"
8
+ embeds_one :account, nullable: true, klass: :person_account_class
9
+
5
10
  def as_json
6
11
  super.update("account" => account&.as_json)
7
12
  end
8
13
 
9
- def account
10
- return @account if defined?(@account)
11
- return @account = nil if doc["account"].nil?
12
- @account = Internal::Account.new(doc["account"])
13
- end
14
-
14
+ # Sets the Account to the person, depending on the paramter received:
15
+ # - `nil`: blanks the account.
16
+ # - `Account`: sets a copy of the object param as account.
17
+ # - `Hash`: slices the properties of `Account`.
18
+ # @note this method does not make dirty the account (meaning that `as_json` will be an empty hash `{}`)
19
+ # @param value [nil, Account, Hash] value to be set.
20
+ # @return [nil, Account] the resulting `Account` set to the person.
15
21
  def account=(value)
16
22
  case value
17
23
  when NilClass
@@ -19,7 +25,9 @@ module Ecoportal
19
25
  when Internal::Account
20
26
  doc["account"] = JSON.parse(value.to_json)
21
27
  when Hash
22
- # TODO
28
+ # TODO:
29
+ # => missing send_invites
30
+ # => better use an Internal::Account::PROPERTIES const for this kind of stuff
23
31
  doc["account"] = value.slice(%w[policy_group_ids landing_page_id permissions_preset permissions_custom preferences prefilter filter_tags login_provider_ids starred_ids])
24
32
  else
25
33
  # TODO
@@ -29,19 +37,13 @@ module Ecoportal
29
37
  return account
30
38
  end
31
39
 
40
+ # Adds an empty account to the person.
41
+ # @note if the person exists, and does not have an account, an this will send an invite.
42
+ # @note this will **not** change the account properties of this person.
32
43
  def add_account
33
44
  self.account = {}
34
45
  end
35
46
 
36
- private
37
-
38
- def person_schema_class
39
- Internal::PersonSchema
40
- end
41
-
42
- def person_details_class
43
- Internal::PersonDetails
44
- end
45
47
  end
46
48
  end
47
49
  end
@@ -2,11 +2,7 @@ module Ecoportal
2
2
  module API
3
3
  class Internal
4
4
  class PersonDetails < V1::PersonDetails
5
- private
6
-
7
- def schema_field_value_class
8
- Internal::SchemaFieldValue
9
- end
5
+ class_resolver :schema_field_value_class, "Ecoportal::API::Internal::SchemaFieldValue"
10
6
  end
11
7
  end
12
8
  end
@@ -2,11 +2,7 @@ module Ecoportal
2
2
  module API
3
3
  class Internal
4
4
  class PersonSchema < V1::PersonSchema
5
- private
6
-
7
- def schema_field_class
8
- Internal::SchemaField
9
- end
5
+ class_resolver :schema_field_class, "Ecoportal::API::Internal::SchemaField"
10
6
  end
11
7
  end
12
8
  end
@@ -2,14 +2,10 @@ module Ecoportal
2
2
  module API
3
3
  class Internal
4
4
  class PersonSchemas < V1::PersonSchemas
5
- private
6
-
7
- def person_schema_class
8
- Internal::PersonSchema
9
- end
5
+ class_resolver :person_schema_class, "Ecoportal::API::Internal::PersonSchema"
10
6
  end
11
7
  end
12
8
  end
13
9
  end
14
10
 
15
- require 'ecoportal/api/internal/person_schema'
11
+ require 'ecoportal/api/internal/person_schema'