Created
March 31, 2015 20:37
-
-
Save bullfight/1356015df30ad88a9160 to your computer and use it in GitHub Desktop.
Read a Youtube channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rss' | |
class YoutubeChannel | |
Video = Struct.new(:response) do | |
def title | |
response.title | |
end | |
def uri_params | |
URI(response.link).query | |
end | |
def id | |
Rack::Utils.parse_query(uri_params)["v"] | |
end | |
def published_at | |
response.pubDate | |
end | |
end | |
include Enumerable | |
include Virtus.model | |
attribute :feed | |
# Avenged Sevenfold Channel | |
# uri = https://www.youtube.com/channel/UCFcqi7MrlzIp9RMTtUlxE8g | |
# rss = http://gdata.youtube.com/feeds/base/users/avengedsevenfold/uploads?alt=rss&v=2&orderby=published | |
def self.build | |
uri = "http://gdata.youtube.com/feeds/base/users/avengedsevenfold/uploads?alt=rss&v=2&orderby=published" | |
open(uri) do |rss| | |
@feed = RSS::Parser.parse(rss) | |
end | |
self.new(feed: @feed) | |
end | |
def each(&block) | |
feed.items.each do |item| | |
block.call(Video.new(item)) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great use of struct! Looks good here 👍