hactor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +7 -0
- data/hactor.gemspec +21 -0
- data/lib/hactor.rb +12 -0
- data/lib/hactor/actor.rb +7 -0
- data/lib/hactor/hal/document.rb +22 -0
- data/lib/hactor/hal/embedded_collection.rb +31 -0
- data/lib/hactor/hal/flat_collection.rb +25 -0
- data/lib/hactor/hal/link.rb +7 -0
- data/lib/hactor/hal/link_collection.rb +31 -0
- data/lib/hactor/hal/resource.rb +40 -0
- data/lib/hactor/http/client.rb +33 -0
- data/lib/hactor/http/response.rb +29 -0
- data/lib/hactor/version.rb +3 -0
- data/spec/hactor/hal/document_spec.rb +22 -0
- data/spec/hactor/hal/embedded_collection_spec.rb +6 -0
- data/spec/hactor/hal/flat_collection_spec.rb +6 -0
- data/spec/hactor/hal/link_collection_spec.rb +30 -0
- data/spec/hactor/hal/resource_spec.rb +75 -0
- data/spec/hactor/http/client_spec.rb +52 -0
- data/spec/hactor/http/response_spec.rb +49 -0
- data/spec/hactor_spec.rb +16 -0
- data/spec/integration.rb +37 -0
- data/spec/spec_helper.rb +17 -0
- metadata +115 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Mike Kelly
|
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,29 @@
|
|
1
|
+
# Hactor
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hactor'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hactor
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/hactor.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hactor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "hactor"
|
8
|
+
gem.version = Hactor::VERSION
|
9
|
+
gem.authors = ["Mike Kelly"]
|
10
|
+
gem.email = ["mikekelly321@gmail.com"]
|
11
|
+
gem.description = %q{A framework for building hypermedia clients}
|
12
|
+
gem.summary = %q{A framework for building hypermedia clients}
|
13
|
+
gem.homepage = "https://github.com/mikekelly/hactor"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_development_dependency 'rspec'
|
20
|
+
gem.add_dependency 'faraday'
|
21
|
+
end
|
data/lib/hactor.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'hactor/version'
|
2
|
+
require 'hactor/http/client'
|
3
|
+
require 'hactor/actor'
|
4
|
+
|
5
|
+
module Hactor
|
6
|
+
def self.start(options)
|
7
|
+
url = options.fetch :url
|
8
|
+
actor = options.fetch :actor
|
9
|
+
http_client = options.fetch(:http_client) { Hactor::HTTP::Client.new }
|
10
|
+
http_client.get(url: url, actor: actor)
|
11
|
+
end
|
12
|
+
end
|
data/lib/hactor/actor.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'json'
|
3
|
+
require 'hactor/hal/resource'
|
4
|
+
|
5
|
+
module Hactor
|
6
|
+
module HAL
|
7
|
+
class Document < SimpleDelegator
|
8
|
+
attr_reader :body, :resource_class
|
9
|
+
|
10
|
+
def initialize(body, options={})
|
11
|
+
@resource_class = options.fetch(:resource_class) { Resource }
|
12
|
+
@body = JSON.parse(body)
|
13
|
+
super(root_resource)
|
14
|
+
end
|
15
|
+
|
16
|
+
def root_resource
|
17
|
+
@root_resource ||= resource_class.new(body)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'hactor/hal/flat_collection'
|
3
|
+
require 'hactor/hal/resource'
|
4
|
+
#require 'hactor/hal/null_resource'
|
5
|
+
|
6
|
+
module Hactor
|
7
|
+
module HAL
|
8
|
+
class EmbeddedCollection
|
9
|
+
extend Forwardable
|
10
|
+
include Enumerable
|
11
|
+
|
12
|
+
attr_reader :hash, :embedded_class, :flat_collection_class
|
13
|
+
def_delegators :each
|
14
|
+
|
15
|
+
def initialize(hash, options={})
|
16
|
+
#TODO: throw/log parsing error if not hash
|
17
|
+
@embedded_class = options.fetch(:embedded_class) { Resource }
|
18
|
+
@flat_collection_class = options.fetch(:flat_collection_class) { FlatCollection }
|
19
|
+
@hash = hash
|
20
|
+
end
|
21
|
+
|
22
|
+
def all
|
23
|
+
@all ||= flat_collection_class.new(hash, item_class: embedded_class)
|
24
|
+
end
|
25
|
+
|
26
|
+
def find_by_rel(rel)
|
27
|
+
all.find(->{ NullResource.new }) { |resource| resource.rel == rel }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Hactor
|
2
|
+
module HAL
|
3
|
+
class FlatCollection < Array
|
4
|
+
attr_reader :item_class
|
5
|
+
|
6
|
+
def initialize(hash, options={})
|
7
|
+
@item_class = options.fetch(:item_class)
|
8
|
+
super flatten(hash)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
def flatten(hash)
|
13
|
+
arr = []
|
14
|
+
hash.each do |rel, value|
|
15
|
+
if value.is_a? Array
|
16
|
+
arr += value.map { |link| item_class.new(link.merge(rel: rel)) }
|
17
|
+
else
|
18
|
+
arr.push item_class.new(value.merge(rel: rel))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
arr
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'hactor/hal/flat_collection'
|
3
|
+
require 'hactor/hal/link'
|
4
|
+
#require 'hactor/hal/null_link'
|
5
|
+
|
6
|
+
module Hactor
|
7
|
+
module HAL
|
8
|
+
class LinkCollection
|
9
|
+
extend Forwardable
|
10
|
+
include Enumerable
|
11
|
+
|
12
|
+
attr_reader :hash, :link_class, :flat_collection_class
|
13
|
+
def_delegators :all, :each
|
14
|
+
|
15
|
+
def initialize(hash, options={})
|
16
|
+
#TODO: throw/log parsing error if not hash
|
17
|
+
@link_class = options.fetch(:link_class) { Link }
|
18
|
+
@flat_collection_class = options.fetch(:flat_collection_class) { FlatCollection }
|
19
|
+
@hash = hash
|
20
|
+
end
|
21
|
+
|
22
|
+
def all
|
23
|
+
@all ||= flat_collection_class.new(hash, item_class: link_class)
|
24
|
+
end
|
25
|
+
|
26
|
+
def find_by_rel(rel)
|
27
|
+
all.find(->{ NullLink.new }) { |link| link.rel == rel }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'hactor/hal/link_collection'
|
2
|
+
require 'hactor/hal/embedded_collection'
|
3
|
+
|
4
|
+
module Hactor
|
5
|
+
module HAL
|
6
|
+
class Resource
|
7
|
+
RESERVED_PROPERTIES = ['_links', '_embedded']
|
8
|
+
|
9
|
+
attr_reader :state, :link_collection_class, :embedded_collection_class
|
10
|
+
|
11
|
+
def initialize(state, options={})
|
12
|
+
@link_collection_class = options.fetch(:link_collection_class) { LinkCollection }
|
13
|
+
@embedded_collection_class = options.fetch(:embedded_collection_class) { EmbeddedCollection }
|
14
|
+
@state = state
|
15
|
+
end
|
16
|
+
|
17
|
+
def properties
|
18
|
+
@properties ||= state.reject { |k,v|
|
19
|
+
RESERVED_PROPERTIES.include? k
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def link(rel, options={})
|
24
|
+
links.find_by_rel(rel)
|
25
|
+
end
|
26
|
+
|
27
|
+
def links
|
28
|
+
@links ||= link_collection_class.new(state['_links'])
|
29
|
+
end
|
30
|
+
|
31
|
+
def embedded_resource(rel)
|
32
|
+
embedded_resources.find(rel)
|
33
|
+
end
|
34
|
+
|
35
|
+
def embedded_resources
|
36
|
+
@embedded_resources ||= embedded_collection_class.new(state['_embedded'])
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'hactor/http/response'
|
2
|
+
require 'faraday'
|
3
|
+
|
4
|
+
module Hactor
|
5
|
+
module HTTP
|
6
|
+
class Client < SimpleDelegator
|
7
|
+
attr_reader :response_class, :backend
|
8
|
+
|
9
|
+
def initialize(options={})
|
10
|
+
@response_class = options.fetch(:response_class) { Hactor::HTTP::Response }
|
11
|
+
@backend = options.fetch(:backend) { Faraday.new }
|
12
|
+
super(@backend)
|
13
|
+
end
|
14
|
+
|
15
|
+
def follow(link, options = {})
|
16
|
+
actor = options.fetch(:actor)
|
17
|
+
context_url = options.fetch(:context_url)
|
18
|
+
|
19
|
+
url = context_url.merge(link.href)
|
20
|
+
get(url: url, actor: actor)
|
21
|
+
end
|
22
|
+
|
23
|
+
def get(options)
|
24
|
+
url = options.fetch :url
|
25
|
+
actor = options.fetch :actor
|
26
|
+
|
27
|
+
response = response_class.new(backend.get(url),
|
28
|
+
http_client: self)
|
29
|
+
actor.call response
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'hactor/hal/document'
|
3
|
+
|
4
|
+
module Hactor
|
5
|
+
module HTTP
|
6
|
+
class Response < SimpleDelegator
|
7
|
+
attr_reader :request_client, :codec
|
8
|
+
|
9
|
+
def initialize(response, options={})
|
10
|
+
@request_client = options.fetch(:http_client)
|
11
|
+
@codec = options.fetch(:codec) { Hactor::HAL::Document }
|
12
|
+
@body = options[:body]
|
13
|
+
super(response)
|
14
|
+
end
|
15
|
+
|
16
|
+
def follow(rel, options={})
|
17
|
+
actor = options.fetch(:actor) { Hactor::NullActor.new }
|
18
|
+
client = options.fetch(:http_client) { request_client }
|
19
|
+
|
20
|
+
link = body.link(rel)
|
21
|
+
client.follow(link, context_url: env[:url], actor: actor)
|
22
|
+
end
|
23
|
+
|
24
|
+
def body
|
25
|
+
@body ||= codec.new(__getobj__.body)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hactor/hal/document'
|
3
|
+
|
4
|
+
describe Hactor::HAL::Document do
|
5
|
+
describe "#root_resource" do
|
6
|
+
let(:resource_class) { mock }
|
7
|
+
let(:sentinel) { stub }
|
8
|
+
let(:body) { '{}' }
|
9
|
+
let(:doc) do
|
10
|
+
Hactor::HAL::Document.new(body,
|
11
|
+
resource_class: resource_class)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns new Resource object" do
|
15
|
+
resource_class.should_receive(:new)
|
16
|
+
.with(JSON.parse(body))
|
17
|
+
.and_return(sentinel)
|
18
|
+
|
19
|
+
doc.root_resource.should == sentinel
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hactor/hal/link_collection'
|
3
|
+
|
4
|
+
describe Hactor::HAL::LinkCollection do
|
5
|
+
let(:simple_link_hash) do
|
6
|
+
{
|
7
|
+
'self' => { 'href' => '/' }
|
8
|
+
}
|
9
|
+
end
|
10
|
+
let(:link_class) { mock }
|
11
|
+
let(:flat_collection_class) { mock }
|
12
|
+
let(:flat_collection) { mock }
|
13
|
+
let(:link) { stub }
|
14
|
+
let(:collection) do
|
15
|
+
Hactor::HAL::LinkCollection.new(simple_link_hash,
|
16
|
+
link_class: link_class,
|
17
|
+
flat_collection_class: flat_collection_class)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#find" do
|
21
|
+
it "finds a link with the supplied rel" do
|
22
|
+
flat_collection_class.should_receive(:new)
|
23
|
+
.with(simple_link_hash, item_class: link_class)
|
24
|
+
.and_return(flat_collection)
|
25
|
+
flat_collection.should_receive(:find).and_return(link)
|
26
|
+
|
27
|
+
collection.find_by_rel(:self).should == link
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hactor/hal/resource'
|
3
|
+
|
4
|
+
describe Hactor::HAL::Resource do
|
5
|
+
let(:json) do
|
6
|
+
{
|
7
|
+
'_links' => {
|
8
|
+
'self' => { 'href' => '/' }
|
9
|
+
},
|
10
|
+
'_embedded' => {
|
11
|
+
'widget' => { 'bla' => 'x' }
|
12
|
+
},
|
13
|
+
'foo' => 'bar',
|
14
|
+
'baz' => 123
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:resource) do
|
19
|
+
Hactor::HAL::Resource.new(json,
|
20
|
+
link_collection_class: link_collection_class,
|
21
|
+
embedded_collection_class: embedded_collection_class)
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:link_collection_class) { mock }
|
25
|
+
let(:embedded_collection_class) { mock }
|
26
|
+
let(:link_collection) { mock }
|
27
|
+
let(:embedded_collection) { mock }
|
28
|
+
let(:rel) { stub }
|
29
|
+
|
30
|
+
describe "#properties" do
|
31
|
+
it "returns a Hash containing the json with the reserved properties removed" do
|
32
|
+
resource.properties.should == { 'foo' => 'bar', 'baz' => 123 }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "links" do
|
37
|
+
before :each do
|
38
|
+
link_collection_class.should_receive(:new).with(json['_links']).and_return(link_collection)
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#link" do
|
42
|
+
it "finds a link with the given rel" do
|
43
|
+
link = stub
|
44
|
+
link_collection.should_receive(:find_by_rel).with(rel).and_return(link)
|
45
|
+
resource.link(rel).should == link
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#links" do
|
50
|
+
it "returns a new link set" do
|
51
|
+
resource.links.should == link_collection
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "embedded resources" do
|
57
|
+
before :each do
|
58
|
+
embedded_collection_class.should_receive(:new).with(json['_embedded']).and_return(embedded_collection)
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#embedded_resource" do
|
62
|
+
it "finds an embedded resource with the given rel" do
|
63
|
+
embedded_resource = stub
|
64
|
+
embedded_collection.should_receive(:find).with(rel).and_return(embedded_resource)
|
65
|
+
resource.embedded_resource(rel).should == embedded_resource
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#embedded_resources" do
|
70
|
+
it "returns a new embedded set" do
|
71
|
+
resource.embedded_resources.should == embedded_collection
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hactor/http/client'
|
3
|
+
|
4
|
+
describe Hactor::HTTP::Client do
|
5
|
+
let(:response_class) { mock }
|
6
|
+
let(:backend) { mock }
|
7
|
+
let(:actor) { mock }
|
8
|
+
let(:client) do
|
9
|
+
Hactor::HTTP::Client.new(response_class: response_class,
|
10
|
+
backend: backend)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#get" do
|
14
|
+
context "a valid URL is supplied" do
|
15
|
+
let(:url) { 'http://example.com/' }
|
16
|
+
let(:response) { stub }
|
17
|
+
let(:hactor_response) { stub }
|
18
|
+
|
19
|
+
it "GETs from URL and passes Response object to the actor" do
|
20
|
+
backend.should_receive(:get)
|
21
|
+
.with(url)
|
22
|
+
.and_return(response)
|
23
|
+
response_class.should_receive(:new)
|
24
|
+
.with(response, http_client: client)
|
25
|
+
.and_return(hactor_response)
|
26
|
+
actor.should_receive(:call)
|
27
|
+
.with(hactor_response)
|
28
|
+
|
29
|
+
client.get(url: url, actor: actor)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#follow" do
|
34
|
+
let(:link) { mock }
|
35
|
+
let(:uri) { stub }
|
36
|
+
let(:context_url) { mock }
|
37
|
+
let(:resolved_uri) { stub }
|
38
|
+
|
39
|
+
it "should use the context url to resolve link href" do
|
40
|
+
Delegator.class_eval { include RSpec::Mocks::Methods }
|
41
|
+
client.stub!(:get).with(url: resolved_uri, actor: actor)
|
42
|
+
link.should_receive(:href)
|
43
|
+
.and_return(uri)
|
44
|
+
context_url.should_receive(:merge)
|
45
|
+
.with(uri)
|
46
|
+
.and_return(resolved_uri)
|
47
|
+
|
48
|
+
client.follow(link, context_url: context_url, actor: actor)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hactor/http/response'
|
3
|
+
|
4
|
+
describe Hactor::HTTP::Response do
|
5
|
+
let(:codec) { mock }
|
6
|
+
let(:body) { mock }
|
7
|
+
let(:wrapped_response) { mock }
|
8
|
+
let(:http_client) { mock }
|
9
|
+
|
10
|
+
describe "#follow" do
|
11
|
+
let(:rel) { stub }
|
12
|
+
let(:link) { stub }
|
13
|
+
let(:actor) { stub }
|
14
|
+
let(:context_url) { stub }
|
15
|
+
let(:response) do
|
16
|
+
Hactor::HTTP::Response.new(wrapped_response,
|
17
|
+
http_client: http_client,
|
18
|
+
codec: codec,
|
19
|
+
body: body)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "follows the link with supplied rel" do
|
23
|
+
body.should_receive(:link).with(rel).and_return(link)
|
24
|
+
http_client.should_receive(:follow)
|
25
|
+
.with(link, context_url: context_url, actor: actor)
|
26
|
+
wrapped_response.should_receive(:env)
|
27
|
+
.and_return({ url: context_url })
|
28
|
+
|
29
|
+
response.follow(rel, actor: actor, http_client: http_client)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#body" do
|
34
|
+
let(:body) { mock }
|
35
|
+
let(:sentinel) { stub }
|
36
|
+
let(:response) do
|
37
|
+
Hactor::HTTP::Response.new(wrapped_response,
|
38
|
+
http_client: http_client,
|
39
|
+
codec: codec)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "passes itself into the codec" do
|
43
|
+
wrapped_response.should_receive(:body).and_return(body)
|
44
|
+
codec.should_receive(:new).with(body).and_return(sentinel)
|
45
|
+
|
46
|
+
response.body.should == sentinel
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/hactor_spec.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hactor'
|
3
|
+
|
4
|
+
describe Hactor do
|
5
|
+
describe ".start" do
|
6
|
+
context "a valid url is supplied" do
|
7
|
+
let(:url) { 'http://example.com/' }
|
8
|
+
let(:actor) { mock }
|
9
|
+
let(:http_client) { mock }
|
10
|
+
it "GETs from the URL and passes a response object to an actor" do
|
11
|
+
http_client.should_receive(:get).with(url: url, actor: actor)
|
12
|
+
Hactor.start(url: url, actor: actor, http_client: http_client)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/integration.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hactor'
|
3
|
+
require 'hactor/actor'
|
4
|
+
|
5
|
+
class UserListActor
|
6
|
+
include Hactor::Actor
|
7
|
+
|
8
|
+
def on_200(response)
|
9
|
+
user_links = response.body.links.select { |link| link.rel == 'ht:user' }
|
10
|
+
user_links.each do |link|
|
11
|
+
puts "#{link.title} (#{link.href})"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class LatestPostActor
|
17
|
+
include Hactor::Actor
|
18
|
+
|
19
|
+
def on_200(response)
|
20
|
+
puts response.body.embedded_resources.all.first.links.all
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class HomeActor
|
25
|
+
include Hactor::Actor
|
26
|
+
|
27
|
+
def on_200(response)
|
28
|
+
response.follow 'ht:users', actor: UserListActor.new
|
29
|
+
response.follow 'ht:latest-posts', actor: LatestPostActor.new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe Hactor do
|
34
|
+
it "should work as expected :)" do
|
35
|
+
Hactor.start url: 'http://haltalk.herokuapp.com/', actor: HomeActor.new
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hactor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mike Kelly
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: faraday
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A framework for building hypermedia clients
|
47
|
+
email:
|
48
|
+
- mikekelly321@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rspec
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- hactor.gemspec
|
60
|
+
- lib/hactor.rb
|
61
|
+
- lib/hactor/actor.rb
|
62
|
+
- lib/hactor/hal/document.rb
|
63
|
+
- lib/hactor/hal/embedded_collection.rb
|
64
|
+
- lib/hactor/hal/flat_collection.rb
|
65
|
+
- lib/hactor/hal/link.rb
|
66
|
+
- lib/hactor/hal/link_collection.rb
|
67
|
+
- lib/hactor/hal/resource.rb
|
68
|
+
- lib/hactor/http/client.rb
|
69
|
+
- lib/hactor/http/response.rb
|
70
|
+
- lib/hactor/version.rb
|
71
|
+
- spec/hactor/hal/document_spec.rb
|
72
|
+
- spec/hactor/hal/embedded_collection_spec.rb
|
73
|
+
- spec/hactor/hal/flat_collection_spec.rb
|
74
|
+
- spec/hactor/hal/link_collection_spec.rb
|
75
|
+
- spec/hactor/hal/resource_spec.rb
|
76
|
+
- spec/hactor/http/client_spec.rb
|
77
|
+
- spec/hactor/http/response_spec.rb
|
78
|
+
- spec/hactor_spec.rb
|
79
|
+
- spec/integration.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
homepage: https://github.com/mikekelly/hactor
|
82
|
+
licenses: []
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.24
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: A framework for building hypermedia clients
|
105
|
+
test_files:
|
106
|
+
- spec/hactor/hal/document_spec.rb
|
107
|
+
- spec/hactor/hal/embedded_collection_spec.rb
|
108
|
+
- spec/hactor/hal/flat_collection_spec.rb
|
109
|
+
- spec/hactor/hal/link_collection_spec.rb
|
110
|
+
- spec/hactor/hal/resource_spec.rb
|
111
|
+
- spec/hactor/http/client_spec.rb
|
112
|
+
- spec/hactor/http/response_spec.rb
|
113
|
+
- spec/hactor_spec.rb
|
114
|
+
- spec/integration.rb
|
115
|
+
- spec/spec_helper.rb
|