restfully 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ 0.4.1
2
+ * added a require 'yaml' in restfully.rb (priteau);
3
+ * added an introductory message when suing the command-line tool;
4
+ * removed [root_path] from the list of options in command-line client 'Usage' banner (priteau);
5
+ * added test for :configuration_file option;
6
+ * tests are no longer failing on ruby1.9;
7
+
1
8
  0.4.0
2
9
  * removed 'root_path' option: 'base_uri' must point to the starting resource;
3
10
  * objects can now be pretty printed using 'pp object';
data/README.rdoc CHANGED
@@ -19,19 +19,19 @@ If the connection was successful, you should get a prompt. You may enter
19
19
  irb(main):001:0> pp root
20
20
 
21
21
  to get back a pretty-printed output of the root resource:
22
- #<Restfully::Resource:0x91f08c
23
- @uri=#<URI::HTTP:0x123e30c URL:http://api.local/sid/grid5000>
24
- LINKS
25
- @environments=#<Restfully::Collection:0x917666>,
26
- @sites=#<Restfully::Collection:0x9170d0>,
27
- @version=#<Restfully::Resource:0x91852a>,
28
- @versions=#<Restfully::Collection:0x917e68>
29
- PROPERTIES
30
- "uid"=>"grid5000",
31
- "type"=>"grid",
32
- "version"=>"4fe96b25d2cbfee16abe5a4fb999c82dbafc2ee8">
33
-
34
- You can see the LINKS and PROPERTIES headers that respectively indicate what links you can follow from there (by calling +root.link_name+) and what properties are available (by calling +root[property_name]+).
22
+ #<Restfully::Resource:0x91f08c
23
+ @uri=#<URI::HTTP:0x123e30c URL:http://api.local/sid/grid5000>
24
+ LINKS
25
+ @environments=#<Restfully::Collection:0x917666>,
26
+ @sites=#<Restfully::Collection:0x9170d0>,
27
+ @version=#<Restfully::Resource:0x91852a>,
28
+ @versions=#<Restfully::Collection:0x917e68>
29
+ PROPERTIES
30
+ "uid"=>"grid5000",
31
+ "type"=>"grid",
32
+ "version"=>"4fe96b25d2cbfee16abe5a4fb999c82dbafc2ee8">
33
+
34
+ You can see the LINKS and PROPERTIES headers that respectively indicate what links you can follow from there (by calling <tt>root.link_name</tt>) and what properties are available (by calling <tt>root[property_name]</tt>).
35
35
 
36
36
  Let's say you want to access the collection of +sites+, you would enter:
37
37
  irb(main):002:0> pp root.sites
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
data/bin/restfully CHANGED
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
  # The command line Restfully client
3
3
 
4
- $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib' unless $LOAD_PATH.include? File.dirname(__FILE__) + '/../lib'
4
+ lib_dir = File.expand_path(File.dirname(__FILE__) + '/../lib')
5
+ $LOAD_PATH.unshift lib_dir unless $LOAD_PATH.include? lib_dir
5
6
 
6
7
  require 'restfully'
7
8
  require 'optparse'
8
9
  require 'logger'
9
- require 'yaml'
10
10
  require 'pp'
11
11
 
12
12
 
@@ -18,7 +18,7 @@ option_parser = OptionParser.new do |opts|
18
18
  * Description
19
19
  Restfully #{Restfully::VERSION} - Access REST APIs effortlessly
20
20
  * Usage
21
- restfully [base_uri] [root_path] [options]
21
+ restfully [base_uri] [options]
22
22
  * Options
23
23
  BANNER
24
24
 
@@ -59,6 +59,8 @@ def root
59
59
  @root ||= session.root.load
60
60
  end
61
61
 
62
+ puts "Restfully/#{Restfully::VERSION} - The root resource is available in the 'root' variable."
63
+
62
64
  require 'irb'
63
65
  require 'irb/completion'
64
66
  ARGV.clear
data/lib/restfully.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'backports'
2
+ require 'yaml'
2
3
  require 'restfully/error'
3
4
  require 'restfully/parsing'
4
5
  require 'restfully/http'
@@ -13,7 +14,7 @@ require 'restfully/collection'
13
14
 
14
15
 
15
16
  module Restfully
16
- VERSION = "0.4.0"
17
+ VERSION = "0.4.1"
17
18
  class << self
18
19
  attr_accessor :adapter
19
20
  end
@@ -1,13 +1,21 @@
1
1
  module Restfully
2
2
  module HTTP
3
3
  class Error < Restfully::Error
4
+ STATUS_CODES = {
5
+ 400 => "Bad Request",
6
+ 401 => "Authorization Required",
7
+ 403 => "Forbiden",
8
+ 406 => "Not Acceptable"
9
+ }
10
+
4
11
  attr_reader :response
5
12
  def initialize(response)
6
13
  @response = response
7
- if response.body.kind_of?(Hash)
8
- message = "#{response.status} #{response.body['title']}. #{response.body['message']}"
14
+ response_body = response.body rescue response.raw_body
15
+ if response_body.kind_of?(Hash)
16
+ message = "#{response.status} #{response_body['title']}. #{response_body['message']}"
9
17
  else
10
- message = response.body
18
+ message = "#{response.status} #{STATUS_CODES[response.status] || (response_body[0..100]+"...")}"
11
19
  end
12
20
  super(message)
13
21
  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.4.0"
8
+ s.version = "0.4.1"
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"]
@@ -49,6 +49,7 @@ Gem::Specification.new do |s|
49
49
  "lib/restfully/special_hash.rb",
50
50
  "restfully.gemspec",
51
51
  "spec/collection_spec.rb",
52
+ "spec/fixtures/configuration_file.yml",
52
53
  "spec/fixtures/grid5000-sites.json",
53
54
  "spec/http/error_spec.rb",
54
55
  "spec/http/headers_spec.rb",
@@ -32,7 +32,7 @@ describe Collection do
32
32
  it "should not load if already loaded and no :reload" do
33
33
  collection = Collection.new(@uri, mock("session"))
34
34
  options = {:headers => {'key' => 'value'}}
35
- collection.should_receive(:executed_requests).and_return({'GET' => {'options' => options, 'body' => {"key", "value"}}})
35
+ collection.should_receive(:executed_requests).and_return({'GET' => {'options' => options, 'body' => {"key" => "value"}}})
36
36
  collection.load(options.merge(:reload => false)).should == collection
37
37
  end
38
38
  it "should load when :reload param is true [already loaded]" do
@@ -0,0 +1,4 @@
1
+ base_uri: http://somewhere.net/x/y/z
2
+ username: u
3
+ password: p
4
+ verbose: false
data/spec/session_spec.rb CHANGED
@@ -4,7 +4,7 @@ restfully_version = File.read(File.dirname(__FILE__)+'/../VERSION').strip
4
4
  include Restfully
5
5
  describe Session do
6
6
  before do
7
- @session = Session.new(:base_uri => 'https://api.grid5000.fr/sid/', :root_path => '/grid5000', :user => 'crohr', :password => 'password', :default_headers => {})
7
+ @session = Session.new(:base_uri => 'https://api.grid5000.fr/sid/', :user => 'crohr', :password => 'password', :default_headers => {})
8
8
  @request = mock("restfully http request", :uri => mock("uri"), :headers => mock("headers"), :body => nil)
9
9
  @response = mock("restfully http response", :status => 200, :headers => mock("headers"))
10
10
  end
@@ -66,15 +66,23 @@ describe Session do
66
66
  it "should yield the loaded root resource and the session object" do
67
67
  Restfully::Resource.stub!(:new).and_return(root_resource = mock(Restfully::Resource))
68
68
  root_resource.should_receive(:load).and_return(root_resource)
69
- Session.new(:base_uri => 'https://api.grid5000.fr', :root_path => '/grid5000', :user => 'crohr', :password => 'password') do |root, session|
69
+ Session.new(:base_uri => 'https://api.grid5000.fr', :user => 'crohr', :password => 'password') do |root, session|
70
70
  session.root.should == root
71
71
  root.should == root_resource
72
72
  end
73
73
  end
74
+
75
+ it "should use the options defined in the given configuration file" do
76
+ config_file =
77
+ session = Session.new(:base_uri => 'https://api.grid5000.fr', :username => 'x', :password => 'y', :verbose => true, :configuration_file => (File.dirname(__FILE__)+'/fixtures/configuration_file.yml'))
78
+ session.base_uri.should == URI.parse('http://somewhere.net/x/y/z')
79
+ session.logger.should_not == Logger::DEBUG
80
+ end
74
81
  end
75
82
 
76
83
  describe "Transmitting requests" do
77
84
  before do
85
+ Session.send(:public, :transmit)
78
86
  @request.should_receive(:add_headers).with("User-Agent"=>"Restfully/#{restfully_version}", "Accept"=>"application/json")
79
87
  end
80
88
  it "should send a head" do
@@ -106,7 +114,10 @@ describe Session do
106
114
  end
107
115
  end
108
116
 
109
- describe "Dealing with errors" do
117
+ describe "Dealing with errors" do
118
+ before do
119
+ Session.send(:public, :deal_with_eventual_errors)
120
+ end
110
121
  it "should raise a Restfully::HTTP::ClientError error on 4xx errors" do
111
122
  response = mock("404 response", :status => 404, :body => {'code' => 404, 'message' => 'The requested resource cannot be found.', 'title' => 'Not Found'}, :headers => mock("headers"))
112
123
  lambda{ @session.send(:deal_with_eventual_errors, response, @request) }.should raise_error(Restfully::HTTP::ClientError, "404 Not Found. The requested resource cannot be found.")
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.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Rohr
@@ -72,6 +72,7 @@ files:
72
72
  - lib/restfully/special_hash.rb
73
73
  - restfully.gemspec
74
74
  - spec/collection_spec.rb
75
+ - spec/fixtures/configuration_file.yml
75
76
  - spec/fixtures/grid5000-sites.json
76
77
  - spec/http/error_spec.rb
77
78
  - spec/http/headers_spec.rb