restfully 0.5.6 → 0.5.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/VERSION +1 -1
- data/lib/restfully.rb +1 -1
- data/lib/restfully/http/adapters/rest_client_adapter.rb +20 -5
- data/lib/restfully/session.rb +8 -3
- data/restfully.gemspec +1 -1
- data/spec/http/rest_client_adapter_spec.rb +3 -2
- data/spec/resource_spec.rb +21 -2
- data/spec/restfully_spec.rb +3 -0
- metadata +1 -1
data/CHANGELOG
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.7
|
data/lib/restfully.rb
CHANGED
@@ -9,30 +9,36 @@ module Restfully
|
|
9
9
|
def initialize(base_uri, options = {})
|
10
10
|
super(base_uri, options)
|
11
11
|
@options[:user] = @options.delete(:username)
|
12
|
+
RestClient.log = logger
|
12
13
|
end # def initialize
|
13
14
|
|
14
15
|
def head(request)
|
15
16
|
in_order_to_get_the_response_to(request) do |resource|
|
16
|
-
resource.head(request.headers)
|
17
|
+
resource.head(convert_header_keys_into_symbols(request.headers))
|
17
18
|
end
|
18
19
|
end # def head
|
19
20
|
|
20
21
|
def get(request)
|
21
22
|
in_order_to_get_the_response_to(request) do |resource|
|
22
|
-
resource.get(request.headers)
|
23
|
+
resource.get(convert_header_keys_into_symbols(request.headers))
|
23
24
|
end
|
24
25
|
end # def get
|
25
26
|
|
26
|
-
|
27
27
|
def delete(request)
|
28
28
|
in_order_to_get_the_response_to(request) do |resource|
|
29
|
-
resource.delete(request.headers)
|
29
|
+
resource.delete(convert_header_keys_into_symbols(request.headers))
|
30
30
|
end
|
31
31
|
end # def delete
|
32
32
|
|
33
|
+
def put(request)
|
34
|
+
in_order_to_get_the_response_to(request) do |resource|
|
35
|
+
resource.put(request.raw_body, convert_header_keys_into_symbols(request.headers))
|
36
|
+
end
|
37
|
+
end # def put
|
38
|
+
|
33
39
|
def post(request)
|
34
40
|
in_order_to_get_the_response_to(request) do |resource|
|
35
|
-
resource.post(request.raw_body, request.headers)
|
41
|
+
resource.post(request.raw_body, convert_header_keys_into_symbols(request.headers))
|
36
42
|
end
|
37
43
|
end # def post
|
38
44
|
|
@@ -53,6 +59,15 @@ module Restfully
|
|
53
59
|
Response.new(status, headers, body)
|
54
60
|
end # def in_order_to_get_the_response_to
|
55
61
|
|
62
|
+
# there is a bug in RestClient, when passing headers whose keys are string that are already defined as default headers, they get overwritten.
|
63
|
+
def convert_header_keys_into_symbols(headers)
|
64
|
+
headers.inject({}) do |final, (key,value)|
|
65
|
+
key = key.to_s.gsub(/-/, "_").downcase.to_sym
|
66
|
+
final[key] = value
|
67
|
+
final
|
68
|
+
end
|
69
|
+
end # def convert_header_keys_into_symbols
|
70
|
+
|
56
71
|
end
|
57
72
|
|
58
73
|
end
|
data/lib/restfully/session.rb
CHANGED
@@ -29,6 +29,7 @@ module Restfully
|
|
29
29
|
yield @root.load, self if block_given?
|
30
30
|
end
|
31
31
|
|
32
|
+
# returns the root resource
|
32
33
|
def root
|
33
34
|
@root.load
|
34
35
|
end
|
@@ -52,6 +53,13 @@ module Restfully
|
|
52
53
|
transmit :post, HTTP::Request.new(uri_for(path), :body => body, :headers => options.delete(:headers), :query => options.delete(:query))
|
53
54
|
end
|
54
55
|
|
56
|
+
# returns an HTTP::Response object or raise a Restfully::HTTP::Error
|
57
|
+
def put(path, body, options = {})
|
58
|
+
options = options.symbolize_keys
|
59
|
+
uri = uri_for(path)
|
60
|
+
transmit :put, HTTP::Request.new(uri_for(path), :body => body, :headers => options.delete(:headers), :query => options.delete(:query))
|
61
|
+
end
|
62
|
+
|
55
63
|
# returns an HTTP::Response object or raise a Restfully::HTTP::Error
|
56
64
|
def delete(path, options = {})
|
57
65
|
options = options.symbolize_keys
|
@@ -68,9 +76,6 @@ module Restfully
|
|
68
76
|
default_headers.each do |header, value|
|
69
77
|
request.headers[header] ||= value
|
70
78
|
end
|
71
|
-
logger.info "#{method.to_s.upcase} #{request.uri}" +
|
72
|
-
"\nHeaders: #{request.headers.inspect}" +
|
73
|
-
"#{request.body ? "\nBody: #{request.body.length} bytes" : ""}"
|
74
79
|
response = connection.send(method.to_sym, request)
|
75
80
|
response = deal_with_eventual_errors(response, request)
|
76
81
|
end
|
data/restfully.gemspec
CHANGED
@@ -4,7 +4,7 @@ describe Restfully::HTTP::Adapters::RestClientAdapter do
|
|
4
4
|
adapter = Restfully::HTTP::Adapters::RestClientAdapter.new("https://api.grid5000.fr", :username => 'crohr', :password => 'password')
|
5
5
|
request = Restfully::HTTP::Request.new('http://api.local/sid/grid5000', :headers => {:accept => 'application/json'}, :query => {:q1 => 'v1'})
|
6
6
|
RestClient::Resource.should_receive(:new).with('http://api.local/sid/grid5000?q1=v1', :password => 'password', :user => 'crohr').and_return(resource = mock("restclient resource"))
|
7
|
-
resource.should_receive(:get).with(
|
7
|
+
resource.should_receive(:get).with({:accept => 'application/json'}).and_return(mock("restclient response", :headers => {:content_type => 'application/json;charset=utf-8'}, :to_s => "", :code => 200))
|
8
8
|
response = adapter.get(request)
|
9
9
|
response.status.should == 200
|
10
10
|
response.body.should be_nil
|
@@ -18,7 +18,8 @@ describe Restfully::HTTP::Adapters::RestClientAdapter do
|
|
18
18
|
adapter.options[:username].should be_nil
|
19
19
|
end
|
20
20
|
it "should raise a not implemented error when trying to use functions not implemented yet" do
|
21
|
-
|
21
|
+
require 'restfully/http/adapters/patron_adapter'
|
22
|
+
adapter = Restfully::HTTP::Adapters::PatronAdapter.new("https://api.grid5000.fr")
|
22
23
|
lambda{adapter.put(mock("restfully request"))}.should raise_error NotImplementedError, "PUT is not supported by your adapter."
|
23
24
|
end
|
24
25
|
it "should rescue any RestClient::Exception and correctly populate the response" do
|
data/spec/resource_spec.rb
CHANGED
@@ -32,6 +32,7 @@ describe Resource do
|
|
32
32
|
|
33
33
|
describe "loading" do
|
34
34
|
before do
|
35
|
+
@session = Restfully::Session.new(:base_uri => "http://api.local")
|
35
36
|
@raw = {
|
36
37
|
'links' => [
|
37
38
|
{'rel' => 'self', 'href' => '/grid5000/sites/rennes'},
|
@@ -100,7 +101,12 @@ describe Resource do
|
|
100
101
|
resource.load(:reload => true)
|
101
102
|
end
|
102
103
|
it "should correctly define the functions to access simple values" do
|
103
|
-
|
104
|
+
stub_request(:get, @uri.to_s).to_return(
|
105
|
+
:status => 200,
|
106
|
+
:body => @raw.to_json,
|
107
|
+
:headers => {'Content-Type' => 'application/json', 'Content-Length' => @raw.length}
|
108
|
+
)
|
109
|
+
resource = Resource.new(@uri, @session)
|
104
110
|
resource.stub!(:define_link) # do not define links
|
105
111
|
resource.load
|
106
112
|
resource['whatever'].should == 'whatever'
|
@@ -108,7 +114,20 @@ describe Resource do
|
|
108
114
|
resource["uid"].should == 'rennes'
|
109
115
|
resource['an_array'].should be_a(SpecialArray)
|
110
116
|
resource['an_array'].should == [1,2,3]
|
111
|
-
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should correctly send custom headers" do
|
120
|
+
stub_request(:get, @uri.to_s).with(:headers => {
|
121
|
+
'User-Agent'=>"Restfully/#{Restfully::VERSION}",
|
122
|
+
'Accept-Encoding'=>'gzip, deflate',
|
123
|
+
'Accept'=>'application/json'
|
124
|
+
}).to_return(
|
125
|
+
:status => 200,
|
126
|
+
:body => @raw.to_json,
|
127
|
+
:headers => {'Content-Type' => 'application/json', 'Content-Length' => @raw.length}
|
128
|
+
)
|
129
|
+
resource = Resource.new(@uri, @session)
|
130
|
+
resource.load(:headers => {:accept => 'application/json'})
|
112
131
|
end
|
113
132
|
|
114
133
|
it "should correctly define a collection link" do
|
data/spec/restfully_spec.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__)+'/spec_helper')
|
2
2
|
|
3
3
|
describe Restfully do
|
4
|
+
after do
|
5
|
+
Restfully.adapter = Restfully::HTTP::Adapters::RestClientAdapter
|
6
|
+
end
|
4
7
|
it "should have a default adapter" do
|
5
8
|
Restfully.adapter.should == Restfully::HTTP::Adapters::RestClientAdapter
|
6
9
|
end
|