cetacean 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0e46de21051edb9c941f19ed926b5a124396a3b0
4
+ data.tar.gz: 60a0c1b28bea7eb5ffcd9e42bf9aa8bfff4fe41b
5
+ SHA512:
6
+ metadata.gz: 6e8313db79a64e6ff05d0d97ada62b92ebe745de37827344b57207b38d0e8cda0c74358d9980e0abe6f3308e5ff7fbd26aa09aa02eb256612eaf41197f9fc3f7
7
+ data.tar.gz: e7e39696b01a9d848a330ddc5d20bf8763001377580843bc689d7b2fa471c86a244f9bdb6be37fe79f9f55e6ea8af16a14357f712aac6f8ac1c50f0ceaaeef3a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/CHANGES.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changes
2
+
3
+ ## 1.0.0
4
+
5
+ * The first glimmers of functionality. - Ben Hamill
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cetacean.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ben Hamill
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
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.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ > Everything the system does *for* you, it also does *to* you.
2
+
3
+ # Cetacean [![Build Status](https://travis-ci.org/benhamill/cetacean.png)](https://travis-ci.org/benhamill/cetacean) [![Code Climate](https://codeclimate.com/github/benhamill/cetacean.png)](https://codeclimate.com/github/benhamill/cetacean)
4
+
5
+ The [HAL](http://stateless.co/hal_specification.html) client that does almost
6
+ nothing for/to you.
7
+
8
+ Cetacean is tightly coupled to [Faraday](http://rubygems.org/gems/faraday), but
9
+ doesn't actually call it. You set up your own Faraday client and use it to make
10
+ requests. You feed Cetacean `Faraday::Request` objects and it helps you figure
11
+ out if they're HAL documents and pull useful data out of them if they are.
12
+
13
+
14
+ ## Usage
15
+
16
+ Something like this:
17
+
18
+ ```ruby
19
+ api = Faraday.new('https://api.example.com/') do |faraday|
20
+ faraday.headers['Accept'] = 'application/hal+json'
21
+ end
22
+
23
+ root = Cetacean.new(api.get)
24
+ users = Cetacean.new(api.get(root.get_uri(:users).to_s))
25
+ user = users.embedded(:users).first
26
+
27
+ important_blog_post = Cetacean.new(api.get(user.get_uri(:post).expand(id: 2)))
28
+
29
+ interesting_blog_posts = Cetacean.new(api.get(root.get_uri(:search_posts).expand(q: 'interesting')))
30
+ ```
31
+
32
+ Check out the specs for more detailed uses.
33
+
34
+
35
+ ## Contributing
36
+
37
+ Help is gladly welcomed. If you have a feature you'd like to add, it's much more
38
+ likely to get in (or get in faster) the closer you stick to these steps:
39
+
40
+ 1. Open an Issue to talk about it. We can discuss whether it's the right
41
+ direction or maybe help track down a bug, etc.
42
+ 1. Fork the project, and make a branch to work on your feature/fix. Master is
43
+ where you'll want to start from.
44
+ 1. Turn the Issue into a Pull Request. There are several ways to do this, but
45
+ [hub](https://github.com/defunkt/hub) is probably the easiest.
46
+ 1. Make sure your Pull Request includes tests.
47
+ 1. Bonus points if your Pull Request updates `CHANGES.md` to include a summary
48
+ of your changes and your name like the other entries. If the last entry is
49
+ the last release, add a new `## Unreleased` heading at the top.
50
+
51
+ If you don't know how to fix something, even just a Pull Request that includes a
52
+ failing test can be helpful. If in doubt, make an Issue to discuss.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/cetacean.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cetacean/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cetacean"
8
+ spec.version = Cetacean::VERSION
9
+ spec.authors = ["Ben Hamill"]
10
+ spec.email = ["git-commits@benhamill.com"]
11
+ spec.description = %q{The HAL client that does almost nothing for/to you.}
12
+ spec.summary = %q{The HAL client that does almost nothing for/to you.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "faraday", "~> 0.8"
22
+ spec.add_dependency "uri_template", "~> 0.6"
23
+ spec.add_dependency "activesupport", "~> 4.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "webmock"
29
+ spec.add_development_dependency "pry"
30
+ spec.add_development_dependency "sinatra"
31
+ end
data/lib/cetacean.rb ADDED
@@ -0,0 +1,11 @@
1
+ require_relative 'cetacean/version'
2
+ require_relative 'cetacean/resource'
3
+ require_relative 'cetacean/embedded_resource'
4
+ require_relative 'cetacean/embedded_resource_collection'
5
+ require_relative 'cetacean/response'
6
+
7
+ module Cetacean
8
+ def self.new(response)
9
+ Cetacean::Response.new(response)
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class Cetacean::EmbeddedResource
2
+ include Cetacean::Resource
3
+
4
+ def initialize(document)
5
+ @hal = document
6
+ end
7
+
8
+ private
9
+
10
+ attr_reader :hal
11
+ end
@@ -0,0 +1,17 @@
1
+ class Cetacean::EmbeddedResourceCollection
2
+ include Enumerable
3
+
4
+ def initialize(document_array)
5
+ @document_array = document_array
6
+ end
7
+
8
+ def each
9
+ document_array.each do |document|
10
+ yield Cetacean::EmbeddedResource.new(document)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :document_array
17
+ end
@@ -0,0 +1,38 @@
1
+ require 'uri_template'
2
+ require 'forwardable'
3
+ require 'active_support'
4
+
5
+ module Cetacean::Resource
6
+ extend Forwardable
7
+
8
+ def_delegators :hal, :[], :fetch
9
+
10
+ def attributes
11
+ hal.except('_links', '_embedded')
12
+ end
13
+
14
+ def embedded(rel=nil)
15
+ return hal['_embedded'] if rel.nil?
16
+
17
+ rel = rel.to_s
18
+
19
+ if (document = embedded[rel])
20
+ case document
21
+ when Array
22
+ Cetacean::EmbeddedResourceCollection.new(document)
23
+ else
24
+ Cetacean::EmbeddedResource.new(document)
25
+ end
26
+ end
27
+ end
28
+
29
+ def get_uri(rel)
30
+ return unless links.include?(rel.to_s)
31
+
32
+ URITemplate.new(links[rel.to_s]['href'])
33
+ end
34
+
35
+ def links
36
+ hal['_links'] || {}
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ require 'forwardable'
2
+
3
+ class Cetacean::Response
4
+ include Cetacean::Resource
5
+ extend Forwardable
6
+
7
+ def_delegators :response, :status, :success?, :body
8
+
9
+ attr_reader :response
10
+
11
+ # Feed me Faraday::Response objects or things that quack like them.
12
+ def initialize(response)
13
+ @response = response
14
+ end
15
+
16
+ # Tells you whether the response thinks it's a HAL document or not. Does no
17
+ # validation.
18
+ def hal?
19
+ response.headers['content-type'] =~ %r{\Aapplication/hal\+json}
20
+ end
21
+
22
+ private
23
+
24
+ def hal
25
+ @hal ||= parse_hal
26
+ end
27
+
28
+ def parse_hal
29
+ hal? ? JSON.parse(response.body) : {}
30
+ rescue JSON::JSONError, TypeError
31
+ {}
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ module Cetacean
2
+ VERSION = "1.0.0"
3
+
4
+ def self.version
5
+ VERSION
6
+ end
7
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ describe Cetacean do
5
+ let(:api) do
6
+ Faraday.new('http://api.example.com') do |f|
7
+ f.headers['Accept'] = 'application/hal+json'
8
+ f.adapter Faraday.default_adapter
9
+ end
10
+ end
11
+
12
+ context "when fed a valid HAL response" do
13
+ before do
14
+ stub_service 'http://api.example.com', Sinatra.new do
15
+ get '/' do
16
+ content_type 'application/hal+json'
17
+ JSON.dump(
18
+ {
19
+ _links: {
20
+ self: { href: '/' },
21
+ },
22
+ api_ranking: 'the best',
23
+ _embedded: {
24
+ singular: {
25
+ _links: { self: { href: '/singular' } }
26
+ },
27
+ plural: [
28
+ { _links: { self: { href: '/plural/1' } } },
29
+ { _links: { self: { href: '/plural/2' } } },
30
+ ]
31
+ }
32
+ }
33
+ )
34
+ end
35
+ end
36
+ end
37
+
38
+ subject { Cetacean.new(api.get) }
39
+
40
+ it "knows it's HAL" do
41
+ expect(subject).to be_hal
42
+ end
43
+
44
+ it "can find links by rel" do
45
+ expect(subject.get_uri(:self)).to eq('/')
46
+ end
47
+
48
+ it "hands out the links hash" do
49
+ expect(subject.links).to eq('self' => { 'href' => '/' })
50
+ end
51
+
52
+ it "returns URITemplates when asked for a uri" do
53
+ expect(subject.get_uri(:self)).to be_a(URITemplate)
54
+ end
55
+
56
+ it "allows access to attributes with []" do
57
+ expect(subject['api_ranking']).to eq('the best')
58
+ end
59
+
60
+ it "allows access to attributes with fetch" do
61
+ expect(subject.fetch('api_ranking')).to eq('the best')
62
+ end
63
+
64
+ it "does defaulting on attribute fetches" do
65
+ expect(subject.fetch('not an attribute', 'default')).to eq('default')
66
+ end
67
+
68
+ it "hands out a hash of attributes" do
69
+ expect(subject.attributes).to eq('api_ranking' => 'the best')
70
+ end
71
+
72
+ it "lists embeds" do
73
+ expect(subject.embedded).to include('singular')
74
+ expect(subject.embedded).to include('plural')
75
+ end
76
+
77
+ it "handles singular embeds" do
78
+ expect(subject.embedded(:singular).get_uri(:self)).to eq('/singular')
79
+ end
80
+
81
+ it "handles plural embeds" do
82
+ subject.embedded(:plural).each_with_index do |plur, index|
83
+ expect(plur.get_uri(:self)).to eq("/plural/#{index + 1}")
84
+ end
85
+ end
86
+ end
87
+
88
+ context "when fed invalid HAL" do
89
+ before do
90
+ stub_service 'http://api.example.com', Sinatra.new do
91
+ get '/' do
92
+ content_type 'application/hal+json'
93
+ "<html></html>"
94
+ end
95
+ end
96
+ end
97
+
98
+ subject { Cetacean.new(api.get) }
99
+
100
+ it "thinks it's HAL" do
101
+ expect(subject).to be_hal
102
+ end
103
+
104
+ it "can't find links by rel" do
105
+ expect(subject.get_uri(:self)).to be_nil
106
+ end
107
+ end
108
+
109
+ context "when fed non-HAL" do
110
+ before do
111
+ stub_service 'http://api.example.com', Sinatra.new do
112
+ get '/' do
113
+ "<html></html>"
114
+ end
115
+ end
116
+ end
117
+
118
+ subject { Cetacean.new(api.get) }
119
+
120
+ it "knows it isn't HAL" do
121
+ expect(subject).to_not be_hal
122
+ end
123
+
124
+ it "can't find links by rel" do
125
+ expect(subject.get_uri(:self)).to be_nil
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,35 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'cetacean'
3
+
4
+ require 'webmock/rspec'
5
+ require 'pry'
6
+ require 'sinatra/base'
7
+ require 'faraday'
8
+
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+ config.order = 'random'
14
+
15
+ config.expect_with :rspec do |expectations|
16
+ expectations.syntax = :expect
17
+ end
18
+
19
+ config.mock_with :rspec do |mocks|
20
+ mocks.syntax = :expect
21
+ end
22
+ end
23
+
24
+ WebMock.disable_net_connect!
25
+
26
+ def stub_service(uri, stub, &block)
27
+ uri = URI.parse(uri)
28
+ port = uri.port != uri.default_port ? ":#{uri.port}" : ""
29
+ stub = block ? Sinatra.new(stub, &block) : stub
30
+
31
+ WebMock.stub_request(
32
+ :any,
33
+ %r{^#{uri.scheme}://(.*:.*@)?#{uri.host}#{port}/.*$}
34
+ ).to_rack(stub)
35
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cetacean do
4
+ describe ".version" do
5
+ it "is available as a constant" do
6
+ expect(Cetacean::VERSION).to_not be_nil
7
+ end
8
+
9
+ it "is available as a method" do
10
+ expect(Cetacean.version).to_not be_nil
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cetacean
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Hamill
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: uri_template
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sinatra
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: The HAL client that does almost nothing for/to you.
140
+ email:
141
+ - git-commits@benhamill.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - .gitignore
147
+ - .rspec
148
+ - .travis.yml
149
+ - CHANGES.md
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - cetacean.gemspec
155
+ - lib/cetacean.rb
156
+ - lib/cetacean/embedded_resource.rb
157
+ - lib/cetacean/embedded_resource_collection.rb
158
+ - lib/cetacean/resource.rb
159
+ - lib/cetacean/response.rb
160
+ - lib/cetacean/version.rb
161
+ - spec/acceptance/cetacean_spec.rb
162
+ - spec/spec_helper.rb
163
+ - spec/unit/cetacean_spec.rb
164
+ homepage: ''
165
+ licenses:
166
+ - MIT
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - '>='
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.0.3
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: The HAL client that does almost nothing for/to you.
188
+ test_files:
189
+ - spec/acceptance/cetacean_spec.rb
190
+ - spec/spec_helper.rb
191
+ - spec/unit/cetacean_spec.rb