Browse Source

[Ruby] Use clamp for queries count (#8853)

* [ruby-padrino] Pin rack version to 2.2

Padrino doesn't work yet with rack 3.
Also remove deprecated `--path` option from bundle command.

* [ruby/grape] Remove deprecated --path option from bundle command

* [Ruby] Use clamp for queries count

Use Ruby's `clamp` instead of using custom logic to clamp the queries
count.

This also introduces constants for min/max queries, where missing.
Petrik de Heus 1 year ago
parent
commit
383714a1f8

+ 6 - 5
frameworks/Ruby/agoo/app.rb

@@ -15,18 +15,19 @@ $pool = ConnectionPool.new(size: 256, timeout: 5) do
                              })
         end
 
+MAX_PK = 10_000
+QUERIES_MIN = 1
+QUERIES_MAX = 500
+
 class BaseHandler
   def self.extract_queries_param(request = nil)
     queries = Rack::Utils.parse_query(request['QUERY_STRING'])['queries'].to_i rescue 1
 
-    return   1 if queries <   1
-    return 500 if queries > 500
-
-    queries
+    queries.clamp(QUERIES_MIN, QUERIES_MAX)
   end
 
   def self.get_one_random_number
-    1 + Random.rand(10000)
+    1 + Random.rand(MAX_PK)
   end
 
   def self.get_one_record(id = get_one_random_number)

+ 1 - 3
frameworks/Ruby/grape/config.ru

@@ -36,9 +36,7 @@ module Acme
     helpers do
       def bounded_queries
         queries = params[:queries].to_i
-        return QUERIES_MIN if queries < QUERIES_MIN
-        return QUERIES_MAX if queries > QUERIES_MAX
-        queries
+        queries.clamp(QUERIES_MIN, QUERIES_MAX)
       end
 
       # Return a random number between 1 and MAX_PK

+ 10 - 11
frameworks/Ruby/padrino/app/controllers.rb

@@ -1,3 +1,7 @@
+MAX_PK = 10_000
+QUERIES_MIN = 1
+QUERIES_MAX = 500
+
 HelloWorld::App.controllers  do
   get '/json', :provides => [:json] do
     response.headers['Server'] = 'padrino'
@@ -8,19 +12,17 @@ HelloWorld::App.controllers  do
   get '/db', :provides => [:json] do
     response.headers['Server'] = 'padrino'
     response.headers['Date'] = Time.now.httpdate
-    id = Random.rand(10000) + 1
+    id = Random.rand(MAX_PK) + 1
     World.get(id).attributes.to_json
   end
 
   get '/queries', :provides => [:json] do
     response.headers['Server'] = 'padrino'
     response.headers['Date'] = Time.now.httpdate
-    queries = params['queries'].to_i
-    queries = 1 if queries < 1
-    queries = 500 if queries > 500
+    queries = params['queries'].to_i.clamp(QUERIES_MIN, QUERIES_MAX)
 
     results = (1..queries).map do
-      World.get(Random.rand(10000) + 1).attributes
+      World.get(Random.rand(MAX_PK) + 1).attributes
     end.to_json
   end
 
@@ -37,15 +39,13 @@ HelloWorld::App.controllers  do
   get '/updates', :provides => [:json] do
     response.headers['Server'] = 'padrino'
     response.headers['Date'] = Time.now.httpdate
-    queries = params['queries'].to_i
-    queries = 1 if queries < 1
-    queries = 500 if queries > 500
+    queries = params['queries'].to_i.clamp(QUERIES_MIN, QUERIES_MAX)
 
     worlds = (1..queries).map do
       # get a random row from the database, which we know has 10000
       # rows with ids 1 - 10000
-      world = World.get(Random.rand(10000) + 1)
-      world.update(:randomNumber => Random.rand(10000) + 1)
+      world = World.get(Random.rand(MAX_PK) + 1)
+      world.update(randomNumber: Random.rand(MAX_PK) + 1)
       world.attributes
     end
 
@@ -60,4 +60,3 @@ HelloWorld::App.controllers  do
   end
 
 end
-

+ 1 - 3
frameworks/Ruby/rack-sequel/hello_world.rb

@@ -12,9 +12,7 @@ class HelloWorld
     params = Rack::Utils.parse_query(env['QUERY_STRING'])
 
     queries = params['queries'].to_i
-    return QUERIES_MIN if queries < QUERIES_MIN
-    return QUERIES_MAX if queries > QUERIES_MAX
-    queries
+    queries.clamp(QUERIES_MIN, QUERIES_MAX)
   end
 
   # Return a random number between 1 and MAX_PK

+ 1 - 7
frameworks/Ruby/rack/pg_db.rb

@@ -46,13 +46,7 @@ class PgDb
 
   def validate_count(count)
     count = count.to_i
-    if count < MIN_QUERIES
-      MIN_QUERIES
-    elsif count > MAX_QUERIES
-      MAX_QUERIES
-    else
-      count
-    end
+    count.clamp(MIN_QUERIES, MAX_QUERIES)
   end
 
   def select_promises(count)

+ 1 - 4
frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

@@ -48,10 +48,7 @@ class HelloWorldController < ApplicationController
 
   def query_count
     queries = params[:queries].to_i
-    return MIN_QUERIES if queries < MIN_QUERIES
-    return MAX_QUERIES if queries > MAX_QUERIES
-
-    queries
+    queries.clamp(MIN_QUERIES, MAX_QUERIES)
   end
 
   def random_id

+ 1 - 3
frameworks/Ruby/roda-sequel/hello_world.rb

@@ -7,9 +7,7 @@ class HelloWorld < Roda
 
   def bounded_queries
     queries = request.params["queries"].to_i
-    return QUERIES_MIN if queries < QUERIES_MIN
-    return QUERIES_MAX if queries > QUERIES_MAX
-    queries
+    queries.clamp(QUERIES_MIN, QUERIES_MAX)
   end
 
   # Return a random number between 1 and MAX_PK

+ 1 - 3
frameworks/Ruby/sinatra-sequel/hello_world.rb

@@ -20,9 +20,7 @@ class HelloWorld < Sinatra::Base
   helpers do
     def bounded_queries
       queries = params[:queries].to_i
-      return QUERIES_MIN if queries < QUERIES_MIN
-      return QUERIES_MAX if queries > QUERIES_MAX
-      queries
+      queries.clamp(QUERIES_MIN, QUERIES_MAX)
     end
 
     def json(data)

+ 1 - 3
frameworks/Ruby/sinatra/hello_world.rb

@@ -17,9 +17,7 @@ class HelloWorld < Sinatra::Base
   helpers do
     def bounded_queries
       queries = params[:queries].to_i
-      return QUERIES_MIN if queries < QUERIES_MIN
-      return QUERIES_MAX if queries > QUERIES_MAX
-      queries
+      queries.clamp(QUERIES_MIN, QUERIES_MAX)
     end
 
     def json(data)