Created
June 28, 2011 02:11
-
-
Save tbeseda/1050345 to your computer and use it in GitHub Desktop.
Talk JSON(P) with Rdio via Sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require './rdio' | |
configure do | |
ENV['RDIO_API_KEY'] = 'your key' | |
ENV['RDIO_API_SHARED_SECRET'] = 'your secret' | |
ENV['DOMAIN'] = 'yourapp.domain.com' | |
end | |
before do | |
@rdio = Rdio.new ENV["RDIO_API_KEY"], ENV["RDIO_API_SHARED_SECRET"] | |
end | |
get '/' do | |
'try /[username]/[API method]' | |
end | |
get '/:name/:method' do |name, method| | |
content_type 'application/json', :charset => 'utf-8' | |
cache_control :public, :max_age => 60*60 | |
callback = params.delete('callback') | |
user = @rdio.findUser vanityName: name | |
json = @rdio.send(method.to_sym, user: user['key']).to_json | |
if callback | |
content_type :js | |
response = "#{callback}(#{json})" | |
else | |
content_type :json | |
response = json | |
end | |
response | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'sinatra' | |
gem 'oauth' | |
gem 'json' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'oauth/consumer' | |
OAuth::VERSION = 1 | |
require 'json' | |
SITE = 'http://api.rdio.com' | |
PATH = '/1/' | |
class Rdio | |
def initialize(key, secret) | |
puts "Initializing Rdio consumer" | |
@consumer = OAuth::Consumer.new key, secret, {:site => SITE} | |
@consumer.http.read_timeout = 600 # ten-minute timeout, thanks | |
puts "Initializing Rdio access token" | |
@access_token = OAuth::AccessToken.new @consumer | |
puts "Rdio API ready" | |
end | |
def method_missing(method, args = {}) | |
args[:method] = method.to_s | |
puts "Rdio request for %s" % method.to_s | |
response = @access_token.post PATH, args | |
puts "Rdio request complete for %s" % method.to_s | |
JSON::parse(response.body)["result"] if response.code.match("2..") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment