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
# my_api/config/routes.rb | |
Rails.application.routes.draw do | |
namespace :api, defaults: { format: "json" } do | |
namespace :v1 do | |
resources :users | |
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
# my_api/app/models/user.rb | |
class User < ActiveRecord::Base | |
validates :email, presence: true, uniqueness: true | |
validates :name, presence: true | |
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
# my_api/app/controller/api/v1/users_controller.rb | |
class Api::V1::UsersController < ApplicationController | |
def index | |
@users = User.all | |
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
# my_api/rspec/requests/users_spec.rb | |
require 'rails_helper' | |
RSpec.describe Api::V1::UsersController, type: :request do | |
describe "GET /users" do | |
before :each do | |
FactoryGirl.create_list(:users, 1) | |
get "/api/v1/users" |
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
# my_api/app/views/api/v1/users/index.json.jbuilder | |
json.array! @users do |user| | |
json.(user, :id, :email, :name) | |
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
class UsersController < ApplicationController | |
before_action :find_course, only: [:show, :edit, :update, :destroy] | |
def index | |
@users = User.all | |
end | |
def show | |
# find_user |
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
describe UsersController do | |
describe 'GET index' do | |
before do | |
@user = FactoryGirl.create(:user) | |
end | |
it 'ok' do | |
get :index | |
expect(response).to have_http_status(:ok) |
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
# my_api/app/models/user.rb | |
class User < ActiveRecord::Base | |
validates :email, presence: true, uniqueness: true | |
validates :name, presence: true | |
def self.with_token(data) | |
User.where(email: data[:email]).first_or_create do |user| | |
user.email = data[:email] |
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
class Token < ApplicationRecord | |
belongs_to :user | |
before_create :generate_token | |
def valid? | |
self.expires_at > DateTime.now | |
end | |
private |
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
# POST api/v1/users, params: { user: {email: '[email protected]', name: 'Giancarlos Isasi' } } | |
def create | |
if params[:user] | |
@user = User.with_token(params[:user]) # le pasamos la información que nuestro metodo 'with_token' usara para crear al usuario | |
@token = @user.tokens.create() # Al usuario que nos devuelve nuestro parametro 'with_token' le creamos un nuevo token | |
render "api/v1/users/show" # en esta vista mostraremos la info del usuario y su token respectivo | |
else | |
render json: { error: 'user params info is missing' } # solo si no nos envian la info necesaria | |
end | |
end |
OlderNewer