Sha256: a0d129c3f65bbf6721bb73683ac5899996408c3b6a6c8febd211e084f71201e8

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module Ratchetio
  module Rails
    module ControllerMethods
    
      def ratchetio_request_data
        { 
          :params => params.to_hash,
          :url => ratchetio_request_url,
          :user_ip => ratchetio_user_ip,
          :headers => ratchetio_request_headers,
          :GET => request.GET.to_hash,
          # leaving out POST for now
          :method => request.method,
        }
      end

      def ratchetio_person_data
        user = begin current_user rescue current_member end
        # include id, username, email if non-empty
        user.attributes.select do |k, v|
          if v.blank?
            false
          elsif ['id', 'username', 'email'].include? k
            true
          else
            false
          end
        end.symbolize_keys
      rescue NoMethodError, NameError
        {}
      end

      private

      def ratchetio_request_url
        url = "#{request.protocol}#{request.host}"
        unless [80, 443].include?(request.port)
          url << ":#{request.port}"
        end
        url << request.fullpath
        url
      end

      def ratchetio_user_ip
        # priority: X-Real-Ip, then X-Forwarded-For, then request.remote_ip
        real_ip = request.env["HTTP_X_REAL_IP"]
        if real_ip
          return real_ip
        end
        forwarded_for = request.env["HTTP_X_FORWARDED_FOR"]
        if forwarded_for
          return forwarded_for
        end
        request.remote_ip
      end

      def ratchetio_request_headers
        headers = {}
        request.env.each_pair do |k,v|
          if k.match(/^HTTP_/)
            # convert HTTP_CONTENT_TYPE to Content-Type, etc.
            name = k.split("_", 2)[1].sub("_", "-").split(/(\W)/).map(&:capitalize).join
            headers[name] = v
          end
        end
        headers
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ratchetio-0.2.0 lib/ratchetio/rails/controller_methods.rb