PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/proxies/sinatra.rb

https://bitbucket.org/bnguyen/bitbucket.js
Ruby | 120 lines | 103 code | 17 blank | 0 comment | 8 complexity | 9acecf1c45bab11fe5493dabc368b1ab MD5 | raw file
  1. require 'rubygems'
  2. require 'json'
  3. require 'oauth'
  4. require 'oauth/consumer'
  5. require 'sinatra'
  6. enable :sessions
  7. module Sinatra
  8. module Bitbucket
  9. def url path
  10. "/api/1.0#{path}"
  11. end
  12. end
  13. helpers Bitbucket
  14. end
  15. before do
  16. begin
  17. text = File.read File.expand_path '../../oauth_keys.json', __FILE__
  18. rescue Errno::ENOENT
  19. raise 'create a file named "oauth_keys.json" inside "bitbucket.js"'
  20. end
  21. begin
  22. keys = JSON.parse text
  23. rescue JSON::ParserError
  24. keys = :invalid
  25. end
  26. unless keys == :invalid || keys.include?('key') && keys.include?('secret')
  27. raise 'JSON must be in the form: {"key": "...", "secret": "..."}'
  28. end
  29. @consumer = OAuth::Consumer.new keys['key'], keys['secret'], {
  30. :site => 'https://bitbucket.org',
  31. :request_token_path => url('/oauth/request_token/'),
  32. :access_token_path => url('/oauth/access_token/'),
  33. :authorize_path => url('/oauth/authenticate/'),
  34. }
  35. oauth = session[:oauth] ||= {}
  36. request_token, request_secret = oauth[:request_token], oauth[:request_secret]
  37. if request_token && request_secret
  38. @request_token = OAuth::RequestToken.new @consumer, request_token, request_secret
  39. end
  40. access_token, access_secret = oauth[:access_token], oauth[:access_secret]
  41. if access_token && access_secret
  42. @access_token = OAuth::AccessToken.new @consumer, access_token, access_secret
  43. end
  44. end
  45. get '/' do
  46. if @access_token
  47. File.read File.expand_path '../../test/test.html', __FILE__
  48. else
  49. callback_url = "#{request.scheme}://#{request.host}:#{request.port}/callback"
  50. @request_token = @consumer.get_request_token :oauth_callback => callback_url
  51. session[:oauth][:request_token] = @request_token.token
  52. session[:oauth][:request_secret] = @request_token.secret
  53. redirect @request_token.authorize_url
  54. end
  55. end
  56. get '/callback' do
  57. @access_token = @request_token.get_access_token :oauth_verifier => params[:oauth_verifier]
  58. session[:oauth][:access_token] = @access_token.token
  59. session[:oauth][:access_secret] = @access_token.secret
  60. redirect '/'
  61. end
  62. get '/bitbucket.js' do
  63. content_type :js
  64. File.read File.expand_path '../../bitbucket.js', __FILE__
  65. end
  66. get %r{/test/(.+\.coffee)} do |name|
  67. content_type :js
  68. File.read File.expand_path "../../test/#{name}", __FILE__
  69. end
  70. delete %r{/!users/(.+)} do |username|
  71. `curl https://api.bitbucket.org/1.0/users/#{username} \
  72. --request DELETE --data yes --user #{username}:#{username}`
  73. end
  74. post %r{(/.+)} do |path|
  75. content_type :json
  76. request.body.rewind
  77. response = @access_token.post url(path), request.body.read,
  78. {'Content-Type' => 'application/x-www-form-urlencoded'}
  79. status response.code
  80. response.body
  81. end
  82. get %r{(/.+)} do |path|
  83. content_type :json
  84. path = url path
  85. path += '?' + request.query_string unless request.query_string.empty?
  86. response = @access_token.get path
  87. status response.code
  88. response.body
  89. end
  90. put %r{(/.+)} do |path|
  91. content_type :json
  92. request.body.rewind
  93. response = @access_token.put url(path), request.body.read,
  94. {'Content-Type' => 'application/x-www-form-urlencoded'}
  95. status response.code
  96. response.body
  97. end
  98. delete %r{(/.+)} do |path|
  99. content_type :json
  100. response = @access_token.delete url path
  101. status response.code
  102. response.body
  103. end