modify receive_web_request to take either 1 or 3 arguments

This commit is contained in:
Albert Sun 2016-04-13 11:19:19 -04:00
parent a4e01126d1
commit 5da40533c6
6 changed files with 56 additions and 32 deletions

View file

@ -24,7 +24,7 @@ class WebRequestsController < ApplicationController
if user
agent = user.agents.find_by_id(params[:agent_id])
if agent
content, status, content_type = agent.trigger_web_request(params.except(:action, :controller, :agent_id, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers, request)
content, status, content_type = agent.trigger_web_request(request)
if content.is_a?(String)
render :text => content, :status => status || 200, :content_type => content_type || 'text/plain'
@ -47,7 +47,7 @@ class WebRequestsController < ApplicationController
secret = params[:secret]
user.agents.of_type(Agents::UserLocationAgent).each { |agent|
if agent.options[:secret] == secret
agent.trigger_web_request(params.except(:action, :controller, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers, request)
agent.trigger_web_request(request)
end
}
render :text => "ok"

View file

@ -95,11 +95,15 @@ class Agent < ActiveRecord::Base
false
end
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
def receive_web_request(params, method, format)
# Implement me in your subclass of Agent.
["not implemented", 404]
end
# alternate method signature for receive_web_request
# def receive_web_request(request=ActionDispatch::Request.new( ... ))
# end
# Implement me in your subclass to decide if your Agent is working.
def working?
raise "Implement me in your subclass"
@ -149,7 +153,8 @@ class Agent < ActiveRecord::Base
end
end
def trigger_web_request(params, method, format, headers, request)
def trigger_web_request(request)
params = request.params.except(:action, :controller, :agent_id, :user_id, :format)
if respond_to?(:receive_webhook)
Rails.logger.warn "DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request."
receive_webhook(params).tap do
@ -157,10 +162,10 @@ class Agent < ActiveRecord::Base
save!
end
else
if method(:receive_web_request).arity == 3
handled_request = receive_web_request(params, method, format)
if method(:receive_web_request).arity == 1
handled_request = receive_web_request(request)
else
handled_request = receive_web_request(params, method, format, headers, request)
handled_request = receive_web_request(params, request.method_symbol.to_s, request.format)
end
handled_request.tap do
self.last_web_request_at = Time.now

View file

@ -81,7 +81,7 @@ module Agents
"#{server_url}/users/#{user.id}/web_requests/#{id}/#{secret}"
end
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
def receive_web_request(params, method, format)
if memory['pending_calls'].has_key? params['secret']
response = Twilio::TwiML::Response.new {|r| r.Say memory['pending_calls'][params['secret']], :voice => 'woman'}
memory['pending_calls'].delete params['secret']

View file

@ -45,7 +45,7 @@ module Agents
}
end
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
def receive_web_request(params, method, format)
# check the secret
secret = params.delete('secret')
return ["Not Authorized", 401] unless secret == interpolated['secret']

View file

@ -5,7 +5,7 @@ describe WebRequestsController do
cannot_receive_events!
cannot_be_scheduled!
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
def receive_web_request(params, method, format)
if params.delete(:secret) == options[:secret]
memory[:web_request_values] = params
memory[:web_request_format] = format

View file

@ -724,25 +724,6 @@ describe Agent do
end
context "when .receive_web_request is defined" do
before do
@agent = Agents::WebRequestReceiver.new(:name => "something")
@agent.user = users(:bob)
@agent.save!
def @agent.receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
memory['last_request'] = [params, method, format, headers]
['Ok!', 200]
end
end
it "calls the .receive_web_request hook, updates last_web_request_at, and saves" do
@agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({}))
expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html", {} ])
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
end
end
context "when .receive_web_request is defined without headers or request" do
before do
@agent = Agents::WebRequestReceiver.new(:name => "something")
@agent.user = users(:bob)
@ -754,13 +735,45 @@ describe Agent do
end
end
it "calls the .trigger_web_request with headers, but they don't get passed to .receive_web_request" do
@agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({}))
it "calls the .receive_web_request hook, updates last_web_request_at, and saves" do
request = ActionDispatch::Request.new({
'action_dispatch.request.request_parameters' => { :some_param => "some_value" },
'REQUEST_METHOD' => "POST",
'HTTP_ACCEPT' => 'text/html'
})
@agent.trigger_web_request(request)
expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html" ])
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
end
end
context "when .receive_web_request is defined with just request" do
before do
@agent = Agents::WebRequestReceiver.new(:name => "something")
@agent.user = users(:bob)
@agent.save!
def @agent.receive_web_request(request)
memory['last_request'] = [request.params, request.method_symbol.to_s, request.format, {'HTTP_X_CUSTOM_HEADER' => request.headers['HTTP_X_CUSTOM_HEADER']}]
['Ok!', 200]
end
end
it "calls the .trigger_web_request with headers, and they get passed to .receive_web_request" do
request = ActionDispatch::Request.new({
'action_dispatch.request.request_parameters' => { :some_param => "some_value" },
'REQUEST_METHOD' => "POST",
'HTTP_ACCEPT' => 'text/html',
'HTTP_X_CUSTOM_HEADER' => "foo"
})
@agent.trigger_web_request(request)
expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html", {'HTTP_X_CUSTOM_HEADER' => "foo"} ])
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
end
end
context "when .receive_webhook is defined" do
before do
@agent = Agents::WebRequestReceiver.new(:name => "something")
@ -774,8 +787,14 @@ describe Agent do
end
it "outputs a deprecation warning and calls .receive_webhook with the params" do
request = ActionDispatch::Request.new({
'action_dispatch.request.request_parameters' => { :some_param => "some_value" },
'REQUEST_METHOD' => "POST",
'HTTP_ACCEPT' => 'text/html'
})
mock(Rails.logger).warn("DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request.")
@agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({}))
@agent.trigger_web_request(request)
expect(@agent.reload.memory['last_webhook_request']).to eq({ "some_param" => "some_value" })
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
end