Permalink
Browse files

New response class

- using page parameters
- updated cassettes
- more tests
  • Loading branch information...
JackCA committed Mar 13, 2012
1 parent 43cfd49 commit 1ed4c855fccf6e3800573bc4e994f72a121e493e

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -2,6 +2,7 @@ module Hypem
end
+require "hypem/response"
require "hypem/request"
require "hypem/user"
require "hypem/track"
View
@@ -1,16 +1,17 @@
module Hypem
class Playlist
attr_accessor :tracks
+
def initialize(response)
- raise ArgumentError unless response.is_a? Hash
- response.shift
+ raise ArgumentError unless response.is_a? Response
@tracks = []
- response.each_value{|v| @tracks << Track.new(v)}
+ response.body.each_value{|v| @tracks << Track.new(v)}
end
def self.get(route, user = nil)
- url = ['playlist',route,user].join('/')
- self.new Request.get(url)
+ url = "playlist/#{route}"
+ url << "/#{user}" unless user.nil?
+ self.new Request.new(url).get.response
end
end
end
View
@@ -1,27 +1,30 @@
require 'faraday'
-require 'multi_json'
module Hypem
- module Request
+ class Request
+ attr_accessor :response
+ attr_reader :url
- def self.get(url)
- parse_response connection.get(url+'/json').body
+ def initialize(url, options={})
+ @page = options[:page] || 1
+ @url = url+"/json/#{@page}"
end
-
+
+ def get
+ raw_response = connection.get(@url)
+ @response = Response.new(raw_response)
+ return self
+ end
+
private
- def self.connection
- @conn ||= Faraday.new(url: 'http://hypem.com') do |builder|
+ def connection
+ @@conn ||= Faraday.new(url: 'http://hypem.com') do |builder|
builder.request :url_encoded
builder.response :logger
+ builder.use VCR::Middleware::Faraday
builder.adapter :net_http
end
end
-
- def self.parse_response(response)
- MultiJson.decode response
- end
-
end
-
end
View
@@ -0,0 +1,16 @@
+require 'multi_json'
+
+module Hypem
+ class Response
+ attr_accessor :body
+
+ def initialize(raw_response)
+ raise ArgumentError if raw_response.nil?
+
+ @body = MultiJson.decode raw_response.body
+ # getting rid of version cell
+ @body.shift
+ end
+
+ end
+end
View
@@ -27,6 +27,7 @@ class Track < Hashie::Trash
property :date_loved, from: :dateloved
property :user_name, from: :username
property :full_name, from: :fullname
+ property :via_query
end
View
@@ -17,11 +17,11 @@
end
it "throws an error with invalid parameters" do
- expect { Hypem::Playlist.new(:not_a_hash) }.to raise_error(ArgumentError)
+ expect { Hypem::Playlist.new(:symbol) }.to raise_error(ArgumentError)
end
it "gets a generic playlist" do
- VCR.use_cassette('popular_playlist') { Hypem::Playlist.get('popular') }.should be_a Hypem::Playlist
+ VCR.use_cassette('popular_playlist') { Hypem::Playlist.get('popular/3day') }.should be_a Hypem::Playlist
end
end
View
@@ -1,10 +1,42 @@
require 'spec_helper'
describe Hypem::Request do
+
+ let(:request) do
+ Hypem::Request.new('/playlist/latest/fresh')
+ end
+
+ let(:response) do
+ VCR.use_cassette('fresh_playlist') do
+ request.get.response
+ end
+ end
+
+ context "at initialization" do
+
+ it "must have a url string parameter" do
+ expect { Hypem::Request.new }.to raise_error ArgumentError
+ end
+
+ it "assigns the correct default url" do
+ request.url.should == '/playlist/latest/fresh/json/1'
+ end
+
+ it "accepts a page option" do
+ response = Hypem::Request.new('/url', page: 2)
+ response.url.should == '/url/json/2'
+ end
+
+ it "assigns the response attribute" do
+ response.should be_a Hypem::Response
+ end
+
+ end
+
it "makes get requests" do
- response = VCR.use_cassette('fresh_playlist') { Hypem::Request.get('/playlist/latest/fresh') }
- response.should_not be_nil
- response.should_not be_empty
+ response.body.should_not be_nil
+ response.body.should_not be_empty
end
+
end
View
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe Hypem::Response do
+
+ context "at initialization" do
+ let(:response) do
+ VCR.use_cassette('fresh_playlist') do
+ Hypem::Request.new('/playlist/latest/fresh').get.response
+ end
+ end
+
+ it "uses a raw response parameter" do
+ response.body.should_not be_nil
+ end
+
+ it "assigns the body attribute" do
+ response.body.should be_a Hash
+ end
+ end
+
+
+end
View
@@ -2,7 +2,9 @@
describe Hypem::Track do
let(:track_hash) do
- VCR.use_cassette('popular_playlist') { Hypem::Playlist.get('popular') }.tracks.first
+ VCR.use_cassette('popular_playlist') do
+ Hypem::Playlist.get('popular/3day').tracks.first
+ end
end
let(:track) { Hypem::Track.new(track_hash) }
View
@@ -12,19 +12,27 @@
end
it "gets a loved playlist" do
- VCR.use_cassette('loved_playlist') { user.loved_playlist }.should be_a Hypem::Playlist
+ VCR.use_cassette('loved_playlist') do
+ user.loved_playlist.should be_a Hypem::Playlist
+ end
end
it "gets a feed playlist" do
- VCR.use_cassette('feed_playlist') { user.feed_playlist }.should be_a Hypem::Playlist
+ VCR.use_cassette('feed_playlist') do
+ user.feed_playlist.should be_a Hypem::Playlist
+ end
end
it "gets friends' playlist" do
- VCR.use_cassette('friends_playlist') { user.friends_playlist }.should be_a Hypem::Playlist
+ VCR.use_cassette('friends_playlist') do
+ user.friends_playlist.should be_a Hypem::Playlist
+ end
end
it "gets a friends' history playlist" do
- VCR.use_cassette('friends_history') { user.friends_history }.should be_a Hypem::Playlist
+ VCR.use_cassette('friends_history') do
+ user.friends_history.should be_a Hypem::Playlist
+ end
end
end

0 comments on commit 1ed4c85

Please sign in to comment.