Module: Bitstamper::Rest::Errors

Included in:
Client
Defined in:
lib/bitstamper/rest/errors.rb

Instance Method Summary collapse

Instance Method Details

#error?(response) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
# File 'lib/bitstamper/rest/errors.rb', line 5

def error?(response)
  if response.is_a?(Hash)
    if response.has_key?("error")
      process_error(response.fetch("error", nil))
    elsif response.fetch("status", nil) == "error"
      process_error(response.fetch("reason", nil))
    end
  end
end

#process_error(error) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bitstamper/rest/errors.rb', line 15

def process_error(error)
  if error.is_a?(String)
    if error == ::Bitstamper::Constants::ERRORS[:matches][:invalid_permissions]
      raise ::Bitstamper::Errors::InvalidPermissionsError.new(::Bitstamper::Constants::ERRORS[:responses][:invalid_permissions])
    elsif error == ::Bitstamper::Constants::ERRORS[:matches][:invalid_order]
      raise ::Bitstamper::Errors::InvalidOrderError.new(::Bitstamper::Constants::ERRORS[:matches][:invalid_order])
    end
  elsif error.is_a?(Hash)
    process_hash_error(error)
  end
end

#process_hash_error(error) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/bitstamper/rest/errors.rb', line 27

def process_hash_error(error)
  if error.has_key?("amount")
    raise ::Bitstamper::Errors::InvalidAmountError.new(error.fetch("amount", [])&.first)
  elsif error.has_key?("address")
    raise ::Bitstamper::Errors::InvalidAddressError.new(error.fetch("address", [])&.first)
  elsif error.has_key?("__all__")
    raise ::Bitstamper::Errors::InvalidOrderError.new(error.fetch("__all__", [])&.first)
  end
end