Skip to content

Commit d866ec0

Browse files
authored
Merge pull request #41 from github/twp/accept-json-encoded-request-body
Refactor params handling
2 parents d52753a + 2a857d0 commit d866ec0

5 files changed

Lines changed: 28 additions & 18 deletions

File tree

Rakefile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
1414
rdoc.rdoc_files.include('lib/**/*.rb')
1515
end
1616

17-
18-
19-
20-
21-
2217
Bundler::GemHelper.install_tasks
2318

2419
require 'rake/testtask'
@@ -30,5 +25,4 @@ Rake::TestTask.new(:test) do |t|
3025
t.verbose = false
3126
end
3227

33-
3428
task default: :test

lib/chatops/controller.rb

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ def list
2727
end
2828

2929
def process(*args)
30-
scrubbed_params = jsonrpc_params.except(
31-
:user, :mention_slug, :method, :controller, :action, :params, :room_id)
32-
33-
scrubbed_params.each { |k, v| params[k] = v }
30+
setup_params!
3431

3532
if params[:chatop].present?
3633
params[:action] = params[:chatop]
@@ -40,7 +37,7 @@ def process(*args)
4037
end
4138
end
4239

43-
super *args
40+
super(*args)
4441
rescue AbstractController::ActionNotFound
4542
return jsonrpc_method_not_found
4643
end
@@ -52,8 +49,27 @@ def execute_chatop
5249

5350
protected
5451

52+
def setup_params!
53+
json_body.each do |key, value|
54+
next if params.has_key? key
55+
params[key] = value
56+
end
57+
58+
@jsonrpc_params = params.delete(:params) if params.has_key? :params
59+
60+
self.params = params.permit(:action, :chatop, :controller, :id, :mention_slug, :method, :room_id, :user)
61+
end
62+
5563
def jsonrpc_params
56-
params["params"] || {}
64+
@jsonrpc_params ||= ActionController::Parameters.new
65+
end
66+
67+
def json_body
68+
hash = {}
69+
if request.content_type =~ %r/\Aapplication\/json\Z/i
70+
hash = ActiveSupport::JSON.decode(request.raw_post) || {}
71+
end
72+
hash.with_indifferent_access
5773
end
5874

5975
# `options` supports any of the optional fields documented

lib/chatops/controller/test_case_helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ def chat(message, user, room_id = "123")
5050

5151
named_params, command = extract_named_params(message)
5252

53-
matcher = matchers.find { |matcher| matcher["regex"].match(command) }
53+
matcher = matchers.find { |m| m["regex"].match(command) }
5454

5555
raise NoMatchingCommandRegex.new("No command matches '#{command}'") unless matcher
5656

5757
match_data = matcher["regex"].match(command)
5858
jsonrpc_params = named_params.dup
5959
matcher["params"].each do |param|
60-
jsonrpc_params[param] = match_data[param.to_sym]
60+
jsonrpc_params[param] ||= match_data[param.to_sym]
6161
end
6262
jsonrpc_params.merge!(user: user, room_id: room_id, mention_slug: user)
6363
chatop matcher["name"].to_sym, jsonrpc_params

lib/chatops/controller/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ChatopsController
2-
VERSION = "3.1.1"
2+
VERSION = "3.2.0"
33
end

spec/lib/chatops/controller_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
chatop :wcid,
1515
/(?:where can i deploy|wcid)(?: (?<app>\S+))?/,
1616
"where can i deploy?" do
17-
return jsonrpc_invalid_params("I need nope, sorry") if params[:app] == "nope"
18-
jsonrpc_success "You can deploy #{params["app"]} just fine."
17+
return jsonrpc_invalid_params("I need nope, sorry") if jsonrpc_params[:app] == "nope"
18+
jsonrpc_success "You can deploy #{jsonrpc_params["app"]} just fine."
1919
end
2020

2121
chatop :foobar,
@@ -35,7 +35,7 @@ def unexcluded_chatop_method
3535
end
3636

3737
def ensure_app_given
38-
return jsonrpc_invalid_params("I need an app, every time") unless params[:app].present?
38+
return jsonrpc_invalid_params("I need an app, every time") unless jsonrpc_params[:app].present?
3939
end
4040
end
4141

0 commit comments

Comments
 (0)