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

Inspirações de outras linguagens: Um conjunto d...

Inspirações de outras linguagens: Um conjunto de ferramentas Ruby (relativamente) novo

Sempre que queremos criar um projeto novo ou adicionar novas features a algo já um já existente, queremos descobrir algo diferente ou que atenda melhor nossas necessidades. Tivemos uma oportunidade destas na Locaweb ao começar o projeto 'Hightower', que consistia em remover dados de clientes do SAP, embora a tecnologia escolhida tenha sido Java, acabei descobrindo uma série de ferramentas novas que podem ser utilizadas em projetos Ruby. Gostaria nesta talk de apresentar duas ferramentas(gems)/conceitos que achei interessantes, Dry-Auto_Inject e Stoplight.

Avatar for Gabriel Malaquias

Gabriel Malaquias

July 25, 2017
Tweet

Other Decks in Programming

Transcript

  1. 1. About me Gabriel Malaquias de Souza. ➔ Locaweb -

    Enterprise Applications Since 2015 ➔ Analysis and Systems development Fatec Zona Leste - 2015 ➔ Blog gabrielmalakias.com.br
  2. 4. Spring @Component public class CreateArticle { private final ArticleRepository

    repository; @Autowired public CreateArticle(ArticleRepository repository) { this.repository = repository; }
  3. About Hystrix You can support graceful degradation in a Hystrix

    command by adding a fallback method that Hystrix will call to obtain a default value or values in case the main command fails. - Github Project Page
  4. 9. Hystrix public class CommandHelloFailure extends HystrixCommand<String> { @Override protected

    String run() { throw new RuntimeException("fail"); } @Override protected String getFallback() { return "Hello Failure " + name + "!"; } }
  5. 12. Another Example irb(main):001:0> lambda = -> { puts "Hello

    World!" } => #<Proc:(irb):1 (lambda)> irb(main):002:0> light = Stoplight('example') { lambda.call }
  6. 13. Running It irb(main):003:0> light.run Hello World! => nil irb(main):004:0>

    light.run Hello World! => nil irb(main):005:0> light.color => "green"
  7. 14. A failure example light = Stoplight('call') { raise StandardError

    } .with_fallback{|_| 'Fallback' } .with_cool_off_time(3)
  8. 15. Running It irb(main):012:0> light.run => “Fallback” irb(main):015:0> light.color =>

    "red" irb(main):016:0> sleep(3) irb(main):017:0> light.color => "yellow"
  9. 16. The Stoplight data store require 'stoplight' require 'redis' redis

    = Redis.new(host: ENV["REDIS"]) Stoplight::Light.default_data_store = Stoplight::DataStore::Redis.new(redis)
  10. 17. Creating a command def fallback(id) ::Article.new end def find(id)

    // It can raise an Exception end def custom_error_handler(error, handle) raise error if error.is_a?(RecordNotFound) handle.call(error) end
  11. 18. Creating a command class Commands::Article::Find def call(id) Stoplight('article.find') do

    find(id) end.with_fallback do |error| fallback(id) end.with_error_handler do |error, handle| custom_error_handler(error, handle) end.run end
  12. 19. Creating a custom notifier module Notifiers class Custom <

    Stoplight::Notifier::Base def notify(light, from_color, to_color, error) puts("[Custom] - Light: #{light.inspect}”) end end end