restfully 0.5.9 → 0.5.10

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.10
2
+ * Direct lookup to the resource if it cannot be found in the collection (for APIs that do not return all the items in a collection) (priteau)
3
+ * Automatically load next pages of a collection when iterating through it
4
+
1
5
  0.5.9
2
6
  * fixed bug in #http_methods that made a resource not removable unless force reloading it
3
7
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.9
1
+ 0.5.10
data/lib/restfully.rb CHANGED
@@ -14,7 +14,7 @@ require 'restfully/collection'
14
14
 
15
15
 
16
16
  module Restfully
17
- VERSION = "0.5.9"
17
+ VERSION = "0.5.10"
18
18
  class << self
19
19
  attr_accessor :adapter
20
20
  end
@@ -26,14 +26,25 @@ module Restfully
26
26
 
27
27
  # Iterates over the collection of items
28
28
  def each(*args, &block)
29
- @items.each(*args, &block)
29
+ @items.each_index{ |i|
30
+ block.call(@items[i])
31
+ if i == @items.length-1 && @items.length < self["total"]
32
+ # load next page
33
+ next_page = Collection.new(uri, session).load(:query => {:offset => self["offset"]+@items.length}) rescue nil
34
+ if next_page && next_page['offset'] != self["offset"]
35
+ @items.push(*next_page.to_a)
36
+ end
37
+ end
38
+ }
30
39
  end
31
40
 
32
41
  # if property is a Symbol, it tries to find the corresponding item whose uid.to_sym is matching the property
33
42
  # else, returns the result of calling <tt>[]</tt> on its superclass.
34
43
  def [](property)
35
44
  if property.kind_of?(Symbol)
36
- find{|i| i['uid'] == property.to_s} || find{|i| i['uid'] == property.to_s.to_i}
45
+ item = find{|i| i['uid'] == property.to_s} ||
46
+ find{|i| i['uid'] == property.to_s.to_i} ||
47
+ Resource.new([uri, property].join("/"), session).load rescue nil
37
48
  else
38
49
  super(property)
39
50
  end
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.9"
8
+ s.version = "0.5.10"
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-03-31}
12
+ s.date = %q{2010-04-20}
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}
@@ -32,6 +32,31 @@ describe Collection do
32
32
  job.class.should == Restfully::Resource
33
33
  job['uid'].should == 319482
34
34
  end
35
+ it "should make a direct request to collection_uri/resource_uid when calling [] and the searched item is not in the collection" do
36
+ Restfully::Resource.should_receive(:new).with('http://api.local/x/y/z/not_in_the_collection', @collection.session).and_return(resource = mock("resource"))
37
+ resource.should_receive(:load).and_return(resource)
38
+ @collection[:not_in_the_collection].should == resource
39
+ end
40
+ it "should go through the 'next' links (if any) to search for the requested item" do
41
+ body = JSON.parse(fixture("grid5000-rennes-jobs.json"))
42
+ body["total"] = 20
43
+ body1 = JSON.parse(fixture("grid5000-rennes-jobs.json"))
44
+ body["total"] = 20
45
+ body1["offset"] = 8
46
+ body2 = JSON.parse(fixture("grid5000-rennes-jobs.json"))
47
+ body["total"] = 20
48
+ body2["offset"] = 16
49
+ body2["items"][1]["uid"] = "18th item"
50
+ collection = Collection.new(@uri, session = mock('session')).load(:body => body)
51
+ session.should_receive(:get).with(@uri, :query => {:offset => 8}).ordered.and_return(response = mock("response", :body => body1, :status => 200, :headers => {}))
52
+ session.should_receive(:get).with(@uri, :query => {:offset => 16}).ordered.and_return(response = mock("response", :body => body2, :status => 200, :headers => {}))
53
+ collection.find{|item| item["uid"] == "18th item"}.should_not be_nil
54
+ end
55
+ it "should always return nil if the searched item is not in the collection" do
56
+ Restfully::Resource.should_receive(:new).with('http://api.local/x/y/z/doesnotexist', @collection.session).and_return(resource = mock("resource"))
57
+ resource.should_receive(:load).and_raise Restfully::HTTP::ClientError.new(mock("response", :body => "Not Found", :status => 404))
58
+ @collection[:doesnotexist].should be_nil
59
+ end
35
60
  end
36
61
 
37
62
  describe "populating collection" do
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.9
4
+ version: 0.5.10
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-03-31 00:00:00 +02:00
12
+ date: 2010-04-20 00:00:00 +02:00
13
13
  default_executable: restfully
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency