Created
May 30, 2016 09:02
-
-
Save tyllo/6cdff419077cfe2abc8168da81c1d260 to your computer and use it in GitHub Desktop.
Simple examle for Ruby on rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Register | |
class VerifyEmail < RegisterBase | |
def call | |
context.fail! if context.validation_fail | |
url = resource_url + '/verification?verification_code=' | |
url += URI.escape(context.verification_code) if context.verification_code | |
result = make_request(::Utils::Stubs.verify_email, :post, url) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe Register::VerifyEmail do | |
let(:verification_code) { 'test verify code' } | |
let(:error_message) { 'Invalid verification code' } | |
let(:client) { spy(:client) } | |
before { allow(client).to receive(:post).and_return response } | |
before { allow(client).to receive(:headers).and_return [] } | |
describe '.call' do | |
context 'when preliminary validation has failed' do | |
subject(:context) { Register::VerifyEmail.call(validation_fail: true) } | |
it 'has failed' do | |
expect(context).to be_a_failure | |
end | |
end | |
context 'when no or invalid data for server' do | |
let(:response) { gen_response(json_response, status) } | |
let(:json_response) { {message: error_message, error_code: status} } | |
let(:status) { 400 } | |
subject(:context) { Register::VerifyEmail.call(client: client) } | |
it 'has failed' do | |
expect(context).to be_a_failure | |
end | |
it 'received an error message from the server' do | |
expect(context.error[:message]).to eq error_message | |
end | |
end | |
context 'when all data is valid' do | |
let(:response) { gen_response(json_response) } | |
let(:json_response) { ::Utils::Stubs.verify_email } | |
subject(:context) do | |
Register::VerifyEmail.call({ | |
client: client, | |
verification_code: | |
verification_code | |
}) | |
end | |
it 'returns the response' do | |
expect(context).to be_a_success | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Organizer::VerifyEmail | |
include Interactor::Organizer | |
organize InitialiseHttpClient, Register::VerifyEmail | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RegisterController < ApplicationController | |
layout 'home' | |
def create | |
result = Organizer::RegisterUser.call(register_params) | |
if result.failure? | |
@errors = result[:error][:errors] | |
@params = register_params | |
render :index, status: result[:error][:code] | |
else | |
render :completed | |
end | |
end | |
def verify | |
if !verification_code | |
redirect_to register_index_path | |
return | |
end | |
result = Organizer::VerifyEmail.call(verification_code) | |
@message = I18n.t 'register.verified' | |
if result.failure? | |
@message = result[:error][:message] | |
render status: result[:error][:code] | |
end | |
end | |
private def register_params | |
params.require(:register).permit(:email, :password) | |
end | |
private def verify_params | |
params.permit(:code) | |
end | |
private def verification_code | |
verify_params[:code] && { verification_code: verify_params[:code] } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment