Class: Eltanin::Eye
- Inherits:
-
Object
- Object
- Eltanin::Eye
- Defined in:
- lib/eltanin.rb
Instance Method Summary collapse
-
#[](suburl) ⇒ Object
Returns a new instance to a suburi of the current.
-
#batch ⇒ Object
Executes all enqueued requests.
-
#delete(options = {}) ⇒ Object
Makes a delete request on top of the current resource.
-
#get(options = {}) ⇒ Object
Makes a get request on top of the current resource.
-
#initialize(root, path = "", options = {}, queue = nil) ⇒ Eye
constructor
Creates a new instance of the resource to Eltanin.
-
#post(value = nil, options = {}) ⇒ Object
Makes a post request on top of the current resource.
-
#put(value = nil, options = {}) ⇒ Object
Makes a put request on top of the current resource.
-
#queue ⇒ Object
Returns a new instance of a object that is a queue for batch calls.
- #request(options) ⇒ Object
Constructor Details
#initialize(root, path = "", options = {}, queue = nil) ⇒ Eye
Creates a new instance of the resource to Eltanin.
It only requires an String that points to the root URI. The object itself is copied with modification each time that a change is required, so every instance will always spot to the same resource.
Parameters:
root-
the URL to the Eltanin REST address, for example us@pas:eltanin-eye.com
22 23 24 25 26 27 |
# File 'lib/eltanin.rb', line 22 def initialize(root, path = "", = {}, queue = nil) @root = root; @path = path; = ; @queue = queue; end |
Instance Method Details
#[](suburl) ⇒ Object
Returns a new instance to a suburi of the current.
For example: Eltanin.eye(“ey.co”) creates a resource that points to http;//ey.co/buyers
33 34 35 |
# File 'lib/eltanin.rb', line 33 def [](suburl) self.class.new @root, @path+"/"+suburl.to_s, , @queue end |
#batch ⇒ Object
Executes all enqueued requests.
It must be called in to an object result of queue(). Makes a single REST query with all pending requests. Each time that get/put/post/delete/request is invoked in a queue an index is returned, batch returns an array with all results indexed by its corresponding index.
For more information and an example see queue()
68 69 70 71 72 73 |
# File 'lib/eltanin.rb', line 68 def batch() raise "Is not a queue" if @queue.nil? r = (self.class.new @root, "/batch").post @queue unless @queue.empty? @queue = [] r end |
#delete(options = {}) ⇒ Object
Makes a delete request on top of the current resource.
91 92 93 |
# File 'lib/eltanin.rb', line 91 def delete( = {}) request ({:method=>:delete}.merge()) end |
#get(options = {}) ⇒ Object
Makes a get request on top of the current resource.
76 77 78 |
# File 'lib/eltanin.rb', line 76 def get( = {}) request ({:method=>:get,:params=>{:limit=>50}}.merge()) end |
#post(value = nil, options = {}) ⇒ Object
Makes a post request on top of the current resource.
81 82 83 |
# File 'lib/eltanin.rb', line 81 def post(value=nil, = {}) request ({:method=>:post,:params=>value}.merge()) end |
#put(value = nil, options = {}) ⇒ Object
Makes a put request on top of the current resource.
86 87 88 |
# File 'lib/eltanin.rb', line 86 def put(value=nil, = {}) request ({:method=>:put,:params=>value}.merge()) end |
#queue ⇒ Object
Returns a new instance of a object that is a queue for batch calls.
Eltanin EYE is designed to be able to group multiple calls into a single call. This method creates a new object that groups all requests. Methods get/put/post/delete/request will have no inmediate effect. These operations are queued waiting for a call to the method batch.
An example:
q = Eltanin.eye("http://ey.co").queue()
buyers_idx = q['buyers'].get
products_idx = q['products'].get
all = q.batch
buyers = all[buyers_idx]
products = all[products_idx]
See:
batch()
55 56 57 |
# File 'lib/eltanin.rb', line 55 def queue() self.class.new @root, @path, , [] end |
#request(options) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/eltanin.rb', line 95 def request() if @queue.nil? resource = Nestful::Resource.new(@root+@path,{:format=>:json}.merge()) begin resource.request rescue Nestful::ClientError => e e end else r = { "method" => [:method], "path" => @path } if [:method] == :get r["path"] += "?"+ (URI.encode_www_form [:params]) else r["body"] = [:params] unless [:params].nil? end @queue << r @queue.size - 1 end end |