Skip to content

Commit

Permalink
Merge pull request #317 from dmathieu/indifferent-hash
Browse files Browse the repository at this point in the history
Use IndifferentHash class instead of method
gudmundur authored Nov 21, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 7834eaf + 124143f commit 51ee665
Showing 2 changed files with 39 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/pliny/helpers/params.rb
Original file line number Diff line number Diff line change
@@ -8,14 +8,23 @@ def body_params

def parse_body_params
if request.media_type == "application/json"
p = indifferent_params(MultiJson.decode(request.body.read))
p = load_params(MultiJson.decode(request.body.read))
request.body.rewind
p
elsif request.form_data?
indifferent_params(request.POST)
load_params(request.POST)
else
{}
end
end

def load_params(data)
# Sinatra 1.x only supports the method. Sinatra 2.x only supports the class
if respond_to?(:indifferent_params)
indifferent_params(data)
else
Sinatra::IndifferentHash[data]
end
end
end
end
28 changes: 28 additions & 0 deletions spec/helpers/params_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "spec_helper"

describe Pliny::Helpers::Params do

def app
Sinatra.new do
helpers Pliny::Helpers::Params
post "/" do
body_params.to_json
end
end
end

it "loads json params" do
post "/", {hello: "world"}.to_json, {'CONTENT_TYPE' => 'application/json'}
assert_equal "{\"hello\":\"world\"}", last_response.body
end

it "loads form data params" do
post "/", {hello: "world"}
assert_equal "{\"hello\":\"world\"}", last_response.body
end

it "loads from an unknown content type" do
post "/", "<hello>world</hello>", {'CONTENT_TYPE' => 'application/xml'}
assert_equal "{}", last_response.body
end
end

0 comments on commit 51ee665

Please sign in to comment.