Class: Rack::SnapSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/snap_search.rb,
lib/rack/snap_search/config.rb

Overview

Use to enable SnapSearch detection within your web application.

Defined Under Namespace

Classes: Config

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) {|Rack::SnapSearch::Config, SnapSearch::Detector| ... } ⇒ SnapSearch

Initialize the middleware.

Parameters:

  • app (#call)

    The Rack application.

  • options (Hash, #to_h) (defaults to: {})

    Options to configure this middleware with.

Yields:

Yield Parameters:

  • config (Rack::SnapSearch::Config)

    Options to configure this middleware with, optionally preset with the ‘options` param.

  • detector (SnapSearch::Detector)

    The instance of the Detector class which will be used for detecting whether requests are coming from a robot.

Raises:

  • (TypeError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/snap_search.rb', line 16

def initialize(app, options={}, &block)
    raise TypeError, 'app must respond to #call' unless app.respond_to?(:call)
    raise TypeError, 'options must be a Hash or respond to #to_h or #to_hash' unless options.is_a?(Hash) || options.respond_to?(:to_h)    || options.respond_to?(:to_hash)
    options = options.to_h rescue options.to_hash
    
    @app = app
    
    setup_config(options)
    
    block.call(@config) if block_given?
    
    setup_client
    setup_detector
    setup_interceptor
end

Instance Method Details

#call(environment) ⇒ Object

Run the middleware.

Parameters:

  • app (Hash, to_h)

    The Rack environment

Raises:

  • (TypeError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rack/snap_search.rb', line 35

def call(environment)
    raise TypeError, 'environment must be a Hash or respond to #to_h or #to_hash' unless environment.is_a?(Hash) || environment.respond_to?(:to_h)    || environment.respond_to?(:to_hash)
    environment = environment.to_h rescue environment.to_hash
    
    setup_x_forwarded_proto(environment) if @config.x_forwarded_proto
    
    setup_api_response_from_environment(environment) # Will set @api_response if one is given from the API
    
    if @api_response
      unless @config.response_callback.nil?
        @callback_response = @config.response_callback.call(*@api_response)
        
        setup_attributes_from_callback_response
      else
        setup_attributes_from_api_response
      end
      
      rack_response
    else
      setup_attributes_from_app(environment)
    end
    
    
end