restfully 0.6.3 → 0.7.0.pre
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.
- data/README.md +166 -0
- data/Rakefile +35 -35
- data/bin/restfully +68 -10
- data/lib/restfully.rb +8 -14
- data/lib/restfully/collection.rb +70 -90
- data/lib/restfully/error.rb +2 -0
- data/lib/restfully/http.rb +3 -3
- data/lib/restfully/http/error.rb +1 -20
- data/lib/restfully/http/helper.rb +49 -0
- data/lib/restfully/http/request.rb +60 -24
- data/lib/restfully/http/response.rb +55 -24
- data/lib/restfully/link.rb +32 -24
- data/lib/restfully/media_type.rb +70 -0
- data/lib/restfully/media_type/abstract_media_type.rb +162 -0
- data/lib/restfully/media_type/application_json.rb +21 -0
- data/lib/restfully/media_type/application_vnd_bonfire_xml.rb +177 -0
- data/lib/restfully/media_type/application_x_www_form_urlencoded.rb +33 -0
- data/lib/restfully/media_type/grid5000.rb +67 -0
- data/lib/restfully/media_type/wildcard.rb +27 -0
- data/lib/restfully/rack.rb +1 -0
- data/lib/restfully/rack/basic_auth.rb +26 -0
- data/lib/restfully/resource.rb +134 -197
- data/lib/restfully/session.rb +127 -70
- data/lib/restfully/version.rb +3 -0
- data/spec/fixtures/bonfire-collection-with-fragments.xml +6 -0
- data/spec/fixtures/bonfire-compute-existing.xml +43 -0
- data/spec/fixtures/bonfire-empty-collection.xml +4 -0
- data/spec/fixtures/bonfire-experiment-collection.xml +51 -0
- data/spec/fixtures/bonfire-network-collection.xml +35 -0
- data/spec/fixtures/bonfire-network-existing.xml +6 -0
- data/spec/fixtures/bonfire-root.xml +5 -0
- data/spec/fixtures/grid5000-rennes-jobs.json +988 -146
- data/spec/fixtures/grid5000-rennes.json +63 -0
- data/spec/restfully/collection_spec.rb +87 -0
- data/spec/restfully/http/helper_spec.rb +18 -0
- data/spec/restfully/http/request_spec.rb +97 -0
- data/spec/restfully/http/response_spec.rb +53 -0
- data/spec/restfully/link_spec.rb +80 -0
- data/spec/restfully/media_type/application_vnd_bonfire_xml_spec.rb +153 -0
- data/spec/restfully/media_type_spec.rb +117 -0
- data/spec/restfully/resource_spec.rb +109 -0
- data/spec/restfully/session_spec.rb +229 -0
- data/spec/spec_helper.rb +10 -9
- metadata +162 -83
- data/.document +0 -5
- data/CHANGELOG +0 -62
- data/README.rdoc +0 -146
- data/TODO.rdoc +0 -3
- data/VERSION +0 -1
- data/examples/grid5000.rb +0 -33
- data/examples/scratch.rb +0 -37
- data/lib/restfully/extensions.rb +0 -34
- data/lib/restfully/http/adapters/abstract_adapter.rb +0 -29
- data/lib/restfully/http/adapters/patron_adapter.rb +0 -16
- data/lib/restfully/http/adapters/rest_client_adapter.rb +0 -75
- data/lib/restfully/http/headers.rb +0 -20
- data/lib/restfully/parsing.rb +0 -66
- data/lib/restfully/special_array.rb +0 -5
- data/lib/restfully/special_hash.rb +0 -5
- data/restfully.gemspec +0 -114
- data/spec/collection_spec.rb +0 -120
- data/spec/fixtures/configuration_file.yml +0 -4
- data/spec/fixtures/grid5000-sites.json +0 -540
- data/spec/http/error_spec.rb +0 -18
- data/spec/http/headers_spec.rb +0 -17
- data/spec/http/request_spec.rb +0 -49
- data/spec/http/response_spec.rb +0 -19
- data/spec/http/rest_client_adapter_spec.rb +0 -35
- data/spec/link_spec.rb +0 -61
- data/spec/parsing_spec.rb +0 -40
- data/spec/resource_spec.rb +0 -320
- data/spec/restfully_spec.rb +0 -16
- data/spec/session_spec.rb +0 -171
data/spec/restfully_spec.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__)+'/spec_helper')
|
2
|
-
|
3
|
-
describe Restfully do
|
4
|
-
after do
|
5
|
-
Restfully.adapter = Restfully::HTTP::Adapters::RestClientAdapter
|
6
|
-
end
|
7
|
-
it "should have a default adapter" do
|
8
|
-
Restfully.adapter.should == Restfully::HTTP::Adapters::RestClientAdapter
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should allow setting other adapters" do
|
12
|
-
require 'restfully/http/adapters/patron_adapter'
|
13
|
-
Restfully.adapter = Restfully::HTTP::Adapters::PatronAdapter
|
14
|
-
Restfully.adapter.should == Restfully::HTTP::Adapters::PatronAdapter
|
15
|
-
end
|
16
|
-
end
|
data/spec/session_spec.rb
DELETED
@@ -1,171 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__)+'/spec_helper')
|
2
|
-
restfully_version = File.read(File.dirname(__FILE__)+'/../VERSION').strip
|
3
|
-
|
4
|
-
include Restfully
|
5
|
-
describe Session do
|
6
|
-
before do
|
7
|
-
@session = Session.new(:base_uri => 'https://api.grid5000.fr/sid/', :user => 'crohr', :password => 'password', :default_headers => {})
|
8
|
-
@request = mock("restfully http request", :uri => mock("uri"), :headers => mock("headers"), :body => nil)
|
9
|
-
@response = mock("restfully http response", :status => 200, :headers => mock("headers"))
|
10
|
-
end
|
11
|
-
describe "initialization" do
|
12
|
-
it "should have a logger reader" do
|
13
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr')
|
14
|
-
session.should_not respond_to(:logger=)
|
15
|
-
session.should respond_to(:logger)
|
16
|
-
end
|
17
|
-
it "should have a base_uri reader" do
|
18
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr')
|
19
|
-
session.should_not respond_to(:base_uri=)
|
20
|
-
session.base_uri.should == URI.parse('https://api.grid5000.fr')
|
21
|
-
end
|
22
|
-
it "should raise an ArgumentError if the base_uri is not a valid URI" do
|
23
|
-
lambda{Session.new(:base_uri => '/home/crohr/whatever')}.should raise_error(ArgumentError, /\/home\/crohr\/whatever is not a valid URI/)
|
24
|
-
end
|
25
|
-
it "should log to NullLogger by default" do
|
26
|
-
NullLogger.should_receive(:new).and_return(logger = mock(NullLogger, :debug => Proc.new{}, :level => Logger::WARN))
|
27
|
-
logger.should_receive(:level=).with(Logger::WARN)
|
28
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr')
|
29
|
-
session.logger.should == logger
|
30
|
-
end
|
31
|
-
it "should use the given logger" do
|
32
|
-
logger = mock("custom logger", :debug => Proc.new{})
|
33
|
-
logger.should_receive(:level=).with(Logger::DEBUG)
|
34
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr', 'logger' => logger, :verbose => true)
|
35
|
-
session.logger.should == logger
|
36
|
-
end
|
37
|
-
it "should set a default Accept HTTP header if no default headers are given" do
|
38
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr')
|
39
|
-
session.default_headers.should == {
|
40
|
-
"User-Agent"=>"Restfully/#{restfully_version}",
|
41
|
-
'Accept' => 'application/json'
|
42
|
-
}
|
43
|
-
end
|
44
|
-
it "should use the given default headers" do
|
45
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr', :default_headers => {:accept => 'application/xml'})
|
46
|
-
session.default_headers.should == {
|
47
|
-
"User-Agent"=>"Restfully/#{restfully_version}",
|
48
|
-
"Accept" => 'application/xml'
|
49
|
-
}
|
50
|
-
end
|
51
|
-
it "should correctly initialize the connection" do
|
52
|
-
mock_logger = mock("logger", :debug => Proc.new{}, :level => Logger::DEBUG)
|
53
|
-
mock_logger.stub!(:level=)
|
54
|
-
Restfully.adapter.should_receive(:new).with('https://api.grid5000.fr/sid', :user => 'crohr', :password => 'password', :logger => mock_logger).and_return(connection = mock("restfully connection"))
|
55
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr/sid', :user => 'crohr', :password => 'password', :logger => mock_logger)
|
56
|
-
session.connection.should == connection
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should initialize the root resource" do
|
60
|
-
stub_request(:get, "https://api.grid5000.fr/grid5000").to_return(
|
61
|
-
:status => 200,
|
62
|
-
:body => {"property1" => "value1"}.to_json,
|
63
|
-
:headers => {'Content-Type' => 'text/plain', 'Content-Length' => 4}
|
64
|
-
)
|
65
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr/grid5000', :user => 'crohr', :password => 'password')
|
66
|
-
session.root.should be_a Restfully::Resource
|
67
|
-
session.root.uri.to_s.should == 'https://api.grid5000.fr/grid5000'
|
68
|
-
session.root.uri.path.should == '/grid5000'
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should yield the loaded root resource and the session object" do
|
72
|
-
Restfully::Resource.stub!(:new).and_return(root_resource = mock(Restfully::Resource))
|
73
|
-
root_resource.should_receive(:load).at_least(1).and_return(root_resource)
|
74
|
-
Session.new(:base_uri => 'https://api.grid5000.fr', :user => 'crohr', :password => 'password') do |root, session|
|
75
|
-
session.root.should == root
|
76
|
-
root.should == root_resource
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should use the options defined in the given configuration file" do
|
81
|
-
config_file =
|
82
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr', :username => 'x', :password => 'y', :verbose => true, :configuration_file => (File.dirname(__FILE__)+'/fixtures/configuration_file.yml'))
|
83
|
-
session.base_uri.should == URI.parse('http://somewhere.net/x/y/z')
|
84
|
-
session.logger.should_not == Logger::DEBUG
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
describe "Transmitting requests" do
|
89
|
-
before do
|
90
|
-
Session.send(:public, :transmit)
|
91
|
-
@request.headers.stub!(:[]).and_return(nil)
|
92
|
-
@request.headers.should_receive(:[]=).with("User-Agent", "Restfully/#{restfully_version}")
|
93
|
-
@request.headers.should_receive(:[]=).with("Accept", "application/json")
|
94
|
-
end
|
95
|
-
it "should send a head" do
|
96
|
-
@session.connection.should_receive(:head).with(@request).and_return(@response)
|
97
|
-
@session.should_receive(:deal_with_eventual_errors).with(@response, @request).and_return(@response)
|
98
|
-
@session.send(:transmit, :head, @request).should == @response
|
99
|
-
end
|
100
|
-
it "should send a get" do
|
101
|
-
@session.connection.should_receive(:get).with(@request).and_return(@response)
|
102
|
-
@session.should_receive(:deal_with_eventual_errors).with(@response, @request).and_return(@response)
|
103
|
-
@session.send(:transmit, :get, @request).should == @response
|
104
|
-
end
|
105
|
-
it "should send a post" do
|
106
|
-
@session.connection.should_receive(:post).with(@request).and_return(@response)
|
107
|
-
@session.should_receive(:deal_with_eventual_errors).with(@response, @request).and_return(@response)
|
108
|
-
@session.send(:transmit, :post, @request).should == @response
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
describe "URI construction" do
|
113
|
-
it "should not use the base_uri if the given path is a complete URI" do
|
114
|
-
@session.uri_for('http://somehost.com/some/path').should == URI.parse('http://somehost.com/some/path')
|
115
|
-
end
|
116
|
-
it "should combine the base_uri with the given path (absolute) if it is not a complete URI" do
|
117
|
-
@session.uri_for('/some/path').should == URI.parse('https://api.grid5000.fr/some/path')
|
118
|
-
end
|
119
|
-
it "should combine the base_uri with the given path (relative) if it is not a complete URI" do
|
120
|
-
@session.uri_for('some/path').should == URI.parse('https://api.grid5000.fr/sid/some/path')
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
describe "Dealing with errors" do
|
125
|
-
before do
|
126
|
-
Session.send(:public, :deal_with_eventual_errors)
|
127
|
-
end
|
128
|
-
it "should raise a Restfully::HTTP::ClientError error on 4xx errors" do
|
129
|
-
response = mock("404 response", :status => 404, :body => {'code' => 404, 'message' => 'The requested resource cannot be found.', 'title' => 'Not Found'}, :headers => mock("headers"))
|
130
|
-
lambda{ @session.send(:deal_with_eventual_errors, response, @request) }.should raise_error(Restfully::HTTP::ClientError, "404 Not Found. The requested resource cannot be found.")
|
131
|
-
end
|
132
|
-
|
133
|
-
it "should raise a Restfully::HTTP::ServerError error on 5xx errors" do
|
134
|
-
response = mock("500 response", :status => 500, :body => {'code' => 500, 'message' => 'Something went wrong.', 'title' => 'Internal Server Error'}, :headers => mock("headers"))
|
135
|
-
lambda{ @session.send(:deal_with_eventual_errors, response, @request) }.should raise_error(Restfully::HTTP::ServerError, "500 Internal Server Error. Something went wrong.")
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
describe "HEADing resources" do
|
140
|
-
it "should create a new Request object and transmit it" do
|
141
|
-
Restfully::HTTP::Request.should_receive(:new).with(URI.parse('https://api.grid5000.fr/sid/some/path'), :headers => nil, :query => nil).and_return(@request)
|
142
|
-
@session.should_receive(:transmit).with(:head, @request)
|
143
|
-
@session.head('some/path')
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
describe "GETting resources" do
|
148
|
-
it "should create a new Request object and transmit it" do
|
149
|
-
Restfully::HTTP::Request.should_receive(:new).with(URI.parse('https://api.grid5000.fr/sid/some/path'), :headers => {:cache_control => 'max-age=0'}, :query => nil).and_return(@request)
|
150
|
-
@session.should_receive(:transmit).with(:get, @request)
|
151
|
-
@session.get('some/path', 'headers' => {:cache_control => 'max-age=0'})
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
describe "POSTing payload" do
|
156
|
-
it "should create a new Request object with a body, and transmit it" do
|
157
|
-
Restfully::HTTP::Request.should_receive(:new).with(URI.parse('https://api.grid5000.fr/sid/some/path'), :body => '{"a":"b"}', :headers => {:content_type => 'application/json'}, :query => nil).and_return(@request)
|
158
|
-
@session.should_receive(:transmit).with(:post, @request)
|
159
|
-
@session.post('some/path', {"a" => "b"}.to_json, 'headers' => {:content_type => 'application/json'})
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
describe "DELETEing resources" do
|
164
|
-
it "should create a new Request object and transmit it" do
|
165
|
-
Restfully::HTTP::Request.should_receive(:new).with(URI.parse('https://api.grid5000.fr/sid/some/path'), :headers => {:accept => 'application/json'}, :query => nil).and_return(@request)
|
166
|
-
@session.should_receive(:transmit).with(:delete, @request)
|
167
|
-
@session.delete('some/path', 'headers' => {:accept => 'application/json'})
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
end
|