Class: SparkApi::Authentication::OAuth2Impl::SparkbarFaradayMiddleware

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/spark_api/authentication/oauth2_impl/faraday_middleware.rb

Overview

OAuth2 Faraday response middleware

HTTP Response after filter to package oauth2 responses and bubble up basic api errors.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SparkbarFaradayMiddleware

Returns a new instance of SparkbarFaradayMiddleware.



43
44
45
# File 'lib/spark_api/authentication/oauth2_impl/faraday_middleware.rb', line 43

def initialize(app)
  super(app)
end

Instance Method Details

#on_complete(env) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spark_api/authentication/oauth2_impl/faraday_middleware.rb', line 47

def on_complete(env)
  body = MultiJson.decode(env[:body])
  SparkApi.logger.debug{ "[sparkbar] Response Body: #{body.inspect}" }
  unless body.is_a?(Hash)
    raise InvalidResponse, "The server response could not be understood"
  end
  case env[:status]
  when 200..299
    SparkApi.logger.debug{ "[sparkbar] Success!" }
    if body.include?("token")
      env[:body] = body
      return
    end
  end
  raise ClientError, {:message => "Unable to process sparkbar token #{body.inspect}", :code =>0, :status => env[:status], :request_path => env[:url]}
end