Class: Ankoder::Browser

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

Overview

This class is used to request the _ankoderapi_session service.

Browser::get '/video', session
Browser::post '/download', {:url => 'http://host.com/video.avi'}, session
Browser::put '/video/54000', {:name => 'new title'}, session
Browser::delete '/video/54000', session

Class Method Summary collapse

Class Method Details

.delete(path, session = nil) ⇒ Object

DELETE on path



74
75
76
77
78
# File 'lib/ankoder/browser.rb', line 74

def delete(path, session=nil)
  res = Net::HTTP.start(Configuration::host,Configuration::port) {|http| http.delete(path+"."+OUT_FORMAT, header(session,"DELETE",path))}
  raise_if_response_error(res)
  true
end

.file_to_multipart(key, filename, mime_type, content) ⇒ Object

:nodoc:



98
99
100
101
102
103
104
# File 'lib/ankoder/browser.rb', line 98

def file_to_multipart(key,filename,mime_type,content) #:nodoc:
  return "Content-Disposition: form-data; name=\"#{CGI::escape(key.to_s)}\"; filename=\"#{filename}\"\r\n" +
         "Content-Transfer-Encoding: binary\r\n" +
         "Content-Type: #{mime_type}\r\n" + 
         "\r\n" + 
         "#{content}\r\n"
end

.get(path, session = nil) ⇒ Object

GET on path



50
51
52
53
54
55
# File 'lib/ankoder/browser.rb', line 50

def get(path, session=nil)
  path += ".#{OUT_FORMAT}" unless path.include? "."
  res = Net::HTTP.start(Configuration::host,Configuration::port) {|http| http.get(path, header(session,"GET",path))}
  raise_if_response_error(res)
  res
end

.header(session = nil, action = nil, path = nil) ⇒ Object

:nodoc:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ankoder/browser.rb', line 24

def header(session=nil,action=nil,path=nil) #:nodoc:
  h = {}
  h.merge!({"Cookie" => "_ankoderapi_session=#{session};"}) if session
  h.merge!({"User-Agent" => "_ankoderapi_session ruby API - #{VERSION::STRING}"})
  if !Configuration::access_key.blank? and !Configuration::private_key.blank?
    h.merge!({"ankoder_access_key" => Configuration::access_key}) 
    h.merge!({"ankoder_date" => Time.now.httpdate })
    
    salt = Digest::SHA1.hexdigest("-#{Time.now.httpdate}-#{action}-#{path}-")[0..19]
    passkey = Base64.encode64(HMAC::SHA1::digest(Configuration::private_key, salt)).strip
    h.merge!({"ankoder_passkey" => "#{passkey}"}) 
  end  
  h
end

.login(login, password) ⇒ Object

Login to _ankoderapi_session service. Return the session ID.

You should not use it directly, use Auth#create instead

Browser:: 'login', 'password'


44
45
46
47
# File 'lib/ankoder/browser.rb', line 44

def (, password) #:nodoc:
  res = Browser::post("/auth/login", :login => , :password => password)
  return res["Set-cookie"].match(/_ankoderapi_session=(.*);/i)[1].to_s
end

.post(path, query = {}, session = nil) ⇒ Object

POST on path and pass the query(Hash)



58
59
60
61
62
# File 'lib/ankoder/browser.rb', line 58

def post(path, query={}, session=nil)
  res = Net::HTTP.start(Configuration::host,Configuration::port) {|http| http.post(path, query.merge(:format => OUT_FORMAT).to_a.map{|x| x.join("=")}.join("&"), self.header(session,"POST",path))}
  raise_if_response_error(res)
  res
end

.post_multipart(path, attributes = {}, session = nil) ⇒ Object

:nodoc:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ankoder/browser.rb', line 80

def post_multipart(path, attributes={}, session=nil) #:nodoc:
  file = attributes.delete(:file)
  params = [file_to_multipart("data", File.basename(file),"application/octet-stream", File.read(file))]
  attributes.merge("format" => OUT_FORMAT).each_pair{|k,v| params << text_to_multipart(k.to_s, v.to_s)}

  boundary = '349832898984244898448024464570528145'
  query = params.collect {|p| '--' + boundary + "\r\n" + p}.join('') + "--" + boundary + "--\r\n"
  res = Net::HTTP.start(Configuration::host,Configuration::port) {|http| http.post(path, query, header(session,"POST",path).merge("Content-Type" => "multipart/form-data; boundary=" + boundary))}
  raise_if_response_error(res)
  res
end

.put(path, query = {}, session = nil) ⇒ Object

PUT on path and pass the query(Hash)



65
66
67
68
69
70
71
# File 'lib/ankoder/browser.rb', line 65

def put(path, query={}, session=nil)
  req = Net::HTTP::Put.new(path, header(session,"PUT",path))
  req.form_data = query.merge(:format => OUT_FORMAT)
  res = Net::HTTP.new(Configuration::host,Configuration::port).start {|http| http.request(req) }
  raise_if_response_error(res)
  true
end

.raise_if_response_error(res) ⇒ Object

Raise when code response != 2xx

Raises:



13
14
15
16
17
18
19
20
21
22
# File 'lib/ankoder/browser.rb', line 13

def raise_if_response_error(res)
  code = res.response.code.to_i
  message = res.response.message
  return if code.to_s =~ /^2/
  
  raise RequestError, Ankoder::response(res.body).content if code == 400
  raise NotAuthorized, message if code == 401
  raise ResourceNotFound, message if code == 404
  raise ServerError, message if code == 500
end

.text_to_multipart(key, value) ⇒ Object

:nodoc:



92
93
94
95
96
# File 'lib/ankoder/browser.rb', line 92

def text_to_multipart(key,value) #:nodoc:
  return "Content-Disposition: form-data; name=\"#{CGI::escape(key.to_s)}\"\r\n" + 
         "\r\n" + 
         "#{value}\r\n"
end