some
[appoyo.git] / vendor / plugins / facebooker / lib / facebooker / batch_request.rb
blob4708905611b353438e48a5b6384e86ea4a9a4d53
1 module Facebooker
2   class BatchRequest
3     instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^respond_to\?$|^new$)/ }
4     attr_reader :uri
5     attr_reader :method
6     class UnexecutedRequest < StandardError; end
7     def initialize(params,proc)
8       @exception  = nil
9       @result     = nil
10       @method     = params[:method]
11       @uri        = params.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.join("&")
12       @proc       = proc
13     end
15     def result=(result_object)
16       @result = @proc.nil? ? result_object : @proc.call(result_object)
17     end
19     def exception_raised=(ex)
20       @exception=ex
21     end
23     def exception_raised?
24       @exception.nil? ? false : raise(@exception)
25     end
27     def respond_to?(name)
28       super || @result.respond_to?(name)
29     end
31     def ===(other)
32       other === @result
33     end
35     def method_missing(name,*args,&proc)
36       if @exception
37         raise @exception
38       elsif @result.nil?
39         raise UnexecutedRequest.new("You must execute the batch before accessing the result: #{@uri}")
40       else
41         @result.send(name,*args,&proc)
42       end
43     end
44   end
45 end