hyperclient 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +2 -0
  4. data/.rubocop_todo.yml +55 -0
  5. data/.travis.yml +2 -0
  6. data/CHANGELOG.md +33 -10
  7. data/CONTRIBUTING.md +117 -0
  8. data/Gemfile +2 -1
  9. data/Guardfile +6 -6
  10. data/LICENSE +2 -2
  11. data/README.md +138 -0
  12. data/Rakefile +5 -3
  13. data/UPGRADING.md +37 -0
  14. data/examples/splines_api.rb +22 -0
  15. data/features/steps/api_navigation.rb +8 -8
  16. data/features/steps/default_config.rb +6 -6
  17. data/features/support/api.rb +4 -4
  18. data/features/support/fixtures.rb +1 -1
  19. data/hyperclient.gemspec +9 -8
  20. data/lib/faraday/connection.rb +2 -2
  21. data/lib/hyperclient.rb +1 -1
  22. data/lib/hyperclient/attributes.rb +1 -1
  23. data/lib/hyperclient/collection.rb +3 -3
  24. data/lib/hyperclient/curie.rb +49 -0
  25. data/lib/hyperclient/entry_point.rb +6 -4
  26. data/lib/hyperclient/link.rb +70 -58
  27. data/lib/hyperclient/link_collection.rb +36 -11
  28. data/lib/hyperclient/resource.rb +49 -18
  29. data/lib/hyperclient/resource_collection.rb +2 -1
  30. data/lib/hyperclient/version.rb +1 -1
  31. data/test/fixtures/element.json +12 -1
  32. data/test/hyperclient/attributes_test.rb +2 -2
  33. data/test/hyperclient/collection_test.rb +6 -7
  34. data/test/hyperclient/curie_test.rb +34 -0
  35. data/test/hyperclient/entry_point_test.rb +3 -2
  36. data/test/hyperclient/link_collection_test.rb +26 -5
  37. data/test/hyperclient/link_test.rb +111 -86
  38. data/test/hyperclient/resource_collection_test.rb +2 -2
  39. data/test/hyperclient/resource_test.rb +67 -30
  40. data/test/test_helper.rb +2 -2
  41. metadata +54 -39
  42. data/Gemfile.lock +0 -112
  43. data/MIT-LICENSE +0 -20
  44. data/Readme.md +0 -180
  45. data/examples/cyberscore.rb +0 -76
  46. data/examples/hal_shop.rb +0 -53
  47. data/lib/faraday/request/digest_authentication.rb +0 -85
  48. data/test/faraday/digest_authentication_test.rb +0 -41
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b87fcd7112fdf8f2cbb75b3e70d9eb63ed7c4d93
4
- data.tar.gz: fbbb826d826c93579534fbe028d3669192f524f7
3
+ metadata.gz: 1bba91bc97f25344d522646fec5c34cb74b183d4
4
+ data.tar.gz: b8289075f6c69400d84da795751f404030b9fdaa
5
5
  SHA512:
6
- metadata.gz: 05065d18d4c25d4c73c9e3023e3ab1710cb41a8750ee9ad4fef18deb69e08949492a2b67ad0a590170f39162c5ff8888b24b72b876d440c12ca4e1fdf98fd4fa
7
- data.tar.gz: 33213521c2e8443cc70e4b48f6327dd83a58c9f016266d2af27f0774cd980b84bb69e98ad1733ffeadd446073a31014bbb2a822c81234d73eda345c247a3af81
6
+ metadata.gz: 26e276baa5ea49d3e54c47b308c12efad26bb259cacc620cab7b44d84ff61e17296b904ea738b3a7917044044e37653944ef854577335575bc6f5d1f46b4a767
7
+ data.tar.gz: 69d655ed12c311619476b43b595b7705ad69d590a960a356775b0cdb554c95f6b20d4a9b0f3dc34ee9b1281566a8aee3e5881de99f32a2d04321bf8eb9b6bee8
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ test/tmp
15
15
  test/version_tmp
16
16
  tmp
17
17
  examples/william.rb
18
+ Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,55 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2014-09-18 12:21:47 -0400 using RuboCop version 0.26.0.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 1
9
+ # Configuration parameters: CountComments.
10
+ Metrics/ClassLength:
11
+ Max: 118
12
+
13
+ # Offense count: 2
14
+ Metrics/CyclomaticComplexity:
15
+ Max: 8
16
+
17
+ # Offense count: 39
18
+ # Configuration parameters: AllowURI.
19
+ Metrics/LineLength:
20
+ Max: 145
21
+
22
+ # Offense count: 1
23
+ # Configuration parameters: CountComments.
24
+ Metrics/MethodLength:
25
+ Max: 14
26
+
27
+ # Offense count: 2
28
+ Metrics/PerceivedComplexity:
29
+ Max: 9
30
+
31
+ # Offense count: 1
32
+ Style/AsciiComments:
33
+ Enabled: false
34
+
35
+ # Offense count: 2
36
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
37
+ Style/ClassAndModuleChildren:
38
+ Enabled: false
39
+
40
+ # Offense count: 14
41
+ Style/Documentation:
42
+ Enabled: false
43
+
44
+ # Offense count: 1
45
+ Style/DoubleNegation:
46
+ Enabled: false
47
+
48
+ # Offense count: 3
49
+ Style/Lambda:
50
+ Enabled: false
51
+
52
+ # Offense count: 5
53
+ # Configuration parameters: MaxSlashes.
54
+ Style/RegexpLiteral:
55
+ Enabled: false
data/.travis.yml CHANGED
@@ -1,3 +1,5 @@
1
1
  rvm:
2
2
  - 1.9.3
3
3
  - 2.0.0
4
+ - 2.1.2
5
+
data/CHANGELOG.md CHANGED
@@ -1,13 +1,36 @@
1
- == 0.3.1
2
- * backwards incompatible changes
3
- * Nothing
1
+ ### 0.5.0 (October 1, 2014)
4
2
 
5
- * enhancements
6
- * Support arrays of Links (by @rehevkor5)
7
- * Created the CHANGELOG.
3
+ This version introduces several backwards incompatible changes. See [UPGRADING](UPGRADING.md) for details.
8
4
 
9
- * bug fix
10
- * Nothing
5
+ * [#63](https://github.com/codegram/hyperclient/pull/63): Navigational methods, including `links`, `get` or `post`, have been renamed to `_links`, `_get`, or `_post` respectively - [@dblock](https://github.com/dblock).
6
+ * [#64](https://github.com/codegram/hyperclient/issues/64): Added support for curies - [@dblock](https://github.com/dblock).
7
+ * [#58](https://github.com/codegram/hyperclient/issues/58): Automatically follow redirects - [@dblock](https://github.com/dblock).
8
+ * [#63](https://github.com/codegram/hyperclient/pull/63): You can omit the navigational elements, `api.links.products` is now equivalent to `api.products` - [@dblock](https://github.com/dblock).
9
+ * [#61](https://github.com/codegram/hyperclient/pull/61): Implemented Rubocop, Ruby-style linter - [@dblock](https://github.com/dblock).
11
10
 
12
- * deprecations
13
- * Nothing
11
+ ### 0.4.0 (May 5, 2014)
12
+
13
+ * [#54](https://github.com/codegram/hyperclient/pull/54): Support Faraday 0.9.0 - [@lucianapazos](https://github.com/lucianapazos).
14
+ * [#30](https://github.com/codegram/hyperclient/pull/30): Use futuroscope to run API calls in the background - [@josepjaume](https://github.com/josepjaume).
15
+
16
+ ### 0.3.2 (December 20, 2013)
17
+
18
+ * [#48](https://github.com/codegram/hyperclient/pull/48): Added support for fetch on the collection class - [@col](https://github.com/col).
19
+ * [#50](https://github.com/codegram/hyperclient/pull/50): Fixed Resource/Attributes mutating the response body - [@col](https://github.com/col).
20
+ * [#46](https://github.com/codegram/hyperclient/pull/46): Made response available inside Resource, provide access to status codes - [@benhamill](https://github.com/benhamill).
21
+ * [#43](https://github.com/codegram/hyperclient/pull/43): Fixed LinkCollection#include? - [@benhamill](https://github.com/benhamill).
22
+ * [#47](https://github.com/codegram/hyperclient/pull/47): Fixed uninitialized constant Hyperclient::Resource::Forwardable - [@benhamill](https://github.com/benhamill).
23
+ * [#39](https://github.com/codegram/hyperclient/pull/39): Exposed templated link properties - [@txus](https://github.com/txus).
24
+ * [#38](https://github.com/codegram/hyperclient/pull/38): Defaulted POST, PUT and PATCH parameters - [@bkeepers](https://github.com/bkeepers).
25
+ * [#37](https://github.com/codegram/hyperclient/pull/37): Fixed calling #flatten on an array of links - [@bkeepers](https://github.com/bkeepers).
26
+ * [#36](https://github.com/codegram/hyperclient/pull/36): Exposed link properties - [@bkeepers](https://github.com/bkeepers).
27
+ * [#31](https://github.com/codegram/hyperclient/pull/31): Allowed underscored attribute names other than the ones reserved by the HAL spec - [@karlin](https://github.com/karlin).
28
+ * [#29](https://github.com/codegram/hyperclient/pull/29): Handled JSON that includes a link with a null value - [@arbylee](https://github.com/arbylee).
29
+
30
+ ### 0.3.1 (April 3, 2013)
31
+
32
+ * [#27](https://github.com/codegram/hyperclient/pull/27): Added support for collections of links - [@rehevkor5](https://github.com/rehevkor5).
33
+
34
+ ### 0.3.0 (February 3, 2013)
35
+
36
+ * Initial public release - [@oriolgual](https://github.com/oriolgual).
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,117 @@
1
+ Contributing to Hyperclient
2
+ ===========================
3
+
4
+ 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).
5
+
6
+ #### Fork the Project
7
+
8
+ Fork the [project on Github](https://github.com/codegram/hyperclient) and check out your copy.
9
+
10
+ ```
11
+ git clone https://github.com/contributor/hyperclient.git
12
+ cd hyperclient
13
+ git remote add upstream https://github.com/codegram/hyperclient.git
14
+ ```
15
+
16
+ #### Create a Topic Branch
17
+
18
+ Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
19
+
20
+ ```
21
+ git checkout master
22
+ git pull upstream master
23
+ git checkout -b my-feature-branch
24
+ ```
25
+
26
+ #### Bundle Install and Test
27
+
28
+ Ensure that you can build the project and run tests.
29
+
30
+ ```
31
+ bundle install
32
+ bundle exec rake
33
+ ```
34
+
35
+ #### Write Tests
36
+
37
+ Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add to [test/hyperclient](test/hyperclient).
38
+
39
+ We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
40
+
41
+ #### Write Code
42
+
43
+ Implement your feature or bug fix. Don't forget to update the [README](README.md).
44
+
45
+ Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop), run `bundle exec rubocop` and fix any style issues highlighted.
46
+
47
+ Make sure that `bundle exec rake` completes without errors.
48
+
49
+ #### Commit Changes
50
+
51
+ Make sure git knows your name and email address:
52
+
53
+ ```
54
+ git config --global user.name "Your Name"
55
+ git config --global user.email "contributor@example.com"
56
+ ```
57
+
58
+ Writing good commit logs is important. A commit log should describe what changed and why.
59
+
60
+ ```
61
+ git add ...
62
+ git commit
63
+ ```
64
+
65
+ #### Push
66
+
67
+ ```
68
+ git push origin my-feature-branch
69
+ ```
70
+
71
+ #### Make a Pull Request
72
+
73
+ Go to https://github.com/contributor/hyperclient and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
74
+
75
+ #### Update CHANGELOG
76
+
77
+ Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
78
+
79
+ ```
80
+ * [#123](https://github.com/codegram/hyperclient/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
81
+ ```
82
+
83
+ We like neat commits. Amend your previous commit and force push the changes.
84
+
85
+ ```
86
+ git commit --amend
87
+ git push origin my-feature-branch -f
88
+ ```
89
+
90
+ #### Rebase
91
+
92
+ If you've been working on a change for a while, rebase with upstream/master often.
93
+
94
+ ```
95
+ git fetch upstream
96
+ git rebase upstream/master
97
+ git push origin my-feature-branch -f
98
+ ```
99
+
100
+ We like neat commits, please try to amend your previous commits and force push the changes.
101
+
102
+ ```
103
+ git commit --amend
104
+ git push origin my-feature-branch -f
105
+ ```
106
+
107
+ #### Check on Your Pull Request
108
+
109
+ Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
110
+
111
+ #### Be Patient
112
+
113
+ It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
114
+
115
+ #### Thank You
116
+
117
+ Please do know that we really appreciate and value your time and work. We love you, really.
data/Gemfile CHANGED
@@ -11,5 +11,6 @@ gem 'pry'
11
11
 
12
12
  gem 'redcarpet'
13
13
  gem 'yard', '~> 0.8'
14
- gem 'yard-tomdoc', git: 'git://github.com/rubyworks/yard-tomdoc'
14
+ gem 'yard-tomdoc'
15
15
  gem 'simplecov', require: false
16
+ gem 'rubocop', '~> 0.26.0', require: false
data/Guardfile CHANGED
@@ -1,13 +1,13 @@
1
1
  guard 'minitest' do
2
- watch(%r|^test/(.*)_test\.rb|)
3
- watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
4
- watch(%r|^(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
5
- watch(%r|^test/test_helper\.rb|) { "test" }
2
+ watch(%r{^test/(.*)_test\.rb})
3
+ watch(%r{^lib/(.*)([^/]+)\.rb}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
4
+ watch(%r{^(.*)([^/]+)\.rb}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
5
+ watch(%r{^test/test_helper\.rb}) { 'test' }
6
6
  end
7
7
 
8
8
  guard 'spinach' do
9
- watch(%r|^features/(.*)\.feature|)
10
- watch(%r|^features/steps/(.*)([^/]+)\.rb|) do |m|
9
+ watch(%r{^features/(.*)\.feature})
10
+ watch(%r{^features/steps/(.*)([^/]+)\.rb}) do |m|
11
11
  "features/#{m[1]}#{m[2]}.feature"
12
12
  end
13
13
  end
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Oriol Gual
1
+ Copyright (c) 2012-214 Oriol Gual
2
2
 
3
3
  MIT License
4
4
 
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # Hyperclient
2
+
3
+ [![Build Status](https://secure.travis-ci.org/codegram/hyperclient.png)](http://travis-ci.org/codegram/hyperclient)
4
+ [![Dependency Status](https://gemnasium.com/codegram/hyperclient.png)](http://gemnasium.com/codegram/hyperclient)
5
+ [![Code Climate](https://codeclimate.com/github/codegram/hyperclient.png)](https://codeclimate.com/github/codegram/hyperclient)
6
+
7
+ Hyperclient is a Hypermedia API client written in Ruby. It fully supports [JSON HAL](http://stateless.co/hal_specification.html).
8
+
9
+ ## Usage
10
+
11
+ The examples in this README use the [Splines Demo API](https://github.com/dblock/grape-with-roar) running [here](https://grape-with-roar.herokuapp.com/api).
12
+
13
+ ### API Client
14
+
15
+ Create an API client.
16
+
17
+ ```ruby
18
+ require 'hyperclient'
19
+
20
+ api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api')
21
+ ```
22
+
23
+ By default, Hyperclient adds `application/json` as `Content-Type` and `Accept` headers. It will also send requests as JSON and parse JSON responses. Specify additional headers or authentication if necessary. Hyperclient supports Basic, Token or Digest auth as well as many other [Faraday](http://github.com/lostisland/faraday) extensions.
24
+
25
+ ```ruby
26
+ api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api').tap do |api|
27
+ api.digest_auth('username', 'password')
28
+ api.headers.update('Accept-Encoding' => 'deflate, gzip')
29
+ end
30
+ ```
31
+
32
+ ### Resources and Attributes
33
+
34
+ Hyperclient will fetch and discover the resources from your API.
35
+
36
+ ```ruby
37
+ api.splines.each do |spline|
38
+ puts "A spline with ID #{spline.uuid}."
39
+ end
40
+ ```
41
+
42
+ ### Links and Embedded Resources
43
+
44
+ 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`.
45
+
46
+ ```ruby
47
+ api._links.splines
48
+ ```
49
+
50
+ ### Templated Links
51
+
52
+ Templated links require variables to be expanded. For example, the demo API has a link called "spline" that requires a spline "uuid".
53
+
54
+ ```ruby
55
+ spline = api.spline(uuid: 'uuid')
56
+ puts "Spline #{spline.uuid} is #{spline.reticulated ? 'reticulated' : 'not reticulated'}."
57
+ ```
58
+
59
+ Invoking `api.spline(uuid: 'uuid').reticulated` is equivalent to `api._links.spline._expand(uuid: 'uuid')._resource._attributes.reticulated`.
60
+
61
+ ### Curies
62
+
63
+ Curies are named tokens that you can define in the document and use to express curie relation URIs in a friendlier, more compact fashion. For example, the demo API contains very long links to images that use an "images" curie. Hyperclient handles curies and resolves these into full links automatically.
64
+
65
+ ```ruby
66
+ puts spline['image:thumbnail'] # => https://grape-with-roar.herokuapp.com/api/splines/uuid/images/thumbnail.jpg
67
+ ```
68
+
69
+ ### Attributes
70
+
71
+ Resource attributes can also be accessed as a hash.
72
+
73
+ ```ruby
74
+ puts spline.to_h # => {"uuid" => "uuid", "reticulated" => true}
75
+ ```
76
+
77
+ The above is equivalent to `spline._attributes.to_h`.
78
+
79
+ ### HTTP
80
+
81
+ 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.
82
+
83
+ For example, you can examine the API raw JSON by invoking `_get` and examining the `_response.body` hash.
84
+
85
+ ```ruby
86
+ api._get
87
+ api._response.body
88
+ ```
89
+
90
+ Other methods, including `_head` or `_options` are also available.
91
+
92
+ ```ruby
93
+ spline = api.spline(uuid: 'uuid')
94
+ spline._head
95
+ spline._options
96
+ ```
97
+
98
+ Invoke `_post` to create resources.
99
+
100
+ ```ruby
101
+ splines = api.splines
102
+ splines._post(uuid: 'new uuid', reticulated: false)
103
+ ```
104
+
105
+ Invoke `_put` or `_patch` to update resources.
106
+
107
+ ```ruby
108
+ spline = api.spline(uuid: 'uuid')
109
+ spline._put(reticulated: true)
110
+ spline._patch(reticulated: true)
111
+ ```
112
+
113
+ Invoke `_delete` to destroy a resource.
114
+
115
+ ```
116
+ spline = api.spline(uuid: 'uuid')
117
+ spline._delete
118
+ ```
119
+
120
+ ### Faraday Connection
121
+
122
+ You can access the Faraday connection directly to add middleware by calling `connection` on the entry point. As an example, you could use the [faraday-http-cache-middleware](https://github.com/plataformatec/faraday-http-cache).
123
+
124
+ ```ruby
125
+ api.connection.use :http_cache
126
+ ```
127
+
128
+ ## Reference
129
+
130
+ [Hyperclient API Reference](http://rubydoc.org/github/codegram/hyperclient/master/frames).
131
+
132
+ ## Contributing
133
+
134
+ 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.
135
+
136
+ ## License
137
+
138
+ MIT License, see [LICENSE](LICENSE) for details. Copyright 2012-2014 [Codegram Technologies](http://codegram.com).
data/Rakefile CHANGED
@@ -14,13 +14,12 @@ if ENV['COVERAGE']
14
14
  end
15
15
 
16
16
  require 'yard'
17
- YARD::Config.load_plugin('yard-tomdoc')
17
+ YARD::Config.load_plugin('yard-tomdoc')
18
18
  YARD::Rake::YardocTask.new do |t|
19
19
  t.files = ['lib/**/*.rb']
20
20
  t.options = %w(-r README.md)
21
21
  end
22
22
 
23
-
24
23
  Bundler::GemHelper.install_tasks
25
24
 
26
25
  require 'rake/testtask'
@@ -37,4 +36,7 @@ task :spinach do
37
36
  ruby '-S spinach'
38
37
  end
39
38
 
40
- task default: [:test, :spinach]
39
+ require 'rubocop/rake_task'
40
+ RuboCop::RakeTask.new(:rubocop)
41
+
42
+ task default: [:rubocop, :test, :spinach]