Class: SnapSearch::Interceptor

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

Overview

This handles callbacks for before and after interception of a robot.

Instance Method Summary collapse

Constructor Details

#initialize(client, detector) ⇒ Interceptor

Create a new Interceptor instance.

Parameters:

  • client (Client)

    The client to send HTTP requests to the SnapChat API with.

  • detector (Detector)

    The detector to detect and intercept robots with.



10
11
12
# File 'lib/snap_search/interceptor.rb', line 10

def initialize(client, detector)
    @client, @detector = client, detector
end

Instance Method Details

#after_intercept {|url| ... } ⇒ Interceptor

After intercept callback. This is intended for client side caching or as an alternative way to respond to interception when integrated into middleware stacks. However it can also be used for other purposes such as logging.

Yields:

  • (url)

    Block to be executed after interception

Yield Parameters:

  • url (String)

    The encoded URL of the request

  • response (Hash)

    The snapshot response

Returns:



65
66
67
68
69
# File 'lib/snap_search/interceptor.rb', line 65

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

#before_intercept {|url| ... } ⇒ Interceptor

Before intercept callback. This is intended for client side caching. It can be used for requesting a client cached resource. However it can also be used for other purposes such as logging. The callable should accept a string parameter which will the current URL that is being requested. If the callable returns a Hash, the Hash will be used as the returned response for Interceptor#intercept

Yields:

  • (url)

    Block to be executed before interception

Yield Parameters:

  • url (String)

    The encoded URL of the request

Returns:



51
52
53
54
55
# File 'lib/snap_search/interceptor.rb', line 51

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

#intercept(options = {}) ⇒ Hash, false

Begins the detection and returns the snapshot if the request was scraped.

Parameters:

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

    The options to pass to the Detector.

Returns:

  • (Hash, false)

    The response from SnapSearch or ‘false`.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/snap_search/interceptor.rb', line 18

def intercept(options={})
    encoded_url = @detector.get_encoded_url( options[:request].params, Addressable::URI.parse(options[:request].url) )
    
    detected = @detector.detect(options)
    
    if detected
      # all the before interceptor and return an Hash response if it has one
      unless @before_intercept.nil?
          result = @before_intercept.call(encoded_url)
          
          return result.to_hash if !result.nil? && (result.respond_to?(:to_h) || result.respond_to?(:to_hash))
      end
      
      response = @client.request(encoded_url)
      
      # call the after response interceptor, and pass in the response Hash (which is always going to be a Hash)
      @after_intercept.call(encoded_url, response) unless @after_intercept.nil?
      
      response
    else
      false
    end
end