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 CHANGED
@@ -1,3 +1,7 @@
1
+ 0.5.7
2
+ * fixed bug due to the way RestClient handles custom headers (esp. the Accept HTTP header)
3
+ * added PUT method
4
+
1
5
  0.5.6
2
6
  * requires rest-client 1.4+
3
7
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.6
1
+ 0.5.7
data/lib/restfully.rb CHANGED
@@ -14,7 +14,7 @@ require 'restfully/collection'
14
14
 
15
15
 
16
16
  module Restfully
17
- VERSION = "0.5.6"
17
+ VERSION = "0.5.7"
18
18
  class << self
19
19
  attr_accessor :adapter
20
20
  end
@@ -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
@@ -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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{restfully}
8
- s.version = "0.5.6"
8
+ s.version = "0.5.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Cyril Rohr"]
@@ -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(request.headers).and_return(mock("restclient response", :headers => {:content_type => 'application/json;charset=utf-8'}, :to_s => "", :code => 200))
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
- adapter = Restfully::HTTP::Adapters::RestClientAdapter.new("https://api.grid5000.fr")
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
@@ -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
- resource = Resource.new(@uri, session = mock("session", :get => @response_200, :logger => @logger))
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
- lambda{resource.clusters}.should raise_error(NoMethodError)
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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restfully
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Rohr