Skip to content

Commit

Permalink
bugfix values_for
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Mar 15, 2019
1 parent 718b4a2 commit e76b31d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 43 deletions.
43 changes: 7 additions & 36 deletions lib/nice_http/manage_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,54 +120,25 @@ def manage_request(*arguments)
}
end
elsif data.kind_of?(Hash)
data_n = Hash.new()
data.each { |key, value|
data_n[key.to_s()] = value
}
if arguments[0].include?(:values_for)
#req[:values_for][:loginName] or req[:values_for]["loginName"]
new_values_hash = Hash.new()
arguments[0][:values_for].each { |kv, vv|
if data_n.keys.include?(kv.to_s())
new_values_hash[kv.to_s()] = vv
end
}
data_n.merge!(new_values_hash)
data = data.set_values(arguments[0][:values_for])
end
data = data_n.to_json()
data = data.to_json()
elsif data.kind_of?(Array)
data_arr = Array.new()
data.each_with_index { |row, indx|
unless row.kind_of?(Hash)
@logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
return :error, :error, :error
end
data_n = Hash.new()
row.each { |key, value|
data_n[key.to_s()] = value
}
if arguments[0].include?(:values_for)
#req[:values_for][:loginName] or req[:values_for]["loginName"]
new_values_hash = Hash.new()
if arguments[0][:values_for].kind_of?(Hash) #values[:mykey][3]
arguments[0][:values_for].each { |kv, vv|
if data_n.keys.include?(kv.to_s()) and !vv[indx].nil?
new_values_hash[kv.to_s()] = vv[indx]
end
}
elsif arguments[0][:values_for].kind_of?(Array) #values[5][:mykey]
if !arguments[0][:values_for][indx].nil?
arguments[0][:values_for][indx].each { |kv, vv|
if data_n.keys.include?(kv.to_s())
new_values_hash[kv.to_s()] = vv
end
}
end
if arguments[0][:values_for].is_a?(Array)
data_n = row.set_values(arguments[0][:values_for][indx])
else
@logger.fatal("Wrong format on request application/json when supplying values, the data is an array of Hashes but the values supplied are not")
return :error, :error, :error
data_n = row.set_values(arguments[0][:values_for])
end
data_n.merge!(new_values_hash)
else
data_n = row
end
data_arr.push(data_n)
}
Expand Down
4 changes: 2 additions & 2 deletions nice_http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'nice_http'
s.version = '1.6.3'
s.version = '1.6.4'
s.summary = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources."
s.description = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Manage different hosts on the fly. Easily get the value you want from the JSON strings. Use hashes on your requests."
s.authors = ["Mario Ruiz"]
Expand All @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = ["LICENSE","README.md"]
s.homepage = 'https://github.com/MarioRuiz/nice_http'
s.license = 'MIT'
s.add_runtime_dependency 'nice_hash', '~> 1.9', '>= 1.9.0'
s.add_runtime_dependency 'nice_hash', '~> 1.10', '>= 1.10.0'
s.add_development_dependency 'rspec', '~> 3.8', '>= 3.8.0'
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ["lib"]
Expand Down
13 changes: 8 additions & 5 deletions spec/nice_http/nice_http_post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@
it "changes :data when supplied :values_for" do
request = {
path: "/api/users",
data: { name: "morpheus", job: "leader" },
headers: {"Content-Type": "application/json"},
data: {name: "morpheus", job: "leader", lab: { doom: 'one', beep: true }, products: [{one:1 ,two:2}, {one:11,two:22}]}
}

request.values_for = { name: "peter" }
request.values_for = {name: "peter", doom: 'two', one: "uno"}
resp = @http.post(request)
expect(resp.code).to eq 201
expect(resp.data.json(:name)).to eq "peter"
expect(resp.data.json(:doom)).to eq "two"
expect(resp.data.json(:one)).to eq (["uno","uno"])
end

it "redirects when auto_redirect is true and http code is 30x" do
Expand Down Expand Up @@ -196,10 +198,10 @@
{ name: "peter", job: "vicepresident" },
],
}
request.values_for = { job: ["dev", "cleaner"] }
request.values_for = { job: "dev" }
resp = @http.post(request)
expect(resp.code).to eq 201
expect(resp.data.json(:job)).to eq ["dev", "cleaner"]
expect(resp.data.json(:job)).to eq ["dev", "dev"]
end

it "shows wrong format on request when not array of hashes" do
Expand Down Expand Up @@ -228,6 +230,7 @@
}
request.values_for = [{ job: "dev" }, { job: "cleaner" }]
resp = @http.post(request)

expect(resp.code).to eq 201
expect(resp.data.json(:job)).to eq ["dev", "cleaner"]
end
Expand Down

0 comments on commit e76b31d

Please sign in to comment.