From c82e603f0ab2c3e428cbbd2a2ba4aa42f0189023 Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Mon, 22 Jan 2018 14:14:51 -0500 Subject: [PATCH 1/6] Add pry to Gemfile and complete task 1 --- Gemfile | 1 + Gemfile.lock | 8 +++++++- client/main.rb | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index b1ff6ac..9868a5e 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,4 @@ source "https://rubygems.org" gem 'httparty' gem 'sidekiq' gem 'sinatra' +gem 'pry' diff --git a/Gemfile.lock b/Gemfile.lock index b26b151..4facf95 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,17 @@ GEM remote: https://rubygems.org/ specs: + coderay (1.1.2) concurrent-ruby (1.0.5) connection_pool (2.2.1) httparty (0.15.6) multi_xml (>= 0.5.2) + method_source (0.9.0) multi_xml (0.6.0) mustermann (1.0.1) + pry (0.11.3) + coderay (~> 1.1.0) + method_source (~> 0.9.0) rack (2.0.3) rack-protection (2.0.0) rack @@ -28,8 +33,9 @@ PLATFORMS DEPENDENCIES httparty + pry sidekiq sinatra BUNDLED WITH - 1.16.0 + 1.16.1 diff --git a/client/main.rb b/client/main.rb index d60d391..875d96d 100644 --- a/client/main.rb +++ b/client/main.rb @@ -13,7 +13,7 @@ # screen. response = HttpConnection.get('/') -puts # +puts JSON.parse(response)["message"] ################################################## From 56c6b79d21fcefe0db92e1e0c300d63bae79b3fd Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Mon, 22 Jan 2018 14:48:35 -0500 Subject: [PATCH 2/6] Complete Exercise 2 Note the change of the description for ex. 2 --- client/main.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/client/main.rb b/client/main.rb index 875d96d..1af437e 100644 --- a/client/main.rb +++ b/client/main.rb @@ -21,11 +21,20 @@ # The server has a big list of numbers. But it's not very good at math :-(, and # it was hoping you could maybe help it sum them up. # A. Send GET requests to /number until it tells you you've got them all. -# B. Send a POST to /sum with 'sum: ' as a parameter in the post body. +# B. Send a POST to /sum with 'the_sum: ' as a parameter in the post body. # C. Use `puts` to print the message portion of the response to the screen. numbers = [] -# +loop do + parsed_response = JSON.parse(HttpConnection.get('/number')) + numbers << parsed_response["number"] + break if parsed_response["stop_asking"] == true +end + +puts numbers + +sum = numbers.reduce(:+) +puts HttpConnection.post('/sum', :body => { "the_sum" => sum}) ################################################## # Exercise 3: Introducing sidekiq From 949c5a301893557d9fced1b49a18a50fd0a5ad2a Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Mon, 22 Jan 2018 15:16:41 -0500 Subject: [PATCH 3/6] Complete exercise 3 --- client/main.rb | 11 +++++------ client/sidekiq_workers.rb | 7 +++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/client/main.rb b/client/main.rb index 1af437e..0c4f681 100644 --- a/client/main.rb +++ b/client/main.rb @@ -26,15 +26,14 @@ numbers = [] loop do - parsed_response = JSON.parse(HttpConnection.get('/number')) - numbers << parsed_response["number"] - break if parsed_response["stop_asking"] == true + response = HttpConnection.get('/number') + numbers << JSON.parse(response)["number"] + break if JSON.parse(response)["stop_asking"] == true end -puts numbers - sum = numbers.reduce(:+) -puts HttpConnection.post('/sum', :body => { "the_sum" => sum}) +response = HttpConnection.post('/sum', :body => { "the_sum" => sum}) +puts JSON.parse(response)["message"] ################################################## # Exercise 3: Introducing sidekiq diff --git a/client/sidekiq_workers.rb b/client/sidekiq_workers.rb index d829914..d769ea2 100644 --- a/client/sidekiq_workers.rb +++ b/client/sidekiq_workers.rb @@ -11,6 +11,13 @@ def perform(path, params={}) # For exercise 3, replace this comment with code that # sends the request, parses the response, and uses `puts` to # print the message part of the response + query = path + "?" + params.each do |k,v| + query += "&#{k}=#{v}" + end + + response = JSON.parse(HttpConnection.get(query)) + puts response["message"] # For exercise 5, replace this comment with code that # retries the request if it fails From df631eef542aaa5316b56c41341c72956b08a4f2 Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Mon, 22 Jan 2018 15:34:31 -0500 Subject: [PATCH 4/6] Complete Exercise 4 --- client/main.rb | 4 ++-- client/sidekiq_workers.rb | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/client/main.rb b/client/main.rb index 0c4f681..129664c 100644 --- a/client/main.rb +++ b/client/main.rb @@ -64,9 +64,9 @@ # Instead, it'll appear in the sidekiq terminal. # - Question to think about: why does all of this have to be this way? -# +GetRequestSender.perform_async('/the_hard_stuff') sleep 0.1 -# +GetRequestSender.perform_async('/the_easy_stuff') verify_ex_4! diff --git a/client/sidekiq_workers.rb b/client/sidekiq_workers.rb index d769ea2..d2a7327 100644 --- a/client/sidekiq_workers.rb +++ b/client/sidekiq_workers.rb @@ -8,9 +8,6 @@ class GetRequestSender sidekiq_retry_in { 0 } def perform(path, params={}) - # For exercise 3, replace this comment with code that - # sends the request, parses the response, and uses `puts` to - # print the message part of the response query = path + "?" params.each do |k,v| query += "&#{k}=#{v}" From 169fad3272f20088b5118efbca636000c85ec69b Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Mon, 22 Jan 2018 15:50:08 -0500 Subject: [PATCH 5/6] Complete Exercise 5 --- client/main.rb | 2 +- client/sidekiq_workers.rb | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/client/main.rb b/client/main.rb index 129664c..9907322 100644 --- a/client/main.rb +++ b/client/main.rb @@ -82,7 +82,7 @@ # # (Remember to restart sidekiq after editing the file.) -# +GetRequestSender.perform_async('/touchy') verify_ex_5! # This can take up to 30 seconds diff --git a/client/sidekiq_workers.rb b/client/sidekiq_workers.rb index d2a7327..ade0a8b 100644 --- a/client/sidekiq_workers.rb +++ b/client/sidekiq_workers.rb @@ -13,10 +13,9 @@ def perform(path, params={}) query += "&#{k}=#{v}" end - response = JSON.parse(HttpConnection.get(query)) - puts response["message"] + response = HttpConnection.get(query) + puts JSON.parse(response)["message"] - # For exercise 5, replace this comment with code that - # retries the request if it fails + GetRequestSender.perform_async(path, params) unless response.code == 200 end end From dcd377dae8c84048eb606c77b1d0b9dd9589309f Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Mon, 22 Jan 2018 16:50:07 -0500 Subject: [PATCH 6/6] Update exercise 5 with better error handling --- client/sidekiq_workers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/sidekiq_workers.rb b/client/sidekiq_workers.rb index ade0a8b..515de90 100644 --- a/client/sidekiq_workers.rb +++ b/client/sidekiq_workers.rb @@ -16,6 +16,6 @@ def perform(path, params={}) response = HttpConnection.get(query) puts JSON.parse(response)["message"] - GetRequestSender.perform_async(path, params) unless response.code == 200 + raise unless response.code == 200 end end