heywatch 0.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/heywatch +135 -0
- data/lib/heywatch.rb +133 -81
- metadata +83 -68
- data/Manifest.txt +0 -18
- data/README.txt +0 -163
- data/Rakefile +0 -74
- data/lib/heywatch/account.rb +0 -32
- data/lib/heywatch/auth.rb +0 -83
- data/lib/heywatch/base.rb +0 -269
- data/lib/heywatch/browser.rb +0 -98
- data/lib/heywatch/discover.rb +0 -39
- data/lib/heywatch/download.rb +0 -30
- data/lib/heywatch/encoded_video.rb +0 -49
- data/lib/heywatch/ext.rb +0 -118
- data/lib/heywatch/job.rb +0 -32
- data/lib/heywatch/version.rb +0 -9
- data/lib/heywatch/video.rb +0 -25
- data/setup.rb +0 -1585
- data/test/test_helper.rb +0 -8
- data/test/test_heywatch.rb +0 -149
data/Manifest.txt
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
Manifest.txt
|
2
|
-
README.txt
|
3
|
-
Rakefile
|
4
|
-
lib/heywatch.rb
|
5
|
-
lib/heywatch/video.rb
|
6
|
-
lib/heywatch/version.rb
|
7
|
-
lib/heywatch/job.rb
|
8
|
-
lib/heywatch/ext.rb
|
9
|
-
lib/heywatch/encoded_video.rb
|
10
|
-
lib/heywatch/download.rb
|
11
|
-
lib/heywatch/discover.rb
|
12
|
-
lib/heywatch/browser.rb
|
13
|
-
lib/heywatch/base.rb
|
14
|
-
lib/heywatch/auth.rb
|
15
|
-
lib/heywatch/account.rb
|
16
|
-
setup.rb
|
17
|
-
test/test_helper.rb
|
18
|
-
test/test_heywatch.rb
|
data/README.txt
DELETED
@@ -1,163 +0,0 @@
|
|
1
|
-
=Hey!Watch - Video Encoding Web Service
|
2
|
-
|
3
|
-
Hey!Watch <http://heywatch.com> provides a simple and robust encoding plateform.
|
4
|
-
The service allows developers to access a fast, scalable and inexpensive web
|
5
|
-
service to encode videos easier. The API can be easily integrated
|
6
|
-
in any web or desktop applications.
|
7
|
-
|
8
|
-
The documentation of the API can be found at http://wiki.heywatch.com/API_Documentation
|
9
|
-
|
10
|
-
|
11
|
-
==Getting started
|
12
|
-
|
13
|
-
===Transfer a video, encode it in ipod format and download the encoded video
|
14
|
-
|
15
|
-
require 'heywatch'
|
16
|
-
include HeyWatch
|
17
|
-
|
18
|
-
Base::establish_connection! :login => 'login', :password => 'password'
|
19
|
-
|
20
|
-
raw_video = Discover.create(:url => 'http://youtube.com/watch?v=SXcpNZCyQJw', :download => true) do |percent, total_size, received|
|
21
|
-
puts "#{percent}%"
|
22
|
-
end
|
23
|
-
|
24
|
-
ipod_format = Format.find_by_name('iPod 4:3')
|
25
|
-
encoded_video = Job.create(:video_id => raw_video.id, :format_id => ipod_format.id) do |percent|
|
26
|
-
puts "#{percent}%"
|
27
|
-
end
|
28
|
-
|
29
|
-
puts "downloading {encoded_video.title}"
|
30
|
-
path = encoded_video.download
|
31
|
-
puts "video saved in {path}"
|
32
|
-
|
33
|
-
|
34
|
-
===Upload a video from the disk, encode it with FTP auto transfer option
|
35
|
-
|
36
|
-
raw_video = Video.create(:file => 'videos/myvideo.avi', :title => 'Funny video') do |percent, total_size, received|
|
37
|
-
puts "#{percent}%"
|
38
|
-
end
|
39
|
-
|
40
|
-
Job.create :video_id => raw_video.id, :default_format => true, :ftp_directive => 'ftp://login:pass@host.com/heywatch_vids/'
|
41
|
-
|
42
|
-
|
43
|
-
===Generate a thumbnail
|
44
|
-
|
45
|
-
v = EncodedVideo.find(5400)
|
46
|
-
v.thumbnail :start => 15, :width => 640, :height => 480
|
47
|
-
|
48
|
-
|
49
|
-
===Update your account
|
50
|
-
|
51
|
-
account = Account.find
|
52
|
-
account.update_attributes :ping_url_after_encode => 'http://yourhost.com/ping/encode'
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
==Integration in a rails application
|
57
|
-
|
58
|
-
This short HOWTO uses the ping options. So in your HeyWatch account, you must
|
59
|
-
configure all the ping URL (except transfer for this example).
|
60
|
-
|
61
|
-
Examples:
|
62
|
-
|
63
|
-
ping_url_after_encode => http://myhost.com/ping/encode
|
64
|
-
ping_url_if_error => http://myhost.com/ping/error
|
65
|
-
|
66
|
-
|
67
|
-
===Config
|
68
|
-
|
69
|
-
In your config/environment.rb:
|
70
|
-
|
71
|
-
require 'heywatch'
|
72
|
-
HeyWatch::Base::establish_connection! :login => 'login', :password => 'passwd'
|
73
|
-
|
74
|
-
|
75
|
-
===Item Model
|
76
|
-
|
77
|
-
create_table "items", :force => true do |t|
|
78
|
-
t.column "title", :string, :null => false
|
79
|
-
t.column "description", :text, :null => false
|
80
|
-
t.column "created_at", :datetime
|
81
|
-
t.column "updated_at", :datetime
|
82
|
-
t.column "status", :string, :default => "working"
|
83
|
-
t.column "url", :string
|
84
|
-
t.column "meta", :text
|
85
|
-
t.column "user_id", :integer
|
86
|
-
end
|
87
|
-
|
88
|
-
In app/models/item.rb:
|
89
|
-
|
90
|
-
class Item < ActiveRecord::Base
|
91
|
-
belongs_to :user
|
92
|
-
serialize :meta
|
93
|
-
after_create :convert
|
94
|
-
|
95
|
-
# When HeyWatch send videos to your FTP,
|
96
|
-
# they will be available to this HTTP url.
|
97
|
-
def base_url
|
98
|
-
"http://myhost.com/flv/#{self.id}/"
|
99
|
-
end
|
100
|
-
|
101
|
-
# Transfer the given URL and convert the video from
|
102
|
-
# this address in format 31 (flv format). When the encode
|
103
|
-
# is done, the video will be sent to the specified FTP
|
104
|
-
# with custom path.
|
105
|
-
#
|
106
|
-
# Note item_id which is a custom field.
|
107
|
-
def convert
|
108
|
-
HeyWatch::Discover.create(
|
109
|
-
:url => self.url,
|
110
|
-
:download => true,
|
111
|
-
:item_id => self.id,
|
112
|
-
:title => self.title,
|
113
|
-
:automatic_encode => true,
|
114
|
-
:format_id => 31,
|
115
|
-
:ftp_directive => "ftp://login:passwd@myhost.com/flv/#{self.id}/"
|
116
|
-
)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
|
121
|
-
===Ping Controller
|
122
|
-
|
123
|
-
In app/controllers/ping_controller.rb:
|
124
|
-
|
125
|
-
class PingController < ApplicationController
|
126
|
-
before_filter :find_item
|
127
|
-
|
128
|
-
def encode
|
129
|
-
@encoded_video = HeyWatch::EncodedVideo.find(params[:encoded_video_id])
|
130
|
-
@full_url = @item.base_url + @encoded_video.filename
|
131
|
-
@item.status = 'finished'
|
132
|
-
@item.meta = {
|
133
|
-
:url => @full_url,
|
134
|
-
:thumb => @full_url + ".jpg",
|
135
|
-
:size => @encoded_video.specs["size"],
|
136
|
-
:mime_type => @encoded_video.specs["mime_type"],
|
137
|
-
:length => @encoded_video.specs.video["length"]
|
138
|
-
}
|
139
|
-
@item.save
|
140
|
-
@encoded_video.job.video.destroy # delete the raw video
|
141
|
-
@encoded_video.destroy # delete the encoded video
|
142
|
-
render :nothing => true
|
143
|
-
end
|
144
|
-
|
145
|
-
def error
|
146
|
-
if params[:discover_id]
|
147
|
-
error_msg = "No video link found"
|
148
|
-
else
|
149
|
-
error_msg = HeyWatch::ErrorCode[params[:error_code].to_i]
|
150
|
-
end
|
151
|
-
@item.update_attributes :status => 'error'
|
152
|
-
ItemMailer.deliver_error(error_msg, @item.user)
|
153
|
-
render :nothing => true
|
154
|
-
end
|
155
|
-
|
156
|
-
private
|
157
|
-
|
158
|
-
# item_id is a custom_field sent in Item#convert.
|
159
|
-
# Thanks to it, we can track the item.
|
160
|
-
def find_item
|
161
|
-
@item = Item.find params[:item_id]
|
162
|
-
end
|
163
|
-
end
|
data/Rakefile
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rake'
|
3
|
-
require 'rake/clean'
|
4
|
-
require 'rake/testtask'
|
5
|
-
require 'rake/packagetask'
|
6
|
-
require 'rake/gempackagetask'
|
7
|
-
require 'rake/rdoctask'
|
8
|
-
require 'rake/contrib/rubyforgepublisher'
|
9
|
-
require 'fileutils'
|
10
|
-
require 'hoe'
|
11
|
-
include FileUtils
|
12
|
-
require File.join(File.dirname(__FILE__), 'lib', 'heywatch', 'version')
|
13
|
-
|
14
|
-
AUTHOR = 'Bruno Celeste' # can also be an array of Authors
|
15
|
-
EMAIL = "bruno.celeste@heywatch.com"
|
16
|
-
DESCRIPTION = "Ruby Library for HeyWatch service (http://heywatch.com)."
|
17
|
-
GEM_NAME = 'heywatch' # what ppl will type to install your gem
|
18
|
-
RUBYFORGE_PROJECT = 'heywatch' # The unix name for your project
|
19
|
-
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
20
|
-
|
21
|
-
|
22
|
-
NAME = "heywatch"
|
23
|
-
REV = nil # UNCOMMENT IF REQUIRED: File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
|
24
|
-
VERS = ENV['VERSION'] || (HeyWatch::VERSION::STRING + (REV ? ".#{REV}" : ""))
|
25
|
-
CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
|
26
|
-
RDOC_OPTS = ['--quiet', '--title', 'heywatch documentation',
|
27
|
-
"--opname", "index.html",
|
28
|
-
"--line-numbers",
|
29
|
-
"--main", "README.txt",
|
30
|
-
"--inline-source"]
|
31
|
-
|
32
|
-
class Hoe
|
33
|
-
def extra_deps
|
34
|
-
@extra_deps.reject { |x| Array(x).first == 'hoe' }
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# Generate all the Rake tasks
|
39
|
-
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
40
|
-
hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
41
|
-
p.author = AUTHOR
|
42
|
-
p.description = DESCRIPTION
|
43
|
-
p.email = EMAIL
|
44
|
-
p.summary = DESCRIPTION
|
45
|
-
p.url = HOMEPATH
|
46
|
-
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
47
|
-
p.test_globs = ["test/**/test_*.rb"]
|
48
|
-
p.clean_globs = CLEAN #An array of file patterns to delete on clean.
|
49
|
-
|
50
|
-
# == Optional
|
51
|
-
p.extra_deps = [ ['xml-simple', '>= 1.0.0'] ] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
|
52
|
-
p.spec_extras = {:extra_rdoc_files => ["README.txt"], :rdoc_options => RDOC_OPTS} # A hash of extra values to set in the gemspec.
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
desc 'Generate website files'
|
57
|
-
task :website_generate do
|
58
|
-
Dir['website/**/*.txt'].each do |txt|
|
59
|
-
sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
desc 'Upload website files to rubyforge'
|
64
|
-
task :website_upload do
|
65
|
-
config = YAML.load(File.read(File.expand_path("~/.rubyforge/user-config.yml")))
|
66
|
-
host = "#{config["username"]}@rubyforge.org"
|
67
|
-
remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/"
|
68
|
-
# remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
69
|
-
local_dir = 'website'
|
70
|
-
sh %{rsync -av #{local_dir}/ #{host}:#{remote_dir}}
|
71
|
-
end
|
72
|
-
|
73
|
-
desc 'Generate and upload website files'
|
74
|
-
task :website => [:website_generate, :website_upload]
|
data/lib/heywatch/account.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
module HeyWatch
|
2
|
-
class Account < Base #:nodoc:
|
3
|
-
def new_record?
|
4
|
-
false
|
5
|
-
end
|
6
|
-
|
7
|
-
def update_attributes(attributes={})
|
8
|
-
attributes.delete "account"
|
9
|
-
attributes.keys.each do |k|
|
10
|
-
attributes.merge!("user[#{k.to_s}]" => attributes[k])
|
11
|
-
attributes.delete k
|
12
|
-
end
|
13
|
-
|
14
|
-
if Account.update(id, attributes)
|
15
|
-
reload
|
16
|
-
true
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def reload
|
21
|
-
@attributes = Account.find.attributes
|
22
|
-
end
|
23
|
-
|
24
|
-
def id
|
25
|
-
""
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.find(*args)
|
29
|
-
new(HeyWatch::response(Browser::get(self.path, self.session).body))
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
data/lib/heywatch/auth.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
module HeyWatch
|
2
|
-
# Authenticate to the Hey!Watch service
|
3
|
-
#
|
4
|
-
# user = Auth.create("login", "password")
|
5
|
-
# user.account
|
6
|
-
#
|
7
|
-
# If you want to recover a session:
|
8
|
-
#
|
9
|
-
# user = Auth.recover(session_id)
|
10
|
-
# user.account
|
11
|
-
#
|
12
|
-
# The initialize and create methods can take blocks.
|
13
|
-
#
|
14
|
-
# Auth.create("login", "password") do |user|
|
15
|
-
# video = user.videos.find(:first)
|
16
|
-
# format = user.formats.find(:first, :conditions => {:name => "iPod 4:3"})
|
17
|
-
# user.jobs.create :video_id => video.id, :format_id => format.id
|
18
|
-
# end
|
19
|
-
#
|
20
|
-
# All the resources (pluralized) are available within the Auth class:
|
21
|
-
#
|
22
|
-
# jobs, encoded_videos, videos, formats, downloads, discovers, logs, account
|
23
|
-
#
|
24
|
-
class Auth
|
25
|
-
attr_reader :session
|
26
|
-
@@sessions = {}
|
27
|
-
|
28
|
-
# Authenticate to the Hey!Watch service
|
29
|
-
#
|
30
|
-
# options can be:
|
31
|
-
#
|
32
|
-
# * <tt>:login</tt> Hey!Watch username
|
33
|
-
# * <tt>:password</tt> Hey!Watch password
|
34
|
-
# * <tt>:session</tt> A previous session, using this option, you will not be reconnected, you will just recover your session
|
35
|
-
def initialize(options={}, &block)
|
36
|
-
if options[:session]
|
37
|
-
@session = options[:session]
|
38
|
-
else
|
39
|
-
@session = Browser::login(options[:login], options[:password])
|
40
|
-
@@sessions.merge!(@session => true)
|
41
|
-
end
|
42
|
-
yield self if block_given?
|
43
|
-
end
|
44
|
-
|
45
|
-
# Same as initialize
|
46
|
-
#
|
47
|
-
# Auth::create 'login', 'password'
|
48
|
-
def self.create(login, password, &block)
|
49
|
-
new(:login => login, :password => password, &block)
|
50
|
-
end
|
51
|
-
|
52
|
-
# Recover a session
|
53
|
-
#
|
54
|
-
# Auth.recover(session_id)
|
55
|
-
def self.recover(session, &block)
|
56
|
-
#raise SessionNotFound if @@sessions[session].nil?
|
57
|
-
new(:session => session, &block)
|
58
|
-
end
|
59
|
-
|
60
|
-
Resources.each do |k|
|
61
|
-
Auth.module_eval(%{
|
62
|
-
def #{k.to_s+"s"}
|
63
|
-
klass = #{k.to_s.camelize}
|
64
|
-
klass.session = @session
|
65
|
-
klass
|
66
|
-
end
|
67
|
-
}
|
68
|
-
)
|
69
|
-
end
|
70
|
-
|
71
|
-
# Delete the current session
|
72
|
-
def destroy
|
73
|
-
@@sessions.delete(@session)
|
74
|
-
@session = nil
|
75
|
-
end
|
76
|
-
|
77
|
-
# Show account info
|
78
|
-
def account
|
79
|
-
Account.session = @session
|
80
|
-
Account.find(:first)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
data/lib/heywatch/base.rb
DELETED
@@ -1,269 +0,0 @@
|
|
1
|
-
module HeyWatch
|
2
|
-
# You can connect to the Hey!Watch service permanently
|
3
|
-
#
|
4
|
-
# Base::establish_connection! :login => "login", :password => "password"
|
5
|
-
#
|
6
|
-
# You can access all the resources and manipulate the objects like in ActiveRecord
|
7
|
-
#
|
8
|
-
# EncodedVideo.find(:all, :conditions => {:title => /bad video/i}).each do |v|
|
9
|
-
# v.destroy
|
10
|
-
# end
|
11
|
-
#
|
12
|
-
# Format.create :name => "my new format", :video_codec => "mpeg4", ...
|
13
|
-
#
|
14
|
-
# f = Format.new
|
15
|
-
# f.container = "avi"
|
16
|
-
# f.audio_codec = "mp3"
|
17
|
-
# ...
|
18
|
-
# f.save
|
19
|
-
#
|
20
|
-
# v = Video.find(15).update_attributes :title => "My edited title"
|
21
|
-
# puts v.title
|
22
|
-
#
|
23
|
-
# Format.find_by_name("iPod 4:3")
|
24
|
-
#
|
25
|
-
# Format.find_all_by_name /ipod/i
|
26
|
-
class Base
|
27
|
-
attr_reader :attributes, :errors
|
28
|
-
|
29
|
-
class << self
|
30
|
-
def session=(session) #:nodoc:
|
31
|
-
@session = session
|
32
|
-
end
|
33
|
-
|
34
|
-
def session #:nodoc:
|
35
|
-
(@session || Base.session) rescue nil
|
36
|
-
end
|
37
|
-
|
38
|
-
# Establish the connection for all your session
|
39
|
-
def establish_connection!(options={})
|
40
|
-
Base.session = Browser::login(options[:login], options[:password])
|
41
|
-
end
|
42
|
-
|
43
|
-
def disconnect!
|
44
|
-
Base.session = nil
|
45
|
-
end
|
46
|
-
|
47
|
-
def method_missing(m, *args) #:nodoc:
|
48
|
-
if m.to_s =~ /find_by_(.*)/
|
49
|
-
find(:first, :conditions => {$1.to_sym => args.first}) rescue nil
|
50
|
-
elsif m.to_s =~ /find_all_by_(.*)/
|
51
|
-
find :all, {:conditions => {$1.to_sym => args.first}}.merge(args[1]||{})
|
52
|
-
else
|
53
|
-
raise NoMethodError, "undefined method `#{m.to_s}' for #{self}"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def path #:nodoc:
|
58
|
-
return @path if @path
|
59
|
-
"/"+self.to_s.split("::").last.
|
60
|
-
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
61
|
-
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
62
|
-
tr("-", "_").
|
63
|
-
downcase
|
64
|
-
end
|
65
|
-
|
66
|
-
# if the path of the resource is not standard, you can set it manually.
|
67
|
-
#
|
68
|
-
# class Log < Base
|
69
|
-
# self.path = "/user_logs"
|
70
|
-
# end
|
71
|
-
def path=(path) #:nodoc:
|
72
|
-
@path = path
|
73
|
-
end
|
74
|
-
|
75
|
-
# Find objects
|
76
|
-
#
|
77
|
-
# Arguments:
|
78
|
-
#
|
79
|
-
# * <tt>ID</tt> ID of the object
|
80
|
-
# * <tt>:first</tt> retrieve the first result
|
81
|
-
# * <tt>:all</tt> retrieve all the results
|
82
|
-
#
|
83
|
-
# Options:
|
84
|
-
#
|
85
|
-
# * <tt>:conditions</tt> {:fieldname => "keyword"} or {:fieldname => /[0-9]+/}
|
86
|
-
# * <tt>:order</tt> to sort the result
|
87
|
-
# * <tt>:limit</tt> limit the number of result to return
|
88
|
-
# * <tt>:include</tt> fetch the object to include
|
89
|
-
#
|
90
|
-
# Format.find(:all, :conditions => {:name => /mobile/i})
|
91
|
-
# Format.find(:all, :conditions => {:width => '<320'})
|
92
|
-
# Job.find(:all, :include => "encoded_video", :order => "created_at DESC", :limit => 3)
|
93
|
-
# Video.find :first
|
94
|
-
# Download.find(5)
|
95
|
-
def find(*args)
|
96
|
-
scope, options = args
|
97
|
-
options ||= {}
|
98
|
-
case scope
|
99
|
-
when :all then find_every(options)
|
100
|
-
when :first then find_every(options).first
|
101
|
-
else find_single(scope, options)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
private
|
106
|
-
|
107
|
-
def find_every(options) #:nodoc:
|
108
|
-
collection = []
|
109
|
-
res = HeyWatch::response(Browser::get(path, session).body)
|
110
|
-
return collection if res.empty?
|
111
|
-
|
112
|
-
[res[res.keys.first]].flatten.each {|object| collection << new(object)}
|
113
|
-
collection.find_with_options(options)
|
114
|
-
end
|
115
|
-
|
116
|
-
def find_single(arg, options) #:nodoc:
|
117
|
-
new(HeyWatch::response(Browser::get(path+"/"+arg.to_s, session).body)).include_heywatch_object(options[:include])
|
118
|
-
end
|
119
|
-
|
120
|
-
public
|
121
|
-
|
122
|
-
# Create the object
|
123
|
-
#
|
124
|
-
# Download.create :url => "http://host.com/video.avi"
|
125
|
-
def create(attributes={})
|
126
|
-
new(HeyWatch::response(Browser::post(path, attributes, session).body))
|
127
|
-
end
|
128
|
-
|
129
|
-
# Update the object passing its ID
|
130
|
-
#
|
131
|
-
# EncodedVideo.update 15, :title => "my title"
|
132
|
-
def update(id, attributes={})
|
133
|
-
Browser::put(path+"/"+id.to_s, attributes, session)
|
134
|
-
end
|
135
|
-
|
136
|
-
# Destroy the object passing its ID
|
137
|
-
#
|
138
|
-
# Format.destroy 12
|
139
|
-
def destroy(id)
|
140
|
-
Browser::delete(path+"/"+id.to_s, session)
|
141
|
-
end
|
142
|
-
|
143
|
-
# Destroy all the objects
|
144
|
-
#
|
145
|
-
# Video.destroy_all
|
146
|
-
def destroy_all
|
147
|
-
find(:all).each do |object|
|
148
|
-
object.destroy
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
# Count request
|
153
|
-
#
|
154
|
-
# Accept :conditions like in Base#find
|
155
|
-
def count(field=nil, options={})
|
156
|
-
find(:all, options).size
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
# Instanciate a new object
|
161
|
-
#
|
162
|
-
# Format.new :name => "test format", :sample_rate => 24000
|
163
|
-
def initialize(attributes={})
|
164
|
-
@attributes = attributes.underscore_keys!
|
165
|
-
@attributes.type_cast!
|
166
|
-
end
|
167
|
-
|
168
|
-
def include_heywatch_object(objects=nil) #:nodoc:
|
169
|
-
return self if objects.nil?
|
170
|
-
objects = objects.to_s.split(",") if objects.is_a?(String) or objects.is_a?(Symbol)
|
171
|
-
objects.each do |ob|
|
172
|
-
begin
|
173
|
-
self.instance_variable_set "@#{ob}", (self.send(ob) rescue nil)
|
174
|
-
self.instance_eval "attr_reader #{ob.to_sym}"
|
175
|
-
rescue
|
176
|
-
end
|
177
|
-
end
|
178
|
-
self
|
179
|
-
end
|
180
|
-
|
181
|
-
# Save the object.
|
182
|
-
#
|
183
|
-
# If the object doesn't exist, it will be created, otherwise updated.
|
184
|
-
# If an error occurred, @errors will be filled. This method doesn't raise.
|
185
|
-
def save
|
186
|
-
begin
|
187
|
-
save!
|
188
|
-
true
|
189
|
-
rescue => e
|
190
|
-
@errors = e.to_s
|
191
|
-
false
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
# Save the current object
|
196
|
-
#
|
197
|
-
# Raise if an error occurred. Return self.
|
198
|
-
def save!
|
199
|
-
if new_record?
|
200
|
-
self.class.create(@attributes)
|
201
|
-
else
|
202
|
-
update_attributes(@attributes)
|
203
|
-
end
|
204
|
-
self
|
205
|
-
end
|
206
|
-
|
207
|
-
def id #:nodoc:
|
208
|
-
@attributes["id"].to_i
|
209
|
-
end
|
210
|
-
|
211
|
-
# Update the object with the given attributes
|
212
|
-
#
|
213
|
-
# Video.find(10).update_attributes :title => "test title"
|
214
|
-
def update_attributes(attributes={})
|
215
|
-
if self.class.update(id, attributes)
|
216
|
-
reload
|
217
|
-
true
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
# Destroy the object
|
222
|
-
#
|
223
|
-
# Video.find(56).destroy
|
224
|
-
def destroy
|
225
|
-
self.class.destroy(id)
|
226
|
-
end
|
227
|
-
|
228
|
-
# Reload the current object
|
229
|
-
#
|
230
|
-
# j = Job.find(5400)
|
231
|
-
# j.reload
|
232
|
-
def reload
|
233
|
-
unless new_record?
|
234
|
-
@attributes = self.class.find(self.id).attributes
|
235
|
-
end
|
236
|
-
self
|
237
|
-
end
|
238
|
-
|
239
|
-
def new_record? #:nodoc:
|
240
|
-
@attributes["id"].nil?
|
241
|
-
end
|
242
|
-
|
243
|
-
def method_missing(m, *args) #:nodoc:
|
244
|
-
method_name = m.to_s
|
245
|
-
case method_name[-1..-1]
|
246
|
-
when '='
|
247
|
-
@attributes[method_name[0..-2]] = *args.first
|
248
|
-
when '?'
|
249
|
-
@attributes[method_name[0..-2]]
|
250
|
-
else
|
251
|
-
if instance_variables.include?("@#{m.to_s}")
|
252
|
-
eval("@#{m.to_s}")
|
253
|
-
else
|
254
|
-
if object_id = @attributes[m.to_s+"_id"] # belongs_to
|
255
|
-
klass = HeyWatch::const_get(m.to_s.camelize)
|
256
|
-
klass.session = self.class.session
|
257
|
-
klass.find(object_id)
|
258
|
-
else
|
259
|
-
@attributes[m.to_s] rescue nil
|
260
|
-
end
|
261
|
-
end
|
262
|
-
end
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
Resources.each do |k|
|
267
|
-
HeyWatch.module_eval(%{class #{k.to_s.camelize} < Base; end})
|
268
|
-
end
|
269
|
-
end
|