Skip to content

Commit

Permalink
Split named_params, rename not_enough_funds error to insufficient_bal…
Browse files Browse the repository at this point in the history
…ance (#2075)

* Split named_params and rename not_enough_funds error to insufficient_balance

* Remove Currency.enabled.codes from desc. Remoe tralling whitespaces

* Rename: errors.rb exception_handlers.rb. Update specs. rescue_from :all
  • Loading branch information
Maksym authored and Yaroslav Savchuk committed Feb 18, 2019
1 parent ab19e94 commit efdeb43
Show file tree
Hide file tree
Showing 23 changed files with 147 additions and 212 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ gem 'jbuilder', '~> 2.7.0'
gem 'figaro', '~> 1.1.1'
gem 'hashie', '~> 3.5.7'
gem 'aasm', '~> 5.0.0'
gem 'active_model-errors_details', '~> 1.3.1'
gem 'bunny', '~> 2.11.0'
gem 'cancancan', '~> 2.2.0'
gem 'enumerize', '~> 2.2.2'
Expand Down
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ GEM
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
active_model-errors_details (1.3.1)
activemodel (>= 3.2.13, < 5.0.0)
activesupport
activejob (4.2.11)
activesupport (= 4.2.11)
globalid (>= 0.3.0)
Expand Down Expand Up @@ -398,7 +395,6 @@ PLATFORMS

DEPENDENCIES
aasm (~> 5.0.0)
active_model-errors_details (~> 1.3.1)
angularjs-rails (~> 1.3.15)
annotate (~> 2.7)
api-pagination (~> 4.8.2)
Expand Down
6 changes: 2 additions & 4 deletions app/api/v2/account/deposits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Deposits < Grape::API
optional :currency,
type: String,
values: { value: -> { Currency.enabled.codes(bothcase: true) }, message: 'account.currency.doesnt_exist' },
desc: -> { "Currency value contains #{Currency.enabled.codes(bothcase: true).join(',')}" }
desc: 'Currency code'
optional :state,
type: String,
values: { value: -> { Deposit::STATES.map(&:to_s) }, message: 'account.deposit.invalid_state' }
Expand Down Expand Up @@ -52,9 +52,7 @@ class Deposits < Grape::API
desc: "Deposit transaction id"
end
get "/deposits/:txid" do
deposit = current_user.deposits.find_by(txid: params[:txid])
raise DepositByTxidNotFoundError, params[:txid] unless deposit

deposit = current_user.deposits.find_by!(txid: params[:txid])
present deposit, with: API::V2::Entities::Deposit
end

Expand Down
1 change: 0 additions & 1 deletion app/api/v2/account/mount.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module API::V2
module Account
class Mount < Grape::API
include NewExceptionsHandlers

before { authenticate! }

Expand Down
4 changes: 2 additions & 2 deletions app/api/v2/account/withdraws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Withdraws < Grape::API
optional :currency,
type: String,
values: { value: -> { Currency.coins.enabled.codes(bothcase: true) }, message: 'account.currency.doesnt_exist'},
desc: -> { "Any supported currencies: #{Currency.enabled.codes(bothcase: true).join(',')}." }
desc: 'Currency code.'
optional :limit,
type: { value: Integer, message: 'account.withdraw.non_integer_limit' },
values: { value: 1..100, message: 'account.withdraw.invalid_limit' },
Expand Down Expand Up @@ -73,7 +73,7 @@ class Withdraws < Grape::API

rescue ::Account::AccountError => e
report_exception_to_screen(e)
error!({ errors: ['account.withdraw.not_enough_funds'] }, 422)
error!({ errors: ['account.withdraw.insufficient_balance'] }, 422)
rescue ActiveRecord::RecordInvalid => e
report_exception_to_screen(e)
error!({ errors: ['account.withdraw.invalid_amount'] }, 422)
Expand Down
79 changes: 0 additions & 79 deletions app/api/v2/errors.rb

This file was deleted.

31 changes: 31 additions & 0 deletions app/api/v2/exception_handlers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# encoding: UTF-8
# frozen_string_literal: true

module API::V2
module ExceptionHandlers
def self.included(base)
base.instance_eval do
rescue_from Grape::Exceptions::ValidationErrors do |e|
errors_array = e.full_messages.map do |err|
err.split.last
end
error!({ errors: errors_array }, 422)
end

rescue_from Peatio::Auth::Error do |e|
report_exception(e)
error!({ errors: ['jwt.decode_and_verify'] }, 401)
end

rescue_from ActiveRecord::RecordNotFound do |_e|
error!({ errors: ['record.not_found'] }, 404)
end

rescue_from :all do |e|
report_exception(e)
error!({ errors: ['server.internal_error'] }, 500)
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/api/v2/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def create_order(attrs)
# TODO: Rewrite this rescue.
rescue ::Account::AccountError => e
report_exception_to_screen(e)
error!({ errors: ['market.account.not_enough_funds']}, 422)
error!({ errors: ['market.account.insufficient_balance']}, 422)
rescue ::Order::InsufficientMarketLiquidity => e
report_exception_to_screen(e)
error!({ errors: ['market.order.insufficient_market_liquidity'] }, 422)
Expand Down
1 change: 0 additions & 1 deletion app/api/v2/market/mount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module API::V2
module Market
class Mount < Grape::API
include NewExceptionsHandlers

before { authenticate! }
before { trading_must_be_permitted! }
Expand Down
78 changes: 78 additions & 0 deletions app/api/v2/market/named_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# encoding: UTF-8
# frozen_string_literal: true

module API
module V2
module Market
module NamedParams
extend ::Grape::API::Helpers

params :currency do
requires :currency,
type: String,
values: { value: -> { Currency.enabled.pluck(:id) }, message: 'account.currency.doesnt_exist' },
desc: 'The currency code.'
end

params :market do
requires :market,
type: String,
values: { value: -> { ::Market.enabled.ids }, message: 'market.market.doesnt_exist' },
desc: -> { V2::Entities::Market.documentation[:id] }
end

params :order do
requires :side,
type: String,
values: { value: %w(sell buy), message: 'market.order.invalid_side' },
desc: -> { V2::Entities::Order.documentation[:side] }
requires :volume,
type: { value: BigDecimal, message: 'market.order.non_decimal_volume' },
values: { value: -> (v){ v.try(:positive?) }, message: 'market.order.non_positive_volume' },
desc: -> { V2::Entities::Order.documentation[:volume] }
optional :ord_type,
type: String,
values: { value: -> { Order::TYPES}, message: 'market.order.invalid_type' },
default: 'limit',
desc: -> { V2::Entities::Order.documentation[:type] }
given ord_type: ->(val) { val == 'limit' } do
requires :price,
type: { value: BigDecimal, message: 'market.order.non_decimal_price' },
values: { value: -> (p){ p.try(:positive?) }, message: 'market.order.non_positive_price' },
desc: -> { V2::Entities::Order.documentation[:price] }
end
end

params :order_id do
requires :id,
type: { value: Integer, message: 'market.order.non_integer_id' },
allow_blank: { value: false, message: 'market.order.empty_id' },
desc: -> { V2::Entities::Order.documentation[:id] }
end

params :trade_filters do
optional :limit,
type: { value: Integer, message: 'market.trade.non_integer_limit' },
values: { value: 1..1000, message: 'market.trade.invalid_limit' },
default: 100,
desc: 'Limit the number of returned trades. Default to 100.'
optional :page,
type: { value: Integer, message: 'market.trade.non_integer_page' },
allow_blank: { value: false, message: 'market.trade.empty_page' },
default: 1,
desc: 'Specify the page of paginated results.'
optional :timestamp,
type: { value: Integer, message: 'market.trade.non_integer_timestamp' },
allow_blank: { value: false, message: 'market.trade.empty_timestamp' },
desc: "An integer represents the seconds elapsed since Unix epoch."\
"If set, only trades executed before the time will be returned."
optional :order_by,
type: String,
values: { value: %w(asc desc), message: 'market.trade.invalid_order_by' },
default: 'desc',
desc: "If set, returned trades will be sorted in specific order, default to 'desc'."
end
end
end
end
end
4 changes: 2 additions & 2 deletions app/api/v2/market/orders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module API
module V2
module Market
class Orders < Grape::API
helpers ::API::V2::NamedParams
helpers ::API::V2::Market::NamedParams

desc 'Get your orders, results is paginated.',
is_array: true,
Expand All @@ -17,7 +17,7 @@ class Orders < Grape::API
desc: -> { V2::Entities::Market.documentation[:id] }
optional :state,
type: String,
values: { value: -> { Order.state.values } , message: 'market.order.invalid_state' },
values: { value: -> { Order.state.values }, message: 'market.order.invalid_state' },
desc: 'Filter order by state.'
optional :limit,
type: { value: Integer, message: 'market.order.non_integer_limit' },
Expand Down
2 changes: 1 addition & 1 deletion app/api/v2/market/trades.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module API
module V2
module Market
class Trades < Grape::API
helpers API::V2::NamedParams
helpers API::V2::Market::NamedParams

desc 'Get your executed trades. Trades are sorted in reverse creation order.',
is_array: true,
Expand Down
2 changes: 1 addition & 1 deletion app/api/v2/mount.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: UTF-8
# frozen_string_literal: true

require_dependency 'v2/errors'
require_dependency 'v2/exception_handlers'
require_dependency 'v2/validations'

module API
Expand Down
76 changes: 0 additions & 76 deletions app/api/v2/named_params.rb

This file was deleted.

0 comments on commit efdeb43

Please sign in to comment.