Class: Ankoder::Base

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

Overview

You can connect to the _ankoderapi_session service permanently

Base::establish_connection! :login => "login", :password => "password"

You can access all the resources and manipulate the objects like in ActiveRecord

v = Video.find(15).update_attributes :name => "My edited title"
puts v.title

Direct Known Subclasses

Account, Download, Job, Profile, Upload, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Instanciate a new object

Profile.new :name => "test format", :sample_rate => 24000


148
149
150
151
# File 'lib/ankoder/base.rb', line 148

def initialize(attributes={})
  @attributes = attributes.underscore_keys!
  @attributes.type_cast!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

:nodoc:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/ankoder/base.rb', line 228

def method_missing(m, *args) #:nodoc:
  method_name = m.to_s
  case method_name[-1..-1]
  when '='
    @attributes[method_name[0..-2]] = *args.first
  when '?'
    @attributes[method_name[0..-2]]
  else
    if instance_variables.include?("@#{m.to_s}")
      eval("@#{m.to_s}")
    else
      if object_id = @attributes[m.to_s+"_id"] # belongs_to
        klass = Ankoder::const_get(m.to_s.camelize)
        klass.session = self.class.session
        klass.find(object_id)
      else
        @attributes[m.to_s] rescue nil
      end
    end
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



11
12
13
# File 'lib/ankoder/base.rb', line 11

def attributes
  @attributes
end

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/ankoder/base.rb', line 11

def errors
  @errors
end

Class Method Details

.count(field = nil, options = {}) ⇒ Object

Count request

Accept :conditions like in Base#find



140
141
142
# File 'lib/ankoder/base.rb', line 140

def count(field=nil, options={})
  find(:all, options).size
end

.create(attributes = {}) ⇒ Object

Create the object

Download.create :url => "http://host.com/video.avi"


110
111
112
# File 'lib/ankoder/base.rb', line 110

def create(attributes={})
  new(Ankoder::response(Browser::post(path, attributes, session).body))
end

.destroy(id) ⇒ Object

Destroy the object passing its ID

Profile.destroy 12


124
125
126
# File 'lib/ankoder/base.rb', line 124

def destroy(id)
  Browser::delete(path+"/"+id.to_s, session)
end

.destroy_allObject

Destroy all the objects

Video.destroy_all


131
132
133
134
135
# File 'lib/ankoder/base.rb', line 131

def destroy_all
  find(:all).each do |object|
    object.destroy
  end
end

.disconnect!Object



27
28
29
# File 'lib/ankoder/base.rb', line 27

def disconnect!
  Base.session = nil
end

.establish_connection!(options = {}) ⇒ Object

Establish the connection for all your session



23
24
25
# File 'lib/ankoder/base.rb', line 23

def establish_connection!(options={})
  Base.session = Browser::(options[:login], options[:password])
end

.find(*args) ⇒ Object

Find objects

Arguments:

  • ID ID of the object

  • :first retrieve the first result

  • :all retrieve all the results

Options:

  • :conditions => “keyword” or => /[0-9]+/

  • :order to sort the result

  • :limit limit the number of result to return

  • :include fetch the object to include

    Profile.find(:all, :conditions => {:name => /mobile/i})
    Profile.find(:all, :conditions => {:width => '<320'})
    Job.find(:all, :include => "video", :order => "created_at DESC", :limit => 3)
    Video.find :first
    Download.find(5)
    


79
80
81
82
83
84
85
86
87
# File 'lib/ankoder/base.rb', line 79

def find(*args)
  scope, options = args     
  options ||= {}
  case scope
    when :all   then find_every(options)
    when :first then find_every(options).first
    else             find_single(scope, options)
  end
end

.method_missing(m, *args) ⇒ Object

:nodoc:



31
32
33
34
35
36
37
38
39
# File 'lib/ankoder/base.rb', line 31

def method_missing(m, *args) #:nodoc:
  if m.to_s =~ /find_by_(.*)/
    find(:first, :conditions => {$1.to_sym => args.first}) rescue nil
  elsif m.to_s =~ /find_all_by_(.*)/
    find :all, {:conditions => {$1.to_sym => args.first}}.merge(args[1]||{})
  else
    raise NoMethodError, "undefined method `#{m.to_s}' for #{self}"
  end
end

.pathObject

:nodoc:



41
42
43
44
45
46
47
48
# File 'lib/ankoder/base.rb', line 41

def path #:nodoc:
  return @path if @path
  "/"+self.to_s.split("::").last.
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end

.path=(path) ⇒ Object

if the path of the resource is not standard, you can set it manually.

class Log < Base
   self.path = "/user_logs"
end


55
56
57
# File 'lib/ankoder/base.rb', line 55

def path=(path) #:nodoc:
  @path = path
end

.sessionObject

:nodoc:



18
19
20
# File 'lib/ankoder/base.rb', line 18

def session #:nodoc:
  (@session || Base.session) rescue nil
end

.session=(session) ⇒ Object

:nodoc:



14
15
16
# File 'lib/ankoder/base.rb', line 14

def session=(session) #:nodoc:
  @session = session
end

.update(id, attributes = {}) ⇒ Object

Update the object passing its ID

Video.update 15, :name => "my title"


117
118
119
# File 'lib/ankoder/base.rb', line 117

def update(id, attributes={})
  Browser::put(path+"/"+id.to_s, attributes, session)
end

Instance Method Details

#destroyObject

Destroy the object

Video.find(56).destroy


209
210
211
# File 'lib/ankoder/base.rb', line 209

def destroy
  self.class.destroy(id)
end

#idObject

:nodoc:



192
193
194
# File 'lib/ankoder/base.rb', line 192

def id #:nodoc:
  @attributes["id"].to_i
end

#include_ankoder_object(objects = nil) ⇒ Object

:nodoc:



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ankoder/base.rb', line 153

def include_ankoder_object(objects=nil) #:nodoc:
  return self if objects.nil?
  objects = objects.to_s.split(",") if objects.is_a?(String) or objects.is_a?(Symbol)
  objects.each do |ob|
    begin
      self.instance_variable_set "@#{ob}", (self.send(ob) rescue nil)
      self.instance_eval "attr_reader #{ob.to_sym}"
    rescue
    end
  end
  self
end

#new_record?Boolean

:nodoc:

Returns:

  • (Boolean)


224
225
226
# File 'lib/ankoder/base.rb', line 224

def new_record? #:nodoc:
  @attributes["id"].nil?
end

#reloadObject

Reload the current object

j = Job.find(5400)
j.reload


217
218
219
220
221
222
# File 'lib/ankoder/base.rb', line 217

def reload
  unless new_record?
    @attributes = self.class.find(self.id).attributes
  end
  self
end

#saveObject

Save the object.

If the object doesn’t exist, it will be created, otherwise updated. If an error occurred, @errors will be filled. This method doesn’t raise.



170
171
172
173
174
175
176
177
178
# File 'lib/ankoder/base.rb', line 170

def save
  begin
    save!
    true
  rescue => e
    @errors = e.to_s
    false
  end
end

#save!Object

Save the current object

Raise if an error occurred. Return self.



183
184
185
186
187
188
189
190
# File 'lib/ankoder/base.rb', line 183

def save!
  if new_record?
    self.class.create(@attributes)
  else
    update_attributes(@attributes)
  end
  self
end

#update_attributes(attributes = {}) ⇒ Object

Update the object with the given attributes

Video.find(10).update_attributes :name => "test title"


199
200
201
202
203
204
# File 'lib/ankoder/base.rb', line 199

def update_attributes(attributes={})
  if self.class.update(id, attributes)
    reload
    true
  end
end