Skip to content

Commit

Permalink
Rename /v2/blocks params
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Apr 24, 2019
1 parent d8f90c3 commit f1feb43
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
20 changes: 11 additions & 9 deletions API_DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,20 @@ Get blocks info list and paginate it.
#### params

Also Support camelCase.

```ruby
{
"numberFrom": "10" or "0xa", # integer or string of hex number
"numberTo": "20" or "0xa", # integer or string of hex number
"transactionFrom": "min transaction count", # integer or string of hex number
"transactionTo": "max transaction count", # integer or string of hex number
"page": "1", # integer, default 1
"perPage": "10", # integer, default 10
"block_number_from": "10" or "0xa", # number or integer
"block_number_to": "20", # number or integer
"min_transaction_count": "min transaction count", # integer
"max_transaction_count": "max transaction count", # integer
"page": "1", # default 1
"per_page": "10", # default 10

# offset and limit has lower priority than page and perPage
"offset": "1", # integer, database offset for pagination
"limit": "10", # integer, database limit for pagination
"offset": "1", # database offset for pagination
"limit": "10", # database limit for pagination
}
```

Expand All @@ -71,7 +74,6 @@ Get blocks info list and paginate it.
```ruby
{
"result": {
"count": 111200,
"blocks": [
{
"version": 0,
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/api/v2/blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ class Api::V2::BlocksController < ApplicationController
#
# params
# {
# "numberFrom": "10" or "0xa", // number or integer
# "numberTo": "20", // number or integer
# "transactionFrom": "min transaction count", // integer
# "transactionTo": "max transaction count", // integer
# "block_number_from": "10" or "0xa", // number or integer
# "block_number_to": "20", // number or integer
# "min_transaction_count": "min transaction count", // integer
# "max_transaction_count": "max transaction count", // integer
# "page": "1", // default 1
# "perPage": "10", // default 10
# "per_page": "10", // default 10
#
# # offset and limit has lower priority than page and perPage
# "offset": "1", // database offset for pagination
# "limit": "10", //database limit for pagination
# "limit": "10", // database limit for pagination
# }
#
# GET /v2/api/blocks
def index
params.transform_keys!(&:underscore)

options = {
block_number_gteq: parse_hex(params[:number_from]),
block_number_lteq: parse_hex(params[:number_to]),
transaction_count_gteq: parse_hex(params[:transaction_from]),
transaction_count_lteq: parse_hex(params[:transaction_to])
block_number_gteq: parse_hex(params[:block_number_from]),
block_number_lteq: parse_hex(params[:block_number_to]),
transaction_count_gteq: parse_hex(params[:min_transaction_count]),
transaction_count_lteq: parse_hex(params[:max_transaction_count])
}

blocks = Block.ransack(options).result.order(block_number: :desc)
Expand Down
40 changes: 20 additions & 20 deletions spec/controllers/api/v2/blocks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
context "params transform" do
let(:params) do
ActionController::Parameters.new({
numberFrom: 0,
numberTo: 10,
transactionFrom: 20,
transactionTo: 30,
blockNumberFrom: 0,
blockNumberTo: 10,
minTransactionCount: 20,
maxTransactionCount: 30,
page: 40,
perPage: 50,
offset: 60,
Expand All @@ -26,10 +26,10 @@

let(:transformed_params) do
ActionController::Parameters.new({
number_from: 0,
number_to: 10,
transaction_from: 20,
transaction_to: 30,
block_number_from: 0,
block_number_to: 10,
min_transaction_count: 20,
max_transaction_count: 30,
page: 40,
per_page: 50,
offset: 60,
Expand Down Expand Up @@ -58,33 +58,33 @@
expect(count).to eq 1
end

it "with numberFrom" do
post :index, params: { numberFrom: 1 }
it "with blockNumberFrom" do
post :index, params: { blockNumberFrom: 1 }
expect(count).to eq 1
end

it "with numberTo" do
post :index, params: { numberTo: 0 }
it "with blockNumberTo" do
post :index, params: { blockNumberTo: 0 }
expect(count).to eq 1
end

it "with numberFrom and NumberTo" do
post :index, params: { numberFrom: 2, numberTo: 1 }
it "with blockNumberFrom and blockNumberTo" do
post :index, params: { blockNumberFrom: 2, blockNumberTo: 1 }
expect(count).to eq 0
end

it "with transactionFrom" do
post :index, params: { transactionFrom: 1 }
it "with minTransactionCount" do
post :index, params: { minTransactionCount: 1 }
expect(count).to eq 1
end

it "with transactionTo" do
post :index, params: { transactionTo: 0 }
it "with maxTransactionCount" do
post :index, params: { maxTransactionCount: 0 }
expect(count).to eq 1
end

it "with transactionFrom and transactionTo" do
post :index, params: { transactionFrom: 1, transactionTo: 1 }
it "with minTransactionCount and maxTransactionCount" do
post :index, params: { minTransactionCount: 1, maxTransactionCount: 1 }
expect(count).to eq 1
end

Expand Down

0 comments on commit f1feb43

Please sign in to comment.