此:
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
吐出:
[1, 2, 3, 20, 21, 22, 23, 24, 25, 27]
而不是它应该得到的JSON。当我用一个单独的调用而不是数组替换循环时,这个非常相似的块后缀非常有效:
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
答案 0 :(得分:2)
您正在寻找map
而不是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
只返回原始集合。