module Api::V1

This commit is contained in:
Devin Howard 2016-08-20 12:32:18 +08:00
parent 9abf6d5ba9
commit c7343761aa
31 changed files with 244 additions and 182 deletions

View file

@ -1,2 +0,0 @@
class Api::MappingsController < API::RestfulController
end

View file

@ -1,2 +0,0 @@
class Api::MapsController < API::RestfulController
end

View file

@ -1,46 +0,0 @@
class API::RestfulController < ActionController::Base
include Pundit
include PunditExtra
snorlax_used_rest!
load_and_authorize_resource only: [:show, :update, :destroy]
def create
instantiate_resource
resource.user = current_user
authorize resource
create_action
respond_with_resource
end
private
def accessible_records
if current_user
visible_records
else
public_records
end
end
def current_user
super || token_user || doorkeeper_user || nil
end
def token_user
token = params[:access_token]
access_token = Token.find_by_token(token)
@token_user ||= access_token.user if access_token
end
def doorkeeper_user
return unless doorkeeper_token.present?
doorkeeper_render_error unless valid_doorkeeper_token?
@doorkeeper_user ||= User.find(doorkeeper_token.resource_owner_id)
end
def permitted_params
@permitted_params ||= PermittedParams.new(params)
end
end

View file

@ -1,2 +0,0 @@
class Api::SynapsesController < API::RestfulController
end

View file

@ -1,17 +0,0 @@
class Api::TokensController < API::RestfulController
def my_tokens
raise Pundit::NotAuthorizedError unless current_user
instantiate_collection page_collection: false, timeframe_collection: false
respond_with_collection
end
private
def resource_serializer
"#{resource_name}_serializer".camelize.constantize
end
def visible_records
current_user.tokens
end
end

View file

@ -1,2 +0,0 @@
class Api::TopicsController < API::RestfulController
end

View file

@ -0,0 +1,6 @@
module Api
module V1
class MappingsController < RestfulController
end
end
end

View file

@ -0,0 +1,6 @@
module Api
module V1
class MapsController < RestfulController
end
end
end

View file

@ -0,0 +1,54 @@
module Api
module V1
class RestfulController < ActionController::Base
include Pundit
include PunditExtra
snorlax_used_rest!
load_and_authorize_resource only: [:show, :update, :destroy]
def create
instantiate_resource
resource.user = current_user
authorize resource
create_action
respond_with_resource
end
private
def accessible_records
if current_user
visible_records
else
public_records
end
end
def current_user
super || token_user || doorkeeper_user || nil
end
def resource_serializer
"Api::V1::#{resource_name.camelize}Serializer".constantize
end
def token_user
token = params[:access_token]
access_token = Token.find_by_token(token)
@token_user ||= access_token.user if access_token
end
def doorkeeper_user
return unless doorkeeper_token.present?
doorkeeper_render_error unless valid_doorkeeper_token?
@doorkeeper_user ||= User.find(doorkeeper_token.resource_owner_id)
end
def permitted_params
@permitted_params ||= PermittedParams.new(params)
end
end
end
end

View file

@ -0,0 +1,6 @@
module Api
module V1
class SynapsesController < RestfulController
end
end
end

View file

@ -0,0 +1,17 @@
module Api
module V1
class TokensController < RestfulController
def my_tokens
raise Pundit::NotAuthorizedError unless current_user
instantiate_collection page_collection: false, timeframe_collection: false
respond_with_collection
end
private
def visible_records
current_user.tokens
end
end
end
end

View file

@ -0,0 +1,6 @@
module Api
module V1
class TopicsController < RestfulController
end
end
end

View file

@ -0,0 +1,18 @@
module Api
module V1
class EventSerializer < ActiveModel::Serializer
attributes :id, :sequence_id, :kind, :map_id, :created_at
has_one :actor, serializer: UserSerializer, root: 'users'
has_one :map, serializer: MapSerializer
def actor
object.user || object.eventable.try(:user)
end
def map
object.eventable.try(:map) || object.eventable.map
end
end
end
end

View file

@ -0,0 +1,19 @@
module Api
module V1
class MapSerializer < ActiveModel::Serializer
attributes :id,
:name,
:desc,
:permission,
:screenshot,
:created_at,
:updated_at
has_many :topics, serializer: TopicSerializer
has_many :synapses, serializer: SynapseSerializer
has_many :mappings, serializer: MappingSerializer
has_many :contributors, root: :users, serializer: UserSerializer
has_many :collaborators, root: :users, serializer: UserSerializer
end
end
end

View file

@ -0,0 +1,22 @@
module Api
module V1
class MappingSerializer < ActiveModel::Serializer
attributes :id,
:xloc,
:yloc,
:created_at,
:updated_at,
:mappable_id,
:mappable_type
has_one :user, serializer: UserSerializer
has_one :map, serializer: MapSerializer
def filter(keys)
keys.delete(:xloc) unless object.mappable_type == 'Topic'
keys.delete(:yloc) unless object.mappable_type == 'Topic'
keys
end
end
end
end

View file

@ -0,0 +1,11 @@
module Api
module V1
class MetacodeSerializer < ActiveModel::Serializer
attributes :id,
:name,
:manual_icon,
:color,
:aws_icon
end
end
end

View file

@ -0,0 +1,17 @@
module Api
module V1
class SynapseSerializer < ActiveModel::Serializer
attributes :id,
:desc,
:category,
:weight,
:permission,
:created_at,
:updated_at
has_one :topic1, root: :topics, serializer: TopicSerializer
has_one :topic2, root: :topics, serializer: TopicSerializer
has_one :user, serializer: UserSerializer
end
end
end

View file

@ -0,0 +1,11 @@
module Api
module V1
class TokenSerializer < ActiveModel::Serializer
attributes :id,
:token,
:description,
:created_at,
:updated_at
end
end
end

View file

@ -0,0 +1,16 @@
module Api
module V1
class TopicSerializer < ActiveModel::Serializer
attributes :id,
:name,
:desc,
:link,
:permission,
:created_at,
:updated_at
has_one :user, serializer: UserSerializer
has_one :metacode, serializer: MetacodeSerializer
end
end
end

View file

@ -0,0 +1,19 @@
module Api
module V1
class UserSerializer < ActiveModel::Serializer
attributes :id,
:name,
:avatar,
:is_admin,
:generation
def avatar
object.image.url(:sixtyfour)
end
def is_admin
object.admin
end
end
end
end

View file

@ -0,0 +1,7 @@
module Api
module V1
class WebhookSerializer < ActiveModel::Serializer
attributes :text, :username, :icon_url # , :attachments
end
end
end

View file

@ -1,14 +0,0 @@
class EventSerializer < ActiveModel::Serializer
attributes :id, :sequence_id, :kind, :map_id, :created_at
has_one :actor, serializer: UserSerializer, root: 'users'
has_one :map, serializer: MapSerializer
def actor
object.user || object.eventable.try(:user)
end
def map
object.eventable.try(:map) || object.eventable.map
end
end

View file

@ -1,15 +0,0 @@
class MapSerializer < ActiveModel::Serializer
attributes :id,
:name,
:desc,
:permission,
:screenshot,
:created_at,
:updated_at
has_many :topics, serializer: TopicSerializer
has_many :synapses, serializer: SynapseSerializer
has_many :mappings, serializer: MappingSerializer
has_many :contributors, root: :users, serializer: UserSerializer
has_many :collaborators, root: :users, serializer: UserSerializer
end

View file

@ -1,18 +0,0 @@
class MappingSerializer < ActiveModel::Serializer
attributes :id,
:xloc,
:yloc,
:created_at,
:updated_at,
:mappable_id,
:mappable_type
has_one :user, serializer: UserSerializer
has_one :map, serializer: MapSerializer
def filter(keys)
keys.delete(:xloc) unless object.mappable_type == 'Topic'
keys.delete(:yloc) unless object.mappable_type == 'Topic'
keys
end
end

View file

@ -1,7 +0,0 @@
class MetacodeSerializer < ActiveModel::Serializer
attributes :id,
:name,
:manual_icon,
:color,
:aws_icon
end

View file

@ -1,13 +0,0 @@
class SynapseSerializer < ActiveModel::Serializer
attributes :id,
:desc,
:category,
:weight,
:permission,
:created_at,
:updated_at
has_one :topic1, root: :topics, serializer: TopicSerializer
has_one :topic2, root: :topics, serializer: TopicSerializer
has_one :user, serializer: UserSerializer
end

View file

@ -1,7 +0,0 @@
class TokenSerializer < ActiveModel::Serializer
attributes :id,
:token,
:description,
:created_at,
:updated_at
end

View file

@ -1,12 +0,0 @@
class TopicSerializer < ActiveModel::Serializer
attributes :id,
:name,
:desc,
:link,
:permission,
:created_at,
:updated_at
has_one :user, serializer: UserSerializer
has_one :metacode, serializer: MetacodeSerializer
end

View file

@ -1,15 +0,0 @@
class UserSerializer < ActiveModel::Serializer
attributes :id,
:name,
:avatar,
:is_admin,
:generation
def avatar
object.image.url(:sixtyfour)
end
def is_admin
object.admin
end
end

View file

@ -1,3 +0,0 @@
class WebhookSerializer < ActiveModel::Serializer
attributes :text, :username, :icon_url # , :attachments
end

View file

@ -9,13 +9,15 @@ Metamaps::Application.routes.draw do
get 'search/mappers', to: 'main#searchmappers', as: :searchmappers
get 'search/synapses', to: 'main#searchsynapses', as: :searchsynapses
namespace :api, path: '/api/v1', defaults: { format: :json } do
resources :maps, only: [:create, :show, :update, :destroy]
resources :synapses, only: [:create, :show, :update, :destroy]
resources :topics, only: [:create, :show, :update, :destroy]
resources :mappings, only: [:create, :show, :update, :destroy]
resources :tokens, only: [:create, :destroy] do
get :my_tokens, on: :collection
namespace :api, path: '/api', default: { format: :json } do
namespace :v1, path: '/v1' do
resources :maps, only: [:create, :show, :update, :destroy]
resources :synapses, only: [:create, :show, :update, :destroy]
resources :topics, only: [:create, :show, :update, :destroy]
resources :mappings, only: [:create, :show, :update, :destroy]
resources :tokens, only: [:create, :destroy] do
get :my_tokens, on: :collection
end
end
end