require 'digest/md5' require 'httparty' module Framey module Api def self.get_video(video_name) res = self.make_request("get","/api/videos/#{video_name}") return Video.new(res["video"]) end def self.delete_video(video_name) res = self.make_request("delete","/api/videos/#{video_name}") return true end def self.make_request(method,url,params={}) timestamp, signature = self.sign params = params.merge({:time_stamp => timestamp, :signature => signature, :api_key => Framey.api_key}) res = begin HTTParty.send(method,Framey.api_host + url,{:query => params}) rescue SocketError => e nil end parsed_res = res ? res.parsed_response : nil if !res || parsed_res["success"] == false err = nil msg = parsed_res ? parsed_res["message"] : "There was a problem connecting to framey. Please try again." case res.code when 400 err = BadRequest.new(msg,res.headers) when 403 err = Forbidden.new(msg,res.headers) when 404 err = NotFound.new(msg,res.headers) when 500 err = InternalServerError.new(msg,res.headers) else err = SocketError.new(msg) end raise err end parsed_res end def self.sign timestamp = Time.now.utc + Framey.api_timeout * 60 signature = Digest::MD5.hexdigest(Framey.api_secret + "&" + timestamp.strftime("%m%d%Y%H%M%S")) return timestamp.to_s, signature end class Error < StandardError attr_reader :http_headers def initialize(message, http_headers={}) @http_headers = Hash[http_headers] super message end end # 400 class BadRequest < Error; end # 403 class Forbidden < Error; end # 404 class NotFound < Error; end # 500 class InternalServerError < Error; end end class Video attr_accessor :name, :url, :filesize, :duration, :state, :views, :data, :url, :thumbnail def initialize(attrs={}) attrs.each do |k,v| self.send("#{k.to_s}=",v) end end def delete! Framey::Api::delete_video(self.name) end end end