Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros committed May 4, 2015
1 parent fd32f11 commit c2f0762
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
23 changes: 14 additions & 9 deletions app/controllers/achievements_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class AchievementsController < ApplicationController
#TODO extract to api.coderwall.com
before_action :ensure_valid_api_key, only: [:award]
skip_before_action :verify_authenticity_token, only: [:award]
layout 'protip'
Expand All @@ -9,8 +10,8 @@ def show
show_achievements_params = params.permit(:id, :username)

@badge = Badge.find(show_achievements_params[:id])
@user = @badge.user
return redirect_to(destination_url) if @badge && @user.username.downcase != show_achievements_params[:username].downcase
@user = @badge.user
redirect_to(destination_url) if @badge && @user.username.downcase != show_achievements_params[:username].downcase
end

def award
Expand All @@ -23,7 +24,7 @@ def award
render_404
else
if @api_access.can_award?(award_params[:badge])
user = User.find_by_provider_username(award_params[provider], provider)
user = User.find_by_provider_username(award_params[provider], provider)
badge = badge_class_factory(award_params[:badge].to_s).new(user, Date.strptime(award_params[:date], '%m/%d/%Y'))
badge.generate_fact!(award_params[:badge], award_params[provider], provider)
unless user.nil?
Expand All @@ -32,19 +33,15 @@ def award
end
render nothing: true, status: 200
else
return render json: { message: "don't have permission to do that. contact support@coderwall.com", status: 403 }.to_json
render json: {message: "don't have permission to do that. contact support@coderwall.com", status: 403}.to_json
end
end
rescue Exception => e
return render json: { message: "something went wrong with your request or the end point may not be ready. contact support@coderwall.com" }.to_json
end

private

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

def badge_class_factory(requested_badge_name)
Expand All @@ -54,4 +51,12 @@ def badge_class_factory(requested_badge_name)
def pick_a_provider(award_params)
(User::LINKABLE_PROVIDERS & award_params.keys.select { |key| %w{twitter linkedin github}.include?(key) }).first
end

rescue_from ActiveRecord::RecordNotFound do
render json: {message: 'no/invalid api_key provided. get your api_key from coderwall.com/settings'}.to_json
end

rescue_from Exception do
render json: {message: 'something went wrong with your request or the end point may not be ready. contact support@coderwall.com'}.to_json
end
end
7 changes: 1 addition & 6 deletions app/models/api_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@
#

class ApiAccess < ActiveRecord::Base
#TODO change column to postgresql array
serialize :awards, Array

class << self
def for(api_key)
where(api_key: api_key).first
end
end

def can_award?(badge_name)
awards.include? badge_name
end
Expand Down

0 comments on commit c2f0762

Please sign in to comment.