Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Single responsibility
- # Creates a DB connection
- class UserRegistration
- EMAIL_REGEXP = /some regexp/
- def initialize
- @connection = Database.new(username: 'root', password: '123', database: 'my_app')
- end
- def perform(params)
- validate(params)
- register_user(params[:email], password[:password])
- send_welcome_email(params)
- end
- private
- def validate(params)
- email = params[:email]
- password = params[:password]
- unless REGEXP.match(email)
- raise ValidationError.new('Invalid email')
- end
- rows = @connection.fetch('SELECT * FROM users WHERE email = :email LIMIT 1', email: email)
- if rows.length == 1
- raise ValidationError.new('Email taken')
- end
- unless password.length > 6
- raise ValidationError.new('Invalid password')
- end
- end
- def register_user(username, password)
- @connection.execute(
- 'INSERT INTO users name, password VALUES (:name, :password)',
- name: name,
- password: password
- )
- end
- def send_welcome_email(params)
- smtp = Smtp.new('username:password@some_server')
- contents = File.contents('app/templates/welcome_email.html')
- contents = Template.new(contents).replace_vars(parmas)
- smtp.send(params[:email], body: body)
- end
- end
- registration = UserRegistration.new
- registration.perform(email: 'foo@bar.com', password: '1235abcd')
Add Comment
Please, Sign In to add comment