Skip to content

Instantly share code, notes, and snippets.

@Hasstrup
Hasstrup / Dockerfile
Created April 12, 2021 09:05 — forked from hopsoft/Dockerfile
Dockerize your Rails app
FROM ruby:3.0-alpine
RUN apk add --no-cache --update \
ack \
bash \
build-base \
curl \
htop \
less \
libsass \
<?xml version="1.0" encoding="UTF-8"?>
<mjml>
<mj-head>
<mj-include path="../../shared/head.mjml" />
<mj-attributes>
<mj-class name="outer-border-empty-top" padding="0 1px 1px 1px" border-radius="6px" background-color="#E7E7ED" />
</mj-attributes>
</mj-head>
<mj-body background-color="#f2f2f6">
<mj-wrapper text-align="center" padding-top="0" padding-bottom="0">
# Implement the sections of code marked TODO above.
# Rules for bidding:
# Agents always bid in sequence, e.g. agent 1 bids first, then agent 2, etc.
# The bidding starts with the first agent deciding on their initial bid (which will be returned by `get_bid_increase`). The amount can also be 0.
# The next agent (e.g. agent 2) must then increase their bid so that their bid is as much as any other agent's bid (which so far is only agent 1), or be forced to withdraw from the bidding completely. (if the agent has withdrawn, they will be skipped from here onwards). They may also decide to bid more, in which case the next agent has to match that higher bid (or withdraw).
# The bidding will come around to agent 1 again if their current bid does not match the highest bid. The agent must increase their bid (by returning the value they want to increase it by from `getBidIncrease`) so that their bid will match the highest bid that any other agent has put in so far, or withdraw from the bidding. They can als
# BaseService serves as a foundational class for creating service objects
# in the application. It provides a standardized interface for service calls
# and integrates error handling mechanisms.
class BaseService
class ServiceError < StandardError; end
class InvalidInputError < StandardError; end
include ErrorHandling::Validatable
# Calls the service with the provided keyword arguments.
#
# frozen_string_literal: true
# The Registration class handles the user registration process.
# It validates input and creates a new user in the database,
# generating an authentication token upon successful registration.
class Users::Contexts::Registration < BaseService
performs_checks
def initialize(input:)
@input = input
# Represents a base input class for handling and validating input data.
class BaseInput
class InputValidationError < StandardError; end
include Validation
class << self
attr_reader :fields, :target
# Defines the attributes that the input class can accept.
# frozen_string_literal: true
# Provides validation methods for input classes.
module Validation
def valid?
validate!
errors.empty?
end
def validate!
# frozen_string_literal: true
# The Context class is used to encapsulate the result of a service operation,
# holding information about success, errors, messages, and any relevant payload.
class Context
attr_reader :success, :errors, :messages, :payload
def initialize
@success = true
@errors = []
# frozen_string_literal: true
class UsersController < ApplicationController
def register
context = ::Users::Contexts::Registration.call(input: users_registration_input)
return error_response(context.message) unless context.success?
render json: ::Tokens::TokenBlueprint.render(context.payload), status: :ok
end
class Users::RegistrationInput < BaseInput
REQUIRED_KEYS = %i[email password].freeze
attributes(*REQUIRED_KEYS, :first_name, :last_name)
def validate!
validate_required_keys!
end
end
############