Upgrade to Pro — share decks privately, control downloads, hide ads and more …

O "R" do REST

O "R" do REST

Avatar for Odinei Ramone

Odinei Ramone

September 13, 2025
Tweet

More Decks by Odinei Ramone

Other Decks in Programming

Transcript

  1. Oi, eu sou o Odinei! • Desenvolvedor de So-ware !"

    • Escritor de So-ware #$ • Visitando sebos procurando discos de vinil no precinho
  2. Esse é o Roy! • Roy Fielding (brabo) • Inventor

    do REST • co-escritor da especificação do protocolo HTTP
  3. Enquanto isso, no quadro de tarefas... [123] editar password "Como

    [persona], eu [quero], [para que]." Como pessoa usuária, eu quero atualizar a minha senha para ter mais segurança na minha conta.
  4. # app/controllers/person_controller.rb class PersonController < ApplicationController # vários códigos aqui

    def edit_password # ... end def update_password # ... end end # config/routes.rb resource :person do get :edit_password, controller: :person, action: :edit_password post :update_password, controller: :person, action: :update_password end
  5. # app/controllers/person/password_controller.rb class Person::PasswordController < ApplicationController def update password =

    Person.find!(params[:id]).then do |person| # a gente pode fazer um PORO Person::Password.new(person: person, password_params) end render :boa if password.update render :eita, errors: password.errors end end
  6. # app/model/person/password.rb class Person::Password include ActiveModel::Validations # validates e accessor

    aqui def update person.update(password: digest_password) if self.valid? self end private def digest_password # altas fita end
  7. # app/controllers/orders/filters_controller.rb class Orders::FiltersController < ApplicationController include Pagy::Backend def index

    filter = Order::Filter.as_params(filter_params) redirect_to orders_path and return if filter.empty? @pagy, @orders = Order.where(**filter).then { pagy it } render "orders/index" end private def filter_params params .permit( # altos params maneiros ) end end
  8. class Order::Filter def initialize(filter_params) filter_params .reject { |_, value| value.blank?

    } .then do @id_fim_a_fim = it.fetch(:id_fim_a_fim, nil) @start_date = it.fetch(:start_date, nil) @end_date = it.fetch(:end_date, nil) end end def as_params filters.reject { |_, value| value.blank? } end private attr_reader :id_fim_a_fim, :start_date, :end_date def created_at_range # códigos end def filters { id_fim_a_fim: id_fim_a_fim, created_at: created_at_range, } end end
  9. class IdFimAFim include ActiveModel::Model include Generator validates :value, id_fim_a_fim: true

    IUGU_ISPB = Rails.configuration.company.dig(:iugu, :ispb) attr_reader :value def initialize(value: nil, ispb: IUGU_ISPB, refund: false) @value = (value || generate(ispb: ispb, refund: refund)).to_s end def to_s = value.to_s def iugu? = value[1..8] == IUGU_ISPB def refund? = value[0] == REFUND_PREFIX end
  10. O que temos? • A API (interface) te guia sobre

    o que você pode fazer • Arquivos pequenos e mais simples (e mais fáceis de testar) • Single Responsability • CRUD
  11. Indo além! • Resource-Oriented Architecture (ROA) • Richardson Maturity Model

    • h8ps:/ /mar=nfowler.com/ar=cles/ richardsonMaturityModel.html
  12. Fontes e Referencias ⛲ • Dissertação do Roy sobre REST

    • h"ps://ics.uci.edu/~fielding/pubs/disserta6on/top.htm • RailsConf 2017: In Relentless Pursuit of REST • h"ps://www.youtube.com/watch?v=HctYHe-YjnE • Ruby Ronin: REST for Rails Engineers • h"ps://www.youtube.com/watch?v=eK3E2DBsVI_