Skip to content

Commit

Permalink
Rename dispatch_event method to avoid potential conflicts and fix JSO…
Browse files Browse the repository at this point in the history
…N serialization issues
  • Loading branch information
emorissettegregoire committed Aug 27, 2024
1 parent babb2e6 commit 9df71aa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
33 changes: 18 additions & 15 deletions app/controllers/stealth/event_controller.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
module Stealth
class EventController < ApplicationController
# skip the default Rails CSRF protection for webhook calls...
skip_before_action :verify_authenticity_token, only: [:dispatch]
skip_before_action :verify_authenticity_token, only: [:dispatch_event]

def dispatch
# Stealth::Logger.l(topic: params[:service], message: 'Received webhook.')
def dispatch_event
Stealth::Logger.l(topic: params[:service], message: 'Received webhook.')

# # JSON params need to be parsed and added to the params
# if request.env['CONTENT_TYPE']&.match(/application\/json/i)
# json_params = MultiJson.load(request.body.read)
# end
# Convert params to a JSON-serializable plain Ruby hash
plain_params = JSON.parse(params.to_json)

# dispatcher = Stealth::Dispatcher.new(
# service: params[:service],
# params: params,
# headers: get_helpers_from_request(request)
# )
if request.env['CONTENT_TYPE']&.match(/application\/json/i)
json_params = MultiJson.load(request.body.read)
plain_params.merge!(json_params)
end

dispatcher = Stealth::Dispatcher.new(
service: plain_params["service"],
params: plain_params,
headers: get_helpers_from_request(request)
)

# headers 'Access-Control-Allow-Origin' => '*',
# 'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST']
# # content_type 'audio/mp3'

# content_type 'audio/mp3'
# content_type 'application/octet-stream'

# dispatcher.coordinate
dispatcher.coordinate
end

private
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

Stealth::Engine.routes.draw do
# Stealth Default Service Routes
post ':service', to: 'event#dispatch'
post ':service', to: 'event#dispatch_event'
end
5 changes: 3 additions & 2 deletions lib/stealth/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
require 'stealth/event_manager'
require 'stealth/event_triggers'

require 'stealth/service_event'

require 'stealth/jobs'
require 'stealth/dispatcher'
require 'stealth/errors'
require 'stealth/logger'
Expand All @@ -28,6 +27,8 @@

# services
require 'stealth/services/base_client'
require 'stealth/services/jobs/handle_event_job'
require 'stealth/service_event'

# helpers
# require 'stealth/helpers/redis'
Expand Down
7 changes: 7 additions & 0 deletions lib/stealth/jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Stealth
class Jobs

include Sidekiq::Worker

end
end
2 changes: 1 addition & 1 deletion lib/stealth/service_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ServiceEvent
:event_type, # Type of event (phone, message, reaction, etc. WIP: We need to standardize these)
:event, # Name of event (call, hangup, message, etc. WIP: We need to standardize these)
:message, # Message content
:payload, # Payload information
:payload # Payload information

def initialize(service:)
@service = service
Expand Down
28 changes: 17 additions & 11 deletions stealth.gemspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
require_relative "lib/stealth/version"

Gem::Specification.new do |spec|
spec.name = 'stealth'
spec.summary = 'Ruby framework for conversational bots'
spec.description = 'Ruby framework for building conversational bots.'
spec.homepage = 'https://github.com/hellostealth/stealth'
spec.licenses = ['MIT']
spec.version = '3.0.0.alpha1'
spec.authors = ['Matthew Black']
spec.email = '[email protected]'
Gem::Specification.new do |s|
s.name = 'stealth'
s.summary = 'Ruby framework for conversational bots'
s.description = 'Ruby framework for building conversational bots.'
s.homepage = 'https://github.com/hellostealth/stealth'
s.licenses = ['MIT']
s.version = '3.0.0.alpha1'
s.authors = ['Matthew Black']
s.email = '[email protected]'

s.add_dependency 'redis', '~> 5.0'
s.add_dependency 'sidekiq', '~> 7.0'

s.add_development_dependency 'rspec', '~> 3.9'
s.add_development_dependency 'rack-test', '~> 2.0'
s.add_development_dependency 'mock_redis', '~> 0.22'

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the "allowed_push_host"
# to allow pushing to a single host or delete this section to allow pushing to any host.
Expand All @@ -19,9 +25,9 @@ Gem::Specification.new do |spec|
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."

spec.files = Dir.chdir(File.expand_path(__dir__)) do
s.files = Dir.chdir(File.expand_path(__dir__)) do
Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
end

spec.add_dependency "rails", ">= 7.1.3.4"
s.add_dependency "rails", ">= 7.1.3.4"
end

0 comments on commit 9df71aa

Please sign in to comment.