module Api::V1
This commit is contained in:
parent
9abf6d5ba9
commit
c7343761aa
31 changed files with 244 additions and 182 deletions
|
@ -1,2 +0,0 @@
|
|||
class Api::MappingsController < API::RestfulController
|
||||
end
|
|
@ -1,2 +0,0 @@
|
|||
class Api::MapsController < API::RestfulController
|
||||
end
|
|
@ -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
|
|
@ -1,2 +0,0 @@
|
|||
class Api::SynapsesController < API::RestfulController
|
||||
end
|
|
@ -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
|
|
@ -1,2 +0,0 @@
|
|||
class Api::TopicsController < API::RestfulController
|
||||
end
|
6
app/controllers/api/v1/mappings_controller.rb
Normal file
6
app/controllers/api/v1/mappings_controller.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class MappingsController < RestfulController
|
||||
end
|
||||
end
|
||||
end
|
6
app/controllers/api/v1/maps_controller.rb
Normal file
6
app/controllers/api/v1/maps_controller.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class MapsController < RestfulController
|
||||
end
|
||||
end
|
||||
end
|
54
app/controllers/api/v1/restful_controller.rb
Normal file
54
app/controllers/api/v1/restful_controller.rb
Normal 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
|
6
app/controllers/api/v1/synapses_controller.rb
Normal file
6
app/controllers/api/v1/synapses_controller.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class SynapsesController < RestfulController
|
||||
end
|
||||
end
|
||||
end
|
17
app/controllers/api/v1/tokens_controller.rb
Normal file
17
app/controllers/api/v1/tokens_controller.rb
Normal 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
|
6
app/controllers/api/v1/topics_controller.rb
Normal file
6
app/controllers/api/v1/topics_controller.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class TopicsController < RestfulController
|
||||
end
|
||||
end
|
||||
end
|
18
app/serializers/api/v1/event_serializer.rb
Normal file
18
app/serializers/api/v1/event_serializer.rb
Normal 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
|
19
app/serializers/api/v1/map_serializer.rb
Normal file
19
app/serializers/api/v1/map_serializer.rb
Normal 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
|
22
app/serializers/api/v1/mapping_serializer.rb
Normal file
22
app/serializers/api/v1/mapping_serializer.rb
Normal 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
|
11
app/serializers/api/v1/metacode_serializer.rb
Normal file
11
app/serializers/api/v1/metacode_serializer.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
module Api
|
||||
module V1
|
||||
class MetacodeSerializer < ActiveModel::Serializer
|
||||
attributes :id,
|
||||
:name,
|
||||
:manual_icon,
|
||||
:color,
|
||||
:aws_icon
|
||||
end
|
||||
end
|
||||
end
|
17
app/serializers/api/v1/synapse_serializer.rb
Normal file
17
app/serializers/api/v1/synapse_serializer.rb
Normal 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
|
11
app/serializers/api/v1/token_serializer.rb
Normal file
11
app/serializers/api/v1/token_serializer.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
module Api
|
||||
module V1
|
||||
class TokenSerializer < ActiveModel::Serializer
|
||||
attributes :id,
|
||||
:token,
|
||||
:description,
|
||||
:created_at,
|
||||
:updated_at
|
||||
end
|
||||
end
|
||||
end
|
16
app/serializers/api/v1/topic_serializer.rb
Normal file
16
app/serializers/api/v1/topic_serializer.rb
Normal 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
|
19
app/serializers/api/v1/user_serializer.rb
Normal file
19
app/serializers/api/v1/user_serializer.rb
Normal 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
|
7
app/serializers/api/v1/webhook_serializer.rb
Normal file
7
app/serializers/api/v1/webhook_serializer.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
module Api
|
||||
module V1
|
||||
class WebhookSerializer < ActiveModel::Serializer
|
||||
attributes :text, :username, :icon_url # , :attachments
|
||||
end
|
||||
end
|
||||
end
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -1,7 +0,0 @@
|
|||
class MetacodeSerializer < ActiveModel::Serializer
|
||||
attributes :id,
|
||||
:name,
|
||||
:manual_icon,
|
||||
:color,
|
||||
:aws_icon
|
||||
end
|
|
@ -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
|
|
@ -1,7 +0,0 @@
|
|||
class TokenSerializer < ActiveModel::Serializer
|
||||
attributes :id,
|
||||
:token,
|
||||
:description,
|
||||
:created_at,
|
||||
:updated_at
|
||||
end
|
|
@ -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
|
|
@ -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
|
|
@ -1,3 +0,0 @@
|
|||
class WebhookSerializer < ActiveModel::Serializer
|
||||
attributes :text, :username, :icon_url # , :attachments
|
||||
end
|
|
@ -9,7 +9,8 @@ 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
|
||||
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]
|
||||
|
@ -18,6 +19,7 @@ Metamaps::Application.routes.draw do
|
|||
get :my_tokens, on: :collection
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
resources :messages, only: [:show, :create, :update, :destroy]
|
||||
resources :mappings, except: [:index, :new, :edit]
|
||||
|
|
Loading…
Add table
Reference in a new issue