ankoder 0.0.1 → 0.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.tar.gz.sig +0 -0
- data/History.txt +5 -0
- data/Manifest.txt +9 -0
- data/config/hoe.rb +1 -2
- data/lib/ankoder/account.rb +32 -0
- data/lib/ankoder/auth.rb +83 -0
- data/lib/ankoder/base.rb +252 -0
- data/lib/ankoder/browser.rb +98 -0
- data/lib/ankoder/download.rb +17 -0
- data/lib/ankoder/ext.rb +118 -0
- data/lib/ankoder/job.rb +18 -0
- data/lib/ankoder/profile.rb +9 -0
- data/lib/ankoder/version.rb +1 -1
- data/lib/ankoder/video.rb +13 -0
- metadata +10 -1
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -6,7 +6,16 @@ Rakefile
|
|
6
6
|
config/hoe.rb
|
7
7
|
config/requirements.rb
|
8
8
|
lib/ankoder.rb
|
9
|
+
lib/ankoder/account.rb
|
10
|
+
lib/ankoder/auth.rb
|
11
|
+
lib/ankoder/base.rb
|
12
|
+
lib/ankoder/browser.rb
|
13
|
+
lib/ankoder/download.rb
|
14
|
+
lib/ankoder/ext.rb
|
15
|
+
lib/ankoder/job.rb
|
16
|
+
lib/ankoder/profile.rb
|
9
17
|
lib/ankoder/version.rb
|
18
|
+
lib/ankoder/video.rb
|
10
19
|
log/debug.log
|
11
20
|
script/destroy
|
12
21
|
script/generate
|
data/config/hoe.rb
CHANGED
@@ -28,8 +28,7 @@ Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
|
28
28
|
end
|
29
29
|
|
30
30
|
|
31
|
-
REV = nil
|
32
|
-
ENV["VERSION"] = "0.0.1"
|
31
|
+
REV = nil
|
33
32
|
# UNCOMMENT IF REQUIRED:
|
34
33
|
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
|
35
34
|
VERS = Ankoder::VERSION::STRING + (REV ? ".#{REV}" : "")
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Ankoder
|
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(Ankoder::response(Browser::get(self.path, self.session).body))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/ankoder/auth.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
module Ankoder
|
2
|
+
# Authenticate to the ankoder 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
|
+
# profile = user.profiles.find(:first, :conditions => {:name => "iPod 4:3"})
|
17
|
+
# user.jobs.create :original_file_id => video.id, :profile_id => profile.id
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# All the resources (pluralized) are available within the Auth class:
|
21
|
+
#
|
22
|
+
# jobs, videos, profiles, downloads, account
|
23
|
+
#
|
24
|
+
class Auth
|
25
|
+
attr_reader :session
|
26
|
+
@@sessions = {}
|
27
|
+
|
28
|
+
# Authenticate to the _ankoderapi_session service
|
29
|
+
#
|
30
|
+
# options can be:
|
31
|
+
#
|
32
|
+
# * <tt>:login</tt> _ankoderapi_session username
|
33
|
+
# * <tt>:password</tt> _ankoderapi_session 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/ankoder/base.rb
ADDED
@@ -0,0 +1,252 @@
|
|
1
|
+
module Ankoder
|
2
|
+
# You can connect to the _ankoderapi_session service permanently
|
3
|
+
#
|
4
|
+
# You can access all the resources and manipulate the objects like in ActiveRecord
|
5
|
+
#
|
6
|
+
# v = Video.find(15).update_attributes :name => "My edited title"
|
7
|
+
# puts v.title
|
8
|
+
class Base
|
9
|
+
attr_reader :attributes, :errors
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def session=(session) #:nodoc:
|
13
|
+
@session = session
|
14
|
+
end
|
15
|
+
|
16
|
+
def session #:nodoc:
|
17
|
+
(@session || Base.session) rescue nil
|
18
|
+
end
|
19
|
+
|
20
|
+
# Establish the connection for all your session
|
21
|
+
def establish_connection!(options={})
|
22
|
+
Base.session = Browser::login(options[:login], options[:password])
|
23
|
+
end
|
24
|
+
|
25
|
+
def disconnect!
|
26
|
+
Base.session = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def method_missing(m, *args) #:nodoc:
|
30
|
+
if m.to_s =~ /find_by_(.*)/
|
31
|
+
find(:first, :conditions => {$1.to_sym => args.first}) rescue nil
|
32
|
+
elsif m.to_s =~ /find_all_by_(.*)/
|
33
|
+
find :all, {:conditions => {$1.to_sym => args.first}}.merge(args[1]||{})
|
34
|
+
else
|
35
|
+
raise NoMethodError, "undefined method `#{m.to_s}' for #{self}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def path #:nodoc:
|
40
|
+
return @path if @path
|
41
|
+
"/"+self.to_s.split("::").last.
|
42
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
43
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
44
|
+
tr("-", "_").
|
45
|
+
downcase
|
46
|
+
end
|
47
|
+
|
48
|
+
# if the path of the resource is not standard, you can set it manually.
|
49
|
+
#
|
50
|
+
# class Log < Base
|
51
|
+
# self.path = "/user_logs"
|
52
|
+
# end
|
53
|
+
def path=(path) #:nodoc:
|
54
|
+
@path = path
|
55
|
+
end
|
56
|
+
|
57
|
+
# Find objects
|
58
|
+
#
|
59
|
+
# Arguments:
|
60
|
+
#
|
61
|
+
# * <tt>ID</tt> ID of the object
|
62
|
+
# * <tt>:first</tt> retrieve the first result
|
63
|
+
# * <tt>:all</tt> retrieve all the results
|
64
|
+
#
|
65
|
+
# Options:
|
66
|
+
#
|
67
|
+
# * <tt>:conditions</tt> {:fieldname => "keyword"} or {:fieldname => /[0-9]+/}
|
68
|
+
# * <tt>:order</tt> to sort the result
|
69
|
+
# * <tt>:limit</tt> limit the number of result to return
|
70
|
+
# * <tt>:include</tt> fetch the object to include
|
71
|
+
#
|
72
|
+
# Profile.find(:all, :conditions => {:name => /mobile/i})
|
73
|
+
# Profile.find(:all, :conditions => {:width => '<320'})
|
74
|
+
# Job.find(:all, :include => "video", :order => "created_at DESC", :limit => 3)
|
75
|
+
# Video.find :first
|
76
|
+
# Download.find(5)
|
77
|
+
def find(*args)
|
78
|
+
scope, options = args
|
79
|
+
options ||= {}
|
80
|
+
case scope
|
81
|
+
when :all then find_every(options)
|
82
|
+
when :first then find_every(options).first
|
83
|
+
else find_single(scope, options)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def find_every(options) #:nodoc:
|
90
|
+
collection = []
|
91
|
+
res = Ankoder::response(Browser::get(path, session).body)
|
92
|
+
|
93
|
+
return collection if res.empty?
|
94
|
+
|
95
|
+
[res[res.keys.first]].flatten.each {|object| collection << new(object)}
|
96
|
+
collection.find_with_options(options)
|
97
|
+
end
|
98
|
+
|
99
|
+
def find_single(arg, options) #:nodoc:
|
100
|
+
new(Ankoder::response(Browser::get(path+"/"+arg.to_s, session).body)).include_ankoder_object(options[:include])
|
101
|
+
end
|
102
|
+
|
103
|
+
public
|
104
|
+
|
105
|
+
# Create the object
|
106
|
+
#
|
107
|
+
# Download.create :url => "http://host.com/video.avi"
|
108
|
+
def create(attributes={})
|
109
|
+
new(Ankoder::response(Browser::post(path, attributes, session).body))
|
110
|
+
end
|
111
|
+
|
112
|
+
# Update the object passing its ID
|
113
|
+
#
|
114
|
+
# Video.update 15, :name => "my title"
|
115
|
+
def update(id, attributes={})
|
116
|
+
Browser::put(path+"/"+id.to_s, attributes, session)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Destroy the object passing its ID
|
120
|
+
#
|
121
|
+
# Profile.destroy 12
|
122
|
+
def destroy(id)
|
123
|
+
Browser::delete(path+"/"+id.to_s, session)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Destroy all the objects
|
127
|
+
#
|
128
|
+
# Video.destroy_all
|
129
|
+
def destroy_all
|
130
|
+
find(:all).each do |object|
|
131
|
+
object.destroy
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# Count request
|
136
|
+
#
|
137
|
+
# Accept :conditions like in Base#find
|
138
|
+
def count(field=nil, options={})
|
139
|
+
find(:all, options).size
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
# Instanciate a new object
|
144
|
+
#
|
145
|
+
# Profile.new :name => "test format", :sample_rate => 24000
|
146
|
+
def initialize(attributes={})
|
147
|
+
@attributes = attributes.underscore_keys!
|
148
|
+
@attributes.type_cast!
|
149
|
+
end
|
150
|
+
|
151
|
+
def include_ankoder_object(objects=nil) #:nodoc:
|
152
|
+
return self if objects.nil?
|
153
|
+
objects = objects.to_s.split(",") if objects.is_a?(String) or objects.is_a?(Symbol)
|
154
|
+
objects.each do |ob|
|
155
|
+
begin
|
156
|
+
self.instance_variable_set "@#{ob}", (self.send(ob) rescue nil)
|
157
|
+
self.instance_eval "attr_reader #{ob.to_sym}"
|
158
|
+
rescue
|
159
|
+
end
|
160
|
+
end
|
161
|
+
self
|
162
|
+
end
|
163
|
+
|
164
|
+
# Save the object.
|
165
|
+
#
|
166
|
+
# If the object doesn't exist, it will be created, otherwise updated.
|
167
|
+
# If an error occurred, @errors will be filled. This method doesn't raise.
|
168
|
+
def save
|
169
|
+
begin
|
170
|
+
save!
|
171
|
+
true
|
172
|
+
rescue => e
|
173
|
+
@errors = e.to_s
|
174
|
+
false
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# Save the current object
|
179
|
+
#
|
180
|
+
# Raise if an error occurred. Return self.
|
181
|
+
def save!
|
182
|
+
if new_record?
|
183
|
+
self.class.create(@attributes)
|
184
|
+
else
|
185
|
+
update_attributes(@attributes)
|
186
|
+
end
|
187
|
+
self
|
188
|
+
end
|
189
|
+
|
190
|
+
def id #:nodoc:
|
191
|
+
@attributes["id"].to_i
|
192
|
+
end
|
193
|
+
|
194
|
+
# Update the object with the given attributes
|
195
|
+
#
|
196
|
+
# Video.find(10).update_attributes :name => "test title"
|
197
|
+
def update_attributes(attributes={})
|
198
|
+
if self.class.update(id, attributes)
|
199
|
+
reload
|
200
|
+
true
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Destroy the object
|
205
|
+
#
|
206
|
+
# Video.find(56).destroy
|
207
|
+
def destroy
|
208
|
+
self.class.destroy(id)
|
209
|
+
end
|
210
|
+
|
211
|
+
# Reload the current object
|
212
|
+
#
|
213
|
+
# j = Job.find(5400)
|
214
|
+
# j.reload
|
215
|
+
def reload
|
216
|
+
unless new_record?
|
217
|
+
@attributes = self.class.find(self.id).attributes
|
218
|
+
end
|
219
|
+
self
|
220
|
+
end
|
221
|
+
|
222
|
+
def new_record? #:nodoc:
|
223
|
+
@attributes["id"].nil?
|
224
|
+
end
|
225
|
+
|
226
|
+
def method_missing(m, *args) #:nodoc:
|
227
|
+
method_name = m.to_s
|
228
|
+
case method_name[-1..-1]
|
229
|
+
when '='
|
230
|
+
@attributes[method_name[0..-2]] = *args.first
|
231
|
+
when '?'
|
232
|
+
@attributes[method_name[0..-2]]
|
233
|
+
else
|
234
|
+
if instance_variables.include?("@#{m.to_s}")
|
235
|
+
eval("@#{m.to_s}")
|
236
|
+
else
|
237
|
+
if object_id = @attributes[m.to_s+"_id"] # belongs_to
|
238
|
+
klass = Ankoder::const_get(m.to_s.camelize)
|
239
|
+
klass.session = self.class.session
|
240
|
+
klass.find(object_id)
|
241
|
+
else
|
242
|
+
@attributes[m.to_s] rescue nil
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
RESOURCES.each do |k|
|
250
|
+
Ankoder.module_eval(%{class #{k.to_s.camelize} < Base; end})
|
251
|
+
end
|
252
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require "net/http"
|
2
|
+
|
3
|
+
module Ankoder
|
4
|
+
# This class is used to request the _ankoderapi_session service.
|
5
|
+
#
|
6
|
+
# Browser::get '/video', session
|
7
|
+
# Browser::post '/download', {:url => 'http://host.com/video.avi'}, session
|
8
|
+
# Browser::put '/video/54000', {:name => 'new title'}, session
|
9
|
+
# Browser::delete '/video/54000', session
|
10
|
+
class Browser
|
11
|
+
class << self
|
12
|
+
# Raise when code response != 2xx
|
13
|
+
def raise_if_response_error(res)
|
14
|
+
code = res.response.code.to_i
|
15
|
+
message = res.response.message
|
16
|
+
return if code.to_s =~ /^2/
|
17
|
+
|
18
|
+
raise RequestError, Ankoder::response(res.body).content if code == 400
|
19
|
+
raise NotAuthorized, message if code == 401
|
20
|
+
raise ResourceNotFound, message if code == 404
|
21
|
+
raise ServerError, message if code == 500
|
22
|
+
end
|
23
|
+
|
24
|
+
def header(session=nil) #:nodoc:
|
25
|
+
h = {}
|
26
|
+
h.merge!({"Cookie" => "_ankoderapi_session=#{session};"}) if session
|
27
|
+
h.merge!({"User-Agent" => "_ankoderapi_session ruby API - #{VERSION::STRING}"})
|
28
|
+
end
|
29
|
+
|
30
|
+
# Login to _ankoderapi_session service. Return the session ID.
|
31
|
+
#
|
32
|
+
# You should not use it directly, use Auth#create instead
|
33
|
+
#
|
34
|
+
# Browser::login 'login', 'password'
|
35
|
+
def login(login, password) #:nodoc:
|
36
|
+
res = Browser::post("/auth/login", :login => login, :password => password)
|
37
|
+
return res["Set-cookie"].match(/_ankoderapi_session=(.*);/i)[1].to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
# GET on path
|
41
|
+
def get(path, session=nil)
|
42
|
+
path += ".#{OUT_FORMAT}" unless path.include? "."
|
43
|
+
res = Net::HTTP.start(HOST) {|http| http.get(path, header(session))}
|
44
|
+
raise_if_response_error(res)
|
45
|
+
res
|
46
|
+
end
|
47
|
+
|
48
|
+
# POST on path and pass the query(Hash)
|
49
|
+
def post(path, query={}, session=nil)
|
50
|
+
res = Net::HTTP.start(HOST) {|http| http.post(path, query.merge(:format => OUT_FORMAT).to_a.map{|x| x.join("=")}.join("&"), self.header(session))}
|
51
|
+
raise_if_response_error(res)
|
52
|
+
res
|
53
|
+
end
|
54
|
+
|
55
|
+
# PUT on path and pass the query(Hash)
|
56
|
+
def put(path, query={}, session=nil)
|
57
|
+
req = Net::HTTP::Put.new(path, header(session))
|
58
|
+
req.form_data = query.merge(:format => OUT_FORMAT)
|
59
|
+
res = Net::HTTP.new(HOST).start {|http| http.request(req) }
|
60
|
+
raise_if_response_error(res)
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
# DELETE on path
|
65
|
+
def delete(path, session=nil)
|
66
|
+
res = Net::HTTP.start(HOST) {|http| http.delete(path+"."+OUT_FORMAT, header(session))}
|
67
|
+
raise_if_response_error(res)
|
68
|
+
true
|
69
|
+
end
|
70
|
+
|
71
|
+
def post_multipart(path, attributes={}, session=nil) #:nodoc:
|
72
|
+
file = attributes.delete(:file)
|
73
|
+
params = [file_to_multipart("data", File.basename(file),"application/octet-stream", File.read(file))]
|
74
|
+
attributes.merge("format" => OUT_FORMAT).each_pair{|k,v| params << text_to_multipart(k.to_s, v.to_s)}
|
75
|
+
|
76
|
+
boundary = '349832898984244898448024464570528145'
|
77
|
+
query = params.collect {|p| '--' + boundary + "\r\n" + p}.join('') + "--" + boundary + "--\r\n"
|
78
|
+
res = Net::HTTP.start(HOST) {|http| http.post(path, query, header(session).merge("Content-Type" => "multipart/form-data; boundary=" + boundary))}
|
79
|
+
raise_if_response_error(res)
|
80
|
+
res
|
81
|
+
end
|
82
|
+
|
83
|
+
def text_to_multipart(key,value) #:nodoc:
|
84
|
+
return "Content-Disposition: form-data; name=\"#{CGI::escape(key.to_s)}\"\r\n" +
|
85
|
+
"\r\n" +
|
86
|
+
"#{value}\r\n"
|
87
|
+
end
|
88
|
+
|
89
|
+
def file_to_multipart(key,filename,mime_type,content) #:nodoc:
|
90
|
+
return "Content-Disposition: form-data; name=\"#{CGI::escape(key.to_s)}\"; filename=\"#{filename}\"\r\n" +
|
91
|
+
"Content-Transfer-Encoding: binary\r\n" +
|
92
|
+
"Content-Type: #{mime_type}\r\n" +
|
93
|
+
"\r\n" +
|
94
|
+
"#{content}\r\n"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Ankoder
|
2
|
+
class DownloadFailed < RuntimeError; end
|
3
|
+
|
4
|
+
class Download < Base
|
5
|
+
# Download file from the given URL
|
6
|
+
#
|
7
|
+
# Download.create('url' => 'http://host.com/file.avi', :postback_url => 'http://your_own_host.com/postback/download')
|
8
|
+
#
|
9
|
+
# the return result would be the Video record that you can save for further checking
|
10
|
+
#
|
11
|
+
# if the postback url is included, the download result will send back to your server in JSON format
|
12
|
+
def self.create(attributes={})
|
13
|
+
download = super
|
14
|
+
return download
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/ankoder/ext.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
class String
|
2
|
+
# convert "undercore_string" in "UnderscoreString"
|
3
|
+
#
|
4
|
+
# Method from Rails
|
5
|
+
def camelize
|
6
|
+
self.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Ankoder
|
11
|
+
module CoreExtension
|
12
|
+
module HashExtension
|
13
|
+
# Replace "-" by "_" in all keys of the hash
|
14
|
+
def underscore_keys!
|
15
|
+
self.keys.each do |k|
|
16
|
+
self[k] = self[k].underscore_keys! if self[k].is_a?(Hash)
|
17
|
+
self[k.to_s.gsub("-", "_")] = self[k]
|
18
|
+
delete(k) if k.to_s =~ /-/
|
19
|
+
end
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
# Cast each value of the hash in the right format (integer, float, time, boolean)
|
24
|
+
def type_cast!
|
25
|
+
self.keys.each do |k|
|
26
|
+
self[k] = self[k].type_cast! if self[k].is_a?(Hash)
|
27
|
+
self[k] = self[k].to_i if self[k] =~ /^[0-9]+$/
|
28
|
+
self[k] = self[k].to_f if self[k] =~ /^[0-9]+\.[0-9]+$/
|
29
|
+
self[k] = Time.parse(self[k]) if self[k] =~ /[a-z]+ [a-z]+ [0-9]+ [0-9]{2}:[0-9]{2}:[0-9]{2} [a-z0-9\+]+ [0-9]{4}/i
|
30
|
+
self[k] = true if self[k] == "true"
|
31
|
+
self[k] = false if self[k] == "false"
|
32
|
+
self[k] = nil if self[k] == {}
|
33
|
+
end
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
# The hash keys are available by calling the method name "key"
|
38
|
+
#
|
39
|
+
# h = {"profile_id" => 54, "video_id" => 12}
|
40
|
+
# h.profile_id
|
41
|
+
# => 54
|
42
|
+
def method_missing(m, *args)
|
43
|
+
if has_key? m.to_s
|
44
|
+
self[m.to_s]
|
45
|
+
else
|
46
|
+
raise NoMethodError, "undefined method `#{m.to_s}' for #{self.class}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
module ArrayExtension
|
53
|
+
def find_with_options(options={})
|
54
|
+
collection = self
|
55
|
+
if options[:conditions]
|
56
|
+
options[:conditions].each_pair do |k,v|
|
57
|
+
collection = collection.find_with_conditions(k => v)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
collection.limit(options[:limit]).order(options[:order]).include_ankoder_object(options[:include])
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
# Select Ankoder#Base object containing the conditions
|
65
|
+
#
|
66
|
+
# Profile.find(:all).find_with_conditions(:name => "aaa")
|
67
|
+
def find_with_conditions(conditions={})
|
68
|
+
return self if conditions.nil? or conditions.empty?
|
69
|
+
res = []
|
70
|
+
self.each do |object|
|
71
|
+
object.attributes.each_pair do |k,v|
|
72
|
+
if conditions[k.to_sym]
|
73
|
+
condition = []
|
74
|
+
if not conditions[k.to_sym].is_a?(Regexp) and conditions[k.to_sym].to_s =~ /([<|>|<=|>=|==]+)[\s]*([0-9]+)$/
|
75
|
+
conditions[k.to_sym].to_s.split(" and ").each do |c|
|
76
|
+
c.match(/([<|>|<=|>=|==]+)[\s]*([0-9]+)$/)
|
77
|
+
condition << "#{v} #{$1} #{$2}"
|
78
|
+
end
|
79
|
+
res << object if eval(condition.join(" and "))
|
80
|
+
elsif conditions[k.to_sym].is_a?(Regexp)
|
81
|
+
res << object if v =~ conditions[k.to_sym]
|
82
|
+
else
|
83
|
+
res << object if v == conditions[k.to_sym]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
return res
|
89
|
+
end
|
90
|
+
|
91
|
+
def limit(lim=nil)
|
92
|
+
return self if lim.nil?
|
93
|
+
self[0..lim-1]
|
94
|
+
end
|
95
|
+
|
96
|
+
def order(order_str=nil)
|
97
|
+
return self if order_str.nil?
|
98
|
+
field, sort = order_str.match(/([a-z0-9_]+)([\s+]*[ASC|DESC]*)/i).to_a[1..-1]
|
99
|
+
sort = "ASC" if sort.nil? or sort.empty?
|
100
|
+
begin
|
101
|
+
self.sort do |x,y|
|
102
|
+
case sort
|
103
|
+
when /ASC/i then x.send(field) <=> y.send(field)
|
104
|
+
when /DESC/i then y.send(field) <=> x.send(field)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
rescue
|
108
|
+
self
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def include_ankoder_object(inc=nil)
|
113
|
+
return self if inc.nil?
|
114
|
+
self.each {|object| object.include_ankoder_object(inc)}
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
data/lib/ankoder/job.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Ankoder
|
2
|
+
class JobFailed < RuntimeError; end
|
3
|
+
|
4
|
+
class Job < Base
|
5
|
+
# Encode the given video into the given format
|
6
|
+
#
|
7
|
+
# Video_ID is the video id that get from Ankoder::Download
|
8
|
+
# Profile_ID is selected from the list of Ankoder::Profile.find(:all)
|
9
|
+
#
|
10
|
+
# Job.create(:original_file_id => "Video_ID", :profile_id => "Profile_ID",
|
11
|
+
# :postback_url => 'http://your_own_host.com/postback/job')
|
12
|
+
def self.create(attributes={}, &block)
|
13
|
+
job = super
|
14
|
+
return job
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
data/lib/ankoder/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ankoder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ""
|
6
6
|
authors:
|
7
7
|
- RoRCraft.com
|
@@ -56,7 +56,16 @@ files:
|
|
56
56
|
- config/hoe.rb
|
57
57
|
- config/requirements.rb
|
58
58
|
- lib/ankoder.rb
|
59
|
+
- lib/ankoder/account.rb
|
60
|
+
- lib/ankoder/auth.rb
|
61
|
+
- lib/ankoder/base.rb
|
62
|
+
- lib/ankoder/browser.rb
|
63
|
+
- lib/ankoder/download.rb
|
64
|
+
- lib/ankoder/ext.rb
|
65
|
+
- lib/ankoder/job.rb
|
66
|
+
- lib/ankoder/profile.rb
|
59
67
|
- lib/ankoder/version.rb
|
68
|
+
- lib/ankoder/video.rb
|
60
69
|
- log/debug.log
|
61
70
|
- script/destroy
|
62
71
|
- script/generate
|
metadata.gz.sig
CHANGED
Binary file
|