Class: Userbin::Request::Middleware::ContextHeaders

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/userbin/request.rb

Overview

Adds request context like IP address and user agent to any request.

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/userbin/request.rb', line 104

def call(env)
  userbin = RequestStore.store[:userbin]
  return @app.call(env) unless userbin

  userbin.request_context.each do |key, value|
    if value
     header =
      "X-Userbin-#{key.to_s.gsub('_', '-').gsub(/\w+/) {|m| m.capitalize}}"
      env[:request_headers][header] = value
    end
  end
  @app.call(env)
end