coin_falcon 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 570c78fd30472cbfec2fd585eb64deacd8e305e66082d2cf1ac0692415521830
4
- data.tar.gz: 1e7cf3facbd7b72c0c5a6212755aab5fd5534b25369c0ef097c62e3e5e64e66d
3
+ metadata.gz: c8e8327a393ac7ccbb94118375d6e8705901e42566c00b1c9bef4d27f7d98c5a
4
+ data.tar.gz: 15c7875175f2655614002a8818d32a22a0fa75968c778b6ab1908c8d4081492d
5
5
  SHA512:
6
- metadata.gz: a52f9f79a3620bb450010fa991e9ec85005228c53bcbd3043a3f9e4143290eaa14c2dbaf1e02a58d830b7c64f7c7f6dc3da455982ce6ec83dbb0a0f54cad3ce2
7
- data.tar.gz: e603e184d54bab783f57b854b94b879c88d66f3d399b94dc61b3b39d24564da7d025f3df92b49886a2bc312f33fcb3fc9dd574dae98c37c642bb911e2f9005d0
6
+ metadata.gz: 981d0ab1d37b4fef79a1021def4d1954a35688b6f16301f1dfda9acfe3d7a7c763a6e632b686b886479c31c073c2cd8fc52d06bc72c11ea83ddff62779beb80f
7
+ data.tar.gz: a1cfb3d7483ccefe0547fbef2e3dafa6d15ebe5d820a66b68b287bd9f162011c51a0bbf1a8b318f2cd8b5ed934afa657087672d28473d821f81df70b5ff5f73f
data/Gemfile.lock CHANGED
@@ -1,12 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- coin_falcon (0.0.0)
4
+ coin_falcon (0.0.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ byebug (10.0.0)
10
+ coderay (1.1.2)
9
11
  diff-lcs (1.3)
12
+ method_source (0.9.0)
13
+ pry (0.11.3)
14
+ coderay (~> 1.1.0)
15
+ method_source (~> 0.9.0)
16
+ pry-byebug (3.6.0)
17
+ byebug (~> 10.0)
18
+ pry (~> 0.10)
10
19
  rake (10.5.0)
11
20
  rspec (3.7.0)
12
21
  rspec-core (~> 3.7.0)
@@ -28,6 +37,7 @@ PLATFORMS
28
37
  DEPENDENCIES
29
38
  bundler (~> 1.16)
30
39
  coin_falcon!
40
+ pry-byebug
31
41
  rake (~> 10.0)
32
42
  rspec (~> 3.0)
33
43
 
data/README.md CHANGED
@@ -1,3 +1,139 @@
1
1
  # CoinFalcon Ruby Library
2
2
 
3
3
  The CoinFalcon Ruby Library provides convenient access to the CoinFalcon API from applications written in the Ruby language.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'coin_falcon'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install coin_falcon
20
+
21
+ ## Usage
22
+
23
+ The library needs to be configured with your account's `key` and `secret` which is available in your CoinFalcon Dashboard:
24
+
25
+ ```ruby
26
+ require 'coin_falcon'
27
+
28
+ client = CoinFalcon::Client.new(key, secret)
29
+ ```
30
+
31
+ It is also possible to set up an API `endpoint` and `version`:
32
+
33
+ ```ruby
34
+ require 'coin_falcon'
35
+
36
+ client = CoinFalcon::Client.new(key, secret, endpoint, version)
37
+ ```
38
+
39
+ Defaults:
40
+
41
+ ```ruby
42
+ ENDPOINT = 'https://staging.coinfalcon.com'
43
+ VERSION = 1
44
+ ```
45
+
46
+ ### Accounts
47
+
48
+ Get a list of your trading accounts.
49
+
50
+ ```ruby
51
+ client.accounts
52
+ ```
53
+
54
+ ### Create order
55
+
56
+ ```ruby
57
+ client.create_order(market: 'ETH-BTC', operation_type: :limit_order, order_type: :buy, size: 1, price: 0.01)
58
+ ```
59
+
60
+ ### Cancel order
61
+
62
+ ```ruby
63
+ client.cancel_order(order_id)
64
+ ```
65
+
66
+ ### List orders
67
+
68
+ ```ruby
69
+ client.my_orders
70
+ ```
71
+
72
+ ### List trades
73
+
74
+ ```ruby
75
+ client.my_trades
76
+ ```
77
+
78
+ ### Deposit address
79
+
80
+ ```ruby
81
+ client.deposit_address('btc')
82
+ ```
83
+
84
+ ### Deposit history
85
+
86
+ ```ruby
87
+ client.deposit_history
88
+ ```
89
+
90
+ ### Deposit details
91
+
92
+ ```ruby
93
+ client.deposit_details(deposit_id)
94
+ ```
95
+
96
+ ### Create withdrawal
97
+
98
+ ```ruby
99
+ client.create_withdrawal(currency: :btc, address: 'your_address_here', amount: 0.1)
100
+ ```
101
+
102
+ ### Withdrawal details
103
+
104
+ ```ruby
105
+ client.withdrawal_details(withdrawal_id)
106
+ ```
107
+
108
+ ### Withdrawal history
109
+
110
+ ```ruby
111
+ client.withdrawal_history(params)
112
+ ```
113
+
114
+ ### Cancel withdrawal
115
+
116
+ ```ruby
117
+ client.cancel_withdrawal(withdrawal_id)
118
+ ```
119
+
120
+ ### List market's trades
121
+
122
+ ```ruby
123
+ client.trades('ETH-BTC')
124
+ ```
125
+
126
+ ### List orderbook
127
+
128
+ ```ruby
129
+ client.orderbook('ETH-BTC')
130
+ client.orderbook('ETH-BTC', level: 3)
131
+ ```
132
+
133
+ ## Contributing
134
+
135
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thetonyrom/coin_falcon
136
+
137
+ ## License
138
+
139
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/coin_falcon.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.16"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency 'pry-byebug'
26
27
  end
@@ -0,0 +1,45 @@
1
+ module CoinFalcon
2
+ class Cipher
3
+ ALGORITHM = 'SHA256'.freeze
4
+
5
+ KEY_HEADER = 'CF-API-KEY'.freeze
6
+ TIME_HEADER = 'CF-API-TIMESTAMP'.freeze
7
+ SIGN_HEADER = 'CF-API-SIGNATURE'.freeze
8
+
9
+ def initialize(key, secret)
10
+ @key = key
11
+ @secret = secret
12
+ end
13
+
14
+ def sign!(request)
15
+ headers(request.to_a).each do |header, value|
16
+ request[header] = value
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :key, :secret
23
+
24
+ def headers(attrs)
25
+ timestamp = Time.now.to_i
26
+
27
+ {
28
+ KEY_HEADER => key,
29
+ TIME_HEADER => timestamp,
30
+ SIGN_HEADER => encode(timestamp, attrs),
31
+ 'Content-Type' => 'application/json'
32
+ }
33
+ end
34
+
35
+ def encode(timestamp, attrs)
36
+ payload = build_payload(timestamp, attrs)
37
+
38
+ OpenSSL::HMAC.hexdigest(ALGORITHM, secret, payload)
39
+ end
40
+
41
+ def build_payload(timestamp, attrs)
42
+ [timestamp, *attrs].compact.join('|')
43
+ end
44
+ end
45
+ end
@@ -2,71 +2,99 @@ module CoinFalcon
2
2
  # Client executes requests against the CoinFalcon API.
3
3
  #
4
4
  class Client
5
- KEY_HEADER = 'CF-API-KEY'.freeze
6
- TIME_HEADER = 'CF-API-TIMESTAMP'.freeze
7
- SIGN_HEADER = 'CF-API-SIGNATURE'.freeze
5
+ ENDPOINT = 'https://staging.coinfalcon.com'.freeze
6
+ VERSION = 1
8
7
 
9
- attr_reader :api_key, :api_secret
8
+ def initialize(key, secret, endpoint = ENDPOINT, version = VERSION)
9
+ @conn = Connection.new(key, secret, endpoint, version)
10
+ end
11
+
12
+ def accounts
13
+ path = 'user/accounts'
10
14
 
11
- def initialize(api_key, api_secret)
12
- @api_key = api_key
13
- @api_secret = api_secret
15
+ conn.get(path)
14
16
  end
15
17
 
16
- def orderbook(market)
17
- url = build_url("markets/#{market}/orders")
18
+ def create_order(order)
19
+ path = 'user/orders'
18
20
 
19
- response = get(url)
21
+ conn.post(path, order)
22
+ end
23
+
24
+ def cancel_order(id)
25
+ path = "user/orders/#{id}"
20
26
 
21
- JSON.parse(response.body)
27
+ conn.delete(path)
22
28
  end
23
29
 
24
- def accounts
25
- url = build_url('user/accounts')
30
+ def my_orders(params = nil)
31
+ path = 'user/orders'
26
32
 
27
- response = get(url)
33
+ conn.get(path, params)
34
+ end
35
+
36
+ def my_trades(params = nil)
37
+ path = 'user/trades'
28
38
 
29
- JSON.parse(response.body)
39
+ conn.get(path, params)
30
40
  end
31
41
 
32
- private
42
+ def deposit_address(currency)
43
+ path = 'account/deposit_address'
33
44
 
34
- def build_url(path)
35
- URI(CoinFalcon.api_base + path)
45
+ conn.get(path, { currency: currency })
36
46
  end
37
47
 
38
- def headers(method, path, body = nil)
39
- timestamp = Time.now.to_i
48
+ def deposit_history(params = nil)
49
+ path = 'account/deposits'
40
50
 
41
- {
42
- KEY_HEADER => api_key,
43
- TIME_HEADER => timestamp,
44
- SIGN_HEADER => sign(timestamp, method, path, body),
45
- 'Content-Type' => 'application/json'
46
- }
51
+ conn.get(path, params)
47
52
  end
48
53
 
49
- def sign(timestamp, method, path, body)
50
- payload = build_payload(timestamp, method, path, body)
54
+ def deposit_details(id)
55
+ path = 'account/deposit'
51
56
 
52
- OpenSSL::HMAC.hexdigest('sha256', api_secret, payload)
57
+ conn.get(path, { id: id })
53
58
  end
54
59
 
55
- def build_payload(timestamp, method, path, body)
56
- [timestamp, method, path, body].compact.join('|')
60
+ def create_withdrawal(params)
61
+ path = 'account/withdraw'
62
+
63
+ conn.post(path, params)
57
64
  end
58
65
 
59
- def get(url)
60
- request = Net::HTTP::Get.new(url)
66
+ def withdrawal_details(id)
67
+ path = 'account/withdrawal'
61
68
 
62
- headers('GET', url.path).each do |header, value|
63
- request[header] = value
64
- end
69
+ conn.get(path, { id: id })
70
+ end
65
71
 
66
- http = Net::HTTP.new(url.hostname, url.port)
67
- http.use_ssl = true
72
+ def withdrawal_history(params = nil)
73
+ path = 'account/withdrawals'
68
74
 
69
- http.request(request)
75
+ conn.get(path, params)
70
76
  end
77
+
78
+ def cancel_withdrawal(id)
79
+ path = "account/withdrawals/#{id}"
80
+
81
+ conn.delete(path)
82
+ end
83
+
84
+ def trades(market, params = nil)
85
+ path = "markets/#{market}/trades"
86
+
87
+ conn.get(path, params)
88
+ end
89
+
90
+ def orderbook(market, params = nil)
91
+ path = "markets/#{market}/orders"
92
+
93
+ conn.get(path, params)
94
+ end
95
+
96
+ private
97
+
98
+ attr_reader :conn
71
99
  end
72
100
  end
@@ -0,0 +1,44 @@
1
+ module CoinFalcon
2
+ class Connection
3
+ def initialize(key, secret, endpoint, version)
4
+ @cipher = Cipher.new(key, secret)
5
+ @http = initialize_http(endpoint)
6
+ @version = version
7
+ end
8
+
9
+ def get(path, params = nil)
10
+ request :get, path, params
11
+ end
12
+
13
+ def post(path, params)
14
+ request :post, path, params
15
+ end
16
+
17
+ def delete(path, params = {})
18
+ request :delete, path, params
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :cipher, :http, :version
24
+
25
+ def initialize_http(endpoint)
26
+ uri = URI.parse(endpoint)
27
+ http = Net::HTTP.new(uri.host, uri.port)
28
+ http.use_ssl = true
29
+
30
+ http
31
+ end
32
+
33
+ def request(method, path, params)
34
+ request = Request.new(method, api(path), params)
35
+ cipher.sign!(request)
36
+
37
+ Response.new(http.request(request))
38
+ end
39
+
40
+ def api(path)
41
+ "/api/v#{version}/#{path}"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,47 @@
1
+ module CoinFalcon
2
+ class Request
3
+ VERBS = {
4
+ :get => Net::HTTP::Get,
5
+ :post => Net::HTTP::Post,
6
+ :delete => Net::HTTP::Delete
7
+ }
8
+
9
+ def initialize(method, path, params)
10
+ case method
11
+ when :get
12
+ full_path = encode_path_params(path, params)
13
+ @request = VERBS[method].new(full_path)
14
+ else
15
+ @request = VERBS[method].new(path)
16
+ @request.body = params.to_json
17
+ end
18
+ end
19
+
20
+ def to_a
21
+ [verb, path, body]
22
+ end
23
+
24
+ def method_missing(method, *args)
25
+ if request.respond_to?(method)
26
+ request.send(method, *args)
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :request
35
+
36
+ def verb
37
+ request.method
38
+ end
39
+
40
+ def encode_path_params(path, params)
41
+ return path unless params
42
+
43
+ encoded = URI.encode_www_form(params)
44
+ [path, encoded].join('?')
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,12 @@
1
+ module CoinFalcon
2
+ class Response
3
+ attr_reader :code, :body
4
+
5
+ def initialize(response)
6
+ @code = response.code.to_i
7
+ @body = JSON.parse(response.body)
8
+ rescue JSON::ParserError
9
+ @body = response.body
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module CoinFalcon
2
- VERSION = '0.0.0'.freeze
2
+ VERSION = '0.0.1'.freeze
3
3
  end
data/lib/coin_falcon.rb CHANGED
@@ -1,15 +1,14 @@
1
1
  require 'net/http'
2
- require 'json'
3
2
  require 'openssl'
3
+ require 'json'
4
4
 
5
+ require 'coin_falcon/cipher'
5
6
  require 'coin_falcon/client'
7
+ require 'coin_falcon/connection'
8
+ require 'coin_falcon/request'
9
+ require 'coin_falcon/response'
6
10
 
7
11
  require 'coin_falcon/version'
8
12
 
9
13
  module CoinFalcon
10
- @api_base = 'https://staging.coinfalcon.com/api/v1/'
11
-
12
- class << self
13
- attr_accessor :api_base
14
- end
15
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coin_falcon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Rom
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-08 00:00:00.000000000 Z
11
+ date: 2018-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description:
56
70
  email:
57
71
  - thetonyrom@gmail.com
@@ -71,7 +85,11 @@ files:
71
85
  - bin/setup
72
86
  - coin_falcon.gemspec
73
87
  - lib/coin_falcon.rb
88
+ - lib/coin_falcon/cipher.rb
74
89
  - lib/coin_falcon/client.rb
90
+ - lib/coin_falcon/connection.rb
91
+ - lib/coin_falcon/request.rb
92
+ - lib/coin_falcon/response.rb
75
93
  - lib/coin_falcon/version.rb
76
94
  homepage: https://github.com/thetonyrom/coin_falcon
77
95
  licenses: