hyperclient 0.8.2 → 0.9.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 1bde74927d1e402d1db9cfd3860e36d692bcba2b
4
- data.tar.gz: f65e61030043163822906124088bfba6ff47b72e
2
+ SHA256:
3
+ metadata.gz: f96f4a987e6b56ca1668ea89fb5d93affb0360f6200b164ad45593dcaa2de8d3
4
+ data.tar.gz: 1aded500fd45aa1e80817df0807ebae6d67814d79313d7b30b2552f2abe08695
5
5
  SHA512:
6
- metadata.gz: 5b4716aad58beaeb9566564ba214afc7d69e31f8ea080ae12c26e10d3322e602d72c552e66bcd5d5653dc2c683499178fc34e0802b47c019848b97c6f35be617
7
- data.tar.gz: c1cd3c5ecbf2ed9112da1a7a6a4cea0f11abcdbb51c0473bed46c15b5f3be299456c7675b10fe9b51dba6c3fbb690b5517331ef4c05e5a8c9302fbd866107075
6
+ metadata.gz: a4c468104b767ac408e5e3763b4620e719b0ce8671e721d5537ce0751b88752b8aa6fdf5abdd335107994ff60d5740b77c88e7d1c1ad5ac317491ad6bbb7f5db
7
+ data.tar.gz: 739ad270184b0d15c574eeb1e7f835ac069f613fe1e90af671c49df5594964a0f7030878fc635331b4e452004a443b3390714bbb80a60fe6a4e1b67987e1ed61
data/.rubocop.yml CHANGED
@@ -1,2 +1,7 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
+ AllCops:
4
+ TargetRubyVersion: 2.2
5
+
6
+ Metrics/BlockLength:
7
+ ExcludedMethods: [it, describe]
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2016-12-21 20:00:37 +0000 using RuboCop version 0.42.0.
3
+ # on 2018-01-05 16:54:58 -0500 using RuboCop version 0.50.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -9,15 +9,15 @@
9
9
  # Offense count: 1
10
10
  # Configuration parameters: CountComments.
11
11
  Metrics/ClassLength:
12
- Max: 109
12
+ Max: 103
13
13
 
14
- # Offense count: 89
15
- # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
14
+ # Offense count: 94
15
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
16
16
  # URISchemes: http, https
17
17
  Metrics/LineLength:
18
18
  Max: 142
19
19
 
20
- # Offense count: 4
20
+ # Offense count: 3
21
21
  # Configuration parameters: CountComments.
22
22
  Metrics/MethodLength:
23
23
  Max: 25
@@ -25,12 +25,7 @@ Metrics/MethodLength:
25
25
  # Offense count: 3
26
26
  # Configuration parameters: CountComments.
27
27
  Metrics/ModuleLength:
28
- Max: 391
29
-
30
- # Offense count: 1
31
- Style/AsciiComments:
32
- Exclude:
33
- - 'lib/hyperclient/collection.rb'
28
+ Max: 265
34
29
 
35
30
  # Offense count: 2
36
31
  # Configuration parameters: EnforcedStyle, SupportedStyles.
@@ -56,25 +51,7 @@ Style/DoubleNegation:
56
51
  - 'lib/hyperclient/curie.rb'
57
52
  - 'lib/hyperclient/link.rb'
58
53
 
59
- # Offense count: 5
60
- # Cop supports --auto-correct.
61
- # Configuration parameters: EnforcedStyle, SupportedStyles.
62
- # SupportedStyles: line_count_dependent, lambda, literal
63
- Style/Lambda:
64
- Exclude:
65
- - 'test/hyperclient/entry_point_test.rb'
66
- - 'test/hyperclient/link_test.rb'
67
- - 'test/hyperclient/resource_test.rb'
68
-
69
54
  # Offense count: 1
70
55
  Style/MethodMissing:
71
56
  Exclude:
72
57
  - 'lib/hyperclient/collection.rb'
73
-
74
- # Offense count: 1
75
- # Cop supports --auto-correct.
76
- # Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
77
- # SupportedStyles: slashes, percent_r, mixed
78
- Style/RegexpLiteral:
79
- Exclude:
80
- - 'features/support/api.rb'
data/.travis.yml CHANGED
@@ -4,19 +4,23 @@ sudo: false
4
4
 
5
5
  matrix:
6
6
  include:
7
- - rvm: 2.3.1
7
+ - rvm: 2.4.1
8
+ - rvm: 2.4.1
8
9
  script:
9
10
  - bundle exec danger
10
- - rvm: 2.3.1
11
- - rvm: 2.3.0
12
- - rvm: 2.2.5
11
+ - rvm: jruby-9.1.12.0
12
+ - rvm: jruby-head
13
+ - rvm: 2.2.7
14
+ - rvm: 2.3.4
13
15
  - rvm: rbx-2
14
16
  - rvm: ruby-head
15
- - rvm: jruby-head
16
- - rvm: jruby-9.1.6.0
17
17
  allow_failures:
18
18
  - rvm: ruby-head
19
19
  - rvm: jruby-head
20
20
  - rvm: rbx-2
21
21
 
22
+ before_install:
23
+ - gem update --system
24
+ - gem install bundler
25
+
22
26
  bundler_args: --without development
data/CHANGELOG.md CHANGED
@@ -1,3 +1,28 @@
1
+ ### 0.9.0 (January 10, 2018)
2
+
3
+ * [#133](https://github.com/codegram/hyperclient/pull/133): Removed futuroscope - [@dblock](https://github.com/dblock).
4
+ * [#131](https://github.com/codegram/hyperclient/pull/131): Upgrade to Rubocop 0.50.0, fix Bundler's insecure git source warning - [@nebolsin](https://github.com/nebolsin).
5
+ * [#132](https://github.com/codegram/hyperclient/pull/132): Swapped yard dependency for danger-toc - [@dblock](https://github.com/dblock).
6
+
7
+ ### 0.8.6 (August 27, 2017)
8
+
9
+ * [#122](https://github.com/codegram/hyperclient/pull/122): Improve error message when server returns invalid data - [@ivoanjo](https://github.com/ivoanjo).
10
+ * [#125](https://github.com/codegram/hyperclient/pull/125): Add table of contents to readme and add note asking users to add their projects to the wiki - [@ivoanjo](https://github.com/ivoanjo).
11
+ * [#127](https://github.com/codegram/hyperclient/pull/127): Minor fixes: Fix warnings, and pry-byebug to dev Gemfile and tweak rubocop execution - [@ivoanjo](https://github.com/ivoanjo).
12
+ * [#128](https://github.com/codegram/hyperclient/pull/128): Fix link delegation returning nil for field with value false - [@ivoanjo](https://github.com/ivoanjo).
13
+
14
+ ### 0.8.5 (July 5, 2017)
15
+
16
+ * [#120](https://github.com/codegram/hyperclient/pull/120): Replace non-working homepage link in gemspec - [@ivoanjo](https://github.com/ivoanjo).
17
+
18
+ ### 0.8.4 (May 16, 2017)
19
+
20
+ * [#117](https://github.com/codegram/hyperclient/issues/117): Require Faraday >= 0.9.0 in gemspec - [@ivoanjo](https://github.com/ivoanjo).
21
+
22
+ ### 0.8.3 (March 30, 2017)
23
+
24
+ * [#115](https://github.com/codegram/hyperclient/pull/115): Fix dropped values from queries by using FlatParamsEncoder - [@ivoanjo](https://github.com/ivoanjo).
25
+
1
26
  ### 0.8.2 (December 31, 2016)
2
27
 
3
28
  #### This version is no longer tested with Ruby < 2.2.
data/Dangerfile CHANGED
@@ -1,2 +1,2 @@
1
- # check for changes in CHANGELOG
2
1
  changelog.check
2
+ toc.check
data/Gemfile CHANGED
@@ -1,3 +1,6 @@
1
+ # NOTE: this is temporary until Bundler 2.0 changes how github: references work.
2
+ git_source(:github) { |repo| "https://github.com/#{repo['/'] ? repo : "#{repo}/#{repo}"}.git" }
3
+
1
4
  source 'https://rubygems.org'
2
5
 
3
6
  gemspec
@@ -8,23 +11,22 @@ group :development do
8
11
  gem 'guard-minitest'
9
12
  gem 'guard-spinach'
10
13
  gem 'pry'
14
+ gem 'pry-byebug', platforms: :ruby
11
15
  end
12
16
 
13
17
  group :development, :test do
14
- gem 'yard', '~> 0.8'
15
- gem 'yard-tomdoc'
16
18
  gem 'rake'
19
+ gem 'rubocop', '~> 0.50.0', require: false
17
20
  gem 'simplecov', require: false
18
- gem 'rubocop', '~> 0.42.0', require: false
19
21
  end
20
22
 
21
23
  group :test do
22
- gem 'futuroscope', github: 'codegram/futuroscope'
23
24
  gem 'danger-changelog', '~> 0.1'
25
+ gem 'danger-toc', '~> 0.1'
24
26
  gem 'minitest'
25
- gem 'turn'
26
- gem 'webmock'
27
27
  gem 'mocha'
28
28
  gem 'rack-test'
29
29
  gem 'spinach'
30
+ gem 'turn'
31
+ gem 'webmock'
30
32
  end
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-214 Oriol Gual
1
+ Copyright (c) 2012-2018 Oriol Gual, Codegram Technologies and Contributors
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -8,11 +8,27 @@
8
8
 
9
9
  Hyperclient is a Hypermedia API client written in Ruby. It fully supports [JSON HAL](http://stateless.co/hal_specification.html).
10
10
 
11
- ## Usage
11
+ # Table of Contents
12
+
13
+ - [Usage](#usage)
14
+ - [API Client](#api-client)
15
+ - [Resources and Attributes](#resources-and-attributes)
16
+ - [Links and Embedded Resources](#links-and-embedded-resources)
17
+ - [Templated Links](#templated-links)
18
+ - [Curies](#curies)
19
+ - [Attributes](#attributes)
20
+ - [HTTP](#http)
21
+ - [Testing Using Hyperclient](#testing-using-hyperclient)
22
+ - [Reference](#reference)
23
+ - [Hyperclient Users](#hyperclient-users)
24
+ - [Contributing](#contributing)
25
+ - [License](#license)
26
+
27
+ # Usage
12
28
 
13
29
  The examples in this README use the [Splines Demo API](https://github.com/ruby-grape/grape-with-roar) running [here](https://grape-with-roar.herokuapp.com/api). If you're upgrading from a previous version, please make sure to read [UPGRADING](UPGRADING.md).
14
30
 
15
- ### API Client
31
+ ## API Client
16
32
 
17
33
  Create an API client.
18
34
 
@@ -85,7 +101,7 @@ api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api')
85
101
  api.connection.use :http_cache
86
102
  ```
87
103
 
88
- ### Resources and Attributes
104
+ ## Resources and Attributes
89
105
 
90
106
  Hyperclient will fetch and discover the resources from your API.
91
107
 
@@ -104,7 +120,7 @@ api.splines.each do |spline|
104
120
  end
105
121
  ```
106
122
 
107
- ### Links and Embedded Resources
123
+ ## Links and Embedded Resources
108
124
 
109
125
  The splines example above followed a link called "splines". While you can, you do not need to specify the HAL navigational structure, including links or embedded resources. Hyperclient will resolve these for you. If you prefer, you can explicitly navigate the link structure via `_links`. In the following example the "splines" link leads to a collection of embedded splines. Invoking `api.splines` is equivalent to `api._links.splines._embedded.splines`.
110
126
 
@@ -112,7 +128,7 @@ The splines example above followed a link called "splines". While you can, you d
112
128
  api._links.splines
113
129
  ```
114
130
 
115
- ### Templated Links
131
+ ## Templated Links
116
132
 
117
133
  Templated links require variables to be expanded. For example, the demo API has a link called "spline" that requires a spline "uuid".
118
134
 
@@ -125,7 +141,7 @@ Invoking `api.spline(uuid: 'uuid').reticulated` is equivalent to `api._links.spl
125
141
 
126
142
  The client is responsible for supplying all the necessary parameters. Templated links don't do any strict parameter name checking and don't support required vs. optional parameters. Parameters not declared by the API will be dropped and will not have any effect when passed to `_expand`.
127
143
 
128
- ### Curies
144
+ ## Curies
129
145
 
130
146
  Curies are a suggested means by which to link documentation of a given resource. For example, the demo API contains very long links to images that use an "images" curie.
131
147
 
@@ -134,7 +150,7 @@ puts spline['image:thumbnail'] # => https://grape-with-roar.herokuapp.com/api/sp
134
150
  puts spline.links._curies['image'].expand('thumbnail') # => /docs/images/thumbnail
135
151
  ```
136
152
 
137
- ### Attributes
153
+ ## Attributes
138
154
 
139
155
  Resource attributes can also be accessed as a hash.
140
156
 
@@ -144,7 +160,7 @@ puts spline.to_h # => {"uuid" => "uuid", "reticulated" => true}
144
160
 
145
161
  The above is equivalent to `spline._attributes.to_h`.
146
162
 
147
- ### HTTP
163
+ ## HTTP
148
164
 
149
165
  Hyperclient uses [Faraday](http://github.com/lostisland/faraday) under the hood to perform HTTP calls. You can call any valid HTTP method on any resource.
150
166
 
@@ -187,24 +203,7 @@ spline._delete
187
203
 
188
204
  HTTP methods always return a new instance of Resource.
189
205
 
190
- ### Asynchronous requests
191
-
192
- By default, Hyperclient requests are performed asynchronously in a background thread pool via the [Futuroscope](https://github.com/codegram/futuroscope) gem. You can control the size of this pool by setting the `min_workers` and `max_workers` settings:
193
-
194
- ```ruby
195
- Futuroscope.default_pool.min_workers = 10
196
- Futuroscope.default_pool.max_workers = 20
197
- ```
198
-
199
- If you want to disable this behavior and have all requests performed synchronously, you can use the client `options` to disable this behavior:
200
-
201
- ```ruby
202
- api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
203
- client.options[:async] = false
204
- end
205
- ```
206
-
207
- ## Testing Using Hyperclient
206
+ # Testing Using Hyperclient
208
207
 
209
208
  You can combine RSpec, Faraday::Adapter::Rack and Hyperclient to test your HAL API without having to ever examine the raw JSON response.
210
209
 
@@ -233,14 +232,20 @@ end
233
232
 
234
233
  For a complete example refer to [this Splines Demo API test](https://github.com/ruby-grape/grape-with-roar/blob/master/spec/api/splines_endpoint_with_hyperclient_spec.rb).
235
234
 
236
- ## Reference
235
+ # Reference
237
236
 
238
237
  [Hyperclient API Reference](http://rubydoc.org/github/codegram/hyperclient/master/frames).
239
238
 
240
- ## Contributing
239
+ # Hyperclient Users
240
+
241
+ Using Hyperclient? Add your project to our wiki, please: <https://github.com/codegram/hyperclient/wiki>.
242
+
243
+ # Contributing
241
244
 
242
245
  Hyperclient is work of [many people](https://github.com/codegram/hyperclient/graphs/contributors). You're encouraged to submit [pull requests](https://github.com/codegram/hyperclient/pulls), [propose features and discuss issues](https://github.com/codegram/hyperclient/issues). See [CONTRIBUTING](CONTRIBUTING.md) for details.
243
246
 
244
- ## License
247
+ # License
248
+
249
+ MIT License, see [LICENSE](LICENSE) for details.
245
250
 
246
- MIT License, see [LICENSE](LICENSE) for details. Copyright 2012-2014 [Codegram Technologies](http://codegram.com).
251
+ Copyright (c) 2012-2018 Oriol Gual, [Codegram Technologies](http://codegram.com) and Contributors
data/Rakefile CHANGED
@@ -13,15 +13,6 @@ if ENV['COVERAGE']
13
13
  end
14
14
  end
15
15
 
16
- require 'yard'
17
- YARD::Config.load_plugin('yard-tomdoc')
18
- YARD::Rake::YardocTask.new do |t|
19
- t.files = ['lib/**/*.rb']
20
- t.options = %w(-r README.md)
21
- end
22
-
23
- Bundler::GemHelper.install_tasks
24
-
25
16
  require 'rake/testtask'
26
17
 
27
18
  Rake::TestTask.new(:test) do |t|
@@ -40,4 +31,4 @@ end
40
31
  require 'rubocop/rake_task'
41
32
  RuboCop::RakeTask.new(:rubocop)
42
33
 
43
- task default: [:rubocop, :test, :spinach]
34
+ task default: %i[test spinach rubocop]
data/UPGRADING.md CHANGED
@@ -1,6 +1,22 @@
1
1
  Upgrading Hyperclient
2
2
  =====================
3
3
 
4
+ ### Upgrading to >= 0.9.0
5
+
6
+ Previous versions of Hyperclient performed asynchronous requests using [futuroscope](https://github.com/codegram/futuroscope) by default, which could be disabled by providing the `:async` option to each Hyperclient instance. This has been removed and you can remove any such code.
7
+
8
+ ```ruby
9
+ api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
10
+ client.options[:async] = false
11
+ end
12
+ ```
13
+
14
+ The default new behavior is synchronous. We recommend [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby) for your asynchronous needs.
15
+
16
+ Include [futuroscope](https://github.com/codegram/futuroscope) in your `Gemfile` and wrap Hyperclient requests into `Futuroscope::Future.new` blocks to get the old behavior.
17
+
18
+ See [#133](https://github.com/codegram/hyperclient/pull/133) and [#123](https://github.com/codegram/hyperclient/issues/123) for more information.
19
+
4
20
  ### Upgrading to >= 0.8.0
5
21
 
6
22
  ### Changes in curies
@@ -12,6 +12,11 @@ Feature: API navigation
12
12
  When I search for a post with a templated link
13
13
  Then the API should receive the request with all the params
14
14
 
15
+ Scenario: Templated links with multiple values
16
+ Given I connect to the API
17
+ When I search for posts by tag with a templated link
18
+ Then the API should receive the request for posts by tag with all the params
19
+
15
20
  Scenario: Attributes
16
21
  Given I connect to the API
17
22
  When I load a single post
@@ -17,6 +17,14 @@ class Spinach::Features::ApiNavigation < Spinach::FeatureSteps
17
17
  assert_requested :get, 'http://api.example.org/search?q=something'
18
18
  end
19
19
 
20
+ step 'I search for posts by tag with a templated link' do
21
+ api._links.tagged._expand(tags: %w[foo bar])._resource
22
+ end
23
+
24
+ step 'the API should receive the request for posts by tag with all the params' do
25
+ assert_requested :get, 'http://api.example.org/search?tags=foo&tags=bar'
26
+ end
27
+
20
28
  step 'I load a single post' do
21
29
  @post = api._links.posts._links.last_post
22
30
  end
@@ -12,7 +12,7 @@ class Spinach::Features::DefaultConfig < Spinach::FeatureSteps
12
12
  end
13
13
 
14
14
  step 'I send some data to the API' do
15
- stub_request(:post, 'http://api.example.org/posts')
15
+ stub_request(:post, 'http://api.example.org/posts').to_return(headers: { 'Content-Type' => 'application/hal+json' })
16
16
  assert_equal 200, api._links.posts._post(title: 'My first blog post')._response.status
17
17
  end
18
18
 
@@ -5,7 +5,9 @@ module API
5
5
  include Spinach::Fixtures
6
6
 
7
7
  before do
8
- stub_request(:any, %r{api.example.org*}).to_return(body: root_response, headers: { 'Content-Type' => 'application/hal+json' })
8
+ WebMock::Config.instance.query_values_notation = :flat_array
9
+
10
+ stub_request(:any, /api.example.org*/).to_return(body: root_response, headers: { 'Content-Type' => 'application/hal+json' })
9
11
  stub_request(:get, 'api.example.org/posts').to_return(body: posts_response, headers: { 'Content-Type' => 'application/hal+json' })
10
12
  stub_request(:get, 'api.example.org/posts/1').to_return(body: post_response, headers: { 'Content-Type' => 'application/hal+json' })
11
13
  stub_request(:get, 'api.example.org/page2').to_return(body: page2_response, headers: { 'Content-Type' => 'application/hal+json' })
@@ -8,6 +8,7 @@ module Spinach
8
8
  "self": { "href": "/" },
9
9
  "posts": { "href": "/posts" },
10
10
  "search": { "href": "/search{?q}", "templated": true },
11
+ "tagged": { "href": "/search{?tags*}", "templated": true },
11
12
  "api:authors": { "href": "/authors" },
12
13
  "next": { "href": "/page2" }
13
14
  }
data/hyperclient.gemspec CHANGED
@@ -1,12 +1,12 @@
1
- # -*- encoding: utf-8 -*-
1
+
2
2
  require File.expand_path('../lib/hyperclient/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ['Oriol Gual']
6
6
  gem.email = ['oriol.gual@gmail.com']
7
- gem.description = 'HyperClient is a Ruby Hypermedia API client.'
7
+ gem.description = 'Hyperclient is a Ruby Hypermedia API client.'
8
8
  gem.summary = ''
9
- gem.homepage = 'http://codegram.github.com/hyperclient/'
9
+ gem.homepage = 'https://github.com/codegram/hyperclient/'
10
10
  gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
11
11
  gem.files = `git ls-files`.split("\n")
12
12
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -14,8 +14,7 @@ Gem::Specification.new do |gem|
14
14
  gem.require_paths = ['lib']
15
15
  gem.version = Hyperclient::VERSION
16
16
 
17
- gem.add_dependency 'faraday'
18
- gem.add_dependency 'futuroscope'
17
+ gem.add_dependency 'faraday', '>= 0.9.0'
19
18
  gem.add_dependency 'faraday_middleware'
20
19
  gem.add_dependency 'faraday_hal_middleware'
21
20
  gem.add_dependency 'uri_template'
@@ -35,7 +35,7 @@ module Hyperclient
35
35
  end
36
36
 
37
37
  # Public: Returns a value from the collection for the given key.
38
- # If the key can’t be found, there are several options:
38
+ # If the key can't be found, there are several options:
39
39
  # With no other arguments, it will raise an KeyError exception;
40
40
  # if default is given, then that will be returned;
41
41
  #
@@ -38,8 +38,11 @@ module Hyperclient
38
38
  def initialize(url, &_block)
39
39
  @link = { 'href' => url }
40
40
  @entry_point = self
41
- @options = { async: true }
41
+ @options = {}
42
42
  @connection = nil
43
+ @resource = nil
44
+ @key = nil
45
+ @uri_variables = nil
43
46
  yield self if block_given?
44
47
  end
45
48
 
@@ -115,9 +118,7 @@ module Hyperclient
115
118
 
116
119
  # Public: Read/Set options.
117
120
  #
118
- # value - A Hash containing the client options. Use { async: false } to
119
- # to disable the default behavior of performing requests asynchronously
120
- # using futures.
121
+ # value - A Hash containing the client options.
121
122
  attr_accessor :options
122
123
 
123
124
  private
@@ -138,6 +139,7 @@ module Hyperclient
138
139
  connection.request :hal_json
139
140
  connection.response :hal_json, content_type: /\bjson$/
140
141
  connection.adapter :net_http
142
+ connection.options.params_encoder = Faraday::FlatParamsEncoder
141
143
  end
142
144
  end
143
145
 
@@ -1,5 +1,4 @@
1
1
  require 'uri_template'
2
- require 'futuroscope'
3
2
 
4
3
  module Hyperclient
5
4
  # Internal: The Link is used to let a Resource interact with the API.
@@ -126,7 +125,8 @@ module Hyperclient
126
125
  # Internal: Delegate the method further down the API if the resource cannot serve it.
127
126
  def method_missing(method, *args, &block)
128
127
  if _resource.respond_to?(method.to_s)
129
- _resource.send(method, *args, &block) || delegate_method(method, *args, &block)
128
+ result = _resource.send(method, *args, &block)
129
+ result.nil? ? delegate_method(method, *args, &block) : result
130
130
  else
131
131
  super
132
132
  end
@@ -167,15 +167,7 @@ module Hyperclient
167
167
 
168
168
  def http_method(method, body = nil)
169
169
  @resource = begin
170
- response =
171
- if @entry_point.options[:async]
172
- Futuroscope::Future.new do
173
- @entry_point.connection.run_request(method, _url, body, nil)
174
- end
175
- else
176
- @entry_point.connection.run_request(method, _url, body, nil)
177
- end
178
-
170
+ response = @entry_point.connection.run_request(method, _url, body, nil)
179
171
  Resource.new(response.body, @entry_point, response)
180
172
  end
181
173
  end
@@ -1,6 +1,17 @@
1
1
  require 'forwardable'
2
2
 
3
3
  module Hyperclient
4
+ # Public: Exception that is raised when passing in invalid representation data
5
+ # for the resource.
6
+ class InvalidRepresentationError < ArgumentError
7
+ attr_reader :representation
8
+
9
+ def initialize(error_description, representation)
10
+ super(error_description)
11
+ @representation = representation
12
+ end
13
+ end
14
+
4
15
  # Public: Represents a resource from your API. Its responsability is to
5
16
  # ease the way you access its attributes, links and embedded resources.
6
17
  class Resource
@@ -29,8 +40,9 @@ module Hyperclient
29
40
  # representation - The hash with the HAL representation of the Resource.
30
41
  # entry_point - The EntryPoint object to inject the configutation.
31
42
  def initialize(representation, entry_point, response = nil)
32
- representation = representation ? representation.dup : {}
43
+ representation = validate(representation)
33
44
  links = representation['_links'] || {}
45
+
34
46
  @_links = LinkCollection.new(links, links['curies'], entry_point)
35
47
  @_embedded = ResourceCollection.new(representation['_embedded'], entry_point)
36
48
  @_attributes = Attributes.new(representation)
@@ -68,6 +80,21 @@ module Hyperclient
68
80
 
69
81
  private
70
82
 
83
+ # Internal: Ensures the received representation is a valid Hash-lookalike.
84
+ def validate(representation)
85
+ return {} unless representation
86
+
87
+ if representation.respond_to?(:to_hash)
88
+ representation.to_hash.dup
89
+ else
90
+ raise InvalidRepresentationError.new(
91
+ "Invalid representation for resource (got #{representation.class}, expected Hash). " \
92
+ "Is your web server returning JSON HAL data with a 'Content-Type: application/hal+json' header?",
93
+ representation
94
+ )
95
+ end
96
+ end
97
+
71
98
  # Internal: Returns the self Link of the Resource. Used to handle the HTTP
72
99
  # methods.
73
100
  def _self_link
@@ -82,7 +109,7 @@ module Hyperclient
82
109
  if args.any? && args.first.is_a?(Hash)
83
110
  _links.send(method, [], &block)._expand(*args)
84
111
  elsif !Array.method_defined?(method)
85
- [:_attributes, :_embedded, :_links].each do |target|
112
+ %i[_attributes _embedded _links].each do |target|
86
113
  target = send(target)
87
114
  return target.send(method, *args, &block) if target.respond_to?(method.to_s)
88
115
  end
@@ -93,7 +120,7 @@ module Hyperclient
93
120
  # Internal: Accessory method to allow the resource respond to
94
121
  # methods that will hit method_missing.
95
122
  def respond_to_missing?(method, include_private = false)
96
- [:_attributes, :_embedded, :_links].each do |target|
123
+ %i[_attributes _embedded _links].each do |target|
97
124
  return true if send(target).respond_to?(method, include_private)
98
125
  end
99
126
  false
@@ -1,3 +1,3 @@
1
1
  module Hyperclient
2
- VERSION = '0.8.2'.freeze
2
+ VERSION = '0.9.0'.freeze
3
3
  end
@@ -32,7 +32,7 @@ module Hyperclient
32
32
  name
33
33
  end
34
34
 
35
- names.must_equal %w(_links title description permitted _hidden_attribute _embedded)
35
+ names.must_equal %w[_links title description permitted _hidden_attribute _embedded]
36
36
  end
37
37
 
38
38
  describe '#to_hash' do
@@ -33,21 +33,24 @@ module Hyperclient
33
33
 
34
34
  it 'creates a Faraday connection with the default block' do
35
35
  handlers = entry_point.connection.builder.handlers
36
+
36
37
  handlers.must_include Faraday::Response::RaiseError
37
38
  handlers.must_include FaradayMiddleware::FollowRedirects
38
39
  handlers.must_include FaradayMiddleware::EncodeHalJson
39
40
  handlers.must_include FaradayMiddleware::ParseHalJson
40
41
  handlers.must_include Faraday::Adapter::NetHttp
42
+
43
+ entry_point.connection.options.params_encoder.must_equal Faraday::FlatParamsEncoder
41
44
  end
42
45
 
43
46
  it 'raises a ConnectionAlreadyInitializedError if attempting to modify headers' do
44
47
  entry_point.connection.must_be_kind_of Faraday::Connection
45
- lambda { entry_point.headers = {} }.must_raise ConnectionAlreadyInitializedError
48
+ -> { entry_point.headers = {} }.must_raise ConnectionAlreadyInitializedError
46
49
  end
47
50
 
48
51
  it 'raises a ConnectionAlreadyInitializedError if attempting to modify the faraday block' do
49
52
  entry_point.connection.must_be_kind_of Faraday::Connection
50
- lambda { entry_point.connection {} }.must_raise ConnectionAlreadyInitializedError
53
+ -> { entry_point.connection {} }.must_raise ConnectionAlreadyInitializedError
51
54
  end
52
55
  end
53
56
 
@@ -56,12 +59,6 @@ module Hyperclient
56
59
  entry_point._url.must_equal 'http://my.api.org'
57
60
  end
58
61
  end
59
-
60
- describe 'options' do
61
- it 'enables the async option by default' do
62
- entry_point.options[:async].must_equal true
63
- end
64
- end
65
62
  end
66
63
 
67
64
  describe 'faraday_options' do
@@ -172,12 +169,15 @@ module Hyperclient
172
169
 
173
170
  it 'creates a Faraday connection with the default block plus any additional handlers' do
174
171
  handlers = entry_point.connection.builder.handlers
172
+
175
173
  handlers.must_include Faraday::Request::OAuth
176
174
  handlers.must_include Faraday::Response::RaiseError
177
175
  handlers.must_include FaradayMiddleware::FollowRedirects
178
176
  handlers.must_include FaradayMiddleware::EncodeHalJson
179
177
  handlers.must_include FaradayMiddleware::ParseHalJson
180
178
  handlers.must_include Faraday::Adapter::NetHttp
179
+
180
+ entry_point.connection.options.params_encoder.must_equal Faraday::FlatParamsEncoder
181
181
  end
182
182
  end
183
183
  end
@@ -7,7 +7,7 @@ module Hyperclient
7
7
  EntryPoint.new('http://api.example.org/')
8
8
  end
9
9
 
10
- %w(type deprecation name profile title hreflang).each do |prop|
10
+ %w[type deprecation name profile title hreflang].each do |prop|
11
11
  describe prop do
12
12
  it 'returns the property value' do
13
13
  link = Link.new('key', { prop => 'value' }, entry_point)
@@ -39,7 +39,7 @@ module Hyperclient
39
39
  it 'returns a list of required variables' do
40
40
  link = Link.new('key', { 'href' => '/orders{?id,owner}', 'templated' => true }, entry_point)
41
41
 
42
- link._variables.must_equal %w(id owner)
42
+ link._variables.must_equal %w[id owner]
43
43
  end
44
44
 
45
45
  it 'returns an empty array for untemplated links' do
@@ -161,35 +161,7 @@ module Hyperclient
161
161
  stub.get('http://api.example.org/productions/1') { [400, {}, nil] }
162
162
  end
163
163
 
164
- lambda { link._get }.must_raise Faraday::ClientError
165
- end
166
-
167
- it 'executes requests asynchronously using futuroscope' do
168
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
169
-
170
- stub_request(entry_point.connection) do |stub|
171
- stub.get('http://api.example.org/productions/1') { [200, {}, nil] }
172
- end
173
-
174
- Futuroscope::Future.expects(:new).returns(stub(body: nil))
175
-
176
- link._get
177
- end
178
-
179
- describe 'when entry_point.options is set to { async: false }' do
180
- it 'executes requests synchronously without futuroscope' do
181
- entry_point.options = { async: false }
182
-
183
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
184
-
185
- stub_request(entry_point.connection) do |stub|
186
- stub.get('http://api.example.org/productions/1') { [200, {}, nil] }
187
- end
188
-
189
- Futuroscope::Future.expects(:new).never
190
-
191
- link._get
192
- end
164
+ -> { link._get }.must_raise Faraday::ClientError
193
165
  end
194
166
  end
195
167
 
@@ -203,34 +175,6 @@ module Hyperclient
203
175
 
204
176
  link._options.must_be_kind_of Resource
205
177
  end
206
-
207
- it 'executes requests asynchronously using futuroscope' do
208
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
209
-
210
- stub_request(entry_point.connection) do |stub|
211
- stub.options('http://api.example.org/productions/1') { [200, {}, nil] }
212
- end
213
-
214
- Futuroscope::Future.expects(:new).returns(stub(body: nil))
215
-
216
- link._options
217
- end
218
-
219
- describe 'when entry_point.options is set to { async: false }' do
220
- it 'executes requests synchronously without futuroscope' do
221
- entry_point.options = { async: false }
222
-
223
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
224
-
225
- stub_request(entry_point.connection) do |stub|
226
- stub.options('http://api.example.org/productions/1') { [200, {}, nil] }
227
- end
228
-
229
- Futuroscope::Future.expects(:new).never
230
-
231
- link._options
232
- end
233
- end
234
178
  end
235
179
 
236
180
  describe '_head' do
@@ -243,34 +187,6 @@ module Hyperclient
243
187
 
244
188
  link._head.must_be_kind_of Resource
245
189
  end
246
-
247
- it 'executes requests asynchronously using futuroscope' do
248
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
249
-
250
- stub_request(entry_point.connection) do |stub|
251
- stub.head('http://api.example.org/productions/1') { [200, {}, nil] }
252
- end
253
-
254
- Futuroscope::Future.expects(:new).returns(stub(body: nil))
255
-
256
- link._head
257
- end
258
-
259
- describe 'when entry_point.options is set to { async: false }' do
260
- it 'executes requests synchronously without futuroscope' do
261
- entry_point.options = { async: false }
262
-
263
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
264
-
265
- stub_request(entry_point.connection) do |stub|
266
- stub.head('http://api.example.org/productions/1') { [200, {}, nil] }
267
- end
268
-
269
- Futuroscope::Future.expects(:new).never
270
-
271
- link._head
272
- end
273
- end
274
190
  end
275
191
 
276
192
  describe '_delete' do
@@ -283,34 +199,6 @@ module Hyperclient
283
199
 
284
200
  link._delete.must_be_kind_of Resource
285
201
  end
286
-
287
- it 'executes requests asynchronously using futuroscope' do
288
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
289
-
290
- stub_request(entry_point.connection) do |stub|
291
- stub.delete('http://api.example.org/productions/1') { [200, {}, nil] }
292
- end
293
-
294
- Futuroscope::Future.expects(:new).returns(stub(body: nil))
295
-
296
- link._delete
297
- end
298
-
299
- describe 'when entry_point.options is set to { async: false }' do
300
- it 'executes requests synchronously without futuroscope' do
301
- entry_point.options = { async: false }
302
-
303
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
304
-
305
- stub_request(entry_point.connection) do |stub|
306
- stub.delete('http://api.example.org/productions/1') { [200, {}, nil] }
307
- end
308
-
309
- Futuroscope::Future.expects(:new).never
310
-
311
- link._delete
312
- end
313
- end
314
202
  end
315
203
 
316
204
  describe '_post' do
@@ -331,34 +219,6 @@ module Hyperclient
331
219
 
332
220
  link._post.must_be_kind_of Resource
333
221
  end
334
-
335
- it 'executes requests asynchronously using futuroscope' do
336
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
337
-
338
- stub_request(entry_point.connection) do |stub|
339
- stub.post('http://api.example.org/productions/1') { [200, {}, nil] }
340
- end
341
-
342
- Futuroscope::Future.expects(:new).returns(stub(body: nil))
343
-
344
- link._post
345
- end
346
-
347
- describe 'when entry_point.options is set to { async: false }' do
348
- it 'executes requests synchronously without futuroscope' do
349
- entry_point.options = { async: false }
350
-
351
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
352
-
353
- stub_request(entry_point.connection) do |stub|
354
- stub.post('http://api.example.org/productions/1') { [200, {}, nil] }
355
- end
356
-
357
- Futuroscope::Future.expects(:new).never
358
-
359
- link._post
360
- end
361
- end
362
222
  end
363
223
 
364
224
  describe '_put' do
@@ -379,34 +239,6 @@ module Hyperclient
379
239
 
380
240
  link._put.must_be_kind_of Resource
381
241
  end
382
-
383
- it 'executes requests asynchronously using futuroscope' do
384
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
385
-
386
- stub_request(entry_point.connection) do |stub|
387
- stub.put('http://api.example.org/productions/1') { [200, {}, nil] }
388
- end
389
-
390
- Futuroscope::Future.expects(:new).returns(stub(body: nil))
391
-
392
- link._put
393
- end
394
-
395
- describe 'when entry_point.options is set to { async: false }' do
396
- it 'executes requests synchronously without futuroscope' do
397
- entry_point.options = { async: false }
398
-
399
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
400
-
401
- stub_request(entry_point.connection) do |stub|
402
- stub.put('http://api.example.org/productions/1') { [200, {}, nil] }
403
- end
404
-
405
- Futuroscope::Future.expects(:new).never
406
-
407
- link._put
408
- end
409
- end
410
242
  end
411
243
 
412
244
  describe '_patch' do
@@ -427,34 +259,6 @@ module Hyperclient
427
259
 
428
260
  link._patch.must_be_kind_of Resource
429
261
  end
430
-
431
- it 'executes requests asynchronously using futuroscope' do
432
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
433
-
434
- stub_request(entry_point.connection) do |stub|
435
- stub.patch('http://api.example.org/productions/1') { [200, {}, nil] }
436
- end
437
-
438
- Futuroscope::Future.expects(:new).returns(stub(body: nil))
439
-
440
- link._patch
441
- end
442
-
443
- describe 'when entry_point.options is set to { async: false }' do
444
- it 'executes requests synchronously without futuroscope' do
445
- entry_point.options = { async: false }
446
-
447
- link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
448
-
449
- stub_request(entry_point.connection) do |stub|
450
- stub.patch('http://api.example.org/productions/1') { [200, {}, nil] }
451
- end
452
-
453
- Futuroscope::Future.expects(:new).never
454
-
455
- link._patch
456
- end
457
- end
458
262
  end
459
263
 
460
264
  describe 'inspect' do
@@ -479,6 +283,16 @@ module Hyperclient
479
283
  resource.orders.first.id.must_equal 1
480
284
  end
481
285
 
286
+ it 'can handle false values in the response' do
287
+ resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)
288
+
289
+ stub_request(entry_point.connection) do |stub|
290
+ stub.get('http://api.example.org/orders') { [200, {}, { 'any' => false }] }
291
+ end
292
+
293
+ resource.orders.any.must_equal false
294
+ end
295
+
482
296
  it "doesn't delegate when link key doesn't match" do
483
297
  resource = Resource.new({ '_links' => { 'foos' => { 'href' => '/orders' } } }, entry_point)
484
298
 
@@ -521,7 +335,7 @@ module Hyperclient
521
335
  end
522
336
 
523
337
  it 'raises an error when the method does not exist in the resource' do
524
- lambda { link.this_method_does_not_exist }.must_raise NoMethodError
338
+ -> { link.this_method_does_not_exist }.must_raise NoMethodError
525
339
  end
526
340
 
527
341
  it 'responds to missing methods' do
@@ -40,6 +40,12 @@ module Hyperclient
40
40
 
41
41
  resource._response.body.must_equal body
42
42
  end
43
+
44
+ describe 'with an invalid representation' do
45
+ it 'raises an InvalidRepresentationError' do
46
+ proc { Resource.new('invalid representation data', entry_point) }.must_raise InvalidRepresentationError
47
+ end
48
+ end
43
49
  end
44
50
 
45
51
  describe '_links' do
@@ -94,7 +100,7 @@ module Hyperclient
94
100
  resource._attributes.expects('respond_to?').with('foo').returns(false)
95
101
  resource._links.expects('respond_to?').with('foo').returns(false)
96
102
  resource._embedded.expects('respond_to?').with('foo').returns(false)
97
- lambda { resource.foo }.must_raise NoMethodError
103
+ -> { resource.foo }.must_raise NoMethodError
98
104
  end
99
105
 
100
106
  it 'delegates []' do
data/test/test_helper.rb CHANGED
@@ -12,7 +12,3 @@ MiniTest::Unit::TestCase.class_eval do
12
12
  conn.builder.swap(adapter_handler, adapter_class, &stubs_block)
13
13
  end
14
14
  end
15
-
16
- require 'futuroscope'
17
- require 'futuroscope/pools/no_pool'
18
- Futuroscope.default_pool = Futuroscope::Pools::NoPool.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oriol Gual
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-31 00:00:00.000000000 Z
11
+ date: 2018-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,28 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.9.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: futuroscope
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
26
+ version: 0.9.0
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: faraday_middleware
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +94,7 @@ dependencies:
108
94
  - - ">="
109
95
  - !ruby/object:Gem::Version
110
96
  version: '0'
111
- description: HyperClient is a Ruby Hypermedia API client.
97
+ description: Hyperclient is a Ruby Hypermedia API client.
112
98
  email:
113
99
  - oriol.gual@gmail.com
114
100
  executables: []
@@ -164,7 +150,7 @@ files:
164
150
  - test/hyperclient/resource_test.rb
165
151
  - test/hyperclient_test.rb
166
152
  - test/test_helper.rb
167
- homepage: http://codegram.github.com/hyperclient/
153
+ homepage: https://github.com/codegram/hyperclient/
168
154
  licenses: []
169
155
  metadata: {}
170
156
  post_install_message:
@@ -183,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
169
  version: '0'
184
170
  requirements: []
185
171
  rubyforge_project:
186
- rubygems_version: 2.5.1
172
+ rubygems_version: 2.7.3
187
173
  signing_key:
188
174
  specification_version: 4
189
175
  summary: ''