Class: Stupeflix::Video

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/stupeflix.rb

Instance Method Summary collapse

Constructor Details

#initialize(id, key, secret) ⇒ Video

Returns a new instance of Video.



11
12
13
14
# File 'lib/stupeflix.rb', line 11

def initialize id, key, secret
  @id, @key, @secret = id, key, secret
  self.class.default_params AccessKey: key
end

Instance Method Details

#definition=(d) ⇒ Object



22
23
24
25
26
# File 'lib/stupeflix.rb', line 22

def definition= d
  params, headers = req('PUT', d, 'text/xml', Time.now.to_i, url = "#{self.url}/definition/")
  r = self.class.put url, query: params, headers: headers, body: d
  raise "Invalid response: #{r.response.code}" unless r.response.code.to_i == 200
end

#profiles=(profiles_xml) ⇒ Object

TODO make this an array or..?



29
30
31
32
33
34
# File 'lib/stupeflix.rb', line 29

def profiles= profiles_xml
  params, headers = req('POST', body = "ProfilesXML=#{::CGI::escape profiles_xml}",
                        'application/x-www-form-urlencoded', Time.now.to_i, url = "#{self.url}/")
  r = self.class.post url, query: params, headers: headers, body: body
  raise "Invalid response: #{r.response.code}" unless r.response.code.to_i == 200
end

#req(method, body, mime, t, url) ⇒ Object



16
17
18
19
20
# File 'lib/stupeflix.rb', line 16

def req method, body, mime, t, url
  md5hex, md5base64 = md5(body) rescue [nil, nil] # md5hex can be compared to etag in response for verification
  [{ Date: t, Signature: sign([method, md5base64, mime, t, url, nil]*"\n") },
   body ? { 'Content-MD5' => md5base64.to_s, 'Content-Length' => body.length.to_s, 'Content-Type' => mime } : nil]
end

#statusObject



36
37
38
39
40
# File 'lib/stupeflix.rb', line 36

def status
  params, headers = req('GET', nil, nil, Time.now.to_i, url = "#{self.url}/status/")
  r = self.class.get url, query: params #, format: :json
  r.parsed_response rescue r
end

#urlObject



42
43
44
# File 'lib/stupeflix.rb', line 42

def url
  "/stupeflix-1.0/#{@id}" # user/resource
end