Class: E9Crm::Rack::EmailAvailabilityChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/e9_crm/rack/email_availability_checker.rb

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/e9_crm/rack/email_availability_checker.rb', line 3

def self.call(env)
  @params  = Rack::Request.new(env).params
  @email   = @params['email']
  @contact = nil
  @url     = nil

  if @user = User.find_by_email(@email)
    if @contact = @user.contact
      @url = if @params['id'] && c = Contact.find_by_id(@params['id'])
        "/admin/crm/merge_contacts/#{@contact.id}/and/#{c.id}"
      else
        "/admin/crm/contacts/#{@contact.id}/edit"
      end
    end

    @body = {
      :email      => @email,
      :id         => @user.id,
      :contact_id => @contact.try(:id),
      :url        => @url
    }.to_json

    [200, {"Content-Type" => "application/json"}, [@body]]
  else
    [404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
  end
end