Class: YTClient
Direct Known Subclasses
Constant Summary collapse
- UPLOAD_URI =
"http://uploads.gdata.youtube.com/feeds/api/users/default/uploads"
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#developer_key ⇒ Object
Returns the value of attribute developer_key.
-
#password ⇒ Object
Returns the value of attribute password.
-
#token ⇒ Object
Returns the value of attribute token.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #all ⇒ Object
- #check_video(id) ⇒ Object
- #comments(id) ⇒ Object
- #count ⇒ Object
- #delete(id) ⇒ Object
-
#initialize(username, password, key, options = {:refresh=>300}) ⇒ YTClient
constructor
A new instance of YTClient.
- #ratings(id) ⇒ Object
- #update(id, xml) ⇒ Object
- #upload(file, options = {}) ⇒ Object
Constructor Details
#initialize(username, password, key, options = {:refresh=>300}) ⇒ YTClient
Returns a new instance of YTClient.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/yt_client.rb', line 17 def initialize(username, password, key, ={:refresh=>300}) @username = username @password = password @developer_key = key @client = GData::Client::YouTube.new @client.source = "acer_timeline_contest" @client.developer_key = @developer_key @token = @client.clientlogin(@username, @password) @options = end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/yt_client.rb', line 9 def client @client end |
#developer_key ⇒ Object
Returns the value of attribute developer_key.
9 10 11 |
# File 'lib/yt_client.rb', line 9 def developer_key @developer_key end |
#password ⇒ Object
Returns the value of attribute password.
9 10 11 |
# File 'lib/yt_client.rb', line 9 def password @password end |
#token ⇒ Object
Returns the value of attribute token.
9 10 11 |
# File 'lib/yt_client.rb', line 9 def token @token end |
#username ⇒ Object
Returns the value of attribute username.
9 10 11 |
# File 'lib/yt_client.rb', line 9 def username @username end |
Instance Method Details
#all ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/yt_client.rb', line 28 def all if @all_vids if Time.parse((@all_vids/"updated").text) < (Time.now - @options[:refresh]) @all_vids = Hpricot.XML(@client.get(self.class.base_uri + "/users/default/uploads").body) else return @all_vids end else @all_vids = Hpricot.XML(@client.get(self.class.base_uri + "/users/default/uploads").body) end end |
#check_video(id) ⇒ Object
44 45 46 |
# File 'lib/yt_client.rb', line 44 def check_video(id) Hpricot.XML(@client.get(self.class.base_uri + "/videos/#{id}").body) end |
#comments(id) ⇒ Object
58 59 60 |
# File 'lib/yt_client.rb', line 58 def comments(id) Hpricot.XML(@client.get(self.class.base_uri + "/videos/#{id}/comments").body) end |
#count ⇒ Object
40 41 42 |
# File 'lib/yt_client.rb', line 40 def count (@all_vids/"entry").nitems end |
#delete(id) ⇒ Object
111 112 113 |
# File 'lib/yt_client.rb', line 111 def delete(id) response = @client.delete(self.class.base_uri + "/users/default/uploads/#{id}") end |
#ratings(id) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/yt_client.rb', line 48 def (id) response = Hpricot.XML(@client.get(self.class.base_uri + "/videos/#{id}").body) = (response/"gd:rating") if .nitems > 0 return else return nil end end |
#update(id, xml) ⇒ Object
107 108 109 |
# File 'lib/yt_client.rb', line 107 def update(id, xml) response = @client.put(self.class.base_uri + "/users/default/uploads/#{id}", xml) end |
#upload(file, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/yt_client.rb', line 62 def upload(file, ={}) upload_uri = URI.parse(UPLOAD_URI) binary_data = read_file(file) request_data = <<-REQDATA --bbe873dc Content-Type: application/atom+xml; charset=utf-8 <?xml version="1.0"?> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"> <media:group> <media:title type="plain">#{[:title]}</media:title> <media:description type="plain"> #{[:description]} </media:description> <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat"> People </media:category> <media:keywords>#{[:keywords]}</media:keywords> </media:group> </entry> --bbe873dc Content-Type: #{[:content_type]} Content-Transfer-Encoding: binary #{binary_data} --bbe873dc REQDATA http = Net::HTTP.new(upload_uri.host) http.read_timeout = 6000 headers = { 'GData-Version' => "2", 'X-GData-Key' => "key=#{@developer_key}", 'Slug' => File.basename(file), 'Authorization' => "GoogleLogin auth=#{@token}", 'Content-Type' => 'multipart/related; boundary="bbe873dc"', 'Content-Length' => binary_data.length.to_s, 'Connection' => 'close' } res = http.post(upload_uri.path, request_data, headers) response = {:code => res.code, :body => Hpricot.XML(res.body)} return response end |