#-- # Copyright (c) 2006 Shane Vitarana # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ require 'net/http' require 'xmlsimple' HOST = 'http://youtube.com' API = '/api2_rest' # Module that does all the work to send out the HTTP request and retrieve # the XML response. Inspired by the Flickr interface by Scott Raymond # . module DirtyWork # All API methods are implemented with this method. # This method is like a remote method call, it encapsulates # the request/response cycle to the remote host. It extracts # the remote method API name based on the ruby method name. private def method_missing(method_id, *params) # params[0] is the argument to the YouTube API request(method_id.to_s.sub('_', '.'), *params) end private def request(method, *params) response = XmlSimple.xml_in(http_get(request_url(method, *params)), { 'ForceArray' => false }) raise response['error']['description'] if response['status'] != 'ok' response end private def request_url(method, *params) param_list = String.new params[0].each_pair do |k,v| param_list << "&"+k.to_s+"="+v.to_s end url = "#{HOST}#{API}?method=youtube."+method+"&dev_id="+@dev_id+param_list end private def http_get(url) Net::HTTP.get_response(URI.parse(url)).body.to_s end end