Giter Club home page Giter Club logo

pre's Introduction

Pretty Reliable Email

=====================

Why?

Regexes are not enough sometimes. We should go all the way and fully parse the email and knock on the server's door to see if they really have a mailbox.

How?

Basics

require 'pre'
validator = Pre::Validator.new
validator.valid? "[email protected]"

With basic config

Pre by default has built in validators for RFC2822 and DNS MX record verification. Validators can be passed through the :validators option.

require 'pre'
# validate with no domain validation
validator = Pre::Validator.new :validators => :format
validator.valid? "[email protected]" # => true

Advanced config

Pre can take blocks for custom validators

require 'pre'
no_gmail = lambda do |address|
	address !~ /gmail.com$/
end
validator = Pre::Validator.new :validators => [:format, no_gmail]
validator.valid? "[email protected]" # => false

Pre can take any object that implements the valid? method

require 'pre'
class ComplexValidator
	def valid? address
		return true unless address =~ /gmail.com$/
		# do not allow [email protected] 
		address !~ /\+.+@/
	end
end
validator = Pre::Validator.new :validators => [:domain, ComplexValidator.new]
validator.valid? "[email protected]" # => false

Pre can also take alternate configuration for a single address

require 'pre'
validator = Pre::Validator.new :validators => :format
validator.valid? "[email protected]", :validators => lambda { |address|
  address =~ /example.co.nz$/
} # => false

Caching

Certain strategies may be more "intense" than others. MX Record lookup and other expensive operations can benefit from providing Pre with a cache store. A cache store must respond to :write(key, val) and :read(key) methods. The cache abstraction layer provided by Rails' ActiveSupport::Cache::Store fits this interface.

require 'pre'
memcache = ActiveSupport::Cache::MemCacheStore.new("localhost", "server-downstairs.localnetwork:8229")
validator = Pre::Validator.new :cache_store => memcache
validator.valid? "[email protected]" # => true

Contributing

  1. Fork
  2. Make tests
  3. Make changes to lib
  4. Ensure tests pass on 1.8/1.9
  5. Submit request
  6. Smile

Roadmap

  • ActiveModel, Mongoid::Document, etc.. integration
  • DSL for configuring Pre validation configuration sets

Ack

The RFC treetop grammars are pulled from the fantastic mail gem.

pre's People

Contributors

lightman76 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.