lib/ey-hmac/adapter/rack.rb in ey-hmac-2.1.0 vs lib/ey-hmac/adapter/rack.rb in ey-hmac-2.2.0
- old
+ new
@@ -1,14 +1,11 @@
require 'rack'
class Ey::Hmac::Adapter::Rack < Ey::Hmac::Adapter
def initialize(request, options)
super
- @request = if request.is_a?(Hash)
- ::Rack::Request.new(request)
- else request
- end
+ @request = request.is_a?(Hash) ? ::Rack::Request.new(request) : request
end
def method
request.request_method.to_s.upcase
end
@@ -16,14 +13,16 @@
def content_type
request.content_type
end
def content_digest
- if existing = request.env['HTTP_CONTENT_DIGEST']
- existing
- elsif digest = body && Digest::MD5.hexdigest(body)
- request.env['HTTP_CONTENT_DIGEST'] = digest
+ request.env['HTTP_CONTENT_DIGEST']
+ end
+
+ def set_content_digest
+ if body
+ request.env['HTTP_CONTENT_DIGEST'] = Digest::MD5.hexdigest(body)
end
end
def body
if request.env["rack.input"]
@@ -34,17 +33,24 @@
else nil
end
end
def date
- request.env['HTTP_DATE'] ||= Time.now.httpdate
+ request.env['HTTP_DATE']
end
+ def set_date
+ request.env['HTTP_DATE'] = Time.now.httpdate
+ end
+
def path
request.path
end
def sign!(key_id, key_secret)
+ set_date
+ set_content_digest
+
if options[:version]
request.env['HTTP_X_SIGNATURE_VERSION'] = options[:version]
end
request.env["HTTP_#{authorization_header.to_s.upcase}"] = authorization(key_id, key_secret)