Sha256: 09bfe27a2a2ef6bd4a46bf9200d04bfd0e64c4b3343d90544f2d0346bfa6410b

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require 'rack'
require 'json'

module MessengerJS
  
  class Middleware
    
    KEY = 'action_dispatch.request.flash_hash'.freeze
    
    def initialize(app, options = {})
      @app = app
      @options = {
        
      }.merge(options)
    end
    
    def call(env)
      @env = env
      @status, @headers, @response = @app.call(env)
      
      session    = Request::Session.find(env) || {}
      flash_hash = env[KEY]
      
      insert_messenger_to_response
      
      [@status, @headers, @response]
    end
    
    
    
    private
    
    def insert_messenger_to_response
      @response.each do |response_part|
        if is_regular_requests? && is_html_response?
          
        elsif is_turbolink_request? && is_html_response?
          
        elsif is_ajax_request? && is_html_response?
          
        elsif is_ajax_request? && is_javascript_response?
          
        end
        
      end
    end
    
    def is_regular_request?
      !(is_ajax_request? || is_turbolink_request?)
    end
    
    def is_turbolink_request?
      @env.has_key?("HTTP_X_XHR_REFERER")
    end
    
    def is_ajax_request?
      @env.has_key?("HTTP_X_REQUESTED_WITH") && @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
    end
    
    def is_html_response?
      @headers["Content-Type"].include?("text/html") if @headers.has_key?("Content-Type")
    end
    
    def is_javascript_response?
      @headers["Content-Type"].include?("text/javascript") if @headers.has_key?("Content-Type")
    end
    
    def render_messenger_scripts
      <<-EOT
        <!-- start Messenger -->
        <script type="text/javascript">
          
        </script>
        <!-- end Messenger -->
      EOT
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
messengerjs-rails-1.3.3 lib/messengerjs-rails/middleware.rb