Class: BittraderBot::BotLogic

Inherits:
Object
  • Object
show all
Defined in:
lib/bittrader-bot/bot_logic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BotLogic

Initializes a BotLogic object



36
37
38
39
40
# File 'lib/bittrader-bot/bot_logic.rb', line 36

def initialize config
  @exchange = config['exchange']
  @telegram_token = config['telegram_token']
  @key = config['key']
end

Instance Attribute Details

#exchangeObject (readonly)

Returns the value of attribute exchange.



32
33
34
# File 'lib/bittrader-bot/bot_logic.rb', line 32

def exchange
  @exchange
end

#keyObject (readonly)

Returns the value of attribute key.



32
33
34
# File 'lib/bittrader-bot/bot_logic.rb', line 32

def key
  @key
end

#secretObject (readonly)

Returns the value of attribute secret.



32
33
34
# File 'lib/bittrader-bot/bot_logic.rb', line 32

def secret
  @secret
end

#telegram_tokenObject (readonly)

Returns the value of attribute telegram_token.



32
33
34
# File 'lib/bittrader-bot/bot_logic.rb', line 32

def telegram_token
  @telegram_token
end

Instance Method Details

#send_get_request(host, request) ⇒ Object

Send GET request to exchange



53
54
55
56
57
# File 'lib/bittrader-bot/bot_logic.rb', line 53

def send_get_request host, request
  https = start_connection host
  req = Net::HTTP::Get.new(request)
  https.request(req)
end

#send_post_request(data) ⇒ Object

Sends POST request to exchange



61
62
63
64
65
66
67
68
# File 'lib/bittrader-bot/bot_logic.rb', line 61

def send_post_request data
  request = Net::HTTP::Post.new(uri.path, initheader: {'Content-Type' =>'application/json'})
  request['key'] = @key
  request['sign'] = sign_post_data(data.to_json)
  request.body = data.to_json
  response = https.request(request)
  puts "Response #{response.code} #{response.message}: #{response.body}"
end

#sign_post_data(data) ⇒ Object

Signs POST data using HMAC-SHA512 method



72
73
74
75
# File 'lib/bittrader-bot/bot_logic.rb', line 72

def sign_post_data data
  digest = OpenSSL::Digest.new('sha512')
  OpenSSL::HMAC.digest(digest, @secret, data)
end

#start_connection(host) ⇒ Object

Starts the connection to the server and provides identification



44
45
46
47
48
49
# File 'lib/bittrader-bot/bot_logic.rb', line 44

def start_connection host
  uri = URI.parse(host)
  https = Net::HTTP.new(uri.host, 443)
  https.use_ssl = true
  return https
end