Class: Vodka::Client::Middleware::SignedRequest

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/vodka/client/middleware/signed_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ SignedRequest

Returns a new instance of SignedRequest.



7
8
9
# File 'lib/vodka/client/middleware/signed_request.rb', line 7

def initialize(app, options = {})
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



5
6
7
# File 'lib/vodka/client/middleware/signed_request.rb', line 5

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/vodka/client/middleware/signed_request.rb', line 5

def env
  @env
end

Instance Method Details

#call(_env) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/vodka/client/middleware/signed_request.rb', line 11

def call(_env)
  @env = _env

  env[:request_headers]['X-Request-Id'] = request_id
  env[:request_headers]['X-Request-Signature'] = request_signature
  env[:request_headers]['X-Response-Locale'] = I18n.locale.to_s if defined?(I18n)

  @app.call(env)
end

#request_idObject



21
22
23
# File 'lib/vodka/client/middleware/signed_request.rb', line 21

def request_id
  @request_id ||= [Time.now.to_i, (rand * 100_000_000).to_i].join(?_)
end

#request_signatureObject



25
26
27
# File 'lib/vodka/client/middleware/signed_request.rb', line 25

def request_signature
  Vodka::Client.config.digest.hexdigest([request_id, Vodka::Client.config.request_secret].join)
end