Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
176 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Animal | ||
# Class with grape api for Animal project. | ||
class API < Grape::API | ||
version 'v1', using: :path | ||
format :json | ||
|
||
helpers do | ||
def current_user | ||
@current_user ||= User.authorize!(env) | ||
end | ||
|
||
def authenticate! | ||
error!('401 Unauthorized', 401) unless current_user | ||
end | ||
end | ||
|
||
http_basic do |username, password| | ||
# verify user's password here | ||
user = User.find_by email: username | ||
user.valid_password? password if user | ||
end | ||
|
||
desc 'Pet index.', params: Entities::Pet.documentation | ||
get '/pet' do | ||
authenticate! | ||
pets = Pet.all | ||
present pets, with: Entities::Pet, type: :full | ||
end | ||
|
||
desc 'Create a pet.', params: Entities::Pet.documentation | ||
params do | ||
requires( | ||
:all, | ||
except: [:id, :email], | ||
using: Entities::Pet.documentation.except(:id, :email) | ||
) | ||
end | ||
post '/pet' do | ||
authenticate! | ||
Pet.transaction do | ||
Pet.create_with_params! params, @current_user | ||
end | ||
end | ||
|
||
desc 'Breed index.', params: Entities::Breed.documentation | ||
get '/breed' do | ||
authenticate! | ||
breeds = Breed.all | ||
present breeds, with: Entities::Breed, type: :full | ||
end | ||
|
||
desc 'Birth index.', params: Entities::Birth.documentation | ||
get '/birth' do | ||
authenticate! | ||
births = Birth.all | ||
present births, with: Entities::Birth, type: :full | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Animal | ||
# Entities for Animal API. | ||
module Entities | ||
# General Entity with id, and timestamps. | ||
class Id < Grape::Entity | ||
expose :id | ||
expose :created_at | ||
expose :updated_at | ||
end | ||
|
||
# General Entity plus name field. | ||
class Named < Id | ||
expose :name | ||
end | ||
|
||
# Pet entity (Pet and Birth model). | ||
class Pet < Named | ||
expose :email, as: :owner_email | ||
expose :breed_id | ||
expose :gender | ||
expose :father_id | ||
expose :mother_id | ||
end | ||
|
||
# Breed entity like Breed model. | ||
class Breed < Named | ||
end | ||
|
||
# Birth entity like Birth model. | ||
class Birth < Id | ||
expose :child_id | ||
expose :father_id | ||
expose :mother_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Be sure to restart your server when you modify this file. | ||
|
||
Rails.application.config.session_store( | ||
:cookie_store, key: '_hello_animals_rails_session' | ||
:cookie_store, key: '_animals_rails_session' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters