Class Scrobbler::REST::Connection
In: lib/scrobbler/rest.rb
Parent: Object

Methods

get   new   post   request  

Public Class methods

[Source]

    # File lib/scrobbler/rest.rb, line 6
 6:                 def initialize(base_url, args = {})
 7:                         @base_url = base_url
 8:                         @username = args[:username]
 9:                         @password = args[:password]
10:                 end

Public Instance methods

[Source]

    # File lib/scrobbler/rest.rb, line 12
12:                 def get(resource, args = nil)
13:                         request(resource, "get", args)
14:                 end

[Source]

    # File lib/scrobbler/rest.rb, line 16
16:                 def post(resource, args = nil)
17:                         request(resource, "post", args)
18:                 end

[Source]

    # File lib/scrobbler/rest.rb, line 20
20:                 def request(resource, method = "get", args = nil)
21:                         url = URI.join(@base_url, resource)
22: 
23:                         if args
24:                                 # TODO: What about keys without value?
25:                                 url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&")
26:                         end
27: 
28:                         case method
29:                         when "get"
30:                                 req = Net::HTTP::Get.new(url.request_uri)
31:                         when "post"
32:                                 req = Net::HTTP::Post.new(url.request_uri)
33:                         end
34: 
35:                         if @username and @password
36:                                 req.basic_auth(@username, @password)
37:                         end
38: 
39:                         http = Net::HTTP.new(url.host, url.port)
40:                         http.use_ssl = (url.port == 443)
41: 
42:                         res = http.start() { |conn| conn.request(req) }
43:                         res.body
44:                 end

[Validate]