1
0
Fork 0

Match current API endpoints (/links, /characters)

This commit is contained in:
Andrew Tomaka 2015-10-08 14:20:56 -04:00
parent dae4760a5b
commit 2297d1341a
16 changed files with 135 additions and 0 deletions

View file

@ -15,6 +15,8 @@ gem 'simple_form'
gem 'omniauth-reddit', :git => 'git://github.com/jackdempsey/omniauth-reddit.git' gem 'omniauth-reddit', :git => 'git://github.com/jackdempsey/omniauth-reddit.git'
gem 'active_model_serializers'
group :production do group :production do
gem 'rails_12factor' gem 'rails_12factor'
gem 'pg' gem 'pg'

View file

@ -27,6 +27,8 @@ GEM
erubis (~> 2.7.0) erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5) rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2) rails-html-sanitizer (~> 1.0, >= 1.0.2)
active_model_serializers (0.9.3)
activemodel (>= 3.2)
activejob (4.2.4) activejob (4.2.4)
activesupport (= 4.2.4) activesupport (= 4.2.4)
globalid (>= 0.3.0) globalid (>= 0.3.0)
@ -210,6 +212,7 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
active_model_serializers
better_errors better_errors
bootstrap-sass bootstrap-sass
bullet bullet

View 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

View 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

View 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

View file

@ -0,0 +1,3 @@
class Api::V1::AwakenTypeSerializer < Api::V1::BaseSerializer
attributes :id, :name
end

View 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

View 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

View file

@ -0,0 +1,3 @@
class Api::V1::CharacterSerializer < Api::V1::BaseSerializer
attributes :id, :name
end

View file

@ -0,0 +1,3 @@
class Api::V1::LeaderSkillSerializer < Api::V1::BaseSerializer
attributes :id, :description
end

View file

@ -0,0 +1,3 @@
class Api::V1::LinkSerializer < Api::V1::BaseSerializer
attributes :id, :name, :description
end

View file

@ -0,0 +1,3 @@
class Api::V1::PassiveSkillSerializer < Api::V1::BaseSerializer
attributes :id, :name, :description
end

View file

@ -0,0 +1,3 @@
class Api::V1::RaritySerializer < Api::V1::BaseSerializer
attributes :id, :name
end

View file

@ -0,0 +1,3 @@
class Api::V1::SuperAttackSerializer < Api::V1::BaseSerializer
attributes :id, :name, :description
end

View file

@ -0,0 +1,3 @@
class Api::V1::TypeSerializer < Api::V1::BaseSerializer
attributes :id, :name
end

View file

@ -15,4 +15,11 @@ Rails.application.routes.draw do
resources :welcome, only: [:index] resources :welcome, only: [:index]
root to: 'welcome#index' root to: 'welcome#index'
namespace :api do
namespace :v1 do
resources :cards, only: [:index, :show]
resources :links, only: [:index, :show]
end
end
end end