receive_web_request can also now receive the ActionDispatch request object

This commit is contained in:
Albert Sun 2016-04-09 11:52:54 -04:00
parent 758573f522
commit 9ddb4e97de
8 changed files with 20 additions and 20 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)
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)
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)
agent.trigger_web_request(params.except(:action, :controller, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers, request)
end
}
render :text => "ok"

View file

@ -95,7 +95,7 @@ class Agent < ActiveRecord::Base
false
end
def receive_web_request(params, method, format, headers={})
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
# Implement me in your subclass of Agent.
["not implemented", 404]
end
@ -149,20 +149,20 @@ class Agent < ActiveRecord::Base
end
end
def trigger_web_request(params, method, format, headers)
def trigger_web_request(params, method, format, headers, request)
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
self.last_web_request_at = Time.now
save!
end
elsif method(:receive_web_request).arity == 3
receive_web_request(params, method, format).tap do
self.last_web_request_at = Time.now
save!
end
else
receive_web_request(params, method, format, headers).tap do
if method(:receive_web_request).arity == 3
handled_request = receive_web_request(params, method, format)
else
handled_request = receive_web_request(params, method, format, headers, request)
end
handled_request.tap do
self.last_web_request_at = Time.now
save!
end

View file

@ -160,7 +160,7 @@ module Agents
interpolated['push_hubs'].presence || []
end
def receive_web_request(params, method, format, headers={})
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
unless interpolated['secrets'].include?(params['secret'])
if format =~ /json/
return [{ error: "Not Authorized" }, 401]

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={})
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
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

@ -57,7 +57,7 @@ module Agents
end
end
def receive_web_request(params, method, format, headers={})
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
params = params.symbolize_keys
if method != 'post'
return ['Not Found', 404]

View file

@ -45,7 +45,7 @@ module Agents
}
end
def receive_web_request(params, method, format, headers={})
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
# 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={})
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
if params.delete(:secret) == options[:secret]
memory[:web_request_values] = params
memory[:web_request_format] = format

View file

@ -729,20 +729,20 @@ describe Agent do
@agent.user = users(:bob)
@agent.save!
def @agent.receive_web_request(params, method, format, headers={})
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", {})
@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" do
context "when .receive_web_request is defined without headers or request" do
before do
@agent = Agents::WebRequestReceiver.new(:name => "something")
@agent.user = users(:bob)
@ -755,7 +755,7 @@ describe Agent do
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", {})
@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
@ -775,7 +775,7 @@ describe Agent do
it "outputs a deprecation warning and calls .receive_webhook with the params" do
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", {})
@agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {}, ActionDispatch::Request.new({}))
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