Class: SnapSearch::Interceptor
- Inherits:
-
Object
- Object
- SnapSearch::Interceptor
- Defined in:
- lib/snap_search/interceptor.rb
Overview
This handles callbacks for before and after interception of a robot.
Instance Method Summary collapse
-
#after_intercept {|url| ... } ⇒ Interceptor
After intercept callback.
-
#before_intercept {|url| ... } ⇒ Interceptor
Before intercept callback.
-
#initialize(client, detector) ⇒ Interceptor
constructor
Create a new Interceptor instance.
-
#intercept(options = {}) ⇒ Hash, false
Begins the detection and returns the snapshot if the request was scraped.
Constructor Details
#initialize(client, detector) ⇒ Interceptor
Create a new Interceptor instance.
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.
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
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.
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(={}) encoded_url = @detector.get_encoded_url( [:request].params, Addressable::URI.parse([:request].url) ) detected = @detector.detect() 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 |