Sha256: 09725b15be8093407e10fe6f9e75f801d90870ff2eba57d196b171bb14d41a0b

Contents?: true

Size: 1.75 KB

Versions: 12

Compression:

Stored size: 1.75 KB

Contents

require 'net/https'
require 'digest/md5'

module Rockstar
  module REST
  	class Connection
  		def initialize(base_url, args = {})
  			@base_url = base_url
  			@username = args[:username]
  			@password = args[:password]
  		end

  		def get(resource, sign_request, args = {})
  			request(resource, "get", args, sign_request)
  		end

  		def post(resource, sign_request, args = nil)
  			request(resource, "post", args, sign_request)
  		end

  		def request(resource, method = "get", args = {}, sign_request=false)
  		 	url = URI.parse(@base_url)
        
        if (!resource.blank?)
          args[:method] = resource
          args[:api_key]= Rockstar.lastfm_api_key
        end
        
  			if args
  			  sorted_keys = args.keys.sort_by{|k|k.to_s}
          query = sorted_keys.collect { |k| "%s=%s" % [escape(k.to_s), escape(args[k].to_s)] }.join("&")

          if !args[:sk].nil? ||sign_request # Session Key available => sign the request or sign_request = true?
            signed = sorted_keys.collect {|k| "%s%s" % [k.to_s, args[k].to_s]}.join()
            
            auth = Digest::MD5.hexdigest("#{signed}#{Rockstar.lastfm_api_secret}")
            query += "&api_sig=#{auth}"
          end
  				url.query = query
  			end

       	case method
  			when "get"
  				req = Net::HTTP::Get.new(url.request_uri)
  			when "post"
  				req = Net::HTTP::Post.new(url.request_uri)
  			end

  			if @username and @password
  				req.basic_auth(@username, @password)
  			end

  			http = Net::HTTP.new(url.host, url.port)
  			http.use_ssl = (url.port == 443)

  			res = http.start() { |conn| conn.request(req) }
  			res.body
  		end
  		
  		private
  		  def escape(str)
  		    URI.escape(str, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  		  end
  	end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
rockstar-0.6.3 lib/rockstar/rest.rb
rockstar-0.6.2 lib/rockstar/rest.rb
rockstar-custom-0.6.1 lib/rockstar/rest.rb
rockstar-0.6.1 lib/rockstar/rest.rb
rockstar-0.6.0 lib/rockstar/rest.rb
rockstar-0.5.2 lib/rockstar/rest.rb
rockstar-0.5.1 lib/rockstar/rest.rb
rockstar-0.5.0 lib/rockstar/rest.rb
rockstar-0.4.2 lib/rockstar/rest.rb
rockstar-0.4.1 lib/rockstar/rest.rb
rockstar-0.4.0 lib/rockstar/rest.rb
rockstar-0.3.0 lib/rockstar/rest.rb