Skip to content

Commit

Permalink
refactored_json
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorshamal committed Sep 18, 2015
1 parent 8ce85bc commit 2e177a6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 85 deletions.
34 changes: 28 additions & 6 deletions app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ class Api::V1::BaseController < ApplicationController
before_filter :check_format!
before_filter :check_auth_token!

# something, somewhere, somehow is causing find_by_id to
# raise an exception when it shouldn't. Therefore:
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

This comment has been minimized.

Copy link
@emiltin

emiltin Sep 18, 2015

Member

we must be able to look at the backtrace to see where the problem occurd? this seems like a hack

This comment has been minimized.

Copy link
@viktorshamal

viktorshamal Sep 18, 2015

Author Contributor

I've narrowed it down to cancancan acting up because it overrides the loading of resources, i'll try to fix it more elegantly


rescue_from CanCan::AccessDenied do |exception|
render status: 401,
json: {
success: false,
info: t('api.flash.unauthorized'),
errors: t('api.flash.unauthorized')
}
unauthorized
end

private
Expand Down Expand Up @@ -44,4 +43,27 @@ def check_format!
end
end

def success(info=nil, options={})
json_response 200, true, info, data:options
end

def failure(resource)
json_response 400, false, nil, errors: resource.errors.full_messages
end

def unauthorized
json_response 401, false, t('api.flash.unauthorized'), errors: t('api.flash.unauthorized')
end

def record_not_found(resource_name=nil)
json_response 404, false, resource_name.to_s + ' ' + t('api.flash.not_found'), errors: resource_name.to_s + ' ' + t('api.flash.not_found')
end

def json_response(status, success, info, options={})
render status: status,
json: ({
success: success,
info: info,
}).merge(options)
end
end
13 changes: 0 additions & 13 deletions app/controllers/api/v1/tracks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ class Api::V1::TracksController < Api::V1::BaseController

before_action :check_privacy_token, only: [:index, :destroy]

# something, somewhere, somehow is causing find_by_id to
# raise an exception when it shouldn't. Therefore:
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

load_and_authorize_resource :user
load_and_authorize_resource :track

Expand Down Expand Up @@ -88,13 +84,4 @@ def check_privacy_token
def privacy_token
@token ||= (params[:signature] || params[:track][:signature])
end

def record_not_found
render status: 404,
json: {
success: false,
info: t('routes.flash.route_not_found'),
errors: t('routes.flash.route_not_found')
}
end
end
80 changes: 21 additions & 59 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,19 @@ def index

def show
@user = User.find_by id: params[:id]

unless @user
render status: 404,
json: {
success: false,
info: t('users.flash.user_not_found'),
errors: t('users.flash.user_not_found')
}
end
end

def destroy
@user = User.find_by id: params[:id]

if @user
if @user.encrypted_password.present?
if @user.destroy_with_tracks params[:user][:password]
render status: 200,
json: {
success: true,
info: t('users.flash.deleted'),
data: {}
}
success t('users.flash.deleted')
else
render status: 401,
json: {
success: false,
info: {},
data: {errors: @user.errors}
}
failure @user
end
else
render status: 404,
json: {
success: false,
info: t('users.flash.user_not_found'),
errors: t('users.flash.user_not_found')
}
elsif @user.destroy
success t('users.flash.deleted')
end
end

Expand All @@ -53,45 +29,21 @@ def change_password

if @user.update_and_generate_signature user_params
sign_in(:user, @user, bypass: true)
notice = if params[:user][:email] != @user.email
t('accounts.flash.activate_new_email')
else
t('accounts.flash.password_changed')
end

render status: 200,
json: {
success: true,
info: notice,
data: { signature: @user.signature }
}

success notice, signature: @user.signature
else
render status: 400,
json: {
success: false,
info: {},
data: { errors: @user.errors.full_messages}
}
failure @user
end
end

def add_password
if current_user.encrypted_password.blank? && current_user.provider == 'facebook'
if current_user.update_attributes password: params[:user][:password]
signature = current_user.generate_signature params[:user][:password]
render status: 200,
json: {
success: true,
info: notice,
data: { signature: signature }
}

success nil, signature: signature
else
render status: 400,
json: {
success: false,
info: {},
data: { errors: @user.errors.full_messages}
}
failure @user
end
end
end
Expand Down Expand Up @@ -126,4 +78,14 @@ def user_params
:account_source
)
end

def notice
self.unconfirmed_email.present? ?
t('accounts.flash.activate_new_email') :
t('accounts.flash.password_changed')
end

def record_not_found
super t('api.resources.user')
end
end
6 changes: 6 additions & 0 deletions config/locales/da.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ da:
flash:
unauthorized: Uautoriseret adgang!
invalid_token: Ugyldigt token!
not_found: findes ikke!
resources:
favourite: Favorit
reported_issue: Issue
route: Rute
user: Bruger

favourites:
flash:
Expand Down
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ en:
flash:
unauthorized: Unauthorized access!
invalid_token: Invalid authentication token!
not_found: doesn't exist!
resources:
favourite: Favourite
reported_issue: Issue
route: Route
user: User

favourites:
flash:
Expand Down
18 changes: 11 additions & 7 deletions spec/api/v1/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,23 @@
it 'add_password' do
fb_user = create :user_with_facebook

attrs = {password:'coolpassword'}

post '/api/users/has_password', {auth_token: fb_user.authentication_token}, headers

expect(json_newest['has_password']).to eq(false)

post '/api/users/add_password', {user:attrs, auth_token: fb_user.authentication_token}, headers
post '/api/users/add_password', {user:{password:'coolpassword'},
auth_token: fb_user.authentication_token
}, headers

expect(response).to be_success
expect(json_newest['data']['signature'].length).to eq(60)
end

it 'has_password' do
fb_user = create :user_with_facebook

post '/api/users/has_password', {auth_token: fb_user.authentication_token}, headers
expect(json_newest['has_password']).to eq(false)

fb_user.update_attributes password: 'coolpassword'

post '/api/users/has_password', {auth_token: fb_user.authentication_token}, headers
expect(json_newest['has_password']).to eq(true)
end

Expand Down

0 comments on commit 2e177a6

Please sign in to comment.