From 2297d1341afcfaa9f5270f6416f1bed4f33cc37f Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Thu, 8 Oct 2015 14:20:56 -0400 Subject: [PATCH] Match current API endpoints (/links, /characters) --- Gemfile | 2 + Gemfile.lock | 3 ++ app/controllers/api/v1/base_controller.rb | 11 +++++ app/controllers/api/v1/cards_controller.rb | 16 +++++++ app/controllers/api/v1/links_controller.rb | 16 +++++++ .../api/v1/awaken_type_serializer.rb | 3 ++ app/serializers/api/v1/base_serializer.rb | 9 ++++ app/serializers/api/v1/card_serializer.rb | 47 +++++++++++++++++++ .../api/v1/character_serializer.rb | 3 ++ .../api/v1/leader_skill_serializer.rb | 3 ++ app/serializers/api/v1/link_serializer.rb | 3 ++ .../api/v1/passive_skill_serializer.rb | 3 ++ app/serializers/api/v1/rarity_serializer.rb | 3 ++ .../api/v1/super_attack_serializer.rb | 3 ++ app/serializers/api/v1/type_serializer.rb | 3 ++ config/routes.rb | 7 +++ 16 files changed, 135 insertions(+) create mode 100644 app/controllers/api/v1/base_controller.rb create mode 100644 app/controllers/api/v1/cards_controller.rb create mode 100644 app/controllers/api/v1/links_controller.rb create mode 100644 app/serializers/api/v1/awaken_type_serializer.rb create mode 100644 app/serializers/api/v1/base_serializer.rb create mode 100644 app/serializers/api/v1/card_serializer.rb create mode 100644 app/serializers/api/v1/character_serializer.rb create mode 100644 app/serializers/api/v1/leader_skill_serializer.rb create mode 100644 app/serializers/api/v1/link_serializer.rb create mode 100644 app/serializers/api/v1/passive_skill_serializer.rb create mode 100644 app/serializers/api/v1/rarity_serializer.rb create mode 100644 app/serializers/api/v1/super_attack_serializer.rb create mode 100644 app/serializers/api/v1/type_serializer.rb diff --git a/Gemfile b/Gemfile index 9363e90..6e421e8 100644 --- a/Gemfile +++ b/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' diff --git a/Gemfile.lock b/Gemfile.lock index 02f487d..679ea43 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb new file mode 100644 index 0000000..47e8a3a --- /dev/null +++ b/app/controllers/api/v1/base_controller.rb @@ -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 diff --git a/app/controllers/api/v1/cards_controller.rb b/app/controllers/api/v1/cards_controller.rb new file mode 100644 index 0000000..9397780 --- /dev/null +++ b/app/controllers/api/v1/cards_controller.rb @@ -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 diff --git a/app/controllers/api/v1/links_controller.rb b/app/controllers/api/v1/links_controller.rb new file mode 100644 index 0000000..fa5c8ac --- /dev/null +++ b/app/controllers/api/v1/links_controller.rb @@ -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 diff --git a/app/serializers/api/v1/awaken_type_serializer.rb b/app/serializers/api/v1/awaken_type_serializer.rb new file mode 100644 index 0000000..a0ff590 --- /dev/null +++ b/app/serializers/api/v1/awaken_type_serializer.rb @@ -0,0 +1,3 @@ +class Api::V1::AwakenTypeSerializer < Api::V1::BaseSerializer + attributes :id, :name +end diff --git a/app/serializers/api/v1/base_serializer.rb b/app/serializers/api/v1/base_serializer.rb new file mode 100644 index 0000000..bdec4bf --- /dev/null +++ b/app/serializers/api/v1/base_serializer.rb @@ -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 diff --git a/app/serializers/api/v1/card_serializer.rb b/app/serializers/api/v1/card_serializer.rb new file mode 100644 index 0000000..c55b9e6 --- /dev/null +++ b/app/serializers/api/v1/card_serializer.rb @@ -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 diff --git a/app/serializers/api/v1/character_serializer.rb b/app/serializers/api/v1/character_serializer.rb new file mode 100644 index 0000000..c28c45a --- /dev/null +++ b/app/serializers/api/v1/character_serializer.rb @@ -0,0 +1,3 @@ +class Api::V1::CharacterSerializer < Api::V1::BaseSerializer + attributes :id, :name +end diff --git a/app/serializers/api/v1/leader_skill_serializer.rb b/app/serializers/api/v1/leader_skill_serializer.rb new file mode 100644 index 0000000..49cea2c --- /dev/null +++ b/app/serializers/api/v1/leader_skill_serializer.rb @@ -0,0 +1,3 @@ +class Api::V1::LeaderSkillSerializer < Api::V1::BaseSerializer + attributes :id, :description +end diff --git a/app/serializers/api/v1/link_serializer.rb b/app/serializers/api/v1/link_serializer.rb new file mode 100644 index 0000000..9a5a5c6 --- /dev/null +++ b/app/serializers/api/v1/link_serializer.rb @@ -0,0 +1,3 @@ +class Api::V1::LinkSerializer < Api::V1::BaseSerializer + attributes :id, :name, :description +end diff --git a/app/serializers/api/v1/passive_skill_serializer.rb b/app/serializers/api/v1/passive_skill_serializer.rb new file mode 100644 index 0000000..17b55bd --- /dev/null +++ b/app/serializers/api/v1/passive_skill_serializer.rb @@ -0,0 +1,3 @@ +class Api::V1::PassiveSkillSerializer < Api::V1::BaseSerializer + attributes :id, :name, :description +end diff --git a/app/serializers/api/v1/rarity_serializer.rb b/app/serializers/api/v1/rarity_serializer.rb new file mode 100644 index 0000000..3eb1a12 --- /dev/null +++ b/app/serializers/api/v1/rarity_serializer.rb @@ -0,0 +1,3 @@ +class Api::V1::RaritySerializer < Api::V1::BaseSerializer + attributes :id, :name +end diff --git a/app/serializers/api/v1/super_attack_serializer.rb b/app/serializers/api/v1/super_attack_serializer.rb new file mode 100644 index 0000000..5b6857c --- /dev/null +++ b/app/serializers/api/v1/super_attack_serializer.rb @@ -0,0 +1,3 @@ +class Api::V1::SuperAttackSerializer < Api::V1::BaseSerializer + attributes :id, :name, :description +end diff --git a/app/serializers/api/v1/type_serializer.rb b/app/serializers/api/v1/type_serializer.rb new file mode 100644 index 0000000..a88845b --- /dev/null +++ b/app/serializers/api/v1/type_serializer.rb @@ -0,0 +1,3 @@ +class Api::V1::TypeSerializer < Api::V1::BaseSerializer + attributes :id, :name +end diff --git a/config/routes.rb b/config/routes.rb index bc3e9bd..4a23fe8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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