Last active
May 9, 2019 14:40
-
-
Save stevo/3909bafbc6e81f723fb21c8680d8f453 to your computer and use it in GitHub Desktop.
Wisper - Pub/Sub Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/events/locks/code_generate_event_spec.rb | |
module Locks | |
RSpec.describe CodeGeneratedEvent do | |
it 'validates presence of code, reservation id and room id' do | |
expect(CodeGeneratedEvent.new(code: nil, reservation_id: 1, room_id: 1)).not_to be_valid | |
expect(CodeGeneratedEvent.new(code: '123', reservation_id: nil, room_id: 1)).not_to be_valid | |
expect(CodeGeneratedEvent.new(code: '123', reservation_id: 1, room_id: nil)).not_to be_valid | |
expect(CodeGeneratedEvent.new(code: '123', reservation_id: 1, room_id: 1)).to be_valid | |
end | |
end | |
end | |
# app/events/locks/code_generate_event.rb | |
module Locks | |
class CodeGeneratedEvent < DomainEvent | |
attribute :code, Types::Strict::String | |
attribute :reservation_id, Types::Strict::Integer | |
attribute :room_id, Types::Strict::Integer | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/services/locks/set_pin_codes_for_upcoming_reservations_spec.rb | |
module Locks | |
RSpec.describe SetPinCodesForUpcomingReservations do | |
describe '.call' do | |
it 'broadcasts locks__code_generated event' do | |
room = create(:room) | |
reservation = create(:reservation, checkin_date: '2018-04-12', room_id: room.id) | |
allow(GrantAccessToGuest).to receive(:call).with(reservation) { '123' } | |
expect { SetPinCodesForUpcomingReservations.call }.to broadcast( | |
:locks__code_generated, | |
code: '123', | |
reservation_id: reservation.id, | |
room_id: room.id | |
) | |
end | |
# ... | |
end | |
end | |
end | |
# app/services/locks/set_pin_codes_for_upcoming_reservations.rb | |
module Locks | |
class SetPinCodesForUpcomingReservation < Patterns::Service | |
include Wisper::Publisher | |
def initialize(reservation) | |
@reservation = reservation | |
end | |
def call | |
# Do the stuff... | |
broadcast( | |
:locks__code_generated, | |
code: get_new_access_code, | |
reservation_id: reservation.id, | |
room_id: room.id | |
) | |
end | |
# ... | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/subscriptions/crm_spec.rb | |
RSpec.describe GRM, subscribers: true do | |
describe 'Locks domain subscriptions' do | |
it 'is asynchronously subscribed to locks__code_generated' do | |
expect(GRM).to subscribe_to(:locks__code_generated). | |
asynchronously | |
end | |
end | |
end | |
# initializer/subscriptions/grm.rb | |
Wisper.subscribe(GRM, on: :locks__code_generated, async: true) | |
# app/domains/crm.rb | |
module GRM | |
def self.locks__code_generated(*event_data) | |
LocksCodeGeneratedHandler.new(*event_data).call | |
end | |
end | |
# app/event_handlers/crm/locks_code_generated_handler.rb | |
module CRM | |
class LocksCodeGeneratedHandler < DomainEventHandler | |
def call; end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module GRM | |
RSpec.describe LocksCodeGeneratedHandler do | |
describe '#call' do | |
it 'pushes pin code to SuperGrm' do | |
reservation = create(:reservation, :with_room, checkin_date: '2018-04-12') | |
event_data = { code: '456', reservation_id: reservation.id, room_id: reservation.room_id } | |
allow(SuperGrm::Client).to receive(:update_entry) | |
LocksCodeGeneratedHandler.new(event_data).call | |
expect(SuperGrm::Client).to have_received(:update_entry).with( | |
entry_id: reservation.id | |
custom_field: { key_code: '456' } | |
) | |
end | |
end | |
end | |
module GRM | |
class LocksCodeGeneratedHandler < DomainEventHandler | |
def call | |
SuperGrm::Client.update_entry( | |
entry_id: event_data.reservation_id, | |
custom_field: { key_code: event_data.code } | |
) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment