Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,9 @@ gem 'sidekiq', '~> 6.4', '>= 6.4.1'

# Object-based searching [https://github.com/activerecord-hackery/ransack]
gem 'ransack', github: 'activerecord-hackery/ransack'

# Blueprinter is a JSON Object Presenter for Ruby [https://github.com/procore/blueprinter]
gem 'blueprinter', '~> 0.25.3'

# The fastest JSON parser and object serializer [https://github.com/ohler55/oj]
gem 'oj', '~> 3.13', '>= 3.13.23'
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ GEM
ast (2.4.2)
awesome_print (1.9.2)
bcrypt (3.1.18)
blueprinter (0.25.3)
bootsnap (1.13.0)
msgpack (~> 1.2)
builder (3.2.4)
Expand Down Expand Up @@ -210,6 +211,7 @@ GEM
nio4r (2.5.8)
nokogiri (1.13.9-arm64-darwin)
racc (~> 1.4)
oj (3.13.23)
orm_adapter (0.5.0)
parallel (1.22.1)
parser (3.1.2.1)
Expand Down Expand Up @@ -313,6 +315,7 @@ PLATFORMS

DEPENDENCIES
awesome_print
blueprinter (~> 0.25.3)
bootsnap
byebug (~> 11.1, >= 11.1.3)
debug
Expand All @@ -330,6 +333,7 @@ DEPENDENCIES
letter_opener (~> 1.4, >= 1.4.1)
minitest-focus
mocha (~> 1.13)
oj (~> 3.13, >= 3.13.23)
pg (~> 1.1)
pry (~> 0.14.1)
puma (~> 5.0)
Expand Down
22 changes: 22 additions & 0 deletions app/blueprints/api/shared/meta_blueprint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Api
module Shared
class MetaBlueprint
def self.render(collection)
{
pagination: {
current: collection.current_page,
previous: collection.prev_page,
next: collection.next_page,
limit: collection.limit_value,
total_pages: collection.total_pages,
total_count: collection.total_count,
first_page: collection.first_page?,
last_page: collection.last_page?
}
}
end
end
end
end
29 changes: 29 additions & 0 deletions app/blueprints/models/doorkeeper/access_token_blueprint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Models
module Doorkeeper
class AccessTokenBlueprint < Blueprinter::Base
identifier :id

view :standard do
fields :token, :refresh_token, :expires_in, :created_at, :revoked_at, :token_type
end

view :with_resource_owner do
association :resource_owner, blueprint: Models::UserBlueprint, view: :standard
end

view :with_application do
association :application, blueprint: Models::Doorkeeper::ApplicationBlueprint, view: :without_secrets
end

view :with_resource_owner_and_application do
include_view :with_resource_owner
include_view :with_application
include_view :standard

exclude :revoked_at
end
end
end
end
19 changes: 19 additions & 0 deletions app/blueprints/models/doorkeeper/application_blueprint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Models
module Doorkeeper
class ApplicationBlueprint < Blueprinter::Base
identifier :id

view :standard do
fields :name, :uid, :secret, :redirect_uri, :scopes, :confidential, :created_at, :updated_at
end

view :without_secrets do
include_view :standard

excludes :uid, :secret, :redirect_uri, :confidential
end
end
end
end
15 changes: 15 additions & 0 deletions app/blueprints/models/user_blueprint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Models
class UserBlueprint < Blueprinter::Base
identifier :id

view :standard do
fields :email, :created_at, :updated_at
end

view :with_devise_trackable do
fields :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip
end
end
end
11 changes: 0 additions & 11 deletions app/contracts/users/passwords/send_instructions_contract.rb

This file was deleted.

16 changes: 0 additions & 16 deletions app/contracts/users/passwords/update_contract.rb

This file was deleted.

12 changes: 0 additions & 12 deletions app/contracts/users/registrations/register_contract.rb

This file was deleted.

13 changes: 13 additions & 0 deletions app/contracts/v1/users_contract/passwords/send_instructions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module V1
module UsersContract
module Passwords
class SendInstructions < ApplicationContract
params do
required(:email).filled(Types::Email)
end
end
end
end
end
18 changes: 18 additions & 0 deletions app/contracts/v1/users_contract/passwords/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module V1
module UsersContract
module Passwords
class Update < ApplicationContract
params do
required(:reset_password_token).filled(:string)
required(:password) { filled? & str? & min_size?(Devise.password_length.min) }
required(:password_confirmation) { filled? & str? & min_size?(Devise.password_length.min) }
end

rule(:password).validate(:password_confirmation)
rule(:password_confirmation).validate(:password_confirmation)
end
end
end
end
14 changes: 14 additions & 0 deletions app/contracts/v1/users_contract/registrations/register.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module V1
module UsersContract
module Registrations
class Register < ApplicationContract
params do
required(:email).filled(Types::Email)
required(:password).filled(:str?, min_size?: Devise.password_length.min)
end
end
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/v1/users/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PasswordsController < ApplicationController
include Doorkeeper::Authorize

def create
operation = ::Users::Passwords::CreateOperation.new(params: password_params).call
operation = V1::UsersOperation::Passwords::Create.new(params: password_params).call

if operation.success?
render json: operation.success, status: :ok
Expand All @@ -16,7 +16,7 @@ def create
end

def update
operation = ::Users::Passwords::UpdateOperation.new(params: password_params).call
operation = V1::UsersOperation::Passwords::Update.new(params: password_params).call

if operation.success?
render json: operation.success, status: :ok
Expand Down
8 changes: 5 additions & 3 deletions app/controllers/v1/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ class RegistrationsController < ApplicationController
include Doorkeeper::Authorize

def create
operation = ::Users::Registrations::CreateOperation.new(params: registration_params,
doorkeeper_application: current_doorkeeper_application).call
operation = V1::UsersOperation::Registrations::Create.new(params: registration_params,
doorkeeper_application: current_doorkeeper_application).call

if operation.success?
render json: operation.success, status: :created
render json: Models::Doorkeeper::AccessTokenBlueprint.render(operation.success,
view: :with_resource_owner_and_application),
status: :created
else
render json: operation.failure, status: :unprocessable_entity
end
Expand Down
23 changes: 0 additions & 23 deletions app/operations/users/passwords/create_operation.rb

This file was deleted.

23 changes: 0 additions & 23 deletions app/operations/users/passwords/update_operation.rb

This file was deleted.

24 changes: 0 additions & 24 deletions app/operations/users/registrations/create_operation.rb

This file was deleted.

25 changes: 25 additions & 0 deletions app/operations/v1/users_operation/passwords/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module V1
module UsersOperation
module Passwords
class Create < ApplicationOperation
option :params
option :contract, default: proc { V1::UsersContract::Passwords::SendInstructions.new }

def call
contract_params = yield validate(contract)
result = yield call_service(contract_params)

Success(result)
end

private

def call_service(contract_params)
UsersService::Passwords::SendInstructions.new(params: contract_params).call
end
end
end
end
end
25 changes: 25 additions & 0 deletions app/operations/v1/users_operation/passwords/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module V1
module UsersOperation
module Passwords
class Update < ApplicationOperation
option :params
option :contract, default: proc { V1::UsersContract::Passwords::Update.new }

def call
contract_params = yield validate(contract)
result = yield call_service(contract_params)

Success(result)
end

private

def call_service(contract_params)
UsersService::Passwords::Update.new(params: contract_params).call
end
end
end
end
end
Loading