Class: Eltanin::Eye

Inherits:
Object
  • Object
show all
Defined in:
lib/eltanin.rb

Instance Method Summary collapse

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:



22
23
24
25
26
27
# File 'lib/eltanin.rb', line 22

def initialize(root, path = "", options = {}, queue = nil)
  @root = root;
  @path = path;
  @options = options;
  @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, @options, @queue
end

#batchObject

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(options = {})
  request ({:method=>:delete}.merge(options))
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(options = {})
  request ({:method=>:get,:params=>{:limit=>50}}.merge(options))
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, options = {})
  request ({:method=>:post,:params=>value}.merge(options))
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, options = {})
  request ({:method=>:put,:params=>value}.merge(options))
end

#queueObject

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, @options, []
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(options)
  if @queue.nil?
    resource = Nestful::Resource.new(@root+@path,{:format=>:json}.merge(@options))
    begin
      resource.request options
    rescue Nestful::ClientError => e
      e
    end
  else
    r = { "method" => options[:method], "path" => @path }
    if options[:method] == :get
      r["path"] += "?"+ (URI.encode_www_form options[:params])
    else
      r["body"] = options[:params] unless options[:params].nil?
    end
    @queue << r
    @queue.size - 1
  end
end