Note that there are some explanatory texts on larger screens.

plurals
  1. PORubymotion multilevel BW::HTTP calls return early
    primarykey
    data
    text
    <p>I am attempting to call a method, from_server, giving it a block that will accept the results. from_server loads a list of items from an api then for each in the list calls a function add_update giving it a block to accept the results for each detail. When all of the details have been handled, the from_server calls its block to return the summary.</p> <p>I can get the low levels to work ok. My problem is the first function calls its block before everything is done.</p> <p>Not sure why or where I need to put the block.call</p> <p>The main caller</p> <pre class="lang-rb prettyprint-override"><code>from_server do |updates, inserts| puts "\n Updates = #{updates}" puts " Inserts = #{inserts}\n\n" puts 'all Done' end </code></pre> <p>the from_server function that calles the </p> <pre class="lang-rb prettyprint-override"><code>def from_server(&amp;block) inserts = 0 updates = 0 channels = [1,2,4,6,7] for channels.each do add_update("/api/channels/#{channel}", &amp;block) do |added, channel| if added inserts += 1 else updates += 1 end #do some things with channel end block.call(updates, inserts) unless block.nil? # problem is here gets returned as soon as all channels have been started on a thread end end </code></pre> <p>gets the list and processes each item</p> <pre class="lang-rb prettyprint-override"><code>def from_server(&amp;block) uri = '/api/channels' ApiRequest.get(uri) do |header, body| new_count = 0 update_count = 0 puts "\nList Body = #{body}\n" body.each do |channel| add_update(channel['uri']) do |new, ad_channel| if new puts "\nNew #{ad_channel}" new_count += 1 else puts "\nUpdate #{ad_channel}" update_count += 1 end puts end end puts "\nDONE\n" block.call(update_count, new_count) unless block.nil? puts "All Requests Done\n" end puts "Request all channels started" end </code></pre> <p>Processes each item</p> <pre class="lang-rb prettyprint-override"><code>def add_update(channel_uri, &amp;block) ApiRequest.get(channel_uri) do |header, body| if ad_channel = AdvChannel.find(name: body['name']).first puts "\n#{ad_channel.name} already exists" new = false else ad_channel = AdvChannel.create(name: body['name'], phone: body['phone'], url: body['uri'], admin_url: body['admin_url'], channel_code: body['channel_code'], uri: channel_uri) puts "\nInserting #{ad_channel.name} - #{ad_channel.id}" new = true end block.call(new, ad_channel) if block_given? end </code></pre> <p>The results from executing the caller are: give me the following:</p> <pre><code>Request all channels started immediately (expected) the List Body string (expected) Add update one started (expected) DONE (unexpected should be after all are done) Updates = 0 (unexpected should be after all are done) Inserts = 0 (unexpected should be after all are done) all Done (unexpected should be after all are done) All Requested Done (unexpected should be after all are done) the insert or update information for each channel (expected) </code></pre> <p>How do I get the summary after all is complete?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload