Files
libr-2/app/controllers/api/v1/posts_controller.rb
T
2015-08-02 13:33:56 +08:00

42 lines
868 B
Ruby

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