zaikio-directory 0.0.6 → 0.0.11
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 +4 -4
- data/README.md +36 -1
- data/lib/zaikio/directory.rb +10 -6
- data/lib/zaikio/directory/asset.rb +55 -0
- data/lib/zaikio/directory/authorization_middleware.rb +3 -5
- data/lib/zaikio/directory/basic_auth_middleware.rb +3 -5
- data/lib/zaikio/directory/current_organization.rb +16 -6
- data/lib/zaikio/directory/current_person.rb +5 -1
- data/lib/zaikio/directory/json_parser.rb +21 -1
- data/lib/zaikio/directory/machine.rb +2 -21
- data/lib/zaikio/directory/organization.rb +2 -0
- data/lib/zaikio/directory/software.rb +5 -18
- data/lib/zaikio/directory/specialist.rb +10 -0
- data/lib/zaikio/directory/version.rb +1 -1
- data/lib/zaikio/error.rb +16 -0
- metadata +10 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07fb7419f188af643dfaa5440d77827a4bedd91b820e18a6714b8d45f194d2f7
|
4
|
+
data.tar.gz: 878c7ba7a273c9526c556727cc9673d02956298d5b820a1f724949557d73eab0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad30c796f5615df1ef8c1f61d8a11f01460d085db6c2e2e054bb25e588b36fec61ac657c66b019b1b6f5412f8568211af50b3aa25797c7cc31a8a63db89ccded
|
7
|
+
data.tar.gz: 65c882cdf8824f5bed2d7c1da73d7b8dcf03e89d74961b72010f04b365da04374289b319e5ef4a93a33f7d73641dde2fd869852dd215fa938443389c728e5e0a
|
data/README.md
CHANGED
@@ -26,7 +26,7 @@ $ gem install zaikio-directory
|
|
26
26
|
# config/initializers/zaikio_directory.rb
|
27
27
|
|
28
28
|
Zaikio::Directory.configure do |config|
|
29
|
-
config.
|
29
|
+
config.environment = :production # sandbox or production
|
30
30
|
end
|
31
31
|
```
|
32
32
|
|
@@ -106,3 +106,38 @@ Zaikio::Directory.with_token(token) do
|
|
106
106
|
Zaikio::Directory::RevokedAccessToken.create
|
107
107
|
end
|
108
108
|
```
|
109
|
+
|
110
|
+
### Error Handling
|
111
|
+
|
112
|
+
If an unexpected error occurs with an API call (i.e. an error that has no status code `2xx`, `404` or `422`) then a `Zaikio::ConnectionError` is thrown automatically (for `404` there will be a `Zaikio::ResourceNotFound`).
|
113
|
+
|
114
|
+
This can be easily caught using the `with_fallback` method. We recommend to always work with fallbacks.
|
115
|
+
|
116
|
+
```rb
|
117
|
+
Zaikio::Directory.with_token(token) do
|
118
|
+
person = Zaikio::Directory::CurrentPerson
|
119
|
+
.find_with_fallback(Zaikio::Directory::CurrentPerson.new(full_name: "Hello World"))
|
120
|
+
|
121
|
+
person.organizations # Automatically uses empty array as fallback
|
122
|
+
end
|
123
|
+
|
124
|
+
Zaikio::Directory.with_token(token) do
|
125
|
+
organization = Zaikio::Directory::CurrentOrganization.new
|
126
|
+
|
127
|
+
organization.machines.with_fallback.all
|
128
|
+
organization.machines
|
129
|
+
.with_fallback(Zaikio::Directory::Machine.new(name: 'My Machine'))
|
130
|
+
.find('machine-id')
|
131
|
+
|
132
|
+
|
133
|
+
organization.machines
|
134
|
+
.with_fallback(Zaikio::Directory::Machine.new(name: 'My Machine'))
|
135
|
+
.find('machine-does-not-exist') # => raises Zaikio::ResourceNotFound
|
136
|
+
|
137
|
+
begin
|
138
|
+
organization.machines.create(name: "Machine Name")
|
139
|
+
rescue Zaikio::ConnectionError
|
140
|
+
# Do something
|
141
|
+
end
|
142
|
+
end
|
143
|
+
```
|
data/lib/zaikio/directory.rb
CHANGED
@@ -6,13 +6,16 @@ require "zaikio/directory/authorization_middleware"
|
|
6
6
|
require "zaikio/directory/basic_auth_middleware"
|
7
7
|
|
8
8
|
# Models
|
9
|
+
require "zaikio/error"
|
9
10
|
require "zaikio/directory/base"
|
11
|
+
require "zaikio/directory/asset"
|
10
12
|
require "zaikio/directory/organization_membership"
|
11
13
|
require "zaikio/directory/business_relationship"
|
12
14
|
require "zaikio/directory/organization"
|
13
15
|
require "zaikio/directory/person"
|
14
16
|
require "zaikio/directory/machine"
|
15
17
|
require "zaikio/directory/software"
|
18
|
+
require "zaikio/directory/specialist"
|
16
19
|
require "zaikio/directory/site"
|
17
20
|
require "zaikio/directory/membership"
|
18
21
|
require "zaikio/directory/current_person"
|
@@ -25,24 +28,25 @@ module Zaikio
|
|
25
28
|
module Directory
|
26
29
|
class << self
|
27
30
|
attr_accessor :configuration
|
31
|
+
class_attribute :connection
|
28
32
|
|
29
33
|
def configure
|
30
|
-
|
34
|
+
self.connection = nil
|
31
35
|
self.configuration ||= Configuration.new
|
32
36
|
yield(configuration)
|
33
37
|
|
34
|
-
Base.connection =
|
38
|
+
Base.connection = create_connection
|
35
39
|
end
|
36
40
|
|
37
41
|
def with_token(token)
|
38
|
-
AuthorizationMiddleware.token token
|
42
|
+
AuthorizationMiddleware.token = token
|
39
43
|
yield
|
40
44
|
ensure
|
41
45
|
AuthorizationMiddleware.reset_token
|
42
46
|
end
|
43
47
|
|
44
48
|
def with_basic_auth(login, password)
|
45
|
-
BasicAuthMiddleware.credentials [login, password]
|
49
|
+
BasicAuthMiddleware.credentials = [login, password]
|
46
50
|
yield
|
47
51
|
ensure
|
48
52
|
BasicAuthMiddleware.reset_credentials
|
@@ -56,8 +60,8 @@ module Zaikio
|
|
56
60
|
create_token_data(payload)
|
57
61
|
end
|
58
62
|
|
59
|
-
def
|
60
|
-
|
63
|
+
def create_connection
|
64
|
+
self.connection = Faraday.new(url: "#{configuration.host}/api/v1") do |c|
|
61
65
|
c.request :json
|
62
66
|
c.response :logger, configuration&.logger
|
63
67
|
c.use JSONParser
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Zaikio
|
2
|
+
module Directory
|
3
|
+
module Asset
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
# Callbacks
|
8
|
+
after_create :make_organization_owner
|
9
|
+
end
|
10
|
+
|
11
|
+
def make_organization_owner
|
12
|
+
if Zaikio::Directory.current_token_data.subject_type == "Organization"
|
13
|
+
self.class.request(:post,
|
14
|
+
"#{collection_name}/#{id}/#{singular_name}_ownership")
|
15
|
+
else
|
16
|
+
org_path = "person/organizations/#{organization_id}"
|
17
|
+
self.class.request(:post,
|
18
|
+
"#{org_path}/#{collection_name}/#{id}/#{singular_name}_ownership")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def destroy
|
23
|
+
if Zaikio::Directory.current_token_data.subject_type == "Organization"
|
24
|
+
self.class.request(:delete,
|
25
|
+
"#{collection_name}/#{id}/#{singular_name}_ownership")
|
26
|
+
else
|
27
|
+
org_path = "person/organizations/#{owner_id || organization_id}"
|
28
|
+
self.class.request(:delete,
|
29
|
+
"#{org_path}/#{collection_name}/#{id}/#{singular_name}_ownership")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def specification
|
34
|
+
prefix = if Zaikio::Directory.current_token_data.subject_type == "Person"
|
35
|
+
"person/organizations/#{owner_id || organization_id}/"
|
36
|
+
else
|
37
|
+
""
|
38
|
+
end
|
39
|
+
self.class.request(:get,
|
40
|
+
"#{prefix}#{collection_name}/#{id}/#{singular_name}_specification")
|
41
|
+
&.body&.dig("data")
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def collection_name
|
47
|
+
self.class.name.demodulize.underscore.pluralize
|
48
|
+
end
|
49
|
+
|
50
|
+
def singular_name
|
51
|
+
self.class.name.demodulize.underscore
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -4,12 +4,10 @@ require "jwt"
|
|
4
4
|
module Zaikio
|
5
5
|
module Directory
|
6
6
|
class AuthorizationMiddleware < Faraday::Middleware
|
7
|
-
|
8
|
-
@token = nil
|
9
|
-
end
|
7
|
+
class_attribute :token
|
10
8
|
|
11
|
-
def self.
|
12
|
-
|
9
|
+
def self.reset_token
|
10
|
+
self.token = nil
|
13
11
|
end
|
14
12
|
|
15
13
|
def call(request_env)
|
@@ -4,12 +4,10 @@ require "base64"
|
|
4
4
|
module Zaikio
|
5
5
|
module Directory
|
6
6
|
class BasicAuthMiddleware < Faraday::Middleware
|
7
|
-
|
8
|
-
@credentials = nil
|
9
|
-
end
|
7
|
+
class_attribute :credentials
|
10
8
|
|
11
|
-
def self.
|
12
|
-
|
9
|
+
def self.reset_credentials
|
10
|
+
self.credentials = nil
|
13
11
|
end
|
14
12
|
|
15
13
|
def call(request_env)
|
@@ -9,24 +9,34 @@ module Zaikio
|
|
9
9
|
all.find_one
|
10
10
|
end
|
11
11
|
|
12
|
+
def self.find_with_fallback(fallback)
|
13
|
+
all.with_fallback(fallback).find_one
|
14
|
+
end
|
15
|
+
|
12
16
|
# Associations
|
13
17
|
has_many :memberships, class_name: "Zaikio::Directory::Membership",
|
14
|
-
uri: "organization/memberships"
|
18
|
+
uri: "organization/memberships(/:id)"
|
15
19
|
has_many :business_relationships, class_name: "Zaikio::Directory::BusinessRelationship",
|
16
|
-
uri: "organization/business_relationships"
|
20
|
+
uri: "organization/business_relationships(/:id)"
|
17
21
|
has_many :software, class_name: "Zaikio::Directory::Software",
|
18
|
-
uri: "software"
|
22
|
+
uri: "software(/:id)"
|
19
23
|
has_many :machines, class_name: "Zaikio::Directory::Machine",
|
20
|
-
uri: "machines"
|
24
|
+
uri: "machines(/:id)"
|
25
|
+
has_many :specialists, class_name: "Zaikio::Directory::Specialist",
|
26
|
+
uri: "specialists(/:id)"
|
21
27
|
has_many :sites, class_name: "Zaikio::Directory::Site",
|
22
|
-
uri: "sites"
|
28
|
+
uri: "sites(/:id)"
|
23
29
|
|
24
30
|
def fetch
|
25
31
|
self.attributes = get
|
26
32
|
end
|
27
33
|
|
34
|
+
def reload
|
35
|
+
self.attributes = self.class.find.attributes
|
36
|
+
end
|
37
|
+
|
28
38
|
def members
|
29
|
-
memberships.map(&:person)
|
39
|
+
memberships.with_fallback.map(&:person)
|
30
40
|
end
|
31
41
|
end
|
32
42
|
end
|
@@ -9,6 +9,10 @@ module Zaikio
|
|
9
9
|
all.find_one
|
10
10
|
end
|
11
11
|
|
12
|
+
def self.find_with_fallback(fallback)
|
13
|
+
all.with_fallback(fallback).find_one
|
14
|
+
end
|
15
|
+
|
12
16
|
# Associations
|
13
17
|
has_many :organization_memberships, class_name: "Zaikio::Directory::OrganizationMembership",
|
14
18
|
uri: nil
|
@@ -18,7 +22,7 @@ module Zaikio
|
|
18
22
|
end
|
19
23
|
|
20
24
|
def organizations
|
21
|
-
organization_memberships.map(&:organization)
|
25
|
+
organization_memberships.with_fallback.map(&:organization)
|
22
26
|
end
|
23
27
|
|
24
28
|
def admin_organizations
|
@@ -3,13 +3,33 @@ require "multi_json"
|
|
3
3
|
module Zaikio
|
4
4
|
module Directory
|
5
5
|
class JSONParser < Faraday::Response::Middleware
|
6
|
-
def
|
6
|
+
def on_complete(env)
|
7
|
+
connection_error(env) unless /^(2\d\d)|422|404$/.match?(env.status.to_s)
|
8
|
+
|
9
|
+
raise Spyke::ResourceNotFound if env.status.to_s == "404"
|
10
|
+
|
11
|
+
env.body = parse_body(env.body)
|
12
|
+
end
|
13
|
+
|
14
|
+
def connection_error(env)
|
15
|
+
Zaikio::Directory.configuration.logger
|
16
|
+
.error("Zaikio::Directory Status Code #{env.status}, #{env.body}")
|
17
|
+
raise Spyke::ConnectionError, "Status Code #{env.status}, #{env.body}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse_body(body)
|
7
21
|
json = MultiJson.load(body, symbolize_keys: true)
|
8
22
|
{
|
9
23
|
data: json,
|
10
24
|
metadata: {},
|
11
25
|
errors: json.is_a?(Hash) ? json[:errors] : {}
|
12
26
|
}
|
27
|
+
rescue MultiJson::ParseError
|
28
|
+
{
|
29
|
+
data: {},
|
30
|
+
metadata: {},
|
31
|
+
errors: {}
|
32
|
+
}
|
13
33
|
end
|
14
34
|
end
|
15
35
|
end
|
@@ -1,29 +1,10 @@
|
|
1
1
|
module Zaikio
|
2
2
|
module Directory
|
3
3
|
class Machine < Base
|
4
|
+
include Asset
|
5
|
+
|
4
6
|
uri "machines(/:id)"
|
5
7
|
include_root_in_json :machine
|
6
|
-
|
7
|
-
# Callbacks
|
8
|
-
after_create :make_organization_owner
|
9
|
-
|
10
|
-
def make_organization_owner
|
11
|
-
if Zaikio::Directory.current_token_data.subject_type == "Organization"
|
12
|
-
self.class.request(:post, "machines/#{id}/machine_ownership")
|
13
|
-
else
|
14
|
-
org_path = "person/organizations/#{organization_id}"
|
15
|
-
self.class.request(:post, "#{org_path}/machines/#{id}/machine_ownership")
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def destroy
|
20
|
-
if Zaikio::Directory.current_token_data.subject_type == "Organization"
|
21
|
-
self.class.request(:delete, "machines/#{id}/machine_ownership")
|
22
|
-
else
|
23
|
-
org_path = "person/organizations/#{owner_id || organization_id}"
|
24
|
-
self.class.request(:delete, "#{org_path}/machines/#{id}/machine_ownership")
|
25
|
-
end
|
26
|
-
end
|
27
8
|
end
|
28
9
|
end
|
29
10
|
end
|
@@ -12,6 +12,8 @@ module Zaikio
|
|
12
12
|
uri: "person/organizations/:organization_id/software"
|
13
13
|
has_many :machines, class_name: "Zaikio::Directory::Machine",
|
14
14
|
uri: "person/organizations/:organization_id/machines"
|
15
|
+
has_many :specialists, class_name: "Zaikio::Directory::Specialist",
|
16
|
+
uri: "person/organizations/:organization_id/specialists"
|
15
17
|
has_many :sites, class_name: "Zaikio::Directory::Site",
|
16
18
|
uri: "person/organizations/:organization_id/sites"
|
17
19
|
end
|
@@ -1,28 +1,15 @@
|
|
1
1
|
module Zaikio
|
2
2
|
module Directory
|
3
3
|
class Software < Base
|
4
|
+
include Asset
|
5
|
+
|
4
6
|
uri "software(/:id)"
|
5
7
|
include_root_in_json :software
|
6
8
|
|
7
|
-
|
8
|
-
after_create :make_organization_owner
|
9
|
-
|
10
|
-
def make_organization_owner
|
11
|
-
if Zaikio::Directory.current_token_data.subject_type == "Organization"
|
12
|
-
self.class.request(:post, "software/#{id}/software_ownership")
|
13
|
-
else
|
14
|
-
org_path = "person/organizations/#{organization_id}"
|
15
|
-
self.class.request(:post, "#{org_path}/software/#{id}/software_ownership")
|
16
|
-
end
|
17
|
-
end
|
9
|
+
private
|
18
10
|
|
19
|
-
def
|
20
|
-
|
21
|
-
self.class.request(:delete, "software/#{id}/software_ownership")
|
22
|
-
else
|
23
|
-
org_path = "person/organizations/#{owner_id || organization_id}"
|
24
|
-
self.class.request(:delete, "#{org_path}/software/#{id}/software_ownership")
|
25
|
-
end
|
11
|
+
def collection_name
|
12
|
+
"software"
|
26
13
|
end
|
27
14
|
end
|
28
15
|
end
|
data/lib/zaikio/error.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Zaikio
|
2
|
+
class Error < StandardError; end
|
3
|
+
class ConnectionError < Zaikio::Error; end
|
4
|
+
class ResourceNotFound < Zaikio::Error; end
|
5
|
+
end
|
6
|
+
|
7
|
+
module Spyke
|
8
|
+
instance_eval do
|
9
|
+
# avoid warning: already initialized constant
|
10
|
+
remove_const("ConnectionError")
|
11
|
+
remove_const("ResourceNotFound")
|
12
|
+
end
|
13
|
+
|
14
|
+
ConnectionError = Class.new Zaikio::ConnectionError
|
15
|
+
ResourceNotFound = Class.new Zaikio::ResourceNotFound
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zaikio-directory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- crispymtn
|
8
|
+
- Jalyna Schröder
|
9
|
+
- Martin Spickermann
|
8
10
|
autorequire:
|
9
11
|
bindir: bin
|
10
12
|
cert_chain: []
|
11
|
-
date: 2020-
|
13
|
+
date: 2020-06-15 00:00:00.000000000 Z
|
12
14
|
dependencies:
|
13
15
|
- !ruby/object:Gem::Dependency
|
14
16
|
name: jwt
|
@@ -68,7 +70,9 @@ dependencies:
|
|
68
70
|
version: 5.3.4
|
69
71
|
description: Ruby API Client for Zaikio's Directory
|
70
72
|
email:
|
73
|
+
- op@crispymtn.com
|
71
74
|
- js@crispymtn.com
|
75
|
+
- spickermann@gmail.com
|
72
76
|
executables: []
|
73
77
|
extensions: []
|
74
78
|
extra_rdoc_files: []
|
@@ -77,6 +81,7 @@ files:
|
|
77
81
|
- README.md
|
78
82
|
- Rakefile
|
79
83
|
- lib/zaikio/directory.rb
|
84
|
+
- lib/zaikio/directory/asset.rb
|
80
85
|
- lib/zaikio/directory/authorization_middleware.rb
|
81
86
|
- lib/zaikio/directory/base.rb
|
82
87
|
- lib/zaikio/directory/basic_auth_middleware.rb
|
@@ -95,7 +100,9 @@ files:
|
|
95
100
|
- lib/zaikio/directory/role.rb
|
96
101
|
- lib/zaikio/directory/site.rb
|
97
102
|
- lib/zaikio/directory/software.rb
|
103
|
+
- lib/zaikio/directory/specialist.rb
|
98
104
|
- lib/zaikio/directory/version.rb
|
105
|
+
- lib/zaikio/error.rb
|
99
106
|
homepage: https://www.zaikio.com/
|
100
107
|
licenses:
|
101
108
|
- MIT
|