/proxies/sinatra.rb
Ruby | 120 lines | 103 code | 17 blank | 0 comment | 8 complexity | 9acecf1c45bab11fe5493dabc368b1ab MD5 | raw file
- require 'rubygems'
- require 'json'
- require 'oauth'
- require 'oauth/consumer'
- require 'sinatra'
- enable :sessions
- module Sinatra
- module Bitbucket
- def url path
- "/api/1.0#{path}"
- end
- end
- helpers Bitbucket
- end
- before do
- begin
- text = File.read File.expand_path '../../oauth_keys.json', __FILE__
- rescue Errno::ENOENT
- raise 'create a file named "oauth_keys.json" inside "bitbucket.js"'
- end
- begin
- keys = JSON.parse text
- rescue JSON::ParserError
- keys = :invalid
- end
- unless keys == :invalid || keys.include?('key') && keys.include?('secret')
- raise 'JSON must be in the form: {"key": "...", "secret": "..."}'
- end
- @consumer = OAuth::Consumer.new keys['key'], keys['secret'], {
- :site => 'https://bitbucket.org',
- :request_token_path => url('/oauth/request_token/'),
- :access_token_path => url('/oauth/access_token/'),
- :authorize_path => url('/oauth/authenticate/'),
- }
- oauth = session[:oauth] ||= {}
- request_token, request_secret = oauth[:request_token], oauth[:request_secret]
- if request_token && request_secret
- @request_token = OAuth::RequestToken.new @consumer, request_token, request_secret
- end
- access_token, access_secret = oauth[:access_token], oauth[:access_secret]
- if access_token && access_secret
- @access_token = OAuth::AccessToken.new @consumer, access_token, access_secret
- end
- end
- get '/' do
- if @access_token
- File.read File.expand_path '../../test/test.html', __FILE__
- else
- callback_url = "#{request.scheme}://#{request.host}:#{request.port}/callback"
- @request_token = @consumer.get_request_token :oauth_callback => callback_url
- session[:oauth][:request_token] = @request_token.token
- session[:oauth][:request_secret] = @request_token.secret
- redirect @request_token.authorize_url
- end
- end
- get '/callback' do
- @access_token = @request_token.get_access_token :oauth_verifier => params[:oauth_verifier]
- session[:oauth][:access_token] = @access_token.token
- session[:oauth][:access_secret] = @access_token.secret
- redirect '/'
- end
- get '/bitbucket.js' do
- content_type :js
- File.read File.expand_path '../../bitbucket.js', __FILE__
- end
- get %r{/test/(.+\.coffee)} do |name|
- content_type :js
- File.read File.expand_path "../../test/#{name}", __FILE__
- end
- delete %r{/!users/(.+)} do |username|
- `curl https://api.bitbucket.org/1.0/users/#{username} \
- --request DELETE --data yes --user #{username}:#{username}`
- end
- post %r{(/.+)} do |path|
- content_type :json
- request.body.rewind
- response = @access_token.post url(path), request.body.read,
- {'Content-Type' => 'application/x-www-form-urlencoded'}
- status response.code
- response.body
- end
- get %r{(/.+)} do |path|
- content_type :json
- path = url path
- path += '?' + request.query_string unless request.query_string.empty?
- response = @access_token.get path
- status response.code
- response.body
- end
- put %r{(/.+)} do |path|
- content_type :json
- request.body.rewind
- response = @access_token.put url(path), request.body.read,
- {'Content-Type' => 'application/x-www-form-urlencoded'}
- status response.code
- response.body
- end
- delete %r{(/.+)} do |path|
- content_type :json
- response = @access_token.delete url path
- status response.code
- response.body
- end