shaf_client 0.5.1 → 0.5.2

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: 1a4938e6a4e81f0aa7b22ac3d93977f399f1405b3a1d1b61d6dd013b9cfc0a88
4
- data.tar.gz: f5255401a3483953aceef590d1bf7aaaedf7ea03eea86b2302a7691f10c4bb1b
3
+ metadata.gz: d602ecdfb2c1d5ec4c86cb20ac1b87f00c65cdd723224f317ac7d18d78646474
4
+ data.tar.gz: 6b7754cf594c922f1b02dea3c25f1fc517313afe02e68a148b6d4d7ecc20bc5e
5
5
  SHA512:
6
- metadata.gz: 6ffdbf2f68b5da3d68dbfc881fb0a9ead6209d6bc6f5406fb4e1a4e774603e91c99c6bdd5adaa9e7e6ea8ee3cc86ca1f464366f5259f34bde7b03c71383c796e
7
- data.tar.gz: 2ce0baf08bd971a286ece19a53e4847f48559bba2c35a5c97de7a4721ad92d995dab1768ec26297a6b143a563ea33dac40054362cea3561d9d6554cca27afee0
6
+ metadata.gz: f5284068a4414d8f3920e89dc0d6f8873f957c46c10ac2507be793eaa17a760f46c3150e6ff3839f8423a96dc0b098245f907a6dccb386358d1a3dd73d953f50
7
+ data.tar.gz: e61858e4b5488b62a25ecea305c5639d309244faa249103e3b6d753fce5649e4a7debab45b19ea9d3f7d5c3cbefea62702ecee9853e743a6813b317140dbba1f
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/shaf_client.rb CHANGED
@@ -3,15 +3,15 @@
3
3
  require 'faraday'
4
4
  require 'faraday-http-cache'
5
5
  require 'json'
6
+ require 'shaf_client/error'
6
7
  require 'shaf_client/middleware/redirect'
7
8
  require 'shaf_client/resource'
8
9
  require 'shaf_client/form'
9
- require 'shaf_client/error'
10
+ require 'shaf_client/api_error'
10
11
  require 'shaf_client/empty_resource'
12
+ require 'shaf_client/unknown_resource'
11
13
 
12
14
  class ShafClient
13
- class Error < StandardError; end
14
-
15
15
  MIME_TYPE_JSON = 'application/json'
16
16
  MIME_TYPE_HAL = 'application/hal+json'
17
17
  DEFAULT_ADAPTER = :net_http
@@ -44,7 +44,7 @@ class ShafClient
44
44
  )
45
45
 
46
46
  body = String(response.body)
47
- response.headers['content-type'] = 'profile=__shaf_client_emtpy__' if body.empty?
47
+ response.headers['content-type'] = '__shaf_client_emtpy__' if body.empty?
48
48
 
49
49
  Resource.build(self, body, response.status, response.headers)
50
50
  end
@@ -0,0 +1,21 @@
1
+ require 'json'
2
+ require 'shaf_client/link'
3
+
4
+ class ShafClient
5
+ class ApiError < Resource
6
+
7
+ profile 'shaf-error'
8
+
9
+ def title
10
+ attribute(:title)
11
+ end
12
+
13
+ def code
14
+ attribute(:code)
15
+ end
16
+
17
+ def fields
18
+ attribute(:fields)
19
+ end
20
+ end
21
+ end
@@ -53,6 +53,12 @@ class ShafClient
53
53
  embedded_resources[rewritten_rel]
54
54
  end
55
55
 
56
+ def rel?(rel)
57
+ !link(rel).nil?
58
+ rescue StandardError
59
+ false
60
+ end
61
+
56
62
  def [](key)
57
63
  attributes[key]
58
64
  end
@@ -132,7 +138,7 @@ class ShafClient
132
138
  unless rel.to_s.include? ':'
133
139
  matches = rels.grep(/[^:]*:#{rel}/)
134
140
  return matches.first if matches.size == 1
135
- raise Error, "Ambiguous rel: #{rel}. (#{matches})" if matches.size > 1
141
+ raise AmbiguousRelError, "Ambiguous rel: #{rel}. (#{matches})" if matches.size > 1
136
142
  end
137
143
 
138
144
  best_match(rels, rel.to_s.tr('_', '-')) if rel.to_s.include? '_'
@@ -4,7 +4,7 @@ class ShafClient
4
4
  class EmptyResource < Resource
5
5
  attr_reader :http_status, :headers
6
6
 
7
- profile '__shaf_client_emtpy__'
7
+ ResourceMapper.register('__shaf_client_emtpy__', self)
8
8
 
9
9
  def initialize(_client, _payload, status = nil, headers = {})
10
10
  @http_status = status
@@ -1,21 +1,6 @@
1
- require 'json'
2
- require 'shaf_client/link'
3
-
4
1
  class ShafClient
5
- class ApiError < Resource
6
-
7
- profile 'shaf-error'
8
-
9
- def title
10
- attribute(:title)
11
- end
12
-
13
- def code
14
- attribute(:code)
15
- end
2
+ class Error < StandardError; end
16
3
 
17
- def fields
18
- attribute(:fields)
19
- end
20
- end
4
+ class AmbiguousRelError; end
5
+ class UnSupportedContentType < Error; end
21
6
  end
@@ -1,33 +1,24 @@
1
1
  require 'json'
2
+ require 'shaf_client/resource_mapper'
2
3
  require 'shaf_client/base_resource'
3
4
 
4
5
  class ShafClient
5
6
  class Resource < BaseResource
6
7
  attr_reader :http_status, :headers
7
8
 
8
- class ResourceMapper
9
- class << self
10
- def all
11
- @all ||= Hash.new(Resource)
12
- end
13
-
14
- def for(name)
15
- all[name&.to_sym]
16
- end
9
+ ResourceMapper.register("application/hal+json", self)
17
10
 
18
- def set(name, clazz)
19
- all[name.to_sym] = clazz
20
- end
21
- end
11
+ def self.content_type(type)
12
+ ResourceMapper.register(type, self)
22
13
  end
23
14
 
24
15
  def self.profile(name)
25
- ResourceMapper.set(name, self)
16
+ content_type "application/hal+json;profile=#{name}"
26
17
  end
27
18
 
28
19
  def self.build(client, payload, status = nil, headers = {})
29
- profile = headers.fetch('content-type', '')[/profile=([\w-]+)\b/, 1]
30
- ResourceMapper.for(profile).new(client, payload, status, headers)
20
+ content_type = headers.fetch('content-type', '')
21
+ ResourceMapper.for(content_type).new(client, payload, status, headers)
31
22
  end
32
23
 
33
24
  def initialize(client, payload, status = nil, headers = {})
@@ -44,7 +35,7 @@ class ShafClient
44
35
  end
45
36
  end
46
37
 
47
- def get_doc(rel:)
38
+ def get_doc(rel)
48
39
  rel = rel.to_s
49
40
  curie_name, rel =
50
41
  if rel.include? ':'
@@ -0,0 +1,27 @@
1
+ require 'shaf_client/error'
2
+
3
+ class ShafClient
4
+ class ResourceMapper
5
+ class << self
6
+ def all
7
+ @all ||= {}
8
+ end
9
+
10
+ def for(content_type)
11
+ all[content_type&.to_sym].tap do |clazz|
12
+ next if clazz
13
+ raise UnSupportedContentType,
14
+ "Can't handle Content-Type: #{content_type}"
15
+ end
16
+ end
17
+
18
+ def register(content_type, clazz)
19
+ all[content_type&.to_sym] = clazz
20
+ end
21
+
22
+ def default=(clazz)
23
+ all.default = clazz
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ require 'shaf_client/resource'
2
+
3
+ class ShafClient
4
+ class UnknownResource < Resource
5
+ attr_reader :http_status, :headers, :body
6
+
7
+ ResourceMapper.default = self
8
+
9
+ def initialize(_client, payload, status = nil, headers = {})
10
+ @body = payload.freeze
11
+ @http_status = status
12
+ @headers = headers
13
+ @attributes = {}.freeze
14
+ @links = {}.freeze
15
+ @curies = {}.freeze
16
+ @embedded_resources = {}.freeze
17
+ end
18
+
19
+ def to_h
20
+ {}
21
+ end
22
+
23
+ %i[get put post delete patch, get_doc, reload!].each do |method|
24
+ define_method(method) do |*_args|
25
+ raise "UnknownResource: #{method} not available"
26
+ end
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shaf_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sammy Henningsson
@@ -30,7 +30,7 @@ cert_chain:
30
30
  ZMhjYR7sRczGJx+GxGU2EaR0bjRsPVlC4ywtFxoOfRG3WaJcpWGEoAoMJX6Z0bRv
31
31
  M40=
32
32
  -----END CERTIFICATE-----
33
- date: 2019-09-06 00:00:00.000000000 Z
33
+ date: 2019-09-09 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: faraday
@@ -101,6 +101,7 @@ extensions: []
101
101
  extra_rdoc_files: []
102
102
  files:
103
103
  - lib/shaf_client.rb
104
+ - lib/shaf_client/api_error.rb
104
105
  - lib/shaf_client/base_resource.rb
105
106
  - lib/shaf_client/curie.rb
106
107
  - lib/shaf_client/empty_resource.rb
@@ -109,6 +110,8 @@ files:
109
110
  - lib/shaf_client/link.rb
110
111
  - lib/shaf_client/middleware/redirect.rb
111
112
  - lib/shaf_client/resource.rb
113
+ - lib/shaf_client/resource_mapper.rb
114
+ - lib/shaf_client/unknown_resource.rb
112
115
  homepage: https://github.com/sammyhenningsson/shaf_client
113
116
  licenses:
114
117
  - MIT
metadata.gz.sig CHANGED
Binary file