public
Description: Scrobbler is a wrapper for the audioscrobbler (last.fm) web services.
Homepage: http://scrobbler.rubyforge.org/
Clone URL: git://github.com/jnunemaker/scrobbler.git
Click here to lend your support to: scrobbler and make a donation at www.pledgie.com !
scrobbler / test / mocks / rest.rb
100644 50 lines (44 sloc) 2.006 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require File.dirname(__FILE__) + '/../../lib/scrobbler/rest'
require 'digest/md5'
 
module Scrobbler
  module REST
    class Connection
     # reads xml fixture file instead of hitting up the internets
     def request(resource, method = "get", args = nil)
     @now_playing_url = 'http://62.216.251.203:80/nowplaying'
     @submission_url = 'http://62.216.251.205:80/protocol_1.2'
     @session_id = '17E61E13454CDD8B68E8D7DEEEDF6170'
    
     if @base_url == Scrobbler::API_URL
       pieces = resource.split('/')
       pieces.shift
       pieces.shift
       folder = pieces.shift
       file = pieces.last[0, pieces.last.index('.xml')]
       base_pieces = pieces.last.split('?')
    
       file = if base_pieces.size > 1
       # if query string params are in resource they are underscore separated for filenames
       base_pieces.last.split('&').inject("#{file}_") { |str, pair| str << pair.split('=').join('_') + '_'; str }.chop!
     else
     file
          end
    
       File.read(File.dirname(__FILE__) + "/../fixtures/xml/#{folder}/#{file}.xml")
     elsif @base_url == Scrobbler::AUTH_URL
          if args[:hs] == "true" && args[:p] == Scrobbler::AUTH_VER.to_s && args[:c] == 'rbs' &&
             args[:v] == Scrobbler::Version.to_s && args[:u] == 'chunky' && !args[:t].blank? &&
             args[:a] == Digest::MD5.hexdigest('7813258ef8c6b632dde8cc80f6bda62f' + args[:t])
            
            "OK\n#{@session_id}\n#{@now_playing_url}\n#{@submission_url}"
          end
        elsif @base_url == @now_playing_url
          if args[:s] == @session_id && ![args[:a], args[:t], args[:b], args[:n]].any?(&:blank?)
             'OK'
          end
        elsif @base_url == @submission_url
          if args[:s] == @session_id &&
             ![args['a[0]'], args['t[0]'], args['i[0]'], args['o[0]'], args['l[0]'], args['b[0]']].any?(&:blank?)
            'OK'
          end
   end
  
   end
   end
  end
end