Skip to content

Commit c2f0762

Browse files
committed
code cleanup
1 parent fd32f11 commit c2f0762

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

app/controllers/achievements_controller.rb

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class AchievementsController < ApplicationController
2+
#TODO extract to api.coderwall.com
23
before_action :ensure_valid_api_key, only: [:award]
34
skip_before_action :verify_authenticity_token, only: [:award]
45
layout 'protip'
@@ -9,8 +10,8 @@ def show
910
show_achievements_params = params.permit(:id, :username)
1011

1112
@badge = Badge.find(show_achievements_params[:id])
12-
@user = @badge.user
13-
return redirect_to(destination_url) if @badge && @user.username.downcase != show_achievements_params[:username].downcase
13+
@user = @badge.user
14+
redirect_to(destination_url) if @badge && @user.username.downcase != show_achievements_params[:username].downcase
1415
end
1516

1617
def award
@@ -23,7 +24,7 @@ def award
2324
render_404
2425
else
2526
if @api_access.can_award?(award_params[:badge])
26-
user = User.find_by_provider_username(award_params[provider], provider)
27+
user = User.find_by_provider_username(award_params[provider], provider)
2728
badge = badge_class_factory(award_params[:badge].to_s).new(user, Date.strptime(award_params[:date], '%m/%d/%Y'))
2829
badge.generate_fact!(award_params[:badge], award_params[provider], provider)
2930
unless user.nil?
@@ -32,19 +33,15 @@ def award
3233
end
3334
render nothing: true, status: 200
3435
else
35-
return render json: { message: "don't have permission to do that. contact [email protected]", status: 403 }.to_json
36+
render json: {message: "don't have permission to do that. contact [email protected]", status: 403}.to_json
3637
end
3738
end
38-
rescue Exception => e
39-
return render json: { message: "something went wrong with your request or the end point may not be ready. contact [email protected]" }.to_json
4039
end
4140

4241
private
4342

4443
def ensure_valid_api_key
45-
@api_key = params.permit(:api_key)[:api_key]
46-
@api_access = ApiAccess.for(@api_key) unless @api_key.nil?
47-
return render json: { message: "no/invalid api_key provided. get your api_key from coderwall.com/settings" }.to_json if @api_access.nil?
44+
@api_access = ApiAccess.find_by_api_key!(params.permit(:api_key)[:api_key])
4845
end
4946

5047
def badge_class_factory(requested_badge_name)
@@ -54,4 +51,12 @@ def badge_class_factory(requested_badge_name)
5451
def pick_a_provider(award_params)
5552
(User::LINKABLE_PROVIDERS & award_params.keys.select { |key| %w{twitter linkedin github}.include?(key) }).first
5653
end
54+
55+
rescue_from ActiveRecord::RecordNotFound do
56+
render json: {message: 'no/invalid api_key provided. get your api_key from coderwall.com/settings'}.to_json
57+
end
58+
59+
rescue_from Exception do
60+
render json: {message: 'something went wrong with your request or the end point may not be ready. contact [email protected]'}.to_json
61+
end
5762
end

app/models/api_access.rb

+1-6
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@
1010
#
1111

1212
class ApiAccess < ActiveRecord::Base
13+
#TODO change column to postgresql array
1314
serialize :awards, Array
1415

15-
class << self
16-
def for(api_key)
17-
where(api_key: api_key).first
18-
end
19-
end
20-
2116
def can_award?(badge_name)
2217
awards.include? badge_name
2318
end

0 commit comments

Comments
 (0)