Class: Oauth2Rails::Base
- Inherits:
-
Object
- Object
- Oauth2Rails::Base
- Defined in:
- lib/oauth2_rails/base.rb
Instance Method Summary collapse
- #call(action, destination, options = {}) ⇒ Object
- #connection(url) ⇒ Object
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/oauth2_rails/base.rb', line 6 def initialize( = {}) @oauth_id = [:oauth_id] || OAUTH2_RAILS_ID @oauth_secret = [:oauth_secret] || OAUTH2_RAILS_SECRET @redirect_uri = [:redirect_uri] || OAUTH2_RAILS_CALLBACK @authorize_site = [:authorize_site] || 'https://www.fitbit.com' @authorize_path = [:authorize_path] || '/oauth2/authorize' @api_site = [:api_site] || 'https://api.fitbit.com' @token_path = [:token_path] || '/oauth2/token' @scope = [:scope] || 'heartrate' end |
Instance Method Details
#call(action, destination, options = {}) ⇒ Object
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 |
# File 'lib/oauth2_rails/base.rb', line 25 def call(action, destination, = {}) user = [:user] site = [:site] || @api_site if user auth_header = "Bearer #{user}" else encoded = Base64.strict_encode64("#{@oauth_id}:#{@oauth_secret}") auth_header = "Basic #{encoded}" end call = connection(site).send(action) do |req| req.url destination req.headers['Content-Type'] = 'application/x-www-form-urlencoded' req.headers['Authorization'] = auth_header req.body = [:body] end response = Response.new(call) case response.status when 400 ; raise Oauth2Rails::Errors::BadRequest, "400 #{response.}" when 404 ; raise Oauth2Rails::Errors::NotFound, "404 #{response.}" when 409 ; raise Oauth2Rails::Errors::Conflict, "409 #{response.}" when 500 ; raise Oauth2Rails::Errors::InternalServer, "500 #{response.}" when 502 ; raise Oauth2Rails::Errors::BadGateway, "502 #{response.}" when 401 ; raise Oauth2Rails::Errors::Unauthorized, "401 #{response.}" else ; response end end |
#connection(url) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/oauth2_rails/base.rb', line 17 def connection(url) Faraday.new(url: url) do |faraday| faraday.request :url_encoded faraday.response :logger faraday.adapter Faraday.default_adapter end end |