Class: Rack::SnapSearch::Config

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

Overview

The configuration class for the Rack middleware. Holds the attributes to initialize the Client, Interceptor, and Detector with.

Constant Summary collapse

ATTRIBUTES =
[
    :email, :key, :api_url, :ca_cert_file, :x_forwarded_proto, :parameters,               # Client options
    :matched_routes, :ignored_routes, :robots_json, :extensions_json, :check_file_extensions # Detector options
]

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Create a new instance.

Parameters:

  • options (Hash) (defaults to: {})

    The options to initialize this instance with.

Options Hash (options):

  • :email (String)

    The email to authenticate with.

  • :key (String)

    The key to authenticate with.

  • :api_url (String)

    The API URL to send requests to.

  • :ca_cert_file (String)

    The CA Cert file to use when sending HTTPS requests to the API.

  • :x_forwarded_proto (String)

    Check X-Forwarded-Proto because Heroku SSL Support terminates at the load balancer

  • :parameters (String)

    Extra parameters to send to the API.

  • :matched_routes (String)

    Whitelisted routes. Should be an Array of Regexp instances.

  • :ignored_routes (String)

    Blacklisted routes. Should be an Array of Regexp instances.

  • :robots_json (String)

    A path of the JSON file containing the user agent whitelist & blacklist.

  • :extensions_json (String)

    A path to the JSON file containing a single Hash with the keys ‘ignore` and `match`. These keys contain Arrays of Strings (user agents)

  • :check_file_extensions (String)

    Set to ‘true` to ignore direct requests to files.

  • :on_exception (Proc, #call)

    The block to run when an exception within SnapSearch occurs.

  • :before_intercept (Proc, #call)

    A block to run before the interception of a bot.

  • :after_intercept (Proc, #call)

    A block to run after the interception of a bot.

  • :response_callback (Proc, #call)

    A block to manipulate the response from the SnapSearch API.

Raises:

  • (TypeError)


33
34
35
36
37
38
39
40
# File 'lib/rack/snap_search/config.rb', line 33

def initialize(options={})
    raise TypeError, 'options must be a Hash or respond to #to_h' unless options.is_a?(Hash) || options.respond_to?(:to_h) || options.respond_to?(:to_hash)
    options = options.to_h rescue options.to_hash
    
    ATTRIBUTES.each do |attribute|
        send( "#{attribute}=", options[attribute] ) if options.has_key?(attribute)
    end
end

Instance Method Details

#after_intercept { ... } ⇒ Proc

Getter/Setter for the ‘after_intercept` attribute on the Interceptor.

Yields:

  • If given, the Proc or callable to set the attribute as.

Returns:

  • (Proc)

    The value of the attribute.



66
67
68
69
70
# File 'lib/rack/snap_search/config.rb', line 66

def after_intercept(&block)
    @after_intercept = block if block_given?
    
    @after_intercept
end

#before_intercept { ... } ⇒ Proc

Getter/Setter for the ‘before_intercept` attribute on the Interceptor.

Yields:

  • If given, the Proc or callable to set the attribute as.

Returns:

  • (Proc)

    The value of the attribute.



56
57
58
59
60
# File 'lib/rack/snap_search/config.rb', line 56

def before_intercept(&block)
    @before_intercept = block if block_given?
    
    @before_intercept
end

#on_exception { ... } ⇒ Proc

Getter/Setter for the ‘on_exception` attribute.

Yields:

  • If given, the Proc or callable to set the attribute as.

Returns:

  • (Proc)

    The value of the attribute.



46
47
48
49
50
# File 'lib/rack/snap_search/config.rb', line 46

def on_exception(&block)
    @on_exception = block if block_given?
    
    @on_exception
end

#response_callback { ... } ⇒ Proc

Getter/Setter for the ‘response_callback` attribute on the Interceptor.

Yields:

  • If given, the Proc or callable to set the attribute as.

Returns:

  • (Proc)

    The value of the attribute.



76
77
78
79
80
# File 'lib/rack/snap_search/config.rb', line 76

def response_callback(&block)
    @response_callback = block if block_given?
    
    @response_callback
end