Module: Videojuicer::Resource::ClassMethods

Defined in:
lib/videojuicer/resource/base.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object

Finds all objects matching the criteria. Also allows filterable methods. Use :limit to throttle the amount of records returned. Use :offset to return all objects after a certain index. Combine with :limit for pagination.

You may specify comparators on attributes by giving them in the following forms: > “id”=>“9” #=> Returns only records with ID 9 > “id.gt” => “9” #=> Returns only records with ID greater than 9 > “id.lt” => “9” #=> Returns only records with ID less than 9 > “id.gte” => “9” #=> Returns only records with ID greater than or equal to 9 > “id.lte” => “9” #=> Returns only records with ID less than or equal to than 9 Range operations are also supported e.g. > :id => ‘1,2,3’



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/videojuicer/resource/base.rb', line 151

def all(options={})
  # Get a proxy
  options = (options.empty?)? {} : {parameter_name=>options} # FIXME this is a hacky workaround for singleton scope.
  response = instance_proxy.get(base_path(:nested=>false), options)
  op = JSON.parse(response.body)
  
  items = (op["items"] rescue op) # If "items" is on the returned object then this is a collection hash.
  # Instantiate objects
  items = items.collect do |attrs|
    o = new; o.attributes = attrs
    o.clean_dirty_attributes!
    o
  end
  return (op.is_a?(Hash))? Videojuicer::Resource::Collection.new(items, op["count"], op["offset"], op["limit"]) : items
end

#create(attrs = {}) ⇒ Object



171
172
173
174
175
# File 'lib/videojuicer/resource/base.rb', line 171

def create(attrs={})
  o = new(attrs)
  o.save
  return o
end

#destroy(id) ⇒ Object



188
189
190
191
# File 'lib/videojuicer/resource/base.rb', line 188

def destroy(id)
  o = new(:id=>id)
  o.destroy
end

#first(options = {}) ⇒ Object



167
168
169
# File 'lib/videojuicer/resource/base.rb', line 167

def first(options={})
  all(options.merge(:limit=>1)).first
end

#get(id) ⇒ Object

Fetches an object given an ID. Straight forward.



178
179
180
181
182
183
184
185
186
# File 'lib/videojuicer/resource/base.rb', line 178

def get(id)
  o = new(:id=>id)
  begin
    o.reload 
    o
  rescue Videojuicer::Exceptions::NoResource
    nil
  end
end

#instance_proxyObject



193
194
195
196
# File 'lib/videojuicer/resource/base.rb', line 193

def instance_proxy
  o = new
  o.proxy_for(o.config)
end