Class: Ankoder::Base
- Inherits:
-
Object
- Object
- Ankoder::Base
- 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
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
-
.count(field = nil, options = {}) ⇒ Object
Count request.
-
.create(attributes = {}) ⇒ Object
Create the object.
-
.destroy(id) ⇒ Object
Destroy the object passing its ID.
-
.destroy_all ⇒ Object
Destroy all the objects.
- .disconnect! ⇒ Object
-
.establish_connection!(options = {}) ⇒ Object
Establish the connection for all your session.
-
.find(*args) ⇒ Object
Find objects.
-
.method_missing(m, *args) ⇒ Object
:nodoc:.
-
.path ⇒ Object
:nodoc:.
-
.path=(path) ⇒ Object
if the path of the resource is not standard, you can set it manually.
-
.session ⇒ Object
:nodoc:.
-
.session=(session) ⇒ Object
:nodoc:.
-
.update(id, attributes = {}) ⇒ Object
Update the object passing its ID.
Instance Method Summary collapse
-
#destroy ⇒ Object
Destroy the object.
-
#id ⇒ Object
:nodoc:.
-
#include_ankoder_object(objects = nil) ⇒ Object
:nodoc:.
-
#initialize(attributes = {}) ⇒ Base
constructor
Instanciate a new object.
-
#method_missing(m, *args) ⇒ Object
:nodoc:.
-
#new_record? ⇒ Boolean
:nodoc:.
-
#reload ⇒ Object
Reload the current object.
-
#save ⇒ Object
Save the object.
-
#save! ⇒ Object
Save the current object.
-
#update_attributes(attributes = {}) ⇒ Object
Update the object with the given attributes.
Constructor Details
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
11 12 13 |
# File 'lib/ankoder/base.rb', line 11 def attributes @attributes end |
#errors ⇒ Object (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, ={}) find(:all, ).size end |
.create(attributes = {}) ⇒ Object
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_all ⇒ Object
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!(={}) Base.session = Browser::login([:login], [: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 includeProfile.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, = args ||= {} case scope when :all then find_every() when :first then find_every().first else find_single(scope, ) 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 |
.path ⇒ Object
: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 |
.session ⇒ Object
: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 |
Instance Method Details
#destroy ⇒ Object
209 210 211 |
# File 'lib/ankoder/base.rb', line 209 def destroy self.class.destroy(id) end |
#id ⇒ Object
: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:
224 225 226 |
# File 'lib/ankoder/base.rb', line 224 def new_record? #:nodoc: @attributes["id"].nil? end |
#reload ⇒ Object
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 |
#save ⇒ Object
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
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 |