ecoportal-api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +53 -0
- data/README.md +34 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ecoportal-api.gemspec +28 -0
- data/lib/ecoportal/api.rb +11 -0
- data/lib/ecoportal/api/common.rb +13 -0
- data/lib/ecoportal/api/common/base_model.rb +39 -0
- data/lib/ecoportal/api/common/batch_operation.rb +94 -0
- data/lib/ecoportal/api/common/batch_response.rb +16 -0
- data/lib/ecoportal/api/common/client.rb +58 -0
- data/lib/ecoportal/api/common/doc_helpers.rb +27 -0
- data/lib/ecoportal/api/common/hash_diff.rb +37 -0
- data/lib/ecoportal/api/common/response.rb +22 -0
- data/lib/ecoportal/api/common/wrapped_response.rb +38 -0
- data/lib/ecoportal/api/internal.rb +31 -0
- data/lib/ecoportal/api/internal/.git-keep +0 -0
- data/lib/ecoportal/api/internal/account.rb +31 -0
- data/lib/ecoportal/api/internal/login_provider.rb +9 -0
- data/lib/ecoportal/api/internal/login_providers.rb +18 -0
- data/lib/ecoportal/api/internal/people.rb +19 -0
- data/lib/ecoportal/api/internal/permissions.rb +9 -0
- data/lib/ecoportal/api/internal/person.rb +40 -0
- data/lib/ecoportal/api/internal/person_details.rb +13 -0
- data/lib/ecoportal/api/internal/person_schema.rb +14 -0
- data/lib/ecoportal/api/internal/person_schemas.rb +15 -0
- data/lib/ecoportal/api/internal/policy_group.rb +9 -0
- data/lib/ecoportal/api/internal/policy_groups.rb +18 -0
- data/lib/ecoportal/api/internal/preferences.rb +9 -0
- data/lib/ecoportal/api/internal/schema_field.rb +8 -0
- data/lib/ecoportal/api/internal/schema_field_value.rb +8 -0
- data/lib/ecoportal/api/v1.rb +23 -0
- data/lib/ecoportal/api/v1/.git-keep +0 -0
- data/lib/ecoportal/api/v1/people.rb +98 -0
- data/lib/ecoportal/api/v1/person.rb +50 -0
- data/lib/ecoportal/api/v1/person_details.rb +51 -0
- data/lib/ecoportal/api/v1/person_schema.rb +46 -0
- data/lib/ecoportal/api/v1/person_schemas.rb +24 -0
- data/lib/ecoportal/api/v1/schema_field.rb +9 -0
- data/lib/ecoportal/api/v1/schema_field_value.rb +67 -0
- data/lib/ecoportal/api/version.rb +5 -0
- 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,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,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,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,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'
|