Sha256: c8f9951590b446e050f47e919eb471c5340e683163bf4adf290ae6fef2933c88

Contents?: true

Size: 529 Bytes

Versions: 1

Compression:

Stored size: 529 Bytes

Contents

require 'rexml/document'
require 'cgi'
require 'open-uri'

module YoutubeSearch
  def self.search(query)
    xml = open("http://gdata.youtube.com/feeds/api/videos/-/#{CGI.escape(query)}").read
    parse(xml)
  end

  def self.parse(xml)
    entries = []
    doc = REXML::Document.new(xml)
    doc.elements.each('feed/entry') do |p|
      entry = Hash[p.children.map do |child|
        [child.name, child.text]
      end]

      entry['video_id'] = entry['id'].split('/').last
      entries << entry
    end
    entries
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
youtube_search-0.1.0 lib/youtube_search.rb