class Api::V1::PostsController < ApplicationController skip_before_action :verify_authenticity_token before_action :authenticate_user_from_token before_action :permit_params, only: [:create] POST_TYPE = { :ARTICLE => 'article', :WEB_PAGE => 'webpage' } def create post = get_post @share_instance = @current_user.share_instances.create sharable: post, description: params[:description] render json: @share_instance.to_json(include: :sharable) end def show end def index end private def permit_params params.permit(:url, :type, :description) end def get_post post = nil case params[:type] when POST_TYPE[:ARTICLE] post = Article.find_or_create_by(url: params[:url]) when POST_TYPE[:WEB_PAGE] post = WebPage.find_or_create_by(url: params[:url]) end post end end