From 9085cec232cb464176b4e9ac8e27267db853a1ec Mon Sep 17 00:00:00 2001 From: Einar Jonsson Date: Sun, 25 Aug 2013 22:41:17 +0000 Subject: [PATCH] Move json hash from users controller to NicknameUnavailable --- app/controllers/users_controller.rb | 11 ++-------- lib/discourse_hub.rb | 25 +++++++++++++++++++---- spec/controllers/users_controller_spec.rb | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bc0f81da585..1d0cd954d53 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -217,15 +217,8 @@ class UsersController < ApplicationController end rescue ActiveRecord::StatementInvalid render json: { success: false, message: I18n.t("login.something_already_taken") } - rescue DiscourseHub::NicknameUnavailable - render json: { success: false, - message: I18n.t( - "login.errors", - errors:I18n.t( - "login.not_available", suggestion: UserNameSuggester.suggest(params[:username]) - ) - ) - } + rescue DiscourseHub::NicknameUnavailable=> e + render json: e.response_message rescue RestClient::Forbidden render json: { errors: [I18n.t("discourse_hub.access_token_problem")] } end diff --git a/lib/discourse_hub.rb b/lib/discourse_hub.rb index 5313bc4ead1..60ec2fea4cd 100644 --- a/lib/discourse_hub.rb +++ b/lib/discourse_hub.rb @@ -3,7 +3,24 @@ require_dependency 'version' module DiscourseHub - class NicknameUnavailable < RuntimeError; end + class NicknameUnavailable < RuntimeError + def initialize(nickname) + @nickname = nickname + end + + def response_message + { + success: false, + message: I18n.t( + "login.errors", + errors:I18n.t( + "login.not_available", suggestion: UserNameSuggester.suggest(@nickname) + ) + ) + } + end + + end def self.nickname_available?(nickname) json = get('/users/nickname_available', {nickname: nickname}) @@ -20,7 +37,7 @@ module DiscourseHub if json.has_key?('success') true else - raise NicknameUnavailable # TODO: report ALL the errors + raise NicknameUnavailable.new(nickname) # TODO: report ALL the errors end end @@ -34,7 +51,7 @@ module DiscourseHub if json.has_key?('success') true else - raise NicknameUnavailable # TODO: report ALL the errors + raise NicknameUnavailable.new(new_nickname) # TODO: report ALL the errors end end @@ -93,4 +110,4 @@ module DiscourseHub def self.accepts [:json, 'application/vnd.discoursehub.v1'] end -end \ No newline at end of file +end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index acfd9f068a6..c1013f085b3 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -477,7 +477,7 @@ describe UsersController do context 'when nickname is unavailable in DiscourseHub' do before do SiteSetting.stubs(:call_discourse_hub?).returns(true) - DiscourseHub.stubs(:register_nickname).raises(DiscourseHub::NicknameUnavailable) + DiscourseHub.stubs(:register_nickname).raises(DiscourseHub::NicknameUnavailable.new(@user.name)) end let(:create_params) {{ name: @user.name,