buda 0.0.0 → 0.1.4.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 796ca70fb64170e768e952558c775263ba8ae90b8be229f1ccffe2369a406363
4
- data.tar.gz: cfd9936ab950257b456a499272bf483b59032e4e25613041326afd51c078fe25
3
+ metadata.gz: 23006cfbbf3807694c9a39c4e28ac34683778e073a26430adb217d42b15e9695
4
+ data.tar.gz: e6dffdf36580ca3998f0873cb8a455fc7d1fee110b170e23ba2f72b3589e3c56
5
5
  SHA512:
6
- metadata.gz: 49040c0c76983eddd0efee5baa4207216129d274b8d7590ec6e60a55ea5db15c1ac4213f2558bb252bb2b37fc252503d950f12ac34d0914061ba8681f97f037f
7
- data.tar.gz: c3f78daac6a143d651cf7de4755759ca9f641f3e2aad63239c65105aaa2bcf69e831f868321f4420c413507fd2ed0cc81ed5e35ddee420edd3555022b97c8272
6
+ metadata.gz: 141403db1a3fdd436b726b84bda3e0ef471b16ffbe165c18950d6a6a2fda5f12312432ee0a655fa4b123f9788fbce2aa7ff00b9bedc82b68397e1ebd5fdc4baa
7
+ data.tar.gz: 4ba772a36838a7918e312219a3118777909d806d81bcaab528f4ce327adb4d6831d863d159d72c598b5ed5f30594244c5179901361dd3a32fa9c99fb82b9c519
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ *.gem
2
+ coverage/*
3
+ Gemfile.lock
4
+ *~
5
+ .bundle
6
+ .rvmrc
7
+ log/*
8
+ measurement/*
9
+ pkg/*
10
+ .DS_Store
11
+ .env
12
+ spec/dummy/tmp/*
13
+ spec/dummy/log/*.log
14
+ demo.rb
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+ AllCops:
5
+ TargetRubyVersion: 2.5
6
+ NewCops: enable
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fintoc.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
8
+ end
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'rake'
2
+ require 'rubocop/rake_task'
3
+
4
+ begin
5
+ require 'bundler/setup'
6
+ Bundler::GemHelper.install_tasks
7
+ rescue LoadError
8
+ puts 'although not required, bundler is recommended for running the tests'
9
+ end
10
+ task default: :spec
11
+ require 'rspec/core/rake_task'
12
+ RSpec::Core::RakeTask.new(:spec)
13
+ require 'rubocop/rake_task'
14
+ RuboCop::RakeTask.new do |task|
15
+ task.requires << 'rubocop-performance'
16
+ task.requires << 'rubocop-rspec'
17
+ end
data/buda.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'buda'
3
+ s.version = '0.1.4.0.1'
4
+ s.summary = 'Fintual.cl client'
5
+ s.description = 'A simple client for buda.com api'
6
+ s.authors = ['Alfredo Enrione']
7
+ s.email = 'aenrione@protonmail.com'
8
+ s.homepage =
9
+ 'https://rubygems.org/gems/buda'
10
+ s.license = 'MIT'
11
+ s.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
12
+ s.add_dependency 'http'
13
+ s.add_dependency 'json'
14
+ s.files = Dir.chdir(File.expand_path(__dir__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+ s.bindir = 'exe'
18
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ s.require_paths = ['lib']
20
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+ require 'buda/utils'
5
+ require 'buda/errors'
6
+ require 'buda/constants'
7
+ require 'buda/version'
8
+ require 'buda/resources/balance'
9
+ require 'buda/resources/market'
10
+ require 'json'
11
+ require 'net/http'
12
+ require 'net/https'
13
+
14
+ module Buda
15
+ # Client component to make the requests and get data
16
+ class Client
17
+ def initialize(api_key, api_secret)
18
+ @api_key = api_key
19
+ @api_secret = api_secret
20
+ @user_agent = "buda-ruby/#{Buda::VERSION}"
21
+ @headers = {
22
+ 'Content-Type': 'application/json', 'X-SBTC-APIKEY': @api_key,
23
+ 'User-Agent': @user_agent
24
+ }
25
+ @default_params = {}
26
+ end
27
+
28
+ def get
29
+ request('get')
30
+ end
31
+
32
+ def delete
33
+ request('delete')
34
+ end
35
+
36
+ def request(method)
37
+ proc do |resource, is_private, **kwargs|
38
+ parameters = params(method, **kwargs)
39
+ response = make_request(method, resource, parameters, is_private)
40
+ content = JSON.parse(response.body, symbolize_names: true)
41
+
42
+ # raise_custom_error(content[:error]) if response.status.client_error? || response.status.server_error?
43
+
44
+ content
45
+ end
46
+ end
47
+
48
+ def balances
49
+ result = get.call('balances', true)[:balances]
50
+ balances = []
51
+ unless result.nil?
52
+ result.each do |balance|
53
+ new_balance = Balance.new(**balance)
54
+ balances << new_balance
55
+ end
56
+ end
57
+ balances
58
+ end
59
+
60
+ def get_market(market_id)
61
+ market = get.call("markets/#{market_id.upcase}")[:market]
62
+ ticker = get.call("markets/#{market_id.upcase}/ticker")[:ticker]
63
+ new_market = Market.new(**market)
64
+ new_market.set_ticker(**ticker)
65
+ new_market
66
+ end
67
+
68
+ def to_s
69
+ visible_chars = 4
70
+ hidden_part = '*' * (@api_key.size - visible_chars)
71
+ visible_key = @api_key.slice(0, visible_chars)
72
+ "Client(🔑=#{hidden_part + visible_key}"
73
+ end
74
+
75
+ private
76
+
77
+ def client
78
+ @client ||= Net::HTTP
79
+ end
80
+
81
+ def make_request(method, resource, _parameters, is_private)
82
+ uri = URI.parse("#{Buda::Constants::SCHEME}#{Buda::Constants::BASE_URL}#{resource}")
83
+ https = Net::HTTP.new(uri.host, uri.port)
84
+ https.use_ssl = true
85
+ if is_private
86
+ req = Net::HTTP::Get.new(uri.path, @headers)
87
+ req['X-SBTC-SIGNATURE'] = sign(method.upcase, uri.path, '')
88
+ req['X-SBTC-NONCE'] = nonce
89
+ else
90
+ req = Net::HTTP::Get.new(uri.path)
91
+ end
92
+ https.request(req)
93
+ end
94
+
95
+ def params(method, **kwargs)
96
+ if method == 'get'
97
+ { params: { **@default_params, **kwargs } }
98
+ else
99
+ { json: { **@default_params, **kwargs } }
100
+ end
101
+ end
102
+
103
+ def raise_custom_error(error)
104
+ raise error_class(error[:code]).new(error[:message], error[:doc_url])
105
+ end
106
+
107
+ def nonce
108
+ (Time.now.to_i * (10**6)).to_s
109
+ end
110
+
111
+ def sign(method, path, _body)
112
+ prep = "#{method} #{path} #{nonce}"
113
+ OpenSSL::HMAC.hexdigest('SHA384', @api_secret, prep)
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Constant values for the client
4
+ module Buda
5
+ module Constants
6
+ FIELDSUBS = [%w[id id_], %w[type type_]].freeze
7
+ GENERAL_DOC_URL = 'https://api.buda.com/'
8
+ SCHEME = 'https://'
9
+ BASE_URL = 'www.buda.com/api/v2/'
10
+ end
11
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'buda/constants'
4
+
5
+ module Buda
6
+ module Errors
7
+ # Error definitions for handling wrong requests
8
+ class BudaError < StandardError
9
+ def initialize(message, doc_url = Buda::Constants::GENERAL_DOC_URL)
10
+ super()
11
+ @message = message
12
+ @doc_url = doc_url
13
+ end
14
+
15
+ def message
16
+ "\n#{@message}\n Please check the docs at: #{@doc_url}"
17
+ end
18
+
19
+ def to_s
20
+ message
21
+ end
22
+ end
23
+
24
+ class InvalidRequestError < BudaError; end
25
+ class LinkError < BudaError; end
26
+ class AuthenticationError < BudaError; end
27
+ class InstitutionError < BudaError; end
28
+ class ApiError < BudaError; end
29
+ class MissingResourceError < BudaError; end
30
+ class InvalidLinkTokenError < BudaError; end
31
+ class InvalidUsernameError < BudaError; end
32
+ class InvalidHolderTypeError < BudaError; end
33
+ class MissingParameterError < BudaError; end
34
+ class EmptyStringError < BudaError; end
35
+ class UnrecognizedRequestError < BudaError; end
36
+ class InvalidDateError < BudaError; end
37
+ class InvalidCredentialsError < BudaError; end
38
+ class LockedCredentialsError < BudaError; end
39
+ class InvalidApiKeyError < BudaError; end
40
+ class UnavailableInstitutionError < BudaError; end
41
+ class InternalServerError < BudaError; end
42
+ end
43
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buda
4
+ # for handling the total balance
5
+ class Balance
6
+ attr_reader :id, :available, :amount, :frozen, :pending
7
+
8
+ def initialize(id:, available_amount:, amount:, frozen_amount:, pending_withdraw_amount:, **)
9
+ @id = id
10
+ @available = available_amount[0]
11
+ @current = amount[0]
12
+ @frozen = frozen_amount[0]
13
+ @pending = pending_withdraw_amount[0]
14
+ end
15
+
16
+ def to_s
17
+ "#{id}: #{@current} (#{@available})"
18
+ end
19
+
20
+ def inspect
21
+ "<Balance #{@current} (#{@available})>"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buda
4
+ # Client component to make the requests and get data
5
+ class Client
6
+ attr_reader :id, :amount
7
+
8
+ def initialize(id, amount)
9
+ @id = id
10
+ @amount = amount
11
+ end
12
+
13
+ def to_s
14
+ "#{@amount} (#{@id})"
15
+ end
16
+
17
+ def inspect
18
+ "<Currency #{@amount} (#{@id})>"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'buda/resources/ticker'
4
+
5
+ module Buda
6
+ # for handling the total balance
7
+ class Market
8
+ attr_reader :id, :name, :base_currency, :quote_currency, :minimum_order_amount, :ticker
9
+
10
+ def initialize(id:, name:, base_currency:, quote_currency:, minimum_order_amount:, **)
11
+ @id = id
12
+ @name = name
13
+ @base_currency = base_currency
14
+ @quote_currency = quote_currency
15
+ @minimum_order_amount = minimum_order_amount
16
+ end
17
+
18
+ def set_ticker(**ticker)
19
+ @ticker = Buda::Ticker.new(**ticker)
20
+ end
21
+
22
+ def to_s
23
+ "#{@name} (#{@id})"
24
+ end
25
+
26
+ def inspect
27
+ "<Market #{@name} (#{@id})>"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buda
4
+ # for handling the total balance
5
+ class Ticker
6
+ attr_reader :market_id, :last_price, :min_ask, :volume,
7
+ :max_bid,:price_variation_7d, :price_variation_24h
8
+
9
+ def initialize(market_id:,
10
+ last_price:, min_ask:,
11
+ max_bid:, volume:,
12
+ price_variation_24h:, price_variation_7d:, **)
13
+ @market_id = market_id
14
+ @last_price = last_price[0]
15
+ @min_ask = min_ask
16
+ @max_bid = max_bid
17
+ @price_variation_24h = price_variation_24h
18
+ @volume = volume
19
+ @price_variation_7d = price_variation_7d
20
+ end
21
+
22
+ def to_s
23
+ "#{@market_id} (#{@last_price})"
24
+ end
25
+
26
+ def inspect
27
+ "<Ticker #{@market_id} (#{@last_price})>"
28
+ end
29
+ end
30
+ end
31
+
data/lib/buda/utils.rb ADDED
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buda
4
+ # A handful of run-of-the-mill utilities
5
+ module Utils
6
+ module_function
7
+
8
+ # Get a flat Array out of a list of lists of Iterables(Enumerators)
9
+ #
10
+ def flatten(sequences)
11
+ Enumerator::Chain.new(*sequences).to_a
12
+ end
13
+
14
+ # If the key exists, you will get that key-value pair.
15
+ #
16
+ # @param dict_ [Hash] the hash to pick from
17
+ # @param key [String] the key to look at
18
+ # @return [Hash] the key-value pair or an empty hash
19
+ def pick(dict_, key)
20
+ key = key.to_sym
21
+ if dict_.key?(key)
22
+ { "#{key}": dict_[key] }
23
+ else
24
+ {}
25
+ end
26
+ end
27
+
28
+ # Get a pluralized noun with its appropiate quantifier
29
+ #
30
+ # @param amount [Integer]
31
+ # @param noun [String] the noun to pluralize
32
+ # @param suffix [String]
33
+ # @return [String]
34
+ def pluralize(amount, noun, suffix = 's')
35
+ quantifier = amount or 'no'
36
+ "#{quantifier} #{amount == 1 ? noun : noun + suffix}"
37
+ end
38
+
39
+ # Transform a snake-cased name to its pascal-cased version.
40
+ #
41
+ # @param name [String]
42
+ # @return [String]
43
+ def snake_to_pascal(name)
44
+ name.split('_').map(&:capitalize).join('')
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buda
4
+ VERSION = '0.1.1'
5
+ end
data/lib/buda.rb CHANGED
@@ -1,5 +1,8 @@
1
- class Buda
2
- def self.hi
3
- puts "Hello world!"
4
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'buda/version'
4
+ require 'buda/client'
5
+
6
+ # Main Module
7
+ module Buda
5
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alfredo Enrione
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-25 00:00:00.000000000 Z
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -38,13 +38,27 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: A simple api client for buda.com
41
+ description: A simple client for buda.com api
42
42
  email: aenrione@protonmail.com
43
43
  executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
+ - ".gitignore"
48
+ - ".rubocop.yml"
49
+ - Gemfile
50
+ - Rakefile
51
+ - buda.gemspec
47
52
  - lib/buda.rb
53
+ - lib/buda/client.rb
54
+ - lib/buda/constants.rb
55
+ - lib/buda/errors.rb
56
+ - lib/buda/resources/balance.rb
57
+ - lib/buda/resources/currency.rb
58
+ - lib/buda/resources/market.rb
59
+ - lib/buda/resources/ticker.rb
60
+ - lib/buda/utils.rb
61
+ - lib/buda/version.rb
48
62
  homepage: https://rubygems.org/gems/buda
49
63
  licenses:
50
64
  - MIT
@@ -64,8 +78,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
78
  - !ruby/object:Gem::Version
65
79
  version: '0'
66
80
  requirements: []
67
- rubygems_version: 3.1.6
81
+ rubygems_version: 3.3.5
68
82
  signing_key:
69
83
  specification_version: 4
70
- summary: Hola!
84
+ summary: Fintual.cl client
71
85
  test_files: []