excon-addressable 0.1.1 → 0.2.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 +20 -4
- data/lib/excon/addressable/version.rb +1 -1
- data/lib/excon/addressable.rb +14 -10
- data/test/excon/addressable_test.rb +31 -6
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90173753b01163b519a0566df85dbc5118c6adbd
|
4
|
+
data.tar.gz: 4693cef069140939f66b2ce2ee2b4932b1986f58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f09281bfad34455f3d6c9f2a0ac469df5c8adcc4135872a7bdaebb4ef4bccbd95a1e09929dc3938a5a43e9ae97a876a844137633d342961dc08fd0fdcaf2581f
|
7
|
+
data.tar.gz: 7bafb1b96ae0214c30107c1ad61f717f9b44817c2be7d8a229977988d4526165bf4459db151a5379e721e67428dacf521a90073bba9b7ca354feea4843f2f1fb
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Excon::Addressable [](https://app.wercker.com/project/bykey/3868c162aa140566b830f517c45d528a)
|
2
2
|
|
3
|
-
Sets [Addressable][] as the default URI parser. Supports parsing
|
3
|
+
Sets [Addressable][addressable] as the default URI parser. Supports parsing
|
4
|
+
[templated uris][].
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -12,14 +13,29 @@ gem 'excon-addressable'
|
|
12
13
|
|
13
14
|
And then execute:
|
14
15
|
|
15
|
-
|
16
|
+
```shell
|
17
|
+
bundle
|
18
|
+
```
|
16
19
|
|
17
20
|
Or install it yourself as:
|
18
21
|
|
19
|
-
|
22
|
+
```shell
|
23
|
+
gem install excon-addressable
|
24
|
+
```
|
20
25
|
|
21
26
|
## Usage
|
22
27
|
|
28
|
+
Be sure to add `Excon::Addressable::Middleware` to the top of the middleware
|
29
|
+
stack, so that the variables get expanded as early as possible. This prevents
|
30
|
+
other middleware from choking on non-valid URIs.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
Excon.defaults[:middlewares].unshift(Excon::Addressable::Middleware)
|
34
|
+
```
|
35
|
+
|
36
|
+
Then simply provide a templated variable, and the values with which to expand
|
37
|
+
the template:
|
38
|
+
|
23
39
|
```ruby
|
24
40
|
conn = Excon.new('http://www.example.com/{uid}', expand: { uid: 'hello' })
|
25
41
|
conn.request.path # => '/hello'
|
@@ -29,5 +45,5 @@ conn.request.path # => '/hello'
|
|
29
45
|
|
30
46
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
31
47
|
|
32
|
-
[
|
48
|
+
[addressable]: https://github.com/sporkmonger/addressable
|
33
49
|
[templated uris]: https://github.com/sporkmonger/addressable#uri-templates
|
data/lib/excon/addressable.rb
CHANGED
@@ -5,19 +5,23 @@ require 'excon/addressable/version'
|
|
5
5
|
|
6
6
|
Excon.defaults[:uri_parser] = Addressable::URI
|
7
7
|
|
8
|
-
# :nodoc:
|
9
8
|
module Excon
|
10
|
-
# Addressable addition to Excon.
|
11
|
-
#
|
12
9
|
module Addressable
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
# Middleware
|
11
|
+
#
|
12
|
+
# Parses a Templated URI string and merges it with the provided variables.
|
13
|
+
#
|
14
|
+
class Middleware < Excon::Middleware::Base
|
15
|
+
def request_call(datum)
|
16
|
+
url = ::Addressable::URI.new(datum)
|
17
|
+
|
18
|
+
if (template = ::Addressable::Template.new(url)) && template.variables.any?
|
19
|
+
uri = template.expand(datum[:expand].to_h)
|
20
|
+
datum.merge!(uri.to_hash)
|
21
|
+
end
|
17
22
|
|
18
|
-
|
23
|
+
super
|
24
|
+
end
|
19
25
|
end
|
20
26
|
end
|
21
|
-
|
22
|
-
singleton_class.prepend Addressable
|
23
27
|
end
|
@@ -8,21 +8,46 @@ module Excon
|
|
8
8
|
#
|
9
9
|
class AddressableTest < Minitest::Test
|
10
10
|
def setup
|
11
|
+
Excon.defaults[:middlewares].unshift(Excon::Addressable::Middleware)
|
11
12
|
Excon.defaults[:mock] = true
|
12
|
-
Excon.stub({},
|
13
|
+
Excon.stub({ path: '/' }, body: 'index')
|
14
|
+
Excon.stub({ path: '/hello' }, body: 'world')
|
15
|
+
Excon.stub({ path: '/hello', query: 'message=world' }, body: 'hi!')
|
16
|
+
Excon.stub({ path: '/world' }, body: 'universe')
|
13
17
|
end
|
14
18
|
|
15
19
|
def test_expand_templated_uri
|
16
|
-
|
20
|
+
connection = Excon.new('http://www.example.com/{uid}', expand: { uid: 'hello' })
|
21
|
+
response = connection.get
|
17
22
|
|
18
|
-
assert_equal '
|
19
|
-
assert_equal 200, conn.get.status
|
23
|
+
assert_equal 'world', response.body
|
20
24
|
end
|
21
25
|
|
22
26
|
def test_templated_uri_with_optional_query_parameters
|
23
|
-
|
27
|
+
connection = Excon.new('http://www.example.com/{?uid}')
|
28
|
+
response = connection.get
|
24
29
|
|
25
|
-
assert_equal '
|
30
|
+
assert_equal 'index', response.body
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_templated_uri_with_excon_shortcut_method
|
34
|
+
response = Excon.get('http://www.example.com/{uid}', expand: { uid: 'world' })
|
35
|
+
|
36
|
+
assert_equal 'universe', response.body
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_templated_uri_with_mixed_connection_and_get
|
40
|
+
connection = Excon.new('http://www.example.com/{uid}')
|
41
|
+
response = connection.get(expand: { uid: 'world' })
|
42
|
+
|
43
|
+
assert_equal 'universe', response.body
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_templated_uri_overriding_variables
|
47
|
+
connection = Excon.new('http://www.example.com/{uid}{?message}', expand: { uid: 'goodbye' })
|
48
|
+
response = connection.get(expand: { uid: 'hello', message: 'world' })
|
49
|
+
|
50
|
+
assert_equal 'hi!', response.body
|
26
51
|
end
|
27
52
|
|
28
53
|
def teardown
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: excon-addressable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-05-
|
12
|
+
date: 2016-05-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -154,4 +154,3 @@ signing_key:
|
|
154
154
|
specification_version: 4
|
155
155
|
summary: Excon, with Addressable baked in.
|
156
156
|
test_files: []
|
157
|
-
has_rdoc:
|