From f659d7184e6bc71a13af249ec4079996a40d9ce3 Mon Sep 17 00:00:00 2001 From: Bradford Folkens Date: Thu, 9 Aug 2012 20:06:58 -0500 Subject: [PATCH] Added response_callback option --- lib/url_validation.rb | 2 ++ spec/url_validator_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/url_validation.rb b/lib/url_validation.rb index 05b5cfb..d561666 100644 --- a/lib/url_validation.rb +++ b/lib/url_validation.rb @@ -59,6 +59,7 @@ # h3. Other options # # | @:request_callback@ | A proc that receives the request object (for ==HTTP(S)== requests, the @HTTPI::Request@ object) before it is executed. You can use this proc to set, e.g., custom headers or timeouts on the request. | +# | @:response_callback@ | A proc that receives the response object after it is executed by @:check_path@, when it is enabled. This is useful for checking redirect URL's, etc. | class UrlValidator < ActiveModel::EachValidator # @private @@ -176,6 +177,7 @@ def http_url_accessible?(uri, options) def url_response_valid?(response, options) return true unless response.kind_of?(HTTPI::Response) and options[:check_path] + options[:response_callback].call(response) if options[:response_callback].respond_to?(:call) response_codes = options[:check_path] == true ? [400..499, 500..599] : Array.wrap(options[:check_path]).flatten return response_codes.none? do |code| # it's good if it's not a bad response case code # and it's a bad response if... diff --git a/spec/url_validator_spec.rb b/spec/url_validator_spec.rb index e4c5a04..0718ff9 100644 --- a/spec/url_validator_spec.rb +++ b/spec/url_validator_spec.rb @@ -227,5 +227,14 @@ class Record called.should be_true end end + + context "[:response_callback]" do + it "should be yielded the HTTPI response" do + called = false + @validator = UrlValidator.new(:attributes => [ :field ], :check_path => true, :response_callback => lambda { |response| called = true; response.should be_kind_of(HTTPI::Response) }) + @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf") + called.should be_true + end + end end end