restfully 0.5.3 → 0.5.4

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.3
1
+ 0.5.4
data/bin/restfully CHANGED
@@ -56,7 +56,7 @@ def session
56
56
  end
57
57
 
58
58
  def root
59
- @root ||= session.root.load
59
+ @root ||= session.root
60
60
  end
61
61
 
62
62
  puts "Restfully/#{Restfully::VERSION} - The root resource is available in the 'root' variable."
@@ -15,7 +15,7 @@ module Restfully
15
15
  in_order_to_get_the_response_to(request) do |resource|
16
16
  resource.head(request.headers)
17
17
  end
18
- end # def get
18
+ end # def head
19
19
 
20
20
  def get(request)
21
21
  in_order_to_get_the_response_to(request) do |resource|
@@ -1,32 +1,64 @@
1
- require 'json'
1
+
2
2
 
3
3
  module Restfully
4
4
 
5
5
  module Parsing
6
-
6
+
7
7
  class ParserNotFound < Restfully::Error; end
8
+
9
+ PARSERS = [
10
+ {
11
+ :supported_types => [/^application\/.*?json/i],
12
+ :parse => lambda{|object, options|
13
+ require 'json'
14
+ JSON.parse(object)
15
+ },
16
+ :dump => lambda{|object, options|
17
+ require 'json'
18
+ JSON.dump(object)
19
+ },
20
+ :object => true
21
+ },
22
+ {
23
+ :supported_types => [/^text\/.*?(plain|html)/i],
24
+ :parse => lambda{|object, options| object},
25
+ :dump => lambda{|object, options| object}
26
+ },
27
+ { # just store the binary data in a 'raw' property
28
+ :supported_types => ["application/zip"],
29
+ :parse => lambda{|object, options| {'raw' => object}},
30
+ :dump => lambda{|object, options| object['raw']}
31
+ }
32
+ ]
33
+
8
34
  def unserialize(object, options = {})
9
35
  content_type = options[:content_type]
10
36
  content_type ||= object.headers['Content-Type'] if object.respond_to?(:headers)
11
- case content_type
12
- when /^application\/.*?json/i
13
- JSON.parse(object)
14
- when /^text\/.*?(plain|html)/i
15
- object.to_s
37
+ parser = select_parser_for(content_type)
38
+ if parser
39
+ parser[:parse].call(object, options)
16
40
  else
17
- raise ParserNotFound.new("Content-Type '#{content_type}' is not supported. Cannot parse the given object.")
41
+ raise ParserNotFound.new("Cannot find a parser to parse '#{content_type}' content.")
18
42
  end
19
43
  end
20
44
 
21
45
  def serialize(object, options = {})
22
46
  content_type = options[:content_type]
23
47
  content_type ||= object.headers['Content-Type'] if object.respond_to?(:headers)
24
- case content_type
25
- when /^application\/.*?json/i
26
- JSON.dump(object)
48
+ parser = select_parser_for(content_type)
49
+ if parser
50
+ parser[:dump].call(object, options)
27
51
  else
28
- raise ParserNotFound, [object, content_type]
52
+ raise ParserNotFound.new("Cannot find a parser to dump object into '#{content_type}' content.")
53
+ end
54
+ end
55
+
56
+ def select_parser_for(content_type)
57
+ content_type.split(",").each do |type|
58
+ parser = PARSERS.find{|parser| parser[:supported_types].find{|supported_type| type =~ (supported_type.kind_of?(String) ? Regexp.new(supported_type) : supported_type) }}
59
+ return parser unless parser.nil?
29
60
  end
61
+ nil
30
62
  end
31
63
 
32
64
  end
@@ -10,9 +10,8 @@ module Restfully
10
10
  end
11
11
  class Session
12
12
  include Parsing, HTTP::Headers
13
- attr_reader :base_uri, :logger, :connection, :root, :default_headers
13
+ attr_reader :base_uri, :logger, :connection, :default_headers
14
14
 
15
- # TODO: use CacheableResource
16
15
  def initialize(options = {})
17
16
  options = options.symbolize_keys
18
17
  if (config_filename = options.delete(:configuration_file)) && File.exists?(File.expand_path(config_filename))
@@ -30,6 +29,10 @@ module Restfully
30
29
  yield @root.load, self if block_given?
31
30
  end
32
31
 
32
+ def root
33
+ @root.load
34
+ end
35
+
33
36
  # returns an HTTP::Response object or raise a Restfully::HTTP::Error
34
37
  def head(path, options = {})
35
38
  options = options.symbolize_keys
data/restfully.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{restfully}
8
- s.version = "0.5.3"
8
+ s.version = "0.5.4"
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"]
12
- s.date = %q{2010-02-05}
12
+ s.date = %q{2010-03-22}
13
13
  s.default_executable = %q{restfully}
14
14
  s.description = %q{Experimental code for auto-generation of wrappers on top of RESTful APIs that follow HATEOAS principles and provide OPTIONS methods and/or Allow headers.}
15
15
  s.email = %q{cyril.rohr@gmail.com}
@@ -4,7 +4,7 @@ describe Restfully::HTTP::Request do
4
4
  it "should correctly initialize the attributes" do
5
5
  request = Restfully::HTTP::Request.new(
6
6
  'https://api.grid5000.fr/sid/grid5000?q1=v1&q2=v2',
7
- :headers => {'accept' => 'application/json', :cache_control => 'max-age=0', 'Content-Type' => 'application/json'},
7
+ :headers => {'accept' => 'application/xml', :cache_control => 'max-age=0', 'Content-Type' => 'application/json'},
8
8
  :query => {'custom_param1' => [3, 4, 5, 6], 'custom_param2' => 'value_custom_param2'},
9
9
  :body => {"key" => "value"}.to_json
10
10
  )
@@ -13,7 +13,7 @@ describe Restfully::HTTP::Request do
13
13
  request.uri.query.should == "q1=v1&q2=v2&custom_param1=3,4,5,6&custom_param2=value_custom_param2"
14
14
  request.headers.should == {
15
15
  "Content-Type"=>"application/json",
16
- 'Accept' => 'application/json',
16
+ 'Accept' => 'application/xml',
17
17
  'Cache-Control' => 'max-age=0'
18
18
  }
19
19
  request.body.should == {"key" => "value"}
data/spec/parsing_spec.rb CHANGED
@@ -12,7 +12,7 @@ describe Restfully::Parsing do
12
12
  klass.should respond_to(:serialize)
13
13
  end
14
14
  it "should raise a ParserNotFound error if the object cannot be parsed" do
15
- lambda{unserialize("whatever", :content_type => 'unknown')}.should raise_error(Restfully::Parsing::ParserNotFound, "Content-Type 'unknown' is not supported. Cannot parse the given object.")
15
+ lambda{unserialize("whatever", :content_type => 'unknown')}.should raise_error(Restfully::Parsing::ParserNotFound, "Cannot find a parser to parse 'unknown' content.")
16
16
  end
17
17
  it "should correctly unserialize json content" do
18
18
  object = {'p1' => 'v1'}
@@ -26,4 +26,15 @@ describe Restfully::Parsing do
26
26
  object = "anything"
27
27
  unserialize(object, :content_type => 'text/plain;charset=utf-8').should == object
28
28
  end
29
+ it "should allow to define its own parser" do
30
+ Restfully::Parsing::PARSERS.push({
31
+ :supported_types => "text/unknown",
32
+ :parse => lambda{|object, options| object.gsub(/x/, "y")},
33
+ :dump => lambda{|object, options| object.gsub(/y/, "x")}
34
+ })
35
+ object = "xyz"
36
+ parsed = unserialize(object, :content_type => 'text/unknown')
37
+ parsed.should == "yyz"
38
+ serialize(parsed, :content_type => 'text/unknown').should == "xxz"
39
+ end
29
40
  end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'restfully'
4
4
  require 'spec'
5
5
  require 'spec/autorun'
6
+ require 'json'
6
7
 
7
8
  def fixture(filename)
8
9
  File.read(File.join(File.dirname(__FILE__), "fixtures", filename))
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.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Rohr
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-05 00:00:00 +01:00
12
+ date: 2010-03-22 00:00:00 +01:00
13
13
  default_executable: restfully
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency