Class: Rockstar::REST::Connection
- Inherits:
-
Object
- Object
- Rockstar::REST::Connection
- Defined in:
- lib/rockstar/rest.rb
Instance Method Summary collapse
- #get(resource, sign_request, args = {}) ⇒ Object
-
#initialize(base_url, args = {}) ⇒ Connection
constructor
A new instance of Connection.
- #post(resource, sign_request, args = nil) ⇒ Object
- #request(resource, method = "get", args = {}, sign_request = false) ⇒ Object
Constructor Details
#initialize(base_url, args = {}) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 11 |
# File 'lib/rockstar/rest.rb', line 7 def initialize(base_url, args = {}) @base_url = base_url @username = args[:username] @password = args[:password] end |
Instance Method Details
#get(resource, sign_request, args = {}) ⇒ Object
13 14 15 |
# File 'lib/rockstar/rest.rb', line 13 def get(resource, sign_request, args = {}) request(resource, "get", args, sign_request) end |
#post(resource, sign_request, args = nil) ⇒ Object
17 18 19 |
# File 'lib/rockstar/rest.rb', line 17 def post(resource, sign_request, args = nil) request(resource, "post", args, sign_request) end |
#request(resource, method = "get", args = {}, sign_request = false) ⇒ Object
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 51 52 53 54 55 56 57 58 |
# File 'lib/rockstar/rest.rb', line 21 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 |