Last active
November 28, 2020 19:25
-
-
Save zloyrusskiy/0e99e36402b1ac3e5fc739acc38194e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rest-client' | |
require 'uri' | |
require 'json' | |
class TinkoffInvestClient | |
API_URL = 'https://api-invest.tinkoff.ru/openapi' | |
API_SANDBOX_URL = 'https://api-invest.tinkoff.ru/openapi/sandbox' | |
def initialize(token, sandbox = false) | |
@token = token | |
@api_url = sandbox ? API_SANDBOX_URL : API_URL | |
end | |
def request method, path, **params | |
headers = {authorization: auth_header, content_type: :json, accept: :json} | |
url = url_from_path(path) | |
resp = case method.downcase | |
when 'get' | |
RestClient.get url, {params: params}.merge(headers) | |
when 'post' | |
RestClient.post url, params.to_json, headers | |
else | |
raise 'Unknown method %s for path %s' % [method, path] | |
end | |
{ status: resp.code, content: JSON.parse(resp.body)} | |
end | |
def url_from_path(path) | |
"%s/%s" % [@api_url.chomp('/'), path.sub(/^\//, '')] | |
end | |
def auth_header | |
"Bearer #{@token}" | |
end | |
end | |
RestClient.log = STDOUT | |
client = TinkoffInvestClient.new(ENV['TOKEN'], true) | |
pp client.request('get', '/market/stocks') | |
pp client.request('post', '/sandbox/currencies/balance', currency: "RUB", balance: 10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment