Sha256: 612e9303d9a9c5a5f0b4b10bcce07767a0af87231143d693dc1dcc921ab581bd

Contents?: true

Size: 786 Bytes

Versions: 12

Compression:

Stored size: 786 Bytes

Contents

module Dugway
  class ContactFormValidator

    attr_accessor :params

    def initialize(params)
      @params = params
    end

    def error_message
      if required_fields.any? { |f| params[f].blank? }
        'All fields are required'
      elsif param_does_not_match(:email, email_format)
        'Invalid email address'
      elsif param_does_not_match(:captcha, captcha_format)
        'Spam check was incorrect'
      end
    end

    private

    def required_fields
      [ :name, :email, :subject, :message, :captcha ]
    end

    def email_format
      /^([^@\s]+)@((?:[-a-zA-Z0-9]+\.)+[a-zA-Z]{2,})$/
    end

    def captcha_format
      /^rQ9pC$/i
    end

    def param_does_not_match(param_name, regex)
      !(params[param_name.to_sym] =~ regex)
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dugway-1.0.0 lib/dugway/contact_form_validator.rb
dugway-0.12.3 lib/dugway/contact_form_validator.rb
dugway-0.12.2 lib/dugway/contact_form_validator.rb
dugway-0.12.1 lib/dugway/contact_form_validator.rb
dugway-0.12.0 lib/dugway/contact_form_validator.rb
dugway-0.11.4 lib/dugway/contact_form_validator.rb
dugway-0.11.3 lib/dugway/contact_form_validator.rb
dugway-0.11.2 lib/dugway/contact_form_validator.rb
dugway-0.11.1 lib/dugway/contact_form_validator.rb
dugway-0.11.0 lib/dugway/contact_form_validator.rb
dugway-0.10.5 lib/dugway/contact_form_validator.rb
dugway-0.10.4 lib/dugway/contact_form_validator.rb