ecoportal-api 0.1.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 (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +53 -0
  7. data/README.md +34 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/ecoportal-api.gemspec +28 -0
  12. data/lib/ecoportal/api.rb +11 -0
  13. data/lib/ecoportal/api/common.rb +13 -0
  14. data/lib/ecoportal/api/common/base_model.rb +39 -0
  15. data/lib/ecoportal/api/common/batch_operation.rb +94 -0
  16. data/lib/ecoportal/api/common/batch_response.rb +16 -0
  17. data/lib/ecoportal/api/common/client.rb +58 -0
  18. data/lib/ecoportal/api/common/doc_helpers.rb +27 -0
  19. data/lib/ecoportal/api/common/hash_diff.rb +37 -0
  20. data/lib/ecoportal/api/common/response.rb +22 -0
  21. data/lib/ecoportal/api/common/wrapped_response.rb +38 -0
  22. data/lib/ecoportal/api/internal.rb +31 -0
  23. data/lib/ecoportal/api/internal/.git-keep +0 -0
  24. data/lib/ecoportal/api/internal/account.rb +31 -0
  25. data/lib/ecoportal/api/internal/login_provider.rb +9 -0
  26. data/lib/ecoportal/api/internal/login_providers.rb +18 -0
  27. data/lib/ecoportal/api/internal/people.rb +19 -0
  28. data/lib/ecoportal/api/internal/permissions.rb +9 -0
  29. data/lib/ecoportal/api/internal/person.rb +40 -0
  30. data/lib/ecoportal/api/internal/person_details.rb +13 -0
  31. data/lib/ecoportal/api/internal/person_schema.rb +14 -0
  32. data/lib/ecoportal/api/internal/person_schemas.rb +15 -0
  33. data/lib/ecoportal/api/internal/policy_group.rb +9 -0
  34. data/lib/ecoportal/api/internal/policy_groups.rb +18 -0
  35. data/lib/ecoportal/api/internal/preferences.rb +9 -0
  36. data/lib/ecoportal/api/internal/schema_field.rb +8 -0
  37. data/lib/ecoportal/api/internal/schema_field_value.rb +8 -0
  38. data/lib/ecoportal/api/v1.rb +23 -0
  39. data/lib/ecoportal/api/v1/.git-keep +0 -0
  40. data/lib/ecoportal/api/v1/people.rb +98 -0
  41. data/lib/ecoportal/api/v1/person.rb +50 -0
  42. data/lib/ecoportal/api/v1/person_details.rb +51 -0
  43. data/lib/ecoportal/api/v1/person_schema.rb +46 -0
  44. data/lib/ecoportal/api/v1/person_schemas.rb +24 -0
  45. data/lib/ecoportal/api/v1/schema_field.rb +9 -0
  46. data/lib/ecoportal/api/v1/schema_field_value.rb +67 -0
  47. data/lib/ecoportal/api/version.rb +5 -0
  48. metadata +146 -0
@@ -0,0 +1,37 @@
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ module HashDiff
5
+ ID_KEYS=%w[id]
6
+ def self.diff(a, b)
7
+ return a if a.class != b.class
8
+ case a
9
+ when Hash
10
+ {}.tap do |diffed|
11
+ a.each do |key, a_value|
12
+ b_value = b[key]
13
+ next if a_value == b_value && !ID_KEYS.include?(key)
14
+ diffed[key] = diff(a_value, b_value)
15
+ diffed.delete[key] if diffed[key] == {}
16
+ end
17
+ # All keys are IDs, so it's actually blank
18
+ if (diffed.keys - ID_KEYS).empty?
19
+ return {}
20
+ end
21
+ end
22
+ when Array
23
+ return a unless a.length == b.length
24
+ a.map.with_index do |a_value, idx|
25
+ b_value = b[idx]
26
+ diff(a_value, b_value)
27
+ end.reject do |el|
28
+ el == {}
29
+ end
30
+ else
31
+ a
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,22 @@
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ class Response
5
+ attr_reader :status, :body
6
+ def initialize(response)
7
+ @status = response.status
8
+ @body = JSON.parse(response.to_s) rescue nil
9
+ response.flush
10
+ end
11
+ def success?
12
+ @status.success?
13
+ end
14
+ def print
15
+ puts "Status: #{@status.code}"
16
+ puts "Body:"
17
+ puts JSON.pretty_generate(@body)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,38 @@
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ class WrappedResponse
5
+ attr_reader :response, :result
6
+ def initialize(response, klass)
7
+ @response = response
8
+ @klass = klass
9
+
10
+ if @response.success?
11
+ @result = if @response.body.is_a?(Array)
12
+ @response.body.map do |doc|
13
+ @klass.new(doc)
14
+ end
15
+ else
16
+ @klass.new(@response.body)
17
+ end
18
+ end
19
+ end
20
+ def each
21
+ [*@result].each do |doc|
22
+ yield doc
23
+ end
24
+ end
25
+ def success?
26
+ @response.success?
27
+ end
28
+ def print
29
+ if success?
30
+ @result&.map(&:print)
31
+ else
32
+ puts "Request failed."
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,31 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ attr_reader :client
5
+ def initialize(api_key, host: "live.ecoportal.com")
6
+ @client = Common::Client.new(
7
+ api_key: api_key,
8
+ host: host,
9
+ version: "v0"
10
+ )
11
+ end
12
+ def people
13
+ Internal::People.new(client)
14
+ end
15
+ def person_schemas
16
+ Internal::PersonSchemas.new(client)
17
+ end
18
+ def policy_groups
19
+ Internal::PolicyGroups.new(client)
20
+ end
21
+ def login_providers
22
+ Internal::LoginProviders.new(client)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ require 'ecoportal/api/internal/people'
29
+ require 'ecoportal/api/internal/person_schemas'
30
+ require 'ecoportal/api/internal/policy_groups'
31
+ require 'ecoportal/api/internal/login_providers'
File without changes
@@ -0,0 +1,31 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
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, to: :doc
6
+
7
+ def preset=(value)
8
+ self.permissions_preset = value == "custom" ? nil : value
9
+ end
10
+
11
+ def preset
12
+ self.permissions_preset.nil? ? "custom" : self.permissions_preset
13
+ end
14
+
15
+ def permissions
16
+ return @permissions if defined?(@permissions)
17
+ return @permissions = nil if doc["permissions_custom"].nil?
18
+ @permissions = Internal::Permissions.new(doc["permissions_custom"])
19
+ end
20
+
21
+ def preferences
22
+ return @preferences if defined?(@preferences)
23
+ return @preferences = nil if doc["preferences"].nil?
24
+ @preferences = Internal::Preferences.new(doc["preferences"])
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ require 'ecoportal/api/internal/permissions'
31
+ require 'ecoportal/api/internal/preferences'
@@ -0,0 +1,9 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class LoginProvider < Common::BaseModel
5
+ passthrough :id, :name, :type, :enabled_for, to: :doc
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class LoginProviders
5
+ attr_reader :client
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+ def get_all
10
+ response = @client.get("/login_providers")
11
+ Common::WrappedResponse.new(response, Internal::LoginProvider)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ require 'ecoportal/api/internal/login_provider'
@@ -0,0 +1,19 @@
1
+ require 'ecoportal/api/common/doc_helpers'
2
+ module Ecoportal
3
+ module API
4
+ class Internal
5
+ class People < V1::People
6
+ private
7
+
8
+ def person_class
9
+ Internal::Person
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ require 'ecoportal/api/internal/person'
17
+ require 'ecoportal/api/internal/person_details'
18
+ require 'ecoportal/api/internal/schema_field_value'
19
+ require 'ecoportal/api/internal/account'
@@ -0,0 +1,9 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class Permissions < Common::BaseModel
5
+ passthrough :files, :data, :reports, :organization, :people, :pages, :page_editor, :registers, to: :doc
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class Person < V1::Person
5
+ def as_json
6
+ super.update("account" => account&.as_json)
7
+ end
8
+
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
+
15
+ def account=(value)
16
+ case value
17
+ when NilClass
18
+ doc["account"] = nil
19
+ when Internal::Account
20
+ doc["account"] = JSON.parse(value.to_json)
21
+ when Hash
22
+ # TODO
23
+ 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
+ else
25
+ # TODO
26
+ raise "Invalid set on account: Need nil, Account or Hash; got #{value.class}"
27
+ end
28
+ remove_instance_variable("@account")
29
+ return account
30
+ end
31
+
32
+ private
33
+
34
+ def person_details_class
35
+ Internal::PersonDetails
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,13 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class PersonDetails < V1::PersonDetails
5
+ private
6
+
7
+ def schema_field_value_class
8
+ Internal::SchemaFieldValue
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class PersonSchema < V1::PersonSchema
5
+ private
6
+
7
+ def schema_field_class
8
+ Internal::SchemaField
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ require 'ecoportal/api/internal/schema_field'
@@ -0,0 +1,15 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class PersonSchemas < V1::PersonSchemas
5
+ private
6
+
7
+ def person_schema_class
8
+ Internal::PersonSchema
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ require 'ecoportal/api/internal/person_schema'
@@ -0,0 +1,9 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class PolicyGroup < Common::BaseModel
5
+ passthrough :id, :name, to: :doc
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class PolicyGroups
5
+ attr_reader :client
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+ def get_all
10
+ response = @client.get("/policy_groups")
11
+ Common::WrappedResponse.new(response, Internal::PolicyGroup)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ require 'ecoportal/api/internal/policy_group'
@@ -0,0 +1,9 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class Preferences < Common::BaseModel
5
+ passthrough :show_sidebar, :show_shortcuts, :show_coming_soon, :show_recently_visited_forms, :show_tasks, to: :doc
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class SchemaField < V1::SchemaField
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class SchemaFieldValue < V1::SchemaFieldValue
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ module Ecoportal
2
+ module API
3
+ class V1
4
+ attr_reader :client
5
+ def initialize(api_key, host: "live.ecoportal.com")
6
+ @client = Common::Client.new(
7
+ api_key: api_key,
8
+ host: host,
9
+ version: "v1"
10
+ )
11
+ end
12
+ def people
13
+ V1::People.new(client)
14
+ end
15
+ def person_schemas
16
+ V1::PersonSchemas.new(client)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ require 'ecoportal/api/v1/people'
23
+ require 'ecoportal/api/v1/person_schemas'
File without changes
@@ -0,0 +1,98 @@
1
+ require 'ecoportal/api/common/doc_helpers'
2
+ module Ecoportal
3
+ module API
4
+ class V1
5
+ class People
6
+ include Common::DocHelpers
7
+ attr_reader :client
8
+
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ def each(params: {}, &block)
14
+ page = 1
15
+ loop do
16
+ response = @client.get("/people", params: params.merge(page: page))
17
+ raise "Request failed." unless response.success?
18
+ response.body["results"].each do |person|
19
+ yield person_class.new(person)
20
+ end
21
+ break if page >= response.body["total_pages"]
22
+ page += 1
23
+ end
24
+ self
25
+ end
26
+
27
+ def get(id)
28
+ response = @client.get("/people/"+CGI::escape(id))
29
+ Common::WrappedResponse.new(response, person_class)
30
+ end
31
+
32
+ def get_all(params: {})
33
+ each(params).to_a
34
+ end
35
+
36
+ def update(doc)
37
+ body = get_body(doc)
38
+ id = get_id(doc)
39
+ @client.patch("/people/"+CGI::escape(id), data: body)
40
+ end
41
+
42
+ def create(doc)
43
+ body = get_body(doc)
44
+ @client.post("/people", data: body)
45
+ end
46
+
47
+ def upsert(doc)
48
+ body = get_body(doc)
49
+ id = get_id(doc)
50
+ @client.post("/people/"+CGI::escape(id), data: body)
51
+ end
52
+
53
+ def batch
54
+ operation = Common::BatchOperation.new("/people", person_class)
55
+ yield operation
56
+ @client.post("/people/batch", data: operation.as_json).tap do |response|
57
+ operation.process_response(response)
58
+ end
59
+ end
60
+
61
+ def delete(doc)
62
+ id = get_id(doc)
63
+ @client.delete("/people/"+CGI::escape(id))
64
+ end
65
+
66
+ private
67
+
68
+ def person_class
69
+ V1::Person
70
+ end
71
+
72
+ def get_body(doc)
73
+ if doc.respond_to?(:as_update)
74
+ doc.as_update
75
+ elsif doc.respond_to?(:as_json)
76
+ doc.as_json
77
+ else
78
+ doc
79
+ end
80
+ end
81
+ def get_id(doc)
82
+ id = nil
83
+ id ||= doc.id if doc.respond_to?(:id)
84
+ id ||= doc.external_id if doc.respond_to?(:external_id)
85
+ id ||= doc["id"] if doc.is_a?(Hash)
86
+ id ||= doc["external_id"] if doc.is_a?(Hash)
87
+ id ||= doc if doc.is_a?(String)
88
+ id or raise "No ID has been given!"
89
+ id
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ require 'ecoportal/api/v1/person'
97
+ require 'ecoportal/api/v1/person_details'
98
+ require 'ecoportal/api/v1/schema_field_value'