excon-addressable 0.1.1 → 0.2.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
  SHA1:
3
- metadata.gz: 2fb78674bba0eea1c3c0375e89057353d0a0ab2c
4
- data.tar.gz: 7d7fbe6b86458feeb6d4df55607060c2d2741205
3
+ metadata.gz: 90173753b01163b519a0566df85dbc5118c6adbd
4
+ data.tar.gz: 4693cef069140939f66b2ce2ee2b4932b1986f58
5
5
  SHA512:
6
- metadata.gz: 5ea5018b4786f5c980043e760d0a4fc2ee358de76d6fa188168aae6625eab58d82159d43b92294c737bcbb5d8b584a0a9d3ea9bfd8be3bdfc475b72a6bd32bc5
7
- data.tar.gz: 350853bfb125a5b8fb56b737b78fd67a8ce5bb6908eccf3092bce45aee830c58b5a492beb8b1ff9c846c16fe123d637d7197bd5d015a23aeb347fa1970e20d16
6
+ metadata.gz: f09281bfad34455f3d6c9f2a0ac469df5c8adcc4135872a7bdaebb4ef4bccbd95a1e09929dc3938a5a43e9ae97a876a844137633d342961dc08fd0fdcaf2581f
7
+ data.tar.gz: 7bafb1b96ae0214c30107c1ad61f717f9b44817c2be7d8a229977988d4526165bf4459db151a5379e721e67428dacf521a90073bba9b7ca354feea4843f2f1fb
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Excon::Addressable [![wercker status](https://app.wercker.com/status/3868c162aa140566b830f517c45d528a/s/master "wercker status")](https://app.wercker.com/project/bykey/3868c162aa140566b830f517c45d528a)
2
2
 
3
- Sets [Addressable][] as the default URI parser. Supports parsing [templated uris][].
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
- $ bundle
16
+ ```shell
17
+ bundle
18
+ ```
16
19
 
17
20
  Or install it yourself as:
18
21
 
19
- $ gem install excon-addressable
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
- [Addressable]: https://github.com/sporkmonger/addressable
48
+ [addressable]: https://github.com/sporkmonger/addressable
33
49
  [templated uris]: https://github.com/sporkmonger/addressable#uri-templates
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Excon
3
3
  module Addressable
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.0'
5
5
  end
6
6
  end
@@ -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
- def new(url, params = {})
14
- if (template = ::Addressable::Template.new(url)) && template.variables.any?
15
- url = template.expand(params.delete(:expand).to_h)
16
- end
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
- super
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({}, status: 200)
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
- conn = Excon.new('http://www.example.com/{uid}', expand: { uid: 'hello' })
20
+ connection = Excon.new('http://www.example.com/{uid}', expand: { uid: 'hello' })
21
+ response = connection.get
17
22
 
18
- assert_equal '/hello', conn.data[:path]
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
- conn = Excon.new('http://www.example.com/{?uid}')
27
+ connection = Excon.new('http://www.example.com/{?uid}')
28
+ response = connection.get
24
29
 
25
- assert_equal '/', conn.data[:path]
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.1.1
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-17 00:00:00.000000000 Z
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: