class Vaultoro::TradingAPI::Sell

Public Instance Methods

execute!() click to toggle source
# File lib/vaultoro/trading_api/sell.rb, line 11
def execute!
  ensure_attribute_has_value :gld, :symbol, :type

  @errors << ":symbol argument must be 'gld'" unless @symbol == 'gld'
  @errors << ":type argument must be 'limit' or 'market'" unless ['limit', 'market'].include?(@type)

  return false if @errors.any?

  params = {}
  params.merge!(gld: @gold) if @gold
  params.merge!(price: @price) if @price && @type == 'market'

  response = Client.post("/sell/#{@symbol}/#{@type}", params)
  code = response.code rescue ""

  case code
  when '200'
    hash = JSON.parse(response.body)

    @status = hash['status'].upcase

    if @status == 'SUCCESS'
      @order_id = hash['data']['Order_ID']
      @order_time = DateTime.strptime(hash['data']['time'], '%Y-%m-%dT%H:%M:%S.%L%z').to_time.utc
    else
      set_errors(response)
      return false
    end

    return true
  else
    set_errors(response)
    return false
  end
end