Last active
August 21, 2018 07:53
-
-
Save crusadergo/160fb7c921c2ac6409d547e366fd6fcf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# frozen_string_literal: true | |
class TotemHelper | |
def initialize(params:, model:) | |
@params = params | |
@model = model | |
end | |
# Checks avatar presence of | |
def avatar_present? | |
model.avatar? | |
end | |
# Checks include totem of avatar | |
def include_totem? | |
model.avatar.url&.split('/')&.last&.include? 'totem' | |
end | |
# Gets model name | |
def model_name | |
params[:controller]&.downcase&.singularize | |
end | |
# Gets totem public path | |
def totem_path | |
Dir[ENV["#{model_name}_totem_avatar"]].first | |
end | |
# Gets sample totem filename | |
def sample_totem | |
Dir[totem_path + '/*'].sample&.split('/').last | |
end | |
# Saves avatar as sample totem of company | |
def save_avatar_as_sample_totem | |
model.avatar = File.open(totem_path + '/' + sample_totem) | |
model.save! | |
end | |
# Saves avatar if not present or updates current totem | |
def set_totem_if_needed | |
save_avatar_as_sample_totem if !avatar_present? || include_totem? | |
end | |
protected | |
attr_accessor :params, :model | |
end |
This file contains hidden or 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
# frozen_string_literal: true | |
class UsersController < ApiController | |
require 'googleauth' | |
def show | |
TotemHelper.new(params: params, model: current_user).set_totem_if_needed # Saves avatar if not present or updates current totem | |
render json: current_user, serializer: CurrentUserSerializer | |
end | |
def google_auth | |
audience = Google::Auth::ClientId.new ENV['google_client_id'], ENV['google_client_secret'] | |
validator = GoogleIDToken::Validator.new | |
begin | |
claim = validator.check(user_params['token_id'], audience.id) | |
user = User.social_auth(claim) | |
render json: user, status: :created, serializer: CurrentUserSerializer | |
rescue GoogleIDToken::ValidationError => e | |
render json: { message: 'Invalid token id' }, status: :unprocessable_entity | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment