Class: EngagingNetworks::Request::MultiTokenAuthentication
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- EngagingNetworks::Request::MultiTokenAuthentication
- Defined in:
- lib/engaging_networks/request/multitoken.rb
Constant Summary collapse
- PUBLIC =
1
- PRIVATE =
2
Instance Method Summary collapse
-
#call(env) ⇒ Object
Request middleware looks for token_type in http params, replaces with actual token value.
-
#initialize(app, *args) ⇒ MultiTokenAuthentication
constructor
A new instance of MultiTokenAuthentication.
Constructor Details
#initialize(app, *args) ⇒ MultiTokenAuthentication
Returns a new instance of MultiTokenAuthentication.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/engaging_networks/request/multitoken.rb', line 55 def initialize(app, *args) @app = app = args. if .has_key? :public_token @public_token = [:public_token] else @public_token = nil end if .has_key? :private_token @private_token = [:private_token] else @private_token = nil end end |
Instance Method Details
#call(env) ⇒ Object
Request middleware looks for token_type in http params, replaces with actual token value
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 |
# File 'lib/engaging_networks/request/multitoken.rb', line 9 def call(env) # decode url param string to hash if env[:url].query params = Hash[URI.decode_www_form(env[:url].query)] elsif env[:body] params = env[:body] else params = {} end if env[:method] == :get && params.has_key?("token_type") token_type = params['token_type'].to_i #because it got stringified in the form # insert necessary token if token_type == MultiTokenAuthentication::PRIVATE params["token"] = @private_token elsif token_type == MultiTokenAuthentication::PUBLIC params["token"] = @public_token else raise ArgumentError, "invalid token_type #{token_type}" end # remove token_type params.delete('token_type') # encode and return to env env[:url].query = URI.encode_www_form(params) elsif env[:method] == :post && params.has_key?(:token_type) token_type = params[:token_type].to_i if token_type == MultiTokenAuthentication::PRIVATE params[:token] = @private_token elsif token_type == MultiTokenAuthentication::PUBLIC params[:token] = @public_token else raise ArgumentError, "invalid token_type #{token_type}" end params.delete(:token_type) env[:body] = params end @app.call env end |