Merge branch 'atomaka/feature/api' into 'master'
Match current API endpoints (/links, /characters) There is a pull request that is currently active (within the last day) for Active Model Serializers that will allow for nested and inline Serializers. This will allow `CardSerializer` to go from the complicated monstrosity that it is to a much simpler representation. For example ``` has_one :character ``` instead of: ``` def character Api::V1::CharacterSerializer.new(object.character) end ``` follow this pull request at https://github.com/rails-api/active_model_serializers/pull/1225 See merge request !1
This commit is contained in:
commit
621a8cf7c4
16 changed files with 135 additions and 0 deletions
2
Gemfile
2
Gemfile
|
@ -15,6 +15,8 @@ gem 'simple_form'
|
|||
|
||||
gem 'omniauth-reddit', :git => 'git://github.com/jackdempsey/omniauth-reddit.git'
|
||||
|
||||
gem 'active_model_serializers'
|
||||
|
||||
group :production do
|
||||
gem 'rails_12factor'
|
||||
gem 'pg'
|
||||
|
|
|
@ -27,6 +27,8 @@ GEM
|
|||
erubis (~> 2.7.0)
|
||||
rails-dom-testing (~> 1.0, >= 1.0.5)
|
||||
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
||||
active_model_serializers (0.9.3)
|
||||
activemodel (>= 3.2)
|
||||
activejob (4.2.4)
|
||||
activesupport (= 4.2.4)
|
||||
globalid (>= 0.3.0)
|
||||
|
@ -210,6 +212,7 @@ PLATFORMS
|
|||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
active_model_serializers
|
||||
better_errors
|
||||
bootstrap-sass
|
||||
bullet
|
||||
|
|
11
app/controllers/api/v1/base_controller.rb
Normal file
11
app/controllers/api/v1/base_controller.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
class Api::V1::BaseController < ApplicationController
|
||||
protect_from_forgery with: :null_session
|
||||
|
||||
before_action :destroy_session
|
||||
|
||||
private
|
||||
|
||||
def destroy_session
|
||||
request.session_options[:skip] = true
|
||||
end
|
||||
end
|
16
app/controllers/api/v1/cards_controller.rb
Normal file
16
app/controllers/api/v1/cards_controller.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
class Api::V1::CardsController < Api::V1::BaseController
|
||||
before_action :set_card, only: [:show]
|
||||
def index
|
||||
render json: Card.all, root: false
|
||||
end
|
||||
|
||||
def show
|
||||
render json: @card, root: false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_card
|
||||
@card = Card.find(params[:id])
|
||||
end
|
||||
end
|
16
app/controllers/api/v1/links_controller.rb
Normal file
16
app/controllers/api/v1/links_controller.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
class Api::V1::LinksController < Api::V1::BaseController
|
||||
before_action :set_link, only: [:show]
|
||||
def index
|
||||
render json: Link.all, root: false
|
||||
end
|
||||
|
||||
def show
|
||||
render json: @link, root: false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_link
|
||||
@link = Link.find(params[:id])
|
||||
end
|
||||
end
|
3
app/serializers/api/v1/awaken_type_serializer.rb
Normal file
3
app/serializers/api/v1/awaken_type_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Api::V1::AwakenTypeSerializer < Api::V1::BaseSerializer
|
||||
attributes :id, :name
|
||||
end
|
9
app/serializers/api/v1/base_serializer.rb
Normal file
9
app/serializers/api/v1/base_serializer.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class Api::V1::BaseSerializer < ActiveModel::Serializer
|
||||
def created_at
|
||||
object.created_at.in_time_zone.iso8601 if object.created_at
|
||||
end
|
||||
|
||||
def updated_at
|
||||
object.updated_at.in_time_zone.iso8601 if object.created_at
|
||||
end
|
||||
end
|
47
app/serializers/api/v1/card_serializer.rb
Normal file
47
app/serializers/api/v1/card_serializer.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
class Api::V1::CardSerializer < Api::V1::BaseSerializer
|
||||
attributes :id, :character, :rarity, :type, :leader_skill, :passive_skill,
|
||||
:super_attack, :awaken_type, :dokkan_card, :links, :title,
|
||||
:gameid
|
||||
|
||||
private
|
||||
|
||||
def links
|
||||
ActiveModel::ArraySerializer.new(
|
||||
object.links,
|
||||
each_serializer: Api::V1::LinkSerializer,
|
||||
root: false
|
||||
)
|
||||
end
|
||||
|
||||
def character
|
||||
Api::V1::CharacterSerializer.new(object.character)
|
||||
end
|
||||
|
||||
def rarity
|
||||
Api::V1::RaritySerializer.new(object.rarity)
|
||||
end
|
||||
|
||||
def type
|
||||
Api::V1::TypeSerializer.new(object.type)
|
||||
end
|
||||
|
||||
def leader_skill
|
||||
Api::V1::LeaderSkillSerializer.new(object.leader_skill)
|
||||
end
|
||||
|
||||
def passive_skill
|
||||
Api::V1::PassiveSkillSerializer.new(object.passive_skill)
|
||||
end
|
||||
|
||||
def super_attack
|
||||
Api::V1::SuperAttackSerializer.new(object.super_attack)
|
||||
end
|
||||
|
||||
def awaken_type
|
||||
Api::V1::AwakenTypeSerializer.new(object.awaken_type)
|
||||
end
|
||||
|
||||
def dokkan_card
|
||||
Api::V1::CardSerializer.new(object.dokkan_card)
|
||||
end
|
||||
end
|
3
app/serializers/api/v1/character_serializer.rb
Normal file
3
app/serializers/api/v1/character_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Api::V1::CharacterSerializer < Api::V1::BaseSerializer
|
||||
attributes :id, :name
|
||||
end
|
3
app/serializers/api/v1/leader_skill_serializer.rb
Normal file
3
app/serializers/api/v1/leader_skill_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Api::V1::LeaderSkillSerializer < Api::V1::BaseSerializer
|
||||
attributes :id, :description
|
||||
end
|
3
app/serializers/api/v1/link_serializer.rb
Normal file
3
app/serializers/api/v1/link_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Api::V1::LinkSerializer < Api::V1::BaseSerializer
|
||||
attributes :id, :name, :description
|
||||
end
|
3
app/serializers/api/v1/passive_skill_serializer.rb
Normal file
3
app/serializers/api/v1/passive_skill_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Api::V1::PassiveSkillSerializer < Api::V1::BaseSerializer
|
||||
attributes :id, :name, :description
|
||||
end
|
3
app/serializers/api/v1/rarity_serializer.rb
Normal file
3
app/serializers/api/v1/rarity_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Api::V1::RaritySerializer < Api::V1::BaseSerializer
|
||||
attributes :id, :name
|
||||
end
|
3
app/serializers/api/v1/super_attack_serializer.rb
Normal file
3
app/serializers/api/v1/super_attack_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Api::V1::SuperAttackSerializer < Api::V1::BaseSerializer
|
||||
attributes :id, :name, :description
|
||||
end
|
3
app/serializers/api/v1/type_serializer.rb
Normal file
3
app/serializers/api/v1/type_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Api::V1::TypeSerializer < Api::V1::BaseSerializer
|
||||
attributes :id, :name
|
||||
end
|
|
@ -15,4 +15,11 @@ Rails.application.routes.draw do
|
|||
resources :welcome, only: [:index]
|
||||
|
||||
root to: 'welcome#index'
|
||||
|
||||
namespace :api do
|
||||
namespace :v1 do
|
||||
resources :cards, only: [:index, :show]
|
||||
resources :links, only: [:index, :show]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue