# frozen_string_literal: true module ApiController # General peatio api functionality class Peatio require 'json' require 'rest-client' require_relative 'barong' def initialize(endpoint, barong_base_url, app_id, email, password) @endpoint = endpoint @barong_base_url = barong_base_url @app_id = app_id @email = email @password = password refresh_jwt end def post_order(market, price, volume, type) response = RestClient.post("#{@endpoint}/orders", { 'market': market, 'side': type, 'volume': volume.to_s, 'price': price.to_s, 'ord_type': 'limit' }, {'Authorization': "Bearer #{@auth_token}", 'content_type': 'application/x-www-form-urlencoded'}) rescue RestClient::Unauthorized => e refresh_jwt puts e puts 'Getting new JWT' end def cancel_order(id) response = RestClient.post("#{@endpoint}/order/delete", { 'id': id }, {'Authorization': "Bearer #{@auth_token}", 'content_type': 'application/x-www-form-urlencoded'}) rescue RestClient::Unauthorized => e refresh_jwt puts e puts 'Getting new JWT' rescue ::StandardError => e puts e end def refresh_jwt @auth_token = ApiController::Barong.new(@barong_base_url, @app_id).log_in(@email, @password) end end end