ecoportal-api 0.8.5 → 0.9.2

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 (56) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +20 -20
  3. data/.rspec +3 -3
  4. data/.rubocop.yml +55 -55
  5. data/.travis.yml +5 -5
  6. data/.yardopts +10 -10
  7. data/CHANGELOG.md +257 -236
  8. data/Gemfile +6 -6
  9. data/LICENSE +21 -21
  10. data/README.md +34 -34
  11. data/Rakefile +27 -27
  12. data/bin/console +14 -14
  13. data/bin/setup +8 -8
  14. data/ecoportal-api.gemspec +36 -36
  15. data/lib/ecoportal/api/common/base_class.rb +33 -29
  16. data/lib/ecoportal/api/common/base_model.rb +195 -177
  17. data/lib/ecoportal/api/common/batch_operation.rb +119 -119
  18. data/lib/ecoportal/api/common/batch_response.rb +34 -34
  19. data/lib/ecoportal/api/common/client.rb +198 -196
  20. data/lib/ecoportal/api/common/doc_helpers.rb +29 -29
  21. data/lib/ecoportal/api/common/elastic_apm_integration.rb +112 -112
  22. data/lib/ecoportal/api/common/hash_diff.rb +41 -41
  23. data/lib/ecoportal/api/common/logging.rb +12 -12
  24. data/lib/ecoportal/api/common/response.rb +31 -31
  25. data/lib/ecoportal/api/common/wrapped_response.rb +54 -54
  26. data/lib/ecoportal/api/common.rb +18 -18
  27. data/lib/ecoportal/api/errors/base.rb +8 -8
  28. data/lib/ecoportal/api/errors/time_out.rb +8 -8
  29. data/lib/ecoportal/api/errors.rb +9 -9
  30. data/lib/ecoportal/api/internal/account.rb +99 -100
  31. data/lib/ecoportal/api/internal/login_provider.rb +9 -9
  32. data/lib/ecoportal/api/internal/login_providers.rb +33 -33
  33. data/lib/ecoportal/api/internal/people.rb +14 -14
  34. data/lib/ecoportal/api/internal/permissions.rb +14 -13
  35. data/lib/ecoportal/api/internal/person.rb +101 -53
  36. data/lib/ecoportal/api/internal/person_details.rb +9 -9
  37. data/lib/ecoportal/api/internal/person_schema.rb +10 -10
  38. data/lib/ecoportal/api/internal/person_schemas.rb +11 -11
  39. data/lib/ecoportal/api/internal/policy_group.rb +9 -9
  40. data/lib/ecoportal/api/internal/policy_groups.rb +32 -32
  41. data/lib/ecoportal/api/internal/preferences.rb +31 -31
  42. data/lib/ecoportal/api/internal/schema_field.rb +8 -8
  43. data/lib/ecoportal/api/internal/schema_field_value.rb +8 -8
  44. data/lib/ecoportal/api/internal.rb +31 -31
  45. data/lib/ecoportal/api/logger.rb +62 -62
  46. data/lib/ecoportal/api/v1/people.rb +218 -218
  47. data/lib/ecoportal/api/v1/person.rb +138 -135
  48. data/lib/ecoportal/api/v1/person_details.rb +94 -82
  49. data/lib/ecoportal/api/v1/person_schema.rb +53 -53
  50. data/lib/ecoportal/api/v1/person_schemas.rb +48 -48
  51. data/lib/ecoportal/api/v1/schema_field.rb +34 -34
  52. data/lib/ecoportal/api/v1/schema_field_value.rb +65 -65
  53. data/lib/ecoportal/api/v1.rb +49 -49
  54. data/lib/ecoportal/api/version.rb +5 -5
  55. data/lib/ecoportal/api.rb +16 -16
  56. metadata +3 -3
@@ -1,112 +1,112 @@
1
- require 'elastic-apm'
2
- module Ecoportal
3
- module API
4
- module Common
5
- module ElasticApmIntegration
6
-
7
- class UnexpectedServerError < StandardError
8
- def initialize(code, msg)
9
- super("Code: #{code} -- Error: #{msg}")
10
- end
11
- end
12
-
13
- APM_SERVICE_NAME = 'ecoportal-api-gem'
14
-
15
- # Log only errors that are only server's responsibility
16
- def log_unexpected_server_error(response)
17
- raise "Expecting Ecoportal::API::Common::Response. Given: #{response.class}" unless response.is_a?(Common::Response)
18
- return nil unless elastic_apm_service
19
- return nil unless unexpected_server_error?(response.status)
20
- if ElasticAPM.running?
21
- ElasticAPM.report(UnexpectedServerError.new(response.status, response.body))
22
- end
23
- end
24
-
25
- private
26
-
27
- def unexpected_server_error?(code)
28
- !code || ((code >= 500) && (code <= 599)) || (code <= 99)
29
- end
30
-
31
- # finalizer to stop the agent
32
- close_elastic_apm = Proc.new do |id|
33
- begin
34
- if ElasticAPM.running?
35
- puts "Stopping ElasticAPM service"
36
- ElasticAPM.stop
37
- end
38
- rescue StandardError => e
39
- # Silent
40
- end
41
- end
42
- ObjectSpace.define_finalizer("ElasticAPM", close_elastic_apm)
43
-
44
- def elastic_apm_service
45
- return false if @disable_apm
46
- begin
47
- ElasticAPM.start(**elastic_apm_options) unless ElasticAPM.running?
48
- rescue StandardError => e
49
- @disable_apm = true
50
- puts "ElasticAPM services not available: #{e}"
51
- end
52
- end
53
-
54
- def elastic_apm_options
55
- {
56
- service_name: APM_SERVICE_NAME,
57
- server_url: elastic_apm_url,
58
- secret_token: elastic_apm_key,
59
- environment: environment,
60
- #http_compression: false,
61
- transaction_sample_rate: 0.1,
62
- transaction_max_spans: 100,
63
- span_frames_min_duration: "5ms"
64
- }.tap do |options|
65
- options.merge!({
66
- log_level: Logger::DEBUG,
67
- log_path: File.join(__dir__, "elastic_apm.log")
68
- }) if false
69
- end
70
- end
71
-
72
- def elastic_apm_url
73
- @elastic_apm_url ||= "https://".tap do |url|
74
- url << "#{elastic_apm_account_id}"
75
- url << ".#{elastic_apm_base_url}"
76
- url << ":#{elastic_apm_port}"
77
- end
78
- end
79
-
80
- def elastic_apm_key
81
- @elastic_apm_key ||= ENV['ELASTIC_APM_KEY']
82
- end
83
-
84
- def elastic_apm_account_id
85
- @elastic_apm_account_id ||= ENV['ELASTIC_APM_ACCOUNT_ID']
86
- end
87
-
88
- def elastic_apm_base_url
89
- @elastic_apm_base_url ||= "apm.#{elastic_apm_region}.aws.cloud.es.io"
90
- end
91
-
92
- def elastic_apm_region
93
- @elastic_apm_region ||= ENV['ELASTIC_APM_REGION'] || "ap-southeast-2"
94
- end
95
-
96
-
97
- def elastic_apm_port
98
- @elastic_apm_port ||= ENV['ELASTIC_APM_PORT'] || "443"
99
- end
100
-
101
- def environment
102
- @environment ||= "unknown".tap do |value|
103
- if instance_variable_defined?(:@host) && env = @host.gsub(".ecoportal.com", '')
104
- value.clear << env
105
- end
106
- end
107
- end
108
-
109
- end
110
- end
111
- end
112
- end
1
+ require 'elastic-apm'
2
+ module Ecoportal
3
+ module API
4
+ module Common
5
+ module ElasticApmIntegration
6
+
7
+ class UnexpectedServerError < StandardError
8
+ def initialize(code, msg)
9
+ super("Code: #{code} -- Error: #{msg}")
10
+ end
11
+ end
12
+
13
+ APM_SERVICE_NAME = 'ecoportal-api-gem'
14
+
15
+ # Log only errors that are only server's responsibility
16
+ def log_unexpected_server_error(response)
17
+ raise "Expecting Ecoportal::API::Common::Response. Given: #{response.class}" unless response.is_a?(Common::Response)
18
+ return nil unless elastic_apm_service
19
+ return nil unless unexpected_server_error?(response.status)
20
+ if ElasticAPM.running?
21
+ ElasticAPM.report(UnexpectedServerError.new(response.status, response.body))
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def unexpected_server_error?(code)
28
+ !code || ((code >= 500) && (code <= 599)) || (code <= 99)
29
+ end
30
+
31
+ # finalizer to stop the agent
32
+ close_elastic_apm = Proc.new do |id|
33
+ begin
34
+ if ElasticAPM.running?
35
+ puts "Stopping ElasticAPM service"
36
+ ElasticAPM.stop
37
+ end
38
+ rescue StandardError => e
39
+ # Silent
40
+ end
41
+ end
42
+ ObjectSpace.define_finalizer("ElasticAPM", close_elastic_apm)
43
+
44
+ def elastic_apm_service
45
+ return false if @disable_apm
46
+ begin
47
+ ElasticAPM.start(**elastic_apm_options) unless ElasticAPM.running?
48
+ rescue StandardError => e
49
+ @disable_apm = true
50
+ puts "ElasticAPM services not available: #{e}"
51
+ end
52
+ end
53
+
54
+ def elastic_apm_options
55
+ {
56
+ service_name: APM_SERVICE_NAME,
57
+ server_url: elastic_apm_url,
58
+ secret_token: elastic_apm_key,
59
+ environment: environment,
60
+ #http_compression: false,
61
+ transaction_sample_rate: 0.1,
62
+ transaction_max_spans: 100,
63
+ span_frames_min_duration: "5ms"
64
+ }.tap do |options|
65
+ options.merge!({
66
+ log_level: Logger::DEBUG,
67
+ log_path: File.join(__dir__, "elastic_apm.log")
68
+ }) if false
69
+ end
70
+ end
71
+
72
+ def elastic_apm_url
73
+ @elastic_apm_url ||= "https://".tap do |url|
74
+ url << "#{elastic_apm_account_id}"
75
+ url << ".#{elastic_apm_base_url}"
76
+ url << ":#{elastic_apm_port}"
77
+ end
78
+ end
79
+
80
+ def elastic_apm_key
81
+ @elastic_apm_key ||= ENV['ELASTIC_APM_KEY']
82
+ end
83
+
84
+ def elastic_apm_account_id
85
+ @elastic_apm_account_id ||= ENV['ELASTIC_APM_ACCOUNT_ID']
86
+ end
87
+
88
+ def elastic_apm_base_url
89
+ @elastic_apm_base_url ||= "apm.#{elastic_apm_region}.aws.cloud.es.io"
90
+ end
91
+
92
+ def elastic_apm_region
93
+ @elastic_apm_region ||= ENV['ELASTIC_APM_REGION'] || "ap-southeast-2"
94
+ end
95
+
96
+
97
+ def elastic_apm_port
98
+ @elastic_apm_port ||= ENV['ELASTIC_APM_PORT'] || "443"
99
+ end
100
+
101
+ def environment
102
+ @environment ||= "unknown".tap do |value|
103
+ if instance_variable_defined?(:@host) && env = @host.gsub(".ecoportal.com", '')
104
+ value.clear << env
105
+ end
106
+ end
107
+ end
108
+
109
+ end
110
+ end
111
+ end
112
+ end
@@ -1,41 +1,41 @@
1
- module Ecoportal
2
- module API
3
- module Common
4
- module HashDiff
5
- ID_KEYS = %w[id]
6
-
7
- class << self
8
-
9
- def diff(a, b, ignore: [])
10
- case a
11
- when Hash
12
- {}.tap do |diffed|
13
- a.each do |key, a_value|
14
- b_value = b && b[key]
15
- no_changes = (a_value == b_value) || ignore.include?(key)
16
- next if !ID_KEYS.include?(key) && no_changes
17
- diffed[key] = diff(a_value, b_value, ignore: ignore)
18
- diffed.delete(key) if diffed[key] == {}
19
- end
20
- # All keys are IDs, so it's actually blank
21
- if (diffed.keys - ID_KEYS).empty?
22
- return {}
23
- end
24
- end
25
- when Array
26
- return a unless b.is_a?(Array) && a.length == b.length
27
- a.map.with_index do |a_value, idx|
28
- b_value = b[idx]
29
- diff(a_value, b_value, ignore: ignore)
30
- end.reject do |el|
31
- el == {}
32
- end
33
- else
34
- a
35
- end
36
- end
37
- end
38
- end
39
- end
40
- end
41
- end
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ module HashDiff
5
+ ID_KEYS = %w[id]
6
+
7
+ class << self
8
+
9
+ def diff(a, b, ignore: [])
10
+ case a
11
+ when Hash
12
+ {}.tap do |diffed|
13
+ a.each do |key, a_value|
14
+ b_value = b && b[key]
15
+ no_changes = (a_value == b_value) || ignore.include?(key)
16
+ next if !ID_KEYS.include?(key) && no_changes
17
+ diffed[key] = diff(a_value, b_value, ignore: ignore)
18
+ diffed.delete(key) if diffed[key] == {}
19
+ end
20
+ # All keys are IDs, so it's actually blank
21
+ if (diffed.keys - ID_KEYS).empty?
22
+ return {}
23
+ end
24
+ end
25
+ when Array
26
+ return a unless b.is_a?(Array) && a.length == b.length
27
+ a.map.with_index do |a_value, idx|
28
+ b_value = b[idx]
29
+ diff(a_value, b_value, ignore: ignore)
30
+ end.reject do |el|
31
+ el == {}
32
+ end
33
+ else
34
+ a
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,12 +1,12 @@
1
- module Ecoportal
2
- module API
3
- module Common
4
- module Logging
5
- private
6
- def default_logger
7
- Ecoportal::API::Logger.new
8
- end
9
- end
10
- end
11
- end
12
- end
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ module Logging
5
+ private
6
+ def default_logger
7
+ Ecoportal::API::Logger.new
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,31 +1,31 @@
1
- module Ecoportal
2
- module API
3
- module Common
4
- class Response
5
-
6
- attr_reader :status, :body
7
-
8
- def initialize(response)
9
- @status = response.status
10
- @body = [].tap do |body_data|
11
- response.body.each do |chunk|
12
- body_data << chunk
13
- end
14
- end.join("")
15
- @body = JSON.parse(@body.to_s) rescue nil
16
- response
17
- end
18
-
19
- def success?
20
- @status.success?
21
- end
22
-
23
- def print_pretty
24
- puts "Status: #{@status.code}"
25
- puts "Body:"
26
- puts JSON.pretty_generate(@body)
27
- end
28
- end
29
- end
30
- end
31
- end
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ class Response
5
+
6
+ attr_reader :status, :body
7
+
8
+ def initialize(response)
9
+ @status = response.status
10
+ @body = [].tap do |body_data|
11
+ response.body.each do |chunk|
12
+ body_data << chunk
13
+ end
14
+ end.join("")
15
+ @body = JSON.parse(@body.to_s) rescue nil
16
+ response
17
+ end
18
+
19
+ def success?
20
+ @status.success?
21
+ end
22
+
23
+ def print_pretty
24
+ puts "Status: #{@status.code}"
25
+ puts "Body:"
26
+ puts JSON.pretty_generate(@body)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,54 +1,54 @@
1
- module Ecoportal
2
- module API
3
- module Common
4
- class WrappedResponse
5
- include Enumerable
6
-
7
- attr_reader :response, :result
8
-
9
- def initialize(response, klass)
10
- @response = response
11
- @klass = klass
12
-
13
- if @response.success?
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
22
- end
23
- end
24
-
25
- def body
26
- response.body.to_s
27
- end
28
-
29
- def each
30
- [*result].each do |doc|
31
- yield doc
32
- end
33
- end
34
-
35
- def status
36
- response.status.code
37
- end
38
-
39
- def success?
40
- response.success?
41
- end
42
-
43
- def print_pretty
44
- if success?
45
- each(&:print_pretty)
46
- else
47
- puts "Request failed."
48
- end
49
- end
50
-
51
- end
52
- end
53
- end
54
- end
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ class WrappedResponse
5
+ include Enumerable
6
+
7
+ attr_reader :response, :result
8
+
9
+ def initialize(response, klass)
10
+ @response = response
11
+ @klass = klass
12
+
13
+ if @response.success?
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
22
+ end
23
+ end
24
+
25
+ def body
26
+ response.body.to_s
27
+ end
28
+
29
+ def each
30
+ [*result].each do |doc|
31
+ yield doc
32
+ end
33
+ end
34
+
35
+ def status
36
+ response.status.code
37
+ end
38
+
39
+ def success?
40
+ response.success?
41
+ end
42
+
43
+ def print_pretty
44
+ if success?
45
+ each(&:print_pretty)
46
+ else
47
+ puts "Request failed."
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,18 +1,18 @@
1
- module Ecoportal
2
- module API
3
- module Common
4
- end
5
- end
6
- end
7
-
8
- require 'ecoportal/api/common/base_class'
9
- require 'ecoportal/api/common/hash_diff'
10
- require 'ecoportal/api/common/base_model'
11
- require 'ecoportal/api/common/doc_helpers'
12
- require 'ecoportal/api/common/logging'
13
- require 'ecoportal/api/common/elastic_apm_integration'
14
- require 'ecoportal/api/common/client'
15
- require 'ecoportal/api/common/response'
16
- require 'ecoportal/api/common/wrapped_response'
17
- require 'ecoportal/api/common/batch_response'
18
- require 'ecoportal/api/common/batch_operation'
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ end
5
+ end
6
+ end
7
+
8
+ require 'ecoportal/api/common/base_class'
9
+ require 'ecoportal/api/common/hash_diff'
10
+ require 'ecoportal/api/common/base_model'
11
+ require 'ecoportal/api/common/doc_helpers'
12
+ require 'ecoportal/api/common/logging'
13
+ require 'ecoportal/api/common/elastic_apm_integration'
14
+ require 'ecoportal/api/common/client'
15
+ require 'ecoportal/api/common/response'
16
+ require 'ecoportal/api/common/wrapped_response'
17
+ require 'ecoportal/api/common/batch_response'
18
+ require 'ecoportal/api/common/batch_operation'
@@ -1,8 +1,8 @@
1
- module Ecoportal
2
- module API
3
- module Errors
4
- class Base < StandardError
5
- end
6
- end
7
- end
8
- end
1
+ module Ecoportal
2
+ module API
3
+ module Errors
4
+ class Base < StandardError
5
+ end
6
+ end
7
+ end
8
+ end
@@ -1,8 +1,8 @@
1
- module Ecoportal
2
- module API
3
- module Errors
4
- class TimeOut < Errors::Base
5
- end
6
- end
7
- end
8
- end
1
+ module Ecoportal
2
+ module API
3
+ module Errors
4
+ class TimeOut < Errors::Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -1,9 +1,9 @@
1
- module Ecoportal
2
- module API
3
- module Errors
4
- end
5
- end
6
- end
7
-
8
- require 'ecoportal/api/errors/base'
9
- require 'ecoportal/api/errors/time_out'
1
+ module Ecoportal
2
+ module API
3
+ module Errors
4
+ end
5
+ end
6
+ end
7
+
8
+ require 'ecoportal/api/errors/base'
9
+ require 'ecoportal/api/errors/time_out'