Skip to content

Commit

Permalink
Transform v2/blocks keys
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Apr 24, 2019
1 parent 6546369 commit d8f90c3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
12 changes: 7 additions & 5 deletions app/controllers/api/v2/blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class Api::V2::BlocksController < ApplicationController
#
# GET /v2/api/blocks
def index
params.transform_keys!(&:underscore)

options = {
block_number_gteq: parse_hex(params[:numberFrom]),
block_number_lteq: parse_hex(params[:numberTo]),
transaction_count_gteq: parse_hex(params[:transactionFrom]),
transaction_count_lteq: parse_hex(params[:transactionTo])
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])
}

blocks = Block.ransack(options).result.order(block_number: :desc)
Expand All @@ -35,7 +37,7 @@ def index
blocks = blocks.offset(offset).limit(limit)
else
# use page and perPage
blocks = blocks.page(params[:page]).per(params[:perPage])
blocks = blocks.page(params[:page]).per(params[:per_page])
end

render json: {
Expand Down
32 changes: 32 additions & 0 deletions spec/controllers/api/v2/blocks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@
let(:result) { Oj.load(response.body).with_indifferent_access[:result] }
let(:count) { result[:blocks].size }

context "params transform" do
let(:params) do
ActionController::Parameters.new({
numberFrom: 0,
numberTo: 10,
transactionFrom: 20,
transactionTo: 30,
page: 40,
perPage: 50,
offset: 60,
limit: 70
})
end

let(:transformed_params) do
ActionController::Parameters.new({
number_from: 0,
number_to: 10,
transaction_from: 20,
transaction_to: 30,
page: 40,
per_page: 50,
offset: 60,
limit: 70
})
end

it "transform underscore" do
expect(params.transform_keys!(&:underscore)).to eq transformed_params
end
end

context "index" do
it "no params" do
post :index, params: {}
Expand Down

0 comments on commit d8f90c3

Please sign in to comment.