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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/videojuicer/resource/base.rb', line 127

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
  end
  return (op.is_a?(Hash))? Videojuicer::Resource::Collection.new(items, op["count"], op["offset"], op["limit"]) : items
end

#create(attrs = {}) ⇒ Object



146
147
148
149
150
# File 'lib/videojuicer/resource/base.rb', line 146

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

#destroy(id) ⇒ Object



163
164
165
166
# File 'lib/videojuicer/resource/base.rb', line 163

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

#first(options = {}) ⇒ Object



142
143
144
# File 'lib/videojuicer/resource/base.rb', line 142

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

#get(id) ⇒ Object

Fetches an object given an ID. Straight forward.



153
154
155
156
157
158
159
160
161
# File 'lib/videojuicer/resource/base.rb', line 153

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

#instance_proxyObject



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

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