ca_virtex 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0fdde674e3c2b2e81ddcbd1000151f5d7dfaaa87
4
+ data.tar.gz: 8d75b8dd59e7468587584a3c387d1d4d0802b40b
5
+ SHA512:
6
+ metadata.gz: d1993fe11f1e90262d4a8371f106f7bf00d541e0342edd0cfc0eaac86031750243744a19b8a3bbdaf6c642bb594ec5e1c6f45a995578e75846e8b12602621956
7
+ data.tar.gz: c4fbde0f8928ca172f0210974367ecd90e713389f7a280b6e2083a294630d549c41bedd4abe5656a59563fa051c29d116089949bac4adc9890691919717329b2
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ *.sw[m-p]
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ /.config
20
+ /coverage/
21
+ /InstalledFiles
22
+ /pkg/
23
+ /spec/reports/
24
+ /test/tmp/
25
+ /test/version_tmp/
26
+ /tmp/
27
+
28
+ ## Specific to RubyMotion:
29
+ .dat*
30
+ .repl_history
31
+ build/
32
+
33
+ ## Documentation cache and generated files:
34
+ /.yardoc/
35
+ /_yardoc/
36
+ /doc/
37
+ /rdoc/
38
+
39
+ ## Environment normalisation:
40
+ /.bundle/
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ca_virtex.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Zaratan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # CaVirtex
2
+
3
+ Gem to access ca_virtex API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ca_virtex'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ca_virtex
18
+
19
+ ## Usage
20
+
21
+ ### Setup
22
+
23
+ ```ruby
24
+ api_key = "XXXXXXXXXXXXXXXXXXXXXX"
25
+ api_secret = "XXXXXXXXXXXXXXXXXXXXXX"
26
+ @client = CaVirtex::API::Client.new(api_key, api_secret)
27
+ ```
28
+
29
+ ## Examples
30
+
31
+ ### Balance
32
+
33
+ ```ruby
34
+ @client.private.balance.balance.CAD
35
+ => 50.0
36
+ ```
37
+
38
+ ### Orderbook
39
+
40
+ ```ruby
41
+ orderbook = @client.public.orderbook(order_currency: :BTC, payement_currency: :CAD).orderbook
42
+
43
+ output = "Price | Quantity \n"
44
+ output += "-----------------------\n"
45
+ orderbook.asks.each do |ask|
46
+ output += ask.price
47
+ output += " | "
48
+ output += ask.quantity
49
+ output += "\n"
50
+ end
51
+
52
+ puts output
53
+
54
+ Price | Quantity
55
+ -----------------------
56
+ 0.00131000 | 2618.0
57
+ 0.00132000 | 20999.9
58
+ 0.00132000 | 20000.0
59
+ 0.00132000 | 1324.2
60
+ 0.00133000 | 60000.0
61
+ 0.00133000 | 100.0
62
+ 0.00133000 | 56000.0
63
+ 0.00134000 | 380000.0
64
+ 0.00135000 | 988320.0
65
+ 0.00137000 | 697000.0
66
+ ```
67
+
68
+ ### Place a Trade
69
+
70
+ ```ruby
71
+ client.private.order(mode: "buy",price: "600.0", amount: "0.001", order_currency: :BTC, payment_currency: :CAD)
72
+
73
+ order.order["id"]
74
+ => 594853
75
+ ```
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it ( http://github.com/<my-github-username>/ca_virtex/fork )
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/ca_virtex.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ca_virtex/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ca_virtex"
8
+ spec.version = CaVirtex::VERSION
9
+ spec.authors = ["Zaratan"]
10
+ spec.email = ["denis.pasin@gmail.com"]
11
+ spec.summary = %q{A gem to map CA Virtex API}
12
+ spec.description = %q{A gem with a full mapping of CA Virtex API}
13
+ spec.homepage = "https://github.com/Skizzk/ca_virtex"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'httparty'
22
+ spec.add_dependency 'activesupport'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ end
data/lib/ca_virtex.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'httparty'
2
+ require 'openssl'
3
+ require 'base64'
4
+ require 'uri'
5
+ require 'date'
6
+ require 'active_support/core_ext/hash'
7
+ require 'ostruct'
8
+
9
+ require "ca_virtex/version"
10
+ require "ca_virtex/core_ext/hash"
11
+ require "ca_virtex/api/error"
12
+ require "ca_virtex/api/base"
13
+ require "ca_virtex/api/public"
14
+ require "ca_virtex/api/private"
15
+ require "ca_virtex/api/client"
@@ -0,0 +1,57 @@
1
+ #require 'hashlib'
2
+ #require 'hmac'
3
+ module CaVirtex
4
+ module API
5
+ class Base
6
+ include HTTParty
7
+ base_uri "https://cavirtex.com/api2"
8
+
9
+ CURRENCIES_KEYS = [:BTCCAD,:LTCCAD,:BTCLTC]
10
+
11
+ def initialize(api_key, api_secret)
12
+ @api_key = api_key
13
+ @api_secret = api_secret
14
+ end
15
+
16
+ private
17
+
18
+ def headers(endpoint, params = {})
19
+ current_nonce = nonce
20
+ {
21
+ token: @api_key,
22
+ nonce: current_nonce,
23
+ signature: generate_api_sign(endpoint, current_nonce, params)
24
+ }
25
+ end
26
+
27
+ def nonce
28
+ sprintf("%0.6f", Time.now.to_f).gsub('.', '')
29
+ end
30
+
31
+ def generate_api_sign(endpoint, nonce, params)
32
+ data = params.to_alphabeticly_ordered || ""
33
+ digest = OpenSSL::Digest.new('sha256')
34
+ OpenSSL::HMAC.hexdigest(digest, @api_secret, nonce + @api_key + '/api2' + endpoint + data)
35
+ end
36
+
37
+ def construct_currency_pair(currency_1, currency_2)
38
+ pair = unless (currency_1 == :BTC || currency_2 == :BTC)
39
+ "LTCCAD"
40
+ else
41
+ if (currency_1 == :CAD || currency_2 == :CAD)
42
+ "BTCCAD"
43
+ else
44
+ "BTCLTC"
45
+ end
46
+ end
47
+ pair
48
+ end
49
+
50
+ def parse_message(message)
51
+ result = OpenStruct.new(JSON.parse(message))
52
+ throw Error.new(result.message) if result.status != "ok"
53
+ result
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,12 @@
1
+ module CaVirtex
2
+ module API
3
+ class Client
4
+ attr_reader :public, :private
5
+
6
+ def initialize(api_key, api_secret)
7
+ @public = CaVirtex::API::Public.new
8
+ @private = CaVirtex::API::Private.new(api_key, api_secret)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ module CaVirtex
2
+ module API
3
+ class Error < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,45 @@
1
+ module CaVirtex
2
+ module API
3
+ class Private < Base
4
+ def balance
5
+ parse_balance(parse_message(balance_call))
6
+ end
7
+
8
+ def order(currency_from: :CAD, currency_to: :BTC, mode: "buy", amount: "0" ,price: "0")
9
+ parse_message(
10
+ order_call(
11
+ construct_currency_pair(currency_from, currency_to),
12
+ mode,
13
+ amount,
14
+ price
15
+ )
16
+ )
17
+ end
18
+
19
+ private
20
+
21
+ def parse_balance(response)
22
+ response.balance = OpenStruct.new(response.balance)
23
+ response
24
+ end
25
+
26
+ def balance_call
27
+ endpoint = '/user/balance.json'
28
+ query = headers(endpoint)
29
+ self.class.post(endpoint, body: query)
30
+ end
31
+
32
+ def order_call(currency_pair, mode, amount, price)
33
+ endpoint = '/user/order.json'
34
+ params={
35
+ currencypair: currency_pair,
36
+ mode: mode,
37
+ amount: amount,
38
+ price: price
39
+ }
40
+ query = headers(endpoint, params)
41
+ self.class.post(endpoint, body: query.merge(params))
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,35 @@
1
+ module CaVirtex
2
+ module API
3
+ class Public < Base
4
+ def initialize
5
+ end
6
+
7
+ def orderbook(params = {})
8
+ params.slice!(:order_currency, :payment_currency, :count)
9
+ response = parse_message(orderbook_call(params).body)
10
+ parse_orders(response, params[:count])
11
+ end
12
+
13
+ private
14
+
15
+ def orderbook_call(order_currency: :BTC, payment_currency: :CAD, count: 10)
16
+ query = {currencypair: construct_currency_pair(order_currency, payment_currency)}
17
+ self.class.get("/orderbook.json", query: query)
18
+ end
19
+
20
+ def parse_orders(orders, count)
21
+ count ||= 1000
22
+ orders.orderbook= OpenStruct.new(orders.orderbook)
23
+ orders.orderbook.bids = parse_order(orders.orderbook.bids).first(count)
24
+ orders.orderbook.asks = parse_order(orders.orderbook.asks).sort_by{|e| e.price}.first(count)
25
+ orders
26
+ end
27
+
28
+ def parse_order(order_list)
29
+ order_list.map do |e|
30
+ Struct.new(:price, :amount).new(e[0], e[1])
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ class Hash
2
+ def to_alphabeticly_ordered
3
+ result = ""
4
+ keys.sort.each do |k|
5
+ result +=self[k].to_s
6
+ end
7
+ result
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module CaVirtex
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ca_virtex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zaratan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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'
69
+ description: A gem with a full mapping of CA Virtex API
70
+ email:
71
+ - denis.pasin@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - ca_virtex.gemspec
82
+ - lib/ca_virtex.rb
83
+ - lib/ca_virtex/api/base.rb
84
+ - lib/ca_virtex/api/client.rb
85
+ - lib/ca_virtex/api/error.rb
86
+ - lib/ca_virtex/api/private.rb
87
+ - lib/ca_virtex/api/public.rb
88
+ - lib/ca_virtex/core_ext/hash.rb
89
+ - lib/ca_virtex/version.rb
90
+ homepage: https://github.com/Skizzk/ca_virtex
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A gem to map CA Virtex API
114
+ test_files: []