zaikio-directory 0.0.6 → 0.0.7

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: 8e9927801204a0fd2cbf4f4b33916e5eb096c864fa9029c0397c36c9e17fad2b
4
- data.tar.gz: 0d178e8cf201ab36d6830e9f897cfbc4aeb6349275ce310ecfcf065b4d2ff220
3
+ metadata.gz: 425d810f240fc3a9a78e86b8a7216621dc2276c34fb1125816df5b4b8d514bb3
4
+ data.tar.gz: 7795f54139e370afe28994137d34614985fb5553f5df6e078fb53c440b8a29a9
5
5
  SHA512:
6
- metadata.gz: 9b3569ef1779d9ab6a1cd7969f34884a75908c8b2b4d2a8b4099d6167abab015aa0e50d22c7baa4dda41a20de58e5fe87f3b096d50508ba86ef99face7b0bfd5
7
- data.tar.gz: 2c711726d46af8c22fbd163f3e04a13fef7eb6357e93f67feef105bfb1c559e23b5945ce4078974158baa93ba4e95fe80d1de19732ae914869bd7865bdd6bf74
6
+ metadata.gz: e248bd3df0c8f53b40d63c141b6f3b8c0ee4f1af7f3aecc0ca2765595bbfd072319e983e7685c3d0328ee9c156e9094b9f051634a922435bbafbd596e897d6bd
7
+ data.tar.gz: 99f5c598b8a6369525dc90904bce3c75dd781a1de13e4abfd7589a149c527333187acddfa4db064d34398541a866d9cea3d524e865ff68607ee41fba17bcb76b
data/README.md CHANGED
@@ -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
+ ```
@@ -9,24 +9,28 @@ 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)"
21
25
  has_many :sites, class_name: "Zaikio::Directory::Site",
22
- uri: "sites"
26
+ uri: "sites(/:id)"
23
27
 
24
28
  def fetch
25
29
  self.attributes = get
26
30
  end
27
31
 
28
32
  def members
29
- memberships.map(&:person)
33
+ memberships.with_fallback.map(&:person)
30
34
  end
31
35
  end
32
36
  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 parse(body)
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,5 +1,5 @@
1
1
  module Zaikio
2
2
  module Directory
3
- VERSION = "0.0.6".freeze
3
+ VERSION = "0.0.7".freeze
4
4
  end
5
5
  end
@@ -6,6 +6,7 @@ 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"
10
11
  require "zaikio/directory/organization_membership"
11
12
  require "zaikio/directory/business_relationship"
@@ -0,0 +1,10 @@
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
+ ConnectionError = Class.new Zaikio::ConnectionError
9
+ ResourceNotFound = Class.new Zaikio::ResourceNotFound
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaikio-directory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zaikio GmbH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-08 00:00:00.000000000 Z
11
+ date: 2020-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -96,6 +96,7 @@ files:
96
96
  - lib/zaikio/directory/site.rb
97
97
  - lib/zaikio/directory/software.rb
98
98
  - lib/zaikio/directory/version.rb
99
+ - lib/zaikio/error.rb
99
100
  homepage: https://www.zaikio.com/
100
101
  licenses:
101
102
  - MIT