Skip to content

Commit

Permalink
Added pagination, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
JackCA committed Mar 21, 2012
1 parent 2c62fdd commit 208edd3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -17,6 +17,7 @@ This is an unoffical Ruby gem for the **Hype Machine** public API. It supports a

- `Hypem.playlist.popular` --- the most popular tracks over a certain timeframe or set. It accepts one of the following arguments:


<table>
<thead>
<tr>
Expand Down Expand Up @@ -48,6 +49,8 @@ This is an unoffical Ruby gem for the **Hype Machine** public API. It supports a
</tbody>
</table>

Each playlist has a `tracks` attribute containing an array of `Hypem::Tracks`'s. Pagination is supported with the `.next_page` and `.prev_page` methods.

******

### User ###
Expand All @@ -66,7 +69,7 @@ This is an unoffical Ruby gem for the **Hype Machine** public API. It supports a
******

### Track ###
All of the above methods return an array of `Hypem::Track`'s. A sample object inspect is as follows:
All of the above methods return an Playlist object with an attribute `tracks` containing an array of `Hypem::Track`'s. A sample object inspect is as follows:

#<Hypem::Track
artist="Hot Chip"
Expand Down
46 changes: 18 additions & 28 deletions lib/hypem/playlist.rb
@@ -1,43 +1,39 @@
module Hypem
class Playlist
POPULAR_ARGS = [%s(3day),:lastweek,:noremix,:artists,:twitter]
GENERIC_METHODS = [:blog, :tags, :search, :artist, :feed, :loved, :obsessed]
attr_accessor :url, :tracks
attr_reader :extended

def initialize(type,arg)
def initialize(type,arg,page=1)
@type = type
@arg = arg
@url = ['playlist',type,arg].join('/')
@page = page
end

def get
response = Request.new(url).get.response
response = Request.new(url,page: @page).get.response
@tracks = []
response.body.each_value{|v| @tracks << Track.new(v)}
return self
end

def self.latest
Playlist.new(:time,:today)
end

def self.popular(arg=POPULAR_ARGS.first)
raise ArgumentError unless POPULAR_ARGS.include?(arg)
Playlist.new(:popular,arg)
end

def self.blog(arg)
Playlist.new(:blog,arg)
def next_page
Playlist.new(@type,@arg,@page+1).get
end

def self.tags(arg)
Playlist.new(:tags,arg)
def prev_page
Playlist.new(@type,@arg,@page-1).get
end

def self.search(arg)
Playlist.new(:search,arg)
def self.latest
Playlist.new(:time,:today)
end

def self.artist(arg)
Playlist.new(:artist,arg)
def self.popular(arg=POPULAR_ARGS.first)
raise ArgumentError unless POPULAR_ARGS.include?(arg)
Playlist.new(:popular,arg)
end

def self.friends_history(user)
Expand All @@ -48,16 +44,10 @@ def self.friends_favorites(user)
Playlist.new(:people,user)
end

def self.feed(user)
Playlist.new(:feed,user)
end

def self.loved(user)
Playlist.new(:loved,user)
# meta method definitions for generic playlists
GENERIC_METHODS.each do |method|
define_singleton_method(method) {|arg| Playlist.new(method,arg) }
end

def self.obsessed(user)
Playlist.new(:obsessed,user)
end
end
end
18 changes: 18 additions & 0 deletions spec/playlist_spec.rb
Expand Up @@ -104,4 +104,22 @@
end
end

describe "pagination" do
before do
playlist
Hypem::Playlist.stub_chain(:new,:get)
end

it "can get next page" do
Hypem::Playlist.should_receive(:new).with(:time,:today,2)
playlist.next_page
end

it "also gets previous page" do
Hypem::Playlist.should_receive(:new).with(:time,:today,0)
playlist.prev_page
end

end

end

0 comments on commit 208edd3

Please sign in to comment.