zaikio-directory 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 425d810f240fc3a9a78e86b8a7216621dc2276c34fb1125816df5b4b8d514bb3
4
- data.tar.gz: 7795f54139e370afe28994137d34614985fb5553f5df6e078fb53c440b8a29a9
3
+ metadata.gz: a72248c2cce21c6086f28c1973032fb48be5a3090887209dd0eff98441506fe1
4
+ data.tar.gz: 6e7bd4cee9fe02cc0faae2f2acfd67eae92346951e9500df050865402996f001
5
5
  SHA512:
6
- metadata.gz: e248bd3df0c8f53b40d63c141b6f3b8c0ee4f1af7f3aecc0ca2765595bbfd072319e983e7685c3d0328ee9c156e9094b9f051634a922435bbafbd596e897d6bd
7
- data.tar.gz: 99f5c598b8a6369525dc90904bce3c75dd781a1de13e4abfd7589a149c527333187acddfa4db064d34398541a866d9cea3d524e865ff68607ee41fba17bcb76b
6
+ metadata.gz: 73add47c0e748bf676d5dc0d1db7da71ae5e5393d191b68eb14025f936dee92cff0e9933db56201d33a03e4b51cd992a14c3ecb7aaf1ce531a6599c490ad34f6
7
+ data.tar.gz: c8e8b679d75533d493090edc93fce47db3ab884833499fd1a40cd238df650583db86c3cb5d3197e3b792cb04b210dfb42db52496bc357b430c114dd7f47d28c6
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.host :production # sandbox or production
29
+ config.environment = :production # sandbox or production
30
30
  end
31
31
  ```
32
32
 
@@ -35,7 +35,7 @@ end
35
35
 
36
36
  The Directory Client has an ORM like design. However, we distinguish between the contexts of the organization and the person.
37
37
 
38
- For the requests to work, a valid JSON Web token with the correct OAuth Scopes must always be provided. Please refer to [zakio-oauth_client](#todo).
38
+ For the requests to work, a valid JSON Web token with the correct OAuth Scopes must always be provided. Please refer to [zakio-oauth_client](https://github.com/crispymtn/zaikio-oauth_client).
39
39
 
40
40
  If you want to know which actions are available and which scopes are required, please refer to the [Directory API Reference](https://docs.zaikio.com/api/directory/directory.html).
41
41
 
@@ -97,6 +97,10 @@ end
97
97
  ```rb
98
98
  Zaikio::Directory.with_basic_auth(client_id, client_secret) do
99
99
  connections = Zaikio::Directory::Connection.all
100
+ subscription = Zaikio::Directory::Subscription.find("Organization-b1475f65-236c-58b8-96e1-e1778b43beb7")
101
+ subscription.plan # => "advanced"
102
+ subscription.activate!
103
+ subscription.increment_usage_by!(:orders_created, 12)
100
104
  end
101
105
 
102
106
  roles = Zaikio::Directory::Role.all
@@ -8,12 +8,14 @@ require "zaikio/directory/basic_auth_middleware"
8
8
  # Models
9
9
  require "zaikio/error"
10
10
  require "zaikio/directory/base"
11
+ require "zaikio/directory/asset"
11
12
  require "zaikio/directory/organization_membership"
12
13
  require "zaikio/directory/business_relationship"
13
14
  require "zaikio/directory/organization"
14
15
  require "zaikio/directory/person"
15
16
  require "zaikio/directory/machine"
16
17
  require "zaikio/directory/software"
18
+ require "zaikio/directory/specialist"
17
19
  require "zaikio/directory/site"
18
20
  require "zaikio/directory/membership"
19
21
  require "zaikio/directory/current_person"
@@ -21,29 +23,31 @@ require "zaikio/directory/current_organization"
21
23
  require "zaikio/directory/role"
22
24
  require "zaikio/directory/revoked_access_token"
23
25
  require "zaikio/directory/connection"
26
+ require "zaikio/directory/subscription"
24
27
 
25
28
  module Zaikio
26
29
  module Directory
27
30
  class << self
28
31
  attr_accessor :configuration
32
+ class_attribute :connection
29
33
 
30
34
  def configure
31
- @connection = nil
35
+ self.connection = nil
32
36
  self.configuration ||= Configuration.new
33
37
  yield(configuration)
34
38
 
35
- Base.connection = connection
39
+ Base.connection = create_connection
36
40
  end
37
41
 
38
42
  def with_token(token)
39
- AuthorizationMiddleware.token token
43
+ AuthorizationMiddleware.token = token
40
44
  yield
41
45
  ensure
42
46
  AuthorizationMiddleware.reset_token
43
47
  end
44
48
 
45
49
  def with_basic_auth(login, password)
46
- BasicAuthMiddleware.credentials [login, password]
50
+ BasicAuthMiddleware.credentials = [login, password]
47
51
  yield
48
52
  ensure
49
53
  BasicAuthMiddleware.reset_credentials
@@ -57,8 +61,9 @@ module Zaikio
57
61
  create_token_data(payload)
58
62
  end
59
63
 
60
- def connection
61
- @connection ||= Faraday.new(url: "#{configuration.host}/api/v1") do |c|
64
+ def create_connection
65
+ self.connection = Faraday.new(url: "#{configuration.host}/api/v1",
66
+ ssl: { verify: configuration.environment != :test }) do |c|
62
67
  c.request :json
63
68
  c.response :logger, configuration&.logger
64
69
  c.use JSONParser
@@ -70,7 +75,7 @@ module Zaikio
70
75
 
71
76
  private
72
77
 
73
- def create_token_data(payload) # rubocop:disable Metrics/AbcSize
78
+ def create_token_data(payload)
74
79
  subjects = payload["sub"].split(">")
75
80
 
76
81
  OpenStruct.new(
@@ -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
- def self.reset_token
8
- @token = nil
9
- end
7
+ class_attribute :token
10
8
 
11
- def self.token(token = nil)
12
- @token = token || @token
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
- def self.reset_credentials
8
- @credentials = nil
9
- end
7
+ class_attribute :credentials
10
8
 
11
- def self.credentials(credentials = nil)
12
- @credentials = credentials || @credentials
9
+ def self.reset_credentials
10
+ self.credentials = nil
13
11
  end
14
12
 
15
13
  def call(request_env)
@@ -4,11 +4,11 @@ module Zaikio
4
4
  module Directory
5
5
  class Configuration
6
6
  HOSTS = {
7
- development: "http://directory.zaikio.test",
8
- test: "http://directory.zaikio.test",
9
- staging: "https://directory.staging.zaikio.com",
10
- sandbox: "https://directory.sandbox.zaikio.com",
11
- production: "https://directory.zaikio.com"
7
+ development: "https://hub.zaikio.test",
8
+ test: "https://hub.zaikio.test",
9
+ staging: "https://hub.staging.zaikio.com",
10
+ sandbox: "https://hub.sandbox.zaikio.com",
11
+ production: "https://hub.zaikio.com"
12
12
  }.freeze
13
13
 
14
14
  attr_accessor :host
@@ -1,9 +1,19 @@
1
1
  module Zaikio
2
2
  module Directory
3
3
  class Connection < Base
4
- uri "connections"
4
+ uri "connections(/:id)"
5
5
 
6
6
  include_root_in_json :connection
7
+
8
+ def initialize(attributes = {})
9
+ if attributes["connectable_id"]
10
+ super(attributes.merge(
11
+ "id" => "#{attributes['connectable_type']}-#{attributes['connectable_id']}"
12
+ ))
13
+ else
14
+ super
15
+ end
16
+ end
7
17
  end
8
18
  end
9
19
  end
@@ -22,6 +22,8 @@ module Zaikio
22
22
  uri: "software(/:id)"
23
23
  has_many :machines, class_name: "Zaikio::Directory::Machine",
24
24
  uri: "machines(/:id)"
25
+ has_many :specialists, class_name: "Zaikio::Directory::Specialist",
26
+ uri: "specialists(/:id)"
25
27
  has_many :sites, class_name: "Zaikio::Directory::Site",
26
28
  uri: "sites(/:id)"
27
29
 
@@ -29,6 +31,10 @@ module Zaikio
29
31
  self.attributes = get
30
32
  end
31
33
 
34
+ def reload
35
+ self.attributes = self.class.find.attributes
36
+ end
37
+
32
38
  def members
33
39
  memberships.with_fallback.map(&:person)
34
40
  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
- # 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, "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 destroy
20
- if Zaikio::Directory.current_token_data.subject_type == "Organization"
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
@@ -0,0 +1,10 @@
1
+ module Zaikio
2
+ module Directory
3
+ class Specialist < Base
4
+ include Asset
5
+
6
+ uri "specialists(/:id)"
7
+ include_root_in_json :specialist
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ module Zaikio
2
+ module Directory
3
+ class Subscription < Base
4
+ uri "subscriptions(/:id)"
5
+
6
+ include_root_in_json :subscription
7
+
8
+ def initialize(attributes = {})
9
+ if attributes["subscriber_id"]
10
+ super(attributes.merge(
11
+ "id" => "#{attributes['subscriber_type']}-#{attributes['subscriber_id']}"
12
+ ))
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ def activate!
19
+ update(status: "active")
20
+ end
21
+
22
+ def increment_usage_by!(usage, by = 1)
23
+ update(increment_usages_in_current_billing_period: { usage => by })
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  module Zaikio
2
2
  module Directory
3
- VERSION = "0.0.7".freeze
3
+ VERSION = "0.1.0".freeze
4
4
  end
5
5
  end
@@ -5,6 +5,12 @@ module Zaikio
5
5
  end
6
6
 
7
7
  module Spyke
8
+ instance_eval do
9
+ # avoid warning: already initialized constant
10
+ remove_const("ConnectionError")
11
+ remove_const("ResourceNotFound")
12
+ end
13
+
8
14
  ConnectionError = Class.new Zaikio::ConnectionError
9
15
  ResourceNotFound = Class.new Zaikio::ResourceNotFound
10
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.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Zaikio GmbH
7
+ - crispymtn
8
+ - Jalyna Schröder
9
+ - Martin Spickermann
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
- date: 2020-04-14 00:00:00.000000000 Z
13
+ date: 2020-08-24 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
16
  name: jwt
@@ -28,16 +30,22 @@ dependencies:
28
30
  name: multi_json
29
31
  requirement: !ruby/object:Gem::Requirement
30
32
  requirements:
31
- - - "~>"
33
+ - - ">="
32
34
  - !ruby/object:Gem::Version
33
35
  version: 1.14.1
36
+ - - "<"
37
+ - !ruby/object:Gem::Version
38
+ version: 1.16.0
34
39
  type: :runtime
35
40
  prerelease: false
36
41
  version_requirements: !ruby/object:Gem::Requirement
37
42
  requirements:
38
- - - "~>"
43
+ - - ">="
39
44
  - !ruby/object:Gem::Version
40
45
  version: 1.14.1
46
+ - - "<"
47
+ - !ruby/object:Gem::Version
48
+ version: 1.16.0
41
49
  - !ruby/object:Gem::Dependency
42
50
  name: oj
43
51
  requirement: !ruby/object:Gem::Requirement
@@ -68,7 +76,9 @@ dependencies:
68
76
  version: 5.3.4
69
77
  description: Ruby API Client for Zaikio's Directory
70
78
  email:
79
+ - op@crispymtn.com
71
80
  - js@crispymtn.com
81
+ - spickermann@gmail.com
72
82
  executables: []
73
83
  extensions: []
74
84
  extra_rdoc_files: []
@@ -77,6 +87,7 @@ files:
77
87
  - README.md
78
88
  - Rakefile
79
89
  - lib/zaikio/directory.rb
90
+ - lib/zaikio/directory/asset.rb
80
91
  - lib/zaikio/directory/authorization_middleware.rb
81
92
  - lib/zaikio/directory/base.rb
82
93
  - lib/zaikio/directory/basic_auth_middleware.rb
@@ -95,6 +106,8 @@ files:
95
106
  - lib/zaikio/directory/role.rb
96
107
  - lib/zaikio/directory/site.rb
97
108
  - lib/zaikio/directory/software.rb
109
+ - lib/zaikio/directory/specialist.rb
110
+ - lib/zaikio/directory/subscription.rb
98
111
  - lib/zaikio/directory/version.rb
99
112
  - lib/zaikio/error.rb
100
113
  homepage: https://www.zaikio.com/