Sha256: 95a595695a58a67773e4c915715b18604067bbe3372464aa99da978d43a94bf5

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'oauth'

module SoundcloudAuth
  module Dispatcher
    class Oauth < OAuth::AccessToken
    
      attr_accessor :user
    
      def append_extension_to(path)
        path, query_string = *(path.split("?"))
        path << '.json' unless path.match(/\.(:?xml|json)\z/i)
        "#{path}#{"?#{query_string}" if query_string}"
      end
    
      def handle_response(response)
        case response
        when Net::HTTPOK 
          begin
            JSON.parse(response.body)
          rescue JSON::ParserError
            response.body
          end
        when Net::HTTPUnauthorized
          raise SoundcloudAuth::Dispatcher::Unauthorized, 'The credentials provided did not authorize the user.'
        else
          message = begin
            JSON.parse(response.body)['error']
          rescue JSON::ParserError
            if match = response.body.match(/<error>(.*)<\/error>/)
              match[1]
            else
              'An error occurred processing your SoundCloud request.'
            end
          end

          raise SoundcloudAuth::Dispatcher::Error, message
        end
      end
    
      def initialize(user)
        self.user = user
        super(SoundcloudAuth.consumer, user.access_token, user.access_secret)
      end

      def request(http_method, path, *arguments)
        path = SoundcloudAuth.path_prefix + path
        path = append_extension_to(path)

        response = super

        handle_response(response)
      end

  end
 end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
soundcloud-auth-0.1.1 lib/soundcloud_auth/dispatcher/oauth.rb