#coding=utf-8
#
require 'socket'

class ResponseIp 
    def initialize(app)                                                                                                                                      
     @app = app                                                                                                                                              
    end 

    def ip
      addr = Socket.ip_address_list.detect{|intf| intf.ipv4_private?}.ip_address rescue
      addr || ''
    end

    def call(env)
      status, headers, body = @app.call(env)                                                                                                             
      headers["X-response-ip"] = ip
      [status, headers, body]  
    end
end