diff --git a/Gemfile b/Gemfile index ee750c9b..003379b7 100644 --- a/Gemfile +++ b/Gemfile @@ -32,6 +32,7 @@ gem 'daemons' gem 'doorkeeper' gem 'doorkeeper-i18n' gem 'rack-cors', require: 'rack/cors' +gem 'active_model_serializers', '~> 0.10.0' gem 'twitter-bootstrap-rails', '~> 2.2.8' gem 'simple-navigation', '~> 3.14.0' # 3.x for simple_navigation_bootstrap gem 'simple-navigation-bootstrap' diff --git a/Gemfile.lock b/Gemfile.lock index 549d302c..f0c681b3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -70,6 +70,11 @@ GEM erubis (~> 2.7.0) rails-dom-testing (~> 1.0, >= 1.0.5) rails-html-sanitizer (~> 1.0, >= 1.0.3) + active_model_serializers (0.10.7) + actionpack (>= 4.1, < 6) + activemodel (>= 4.1, < 6) + case_transform (>= 0.2) + jsonapi-renderer (>= 0.1.1.beta1, < 0.3) activejob (4.2.10) activesupport (= 4.2.10) globalid (>= 0.3.0) @@ -115,6 +120,8 @@ GEM capybara-webkit (1.14.0) capybara (>= 2.3.0, < 2.14.0) json + case_transform (0.2) + activesupport chronic (0.10.2) coderay (1.1.2) coffee-rails (4.2.2) @@ -210,6 +217,7 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (2.1.0) + jsonapi-renderer (0.2.0) kaminari (1.1.1) activesupport (>= 4.1.0) kaminari-actionview (= 1.1.1) @@ -488,6 +496,7 @@ PLATFORMS ruby DEPENDENCIES + active_model_serializers (~> 0.10.0) acts_as_tree acts_as_versioned! attribute_normalizer diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 6c229faa..9a6cc1d0 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -2,6 +2,8 @@ class Api::V1::BaseController < ApplicationController protect_from_forgery with: :null_session before_action :skip_session + before_action :authenticate + rescue_from ActiveRecord::RecordNotFound, with: :not_found_handler rescue_from ActiveRecord::RecordNotSaved, with: :not_acceptable_handler rescue_from ActiveRecord::RecordInvalid, with: :not_acceptable_handler diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb new file mode 100644 index 00000000..f260c0c9 --- /dev/null +++ b/app/controllers/api/v1/users_controller.rb @@ -0,0 +1,7 @@ +class Api::V1::UsersController < Api::V1::BaseController + + def show + render json: current_user + end + +end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb new file mode 100644 index 00000000..5510b747 --- /dev/null +++ b/app/serializers/user_serializer.rb @@ -0,0 +1,3 @@ +class UserSerializer < ActiveModel::Serializer + attributes :id, :name, :email, :locale +end diff --git a/config/initializers/active_model_serializers.rb b/config/initializers/active_model_serializers.rb new file mode 100644 index 00000000..aba3586b --- /dev/null +++ b/config/initializers/active_model_serializers.rb @@ -0,0 +1 @@ +ActiveModelSerializers.config.adapter = :json diff --git a/config/routes.rb b/config/routes.rb index 20b3ec4d..0dc22596 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -241,6 +241,14 @@ Foodsoft::Application.routes.draw do end end + ############## API + + namespace :api do + namespace :v1 do + resource :user, only: [:show] + end + end + ############## Feedback resource :feedback, only: [:new, :create], controller: 'feedback'