Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added response_callback option
  • Loading branch information
bfolkens committed Aug 10, 2012
1 parent 09e82f3 commit f659d71
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/url_validation.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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...
Expand Down
9 changes: 9 additions & 0 deletions spec/url_validator_spec.rb
Expand Up @@ -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

0 comments on commit f659d71

Please sign in to comment.