excon-hypermedia 0.2.0 → 0.3.0
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 +43 -15
- data/excon-hypermedia.gemspec +2 -2
- data/lib/excon/hypermedia/response.rb +12 -25
- data/lib/excon/hypermedia/version.rb +1 -1
- data/test/excon/hypermedia_test.rb +12 -5
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c0a3189fb4185c1db3c29a86b699ea1de7549db
|
4
|
+
data.tar.gz: b4d04af297a0ea267d0a2dce0765385bfd5d8483
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7576259b25677acc8f9b23171705babafff2bd0510c055723bee712e1f77a329355b090be9544631aabdd199c0e4c068f0818765ffcf1c4abdd5f03a94b004a
|
7
|
+
data.tar.gz: eb29ba745ea6cf03434f3232fd459e7e9bf3db791fba56fc88772e95a10800510f54e5c793e8771df9b77bae240522fd024b305f8bbf5186236d7a0681c815fd
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Excon::Hypermedia
|
1
|
+
# Excon::Hypermedia [](https://app.wercker.com/project/bykey/f3fd6cf2045566072ef26354d5a73e9f)
|
2
2
|
|
3
3
|
Teaches [Excon][] how to talk to [HyperMedia APIs][hypermedia].
|
4
4
|
|
@@ -24,18 +24,13 @@ gem install excon-hypermedia
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
relations. It returns raw response bodies in string format.
|
30
|
-
|
31
|
-
This gem adds a thin layer on top of [Excon][excon] to make it talk with an
|
32
|
-
HyperMedia-enabled API. To let Excon know the connection supports HyperMedia,
|
33
|
-
simply enable the correct middleware (either globally, or per-connection):
|
27
|
+
To let Excon know the API supports HyperMedia, simply enable the correct
|
28
|
+
middleware (either globally, or per-connection):
|
34
29
|
|
35
30
|
```ruby
|
36
31
|
Excon.defaults[:middlewares].push(Excon::HyperMedia::Middleware)
|
37
32
|
|
38
|
-
api = Excon.get('
|
33
|
+
api = Excon.get('https://www.example.org/api.json')
|
39
34
|
api.class # => Excon::Response
|
40
35
|
```
|
41
36
|
|
@@ -43,7 +38,7 @@ Using the `HyperMedia` middleware, the `Excon::Response` object now knows how
|
|
43
38
|
to handle the HyperMedia aspect of the API:
|
44
39
|
|
45
40
|
```ruby
|
46
|
-
product = api.product(expand: { uid: '
|
41
|
+
product = api.product(expand: { uid: 'bicycle' })
|
47
42
|
product.class # => Excon::Connection
|
48
43
|
|
49
44
|
response = product.get
|
@@ -62,14 +57,47 @@ are available as well:
|
|
62
57
|
product.get(idempotent: true, retry_limit: 6)
|
63
58
|
```
|
64
59
|
|
65
|
-
|
60
|
+
### Links
|
66
61
|
|
67
|
-
|
62
|
+
You can access all links in a resource using the `links` method:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
api.links.first.class # => Excon::HyperMedia::Link
|
66
|
+
api.links.first.name # => 'product'
|
67
|
+
api.links.first.href # => 'https://www.example.org/product/{uid}'
|
68
|
+
```
|
68
69
|
|
69
|
-
|
70
|
+
You can also access a link directly, using its name:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
api.link('product').href # => 'https://www.example.org/product/{uid}'
|
74
|
+
```
|
75
|
+
|
76
|
+
If you want to access a relation through its link, but can't use the implicit
|
77
|
+
naming, you can use the `rel` method:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
api.links.last.name # => 'customer-orders'
|
70
81
|
|
71
|
-
|
72
|
-
|
82
|
+
# won't work, due to invalid ruby method name:
|
83
|
+
api.customer-orders(expand: { sort: 'desc' }) # => NoMethodError: undefined method `customer'
|
84
|
+
|
85
|
+
# works
|
86
|
+
api.rel('customer-orders', expand: { sort: 'desc' })
|
87
|
+
```
|
88
|
+
|
89
|
+
### Attributes
|
90
|
+
|
91
|
+
Attributes are available through the `attributes` method:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
product.attributes.to_h # => { uid: 'bicycle', stock: 5 }
|
95
|
+
product.attributes.uid # => 'bicycle'
|
96
|
+
```
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
73
101
|
|
74
102
|
[excon]: https://github.com/excon/excon
|
75
103
|
[hypermedia]: https://en.wikipedia.org/wiki/HATEOAS
|
data/excon-hypermedia.gemspec
CHANGED
@@ -22,8 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency 'minitest', '~> 5.0'
|
23
23
|
spec.add_development_dependency 'rubocop', '~> 0.40'
|
24
24
|
spec.add_development_dependency 'pry', '~> 0.10'
|
25
|
-
spec.add_development_dependency 'm', '~> 1.5
|
25
|
+
spec.add_development_dependency 'm', '~> 1.5'
|
26
26
|
|
27
27
|
spec.add_dependency 'excon', '~> 0.49'
|
28
|
-
spec.add_dependency 'excon-addressable', '~> 0.
|
28
|
+
spec.add_dependency 'excon-addressable', '~> 0.2'
|
29
29
|
end
|
@@ -10,8 +10,6 @@ module Excon
|
|
10
10
|
# requests and attribute values.
|
11
11
|
#
|
12
12
|
class Response
|
13
|
-
attr_reader :response
|
14
|
-
|
15
13
|
def initialize(response)
|
16
14
|
@response = response
|
17
15
|
end
|
@@ -21,23 +19,22 @@ module Excon
|
|
21
19
|
# Correctly handle the hypermedia request.
|
22
20
|
#
|
23
21
|
def handle(method_name, *params)
|
24
|
-
return false
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
return false unless enabled?
|
23
|
+
|
24
|
+
if method_name == :rel
|
25
|
+
handle_link(params.shift, params)
|
26
|
+
elsif resource.type?(method_name) == :link
|
27
|
+
handle_link(method_name, params)
|
28
|
+
elsif resource.respond_to?(method_name, false)
|
29
|
+
resource.send(method_name, *params)
|
30
|
+
else
|
31
|
+
false
|
29
32
|
end
|
30
|
-
|
31
|
-
respond_to?(method_name) ? send(method_name) : false
|
32
33
|
end
|
33
34
|
|
34
|
-
|
35
|
-
resource.links
|
36
|
-
end
|
35
|
+
private
|
37
36
|
|
38
|
-
|
39
|
-
resource.attributes
|
40
|
-
end
|
37
|
+
attr_reader :response
|
41
38
|
|
42
39
|
def resource
|
43
40
|
@resource ||= Resource.new(response.body)
|
@@ -47,19 +44,9 @@ module Excon
|
|
47
44
|
response.data[:hypermedia] == true
|
48
45
|
end
|
49
46
|
|
50
|
-
def disabled?
|
51
|
-
!enabled?
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
47
|
def handle_link(name, params)
|
57
48
|
Excon.new(resource.link(name).href, params.first.to_h.merge(hypermedia: true))
|
58
49
|
end
|
59
|
-
|
60
|
-
def handle_attribute(name)
|
61
|
-
attributes[name.to_s]
|
62
|
-
end
|
63
50
|
end
|
64
51
|
end
|
65
52
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
# rubocop:disable Metrics/LineLength
|
3
4
|
require_relative '../test_helper'
|
4
5
|
|
@@ -47,6 +48,12 @@ module Excon
|
|
47
48
|
assert response.body.include?('http://www.example.com/hello/world/goodbye{?message}')
|
48
49
|
end
|
49
50
|
|
51
|
+
def test_request_using_link
|
52
|
+
response = client.rel('hello', expand: { location: 'world' }).get
|
53
|
+
|
54
|
+
assert response.body.include?('http://www.example.com/hello/world/goodbye{?message}')
|
55
|
+
end
|
56
|
+
|
50
57
|
def test_nested_request
|
51
58
|
hello = client.hello(expand: { location: 'world' })
|
52
59
|
response = hello.get.goodbye.get
|
@@ -67,22 +74,22 @@ module Excon
|
|
67
74
|
assert response.body.include?('http://www.example.com/hello/world/goodbye{?message}')
|
68
75
|
end
|
69
76
|
|
70
|
-
def
|
77
|
+
def test_links
|
71
78
|
response = client.hello(expand: { location: 'world' }).get
|
72
79
|
|
73
|
-
assert_equal response.
|
74
|
-
assert_equal response.message, 'goodbye!'
|
80
|
+
assert_equal response.links.first.name, 'goodbye'
|
75
81
|
end
|
76
82
|
|
77
|
-
def
|
83
|
+
def test_link
|
78
84
|
response = client.hello(expand: { location: 'world' }).get
|
79
85
|
|
80
|
-
assert_equal response.
|
86
|
+
assert_equal response.link('goodbye').name, 'goodbye'
|
81
87
|
end
|
82
88
|
|
83
89
|
def test_attributes
|
84
90
|
response = client.hello(expand: { location: 'world' }).get
|
85
91
|
|
92
|
+
assert_equal response.attributes.to_h, uid: 'hello', message: 'goodbye!'
|
86
93
|
assert_equal response.attributes.uid, 'hello'
|
87
94
|
end
|
88
95
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: excon-hypermedia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean
|
@@ -87,14 +87,14 @@ dependencies:
|
|
87
87
|
requirements:
|
88
88
|
- - "~>"
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version: 1.5
|
90
|
+
version: '1.5'
|
91
91
|
type: :development
|
92
92
|
prerelease: false
|
93
93
|
version_requirements: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
95
|
- - "~>"
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version: 1.5
|
97
|
+
version: '1.5'
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
99
|
name: excon
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
@@ -115,14 +115,14 @@ dependencies:
|
|
115
115
|
requirements:
|
116
116
|
- - "~>"
|
117
117
|
- !ruby/object:Gem::Version
|
118
|
-
version: '0.
|
118
|
+
version: '0.2'
|
119
119
|
type: :runtime
|
120
120
|
prerelease: false
|
121
121
|
version_requirements: !ruby/object:Gem::Requirement
|
122
122
|
requirements:
|
123
123
|
- - "~>"
|
124
124
|
- !ruby/object:Gem::Version
|
125
|
-
version: '0.
|
125
|
+
version: '0.2'
|
126
126
|
description: Excon, with Hypermedia traversing baked in.
|
127
127
|
email:
|
128
128
|
- jean@mertz.fm
|