Skip to content

Instantly share code, notes, and snippets.

@stve
Created June 26, 2011 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stve/1047870 to your computer and use it in GitHub Desktop.
Save stve/1047870 to your computer and use it in GitHub Desktop.
require 'feedzirra'
module ItunesFeed
module Music
DEFAULT_FEED_TYPE = 'topalbums'
def music
@feed_type = ItunesFeed::Music::DEFAULT_FEED_TYPE
self
end
def top_albums
@feed_type = 'topalbums'
self
end
def top_songs
@feed_type = 'topsongs'
self
end
end
class Client
DEFAULT_LIMIT = 10
DEFAULT_EXPLICIT = true
include ItunesFeed::Music
def initialize(options={})
@country = options[:country]
@limit = options[:limit] || DEFAULT_LIMIT
@explicit = options[:explicit] || DEFAULT_EXPLICIT
@feed_type = options[:feed_type]
end
def country(c)
@country = c
self
end
def limit(l=DEFAULT_LIMIT)
@limit = l
self
end
def explicit(e=DEFAULT_EXPLICIT)
@explicit = e
self
end
def feed_type(ft)
@feed_type = ft
self
end
def genre(g)
@genre = g
self
end
def fetch
Feedzirra::Feed.fetch_and_parse(feed_url)
end
private
def feed_url
url = 'http://itunes.apple.com/'
url << @country
url << '/rss/'
url << "#{@feed_type}/"
url << "limit=#{@limit}/"
url << "genre=#{@genre}/" if @genre
url << "explicit=true/" if @explicit
url << "xml"
end
end
end
feed = ItunesFeed::Client.new.country('us').music.fetch
puts feed.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment