Mirror of Github Repo. https://github.com/flockingbird/roost
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sink 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env ruby
  2. # frozen_string_literal: true
  3. $LOAD_PATH << '.'
  4. require 'bundler/setup'
  5. require 'bcrypt'
  6. require 'logger'
  7. require 'securerandom'
  8. require 'yajl'
  9. require_relative '../config/environment'
  10. require_relative '../config/database'
  11. require_relative '../app/aggregates/member'
  12. require_relative '../app/commands/application_command'
  13. require_relative '../app/commands/application_command_handler'
  14. require_relative '../app/commands/member/add_member'
  15. LOG_LEVEL = ENV['LOG_LEVEL'].to_i || Logger::DEBUG
  16. # Handles a stream of nodes in STDIN emits them as events through the
  17. # AddMemberCommand.
  18. class EventSink
  19. include BCrypt
  20. def initialize
  21. @parser = Yajl::Parser.new(symbolize_keys: true)
  22. @parser.on_parse_complete = method(:object_parsed)
  23. @logger = Logger.new(STDOUT)
  24. EventSourcery.logger.level = @logger.level = LOG_LEVEL
  25. end
  26. def object_parsed(obj)
  27. log(Logger::DEBUG, '-- parsed object')
  28. obj[:aggregate_id] ||= SecureRandom.uuid
  29. obj[:password] = Password.create(obj[:password]) if obj.key?(:password)
  30. command = Commands::Member::AddMember::Command.new(obj)
  31. Commands::Member::AddMember::CommandHandler.new(command: command).handle
  32. rescue BadRequest => e
  33. log(Logger::ERROR, e.message)
  34. end
  35. def call(io)
  36. @parser.parse(io)
  37. rescue Yajl::ParseError => e
  38. log(Logger::ERROR, e.message)
  39. close_connection
  40. end
  41. private
  42. def log(severity, message = nil, progname = 'sink')
  43. @logger.add(severity, message, progname)
  44. end
  45. end
  46. EventSink.new.call(STDIN)