Module: Mailchecker

Defined in:
lib/mailchecker.rb,
lib/mailchecker/version.rb,
lib/generators/mailchecker/assets_generator.rb,
lib/generators/mailchecker/install_generator.rb

Defined Under Namespace

Modules: Generators, Rails, Validations

Constant Summary collapse

VERSION =
"0.0.2"
@@url =
"http://www.mail-checker.com/check"
@@token =
nil
@@domain =
nil

Class Method Summary collapse

Class Method Details

.record_validation(record, attr_name, value) ⇒ Object



48
49
50
51
# File 'lib/mailchecker.rb', line 48

def self.record_validation record, attr_name, value
  error = Mailchecker::validate_email value.to_s
  record.errors.add attr_name, error if error
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Mailchecker)

    the object that the method was called on



20
21
22
# File 'lib/mailchecker.rb', line 20

def self.setup
  yield self
end

.validate_email(email) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mailchecker.rb', line 24

def self.validate_email email
  params = { email: email, token: Mailchecker.token, domain: Mailchecker.domain, format: :json }

  uri      = URI.parse Mailchecker.url + "?" + params.to_param
  http     = Net::HTTP.new uri.host, uri.port
  request  = Net::HTTP::Get.new uri.request_uri 
  response = http.request request

  case response.code.to_i
    when 200
      nil
    when 422
      I18n.t :wrong_format, scope: [:mailchecker, :errors, :messages], default: 'the format is not valid ([email protected])'
    when 502
      I18n.t :wrong_gateway, scope: [:mailchecker, :errors, :messages], default: 'the domain is not responding'
    when 406
      I18n.t :temporary_email, scope: [:mailchecker, :errors, :messages], default: 'this kind of email is not valid (temporary email)'
    when 400
      raise "Some parameters are missing... did you add the right token in the init file or the domain is valid?"
    else
      raise "Please report this issue (probably a lack of a new functionality), the status code is #{response.code}"
  end
end