Skip to content

Commit 383714a

Browse files
authored
[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.
1 parent f1c8109 commit 383714a

File tree

9 files changed

+23
-42
lines changed

9 files changed

+23
-42
lines changed

frameworks/Ruby/agoo/app.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
})
1616
end
1717

18+
MAX_PK = 10_000
19+
QUERIES_MIN = 1
20+
QUERIES_MAX = 500
21+
1822
class BaseHandler
1923
def self.extract_queries_param(request = nil)
2024
queries = Rack::Utils.parse_query(request['QUERY_STRING'])['queries'].to_i rescue 1
2125

22-
return 1 if queries < 1
23-
return 500 if queries > 500
24-
25-
queries
26+
queries.clamp(QUERIES_MIN, QUERIES_MAX)
2627
end
2728

2829
def self.get_one_random_number
29-
1 + Random.rand(10000)
30+
1 + Random.rand(MAX_PK)
3031
end
3132

3233
def self.get_one_record(id = get_one_random_number)

frameworks/Ruby/grape/config.ru

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ module Acme
3636
helpers do
3737
def bounded_queries
3838
queries = params[:queries].to_i
39-
return QUERIES_MIN if queries < QUERIES_MIN
40-
return QUERIES_MAX if queries > QUERIES_MAX
41-
queries
39+
queries.clamp(QUERIES_MIN, QUERIES_MAX)
4240
end
4341

4442
# Return a random number between 1 and MAX_PK

frameworks/Ruby/padrino/app/controllers.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
MAX_PK = 10_000
2+
QUERIES_MIN = 1
3+
QUERIES_MAX = 500
4+
15
HelloWorld::App.controllers do
26
get '/json', :provides => [:json] do
37
response.headers['Server'] = 'padrino'
@@ -8,19 +12,17 @@
812
get '/db', :provides => [:json] do
913
response.headers['Server'] = 'padrino'
1014
response.headers['Date'] = Time.now.httpdate
11-
id = Random.rand(10000) + 1
15+
id = Random.rand(MAX_PK) + 1
1216
World.get(id).attributes.to_json
1317
end
1418

1519
get '/queries', :provides => [:json] do
1620
response.headers['Server'] = 'padrino'
1721
response.headers['Date'] = Time.now.httpdate
18-
queries = params['queries'].to_i
19-
queries = 1 if queries < 1
20-
queries = 500 if queries > 500
22+
queries = params['queries'].to_i.clamp(QUERIES_MIN, QUERIES_MAX)
2123

2224
results = (1..queries).map do
23-
World.get(Random.rand(10000) + 1).attributes
25+
World.get(Random.rand(MAX_PK) + 1).attributes
2426
end.to_json
2527
end
2628

@@ -37,15 +39,13 @@
3739
get '/updates', :provides => [:json] do
3840
response.headers['Server'] = 'padrino'
3941
response.headers['Date'] = Time.now.httpdate
40-
queries = params['queries'].to_i
41-
queries = 1 if queries < 1
42-
queries = 500 if queries > 500
42+
queries = params['queries'].to_i.clamp(QUERIES_MIN, QUERIES_MAX)
4343

4444
worlds = (1..queries).map do
4545
# get a random row from the database, which we know has 10000
4646
# rows with ids 1 - 10000
47-
world = World.get(Random.rand(10000) + 1)
48-
world.update(:randomNumber => Random.rand(10000) + 1)
47+
world = World.get(Random.rand(MAX_PK) + 1)
48+
world.update(randomNumber: Random.rand(MAX_PK) + 1)
4949
world.attributes
5050
end
5151

@@ -60,4 +60,3 @@
6060
end
6161

6262
end
63-

frameworks/Ruby/rack-sequel/hello_world.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ def bounded_queries(env)
1212
params = Rack::Utils.parse_query(env['QUERY_STRING'])
1313

1414
queries = params['queries'].to_i
15-
return QUERIES_MIN if queries < QUERIES_MIN
16-
return QUERIES_MAX if queries > QUERIES_MAX
17-
queries
15+
queries.clamp(QUERIES_MIN, QUERIES_MAX)
1816
end
1917

2018
# Return a random number between 1 and MAX_PK

frameworks/Ruby/rack/pg_db.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,7 @@ def select_world(id)
4646

4747
def validate_count(count)
4848
count = count.to_i
49-
if count < MIN_QUERIES
50-
MIN_QUERIES
51-
elsif count > MAX_QUERIES
52-
MAX_QUERIES
53-
else
54-
count
55-
end
49+
count.clamp(MIN_QUERIES, MAX_QUERIES)
5650
end
5751

5852
def select_promises(count)

frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ def update
4848

4949
def query_count
5050
queries = params[:queries].to_i
51-
return MIN_QUERIES if queries < MIN_QUERIES
52-
return MAX_QUERIES if queries > MAX_QUERIES
53-
54-
queries
51+
queries.clamp(MIN_QUERIES, MAX_QUERIES)
5552
end
5653

5754
def random_id

frameworks/Ruby/roda-sequel/hello_world.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ class HelloWorld < Roda
77

88
def bounded_queries
99
queries = request.params["queries"].to_i
10-
return QUERIES_MIN if queries < QUERIES_MIN
11-
return QUERIES_MAX if queries > QUERIES_MAX
12-
queries
10+
queries.clamp(QUERIES_MIN, QUERIES_MAX)
1311
end
1412

1513
# Return a random number between 1 and MAX_PK

frameworks/Ruby/sinatra-sequel/hello_world.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ class HelloWorld < Sinatra::Base
2020
helpers do
2121
def bounded_queries
2222
queries = params[:queries].to_i
23-
return QUERIES_MIN if queries < QUERIES_MIN
24-
return QUERIES_MAX if queries > QUERIES_MAX
25-
queries
23+
queries.clamp(QUERIES_MIN, QUERIES_MAX)
2624
end
2725

2826
def json(data)

frameworks/Ruby/sinatra/hello_world.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ class HelloWorld < Sinatra::Base
1717
helpers do
1818
def bounded_queries
1919
queries = params[:queries].to_i
20-
return QUERIES_MIN if queries < QUERIES_MIN
21-
return QUERIES_MAX if queries > QUERIES_MAX
22-
queries
20+
queries.clamp(QUERIES_MIN, QUERIES_MAX)
2321
end
2422

2523
def json(data)

0 commit comments

Comments
 (0)