0

This:

api_url = [1, 2, 3, 20, 21, 22, 23, 24, 25, 27]
api_url.each do |deal|
  response = HTTParty.get('http://api.pipedrive.com/v1/deals?filter_id=' + deal.to_s + '&start=0&sort_mode=asc&api_token=example')
  result = JSON.parse(response.body)
end

spits out:

[1, 2, 3, 20, 21, 22, 23, 24, 25, 27]

rather than the resulting JSON it's supposed to. When I replace loop with just one single call rather than an array, this very similar block afterwords works perfectly:

stage_deal_ids.each do |deal|
  response = HTTParty.get('http://api.pipedrive.com/v1/deals/' + deal.to_s + '/activities?start=0&api_token=example')
  result = JSON.parse(response.body)
end
1
  • What is your question? Commented Oct 3, 2014 at 16:30

1 Answer 1

2

You're looking for map not each:

api_url = [1, 2, 3, 20, 21, 22, 23, 24, 25, 27]
responses = api_url.map do |deal|
  response = HTTParty.get("http://api.pipedrive.com/v1/deals?filter_id=#{deal}&start=0&sort_mode=asc&api_token=example")
  JSON.parse(response.body)
end
puts responses.inspect

each just returns the original collection.

Sign up to request clarification or add additional context in comments.

2 Comments

Also it's always better to use string interpolation which calls to_s by default.
Updated the example for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.