bitsharesws 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb7a8e55ee7eb9d3ef3a2cfe7e291b650619bb9e
4
+ data.tar.gz: ec418e46a08ddd5edf5a60665dd45326fdf6dad5
5
+ SHA512:
6
+ metadata.gz: bdc4d0c543c8922c251aff87ae999cb1f43a7a0d7195d8432a2e5830ba003b6072f1a053e90c1d0f206f1d1c30b0379157c6850eb082565e5dd9c94fe2166f7f
7
+ data.tar.gz: f8d3a917fe3be4464b69d0dbbeb925fa6045437fbee265f218fa41bc881b8ea51621e3e2b39a4b2143762ffa3bcb2973ae3357ba01f95950a5eec20977a05899
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.6
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bitsharesws.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Nikolay Nozdrin-Plotnitsky
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Bitsharesws
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bitsharesws`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bitsharesws'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bitsharesws
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bitsharesws.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bitsharesws"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bitshares/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bitsharesws"
8
+ spec.version = Bitshares::VERSION
9
+ spec.authors = ["scientistnik"]
10
+ spec.email = ["nozdrin.plotnitsky@gmail.com"]
11
+
12
+ spec.summary = %q{WebSocket client for BitShares API}
13
+ spec.description = %q{WebSocket client for BitShares API}
14
+ spec.homepage = "https://github.com/scientistnik/bitsharesws.git"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_runtime_dependency 'json'
25
+ spec.add_runtime_dependency 'websocket-eventmachine-client'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.15"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
@@ -0,0 +1,68 @@
1
+ require 'bitshares/version'
2
+
3
+ module BitShares
4
+ autoload :WSocket, 'bitshares/wsocket'
5
+ autoload :Wallet, 'bitshares/wallet'
6
+ autoload :Asset, 'bitshares/asset'
7
+ autoload :Account, 'bitshares/account'
8
+
9
+ class << self
10
+
11
+ attr_accessor :login, :pass
12
+
13
+ def config autoconnect=true, &block
14
+ @node = 'wss://node.testnet.bitshares.eu'
15
+ @login = ''
16
+ @pass = ''
17
+ instance_eval(&block) if block_given?
18
+ start if autoconnect
19
+ end
20
+
21
+ def node n=nil
22
+ n ||= @node
23
+ @node = n
24
+ end
25
+
26
+ def start
27
+ WSocket.start
28
+ WSocket.send id: 2, method: 'call', params: [1,'login',[@login,@pass]]
29
+ end
30
+
31
+ def stop() WSocket.stop end
32
+
33
+ def database_id
34
+ @database_id ||= WSocket.send(id: 2, method: 'call', params: [1,'database',[]])['result']
35
+ end
36
+
37
+ def account name
38
+ answer = WSocket.send id: 2, method: "call", params: [database_id,"get_account_by_name",[name]]
39
+ raise 'Bad request...' if answer.key? 'error'
40
+ Account.new answer['result']
41
+ end
42
+
43
+ def balance name,*ids
44
+ answer = WSocket.send id: 2, method: "call", params: [database_id,"get_named_account_balances",[name,ids]]
45
+ raise 'Bad request...' if answer.key? 'error'
46
+ answer['result'].inject([]) { |m,w| m << Wallet.new(w) }
47
+ end
48
+
49
+ def assets *ids
50
+ answer = WSocket.send id: 2, method: 'call', params: [database_id,'get_assets',[ids]]
51
+ raise 'Bad request...' if answer.key? 'error'
52
+ answer['result'].inject([]) {|m,a| m << Asset.new(a) }
53
+ end
54
+
55
+ def list_assets name, limit=10
56
+ answer = WSocket.send id: 2, method: 'call', params: [database_id,'list_assets',[name,limit]]
57
+ raise "Bad request...#{answer}" if answer.key? 'error'
58
+ answer['result'].inject([]) {|m,a| m << Asset.new(a) }
59
+ end
60
+
61
+ def subscribe_callback id, clear_filter=true
62
+ answer = WSocket.send id: 2, method: 'call', params: [database_id,'set_subscribe_callback',[id,clear_filter]]
63
+ raise "Bad request...#{answer}" if answer.key? 'error'
64
+ answer['result']
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,13 @@
1
+ module BitShares
2
+ class Account
3
+ attr_reader :id, :name
4
+
5
+ def initialize h
6
+ @id = h['id'][/\d*$/]
7
+ @name = h['name']
8
+ @hash = h
9
+ end
10
+
11
+ def to_s() @name end
12
+ end
13
+ end
@@ -0,0 +1,35 @@
1
+ module BitShares
2
+ class Asset
3
+ attr_reader :id, :name
4
+
5
+ def initialize hash
6
+ @id = hash['id']
7
+ @name = hash['symbol']
8
+ @hash = hash
9
+ self.class.add self
10
+ end
11
+
12
+ def to_s
13
+ @name
14
+ end
15
+
16
+ class << self
17
+ def [] id
18
+ if /^\d\.\d*\.\d*/.match(id)
19
+ BitShares.assets id unless hash.key? id
20
+ hash[id]
21
+ else
22
+ hash.each_pair do |k,v|
23
+ return v if v.name == id
24
+ end
25
+ end
26
+ end
27
+
28
+ def add h
29
+ hash[h.id] = h unless hash.key? h.id
30
+ end
31
+
32
+ def hash() @h ||= {} end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Bitshares
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ module BitShares
2
+ class Wallet
3
+ attr_accessor :amount, :asset
4
+ def initialize hash
5
+ @amount = hash['amount']
6
+ @asset = BitShares::Asset[hash['asset_id']]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,112 @@
1
+ require 'websocket-eventmachine-client'
2
+ require 'json'
3
+
4
+ module BitShares
5
+ module WSocket
6
+ class << self
7
+
8
+ def start
9
+ @msgs = {}
10
+ @thread ||= Thread.new { run }
11
+ Thread.new do
12
+ while @status != :disconnected do
13
+ @thread[:ws].ping 'Hi' if @status == :connected
14
+ sleep 50
15
+ end
16
+ end
17
+ count = 0
18
+ while @status != :connected and @status != :disconnected do
19
+ sleep 0.1
20
+ count += 1
21
+ raise "TimeoutError..." if count > 100
22
+ end
23
+ end
24
+
25
+ def thread() @thread end
26
+
27
+ def stop
28
+ EventMachine.stop
29
+ end
30
+
31
+ def send hash
32
+ id = hash[:id]
33
+ count = 0
34
+ while @msgs[id] != nil do
35
+ sleep 0.1
36
+ count += 1
37
+ raise "TimeoutError..." if count > 100
38
+ end
39
+ @thread[:ws].send hash.to_json if @status == :connected
40
+ count = 0
41
+ while @msgs[id].nil? and @status == :connected do
42
+ sleep 0.1
43
+ count += 1
44
+ raise "TimeoutError..." if count > 100
45
+ end
46
+
47
+ result = @msgs[id]
48
+ lock.synchronize do
49
+ @msgs[id] = nil
50
+ end
51
+ result
52
+ end
53
+
54
+ def lock() @lock ||= Mutex.new end
55
+
56
+ def get_notice
57
+ if @msgs.key?('notice') && !@msgs['notice'].nil?
58
+ msg = @msgs['notice']
59
+ lock.synchronize do
60
+ @msgs['notice'] = nil
61
+ end
62
+ msg
63
+ else
64
+ nil
65
+ end
66
+ end
67
+
68
+ private
69
+ def run
70
+ begin
71
+ @status ||= :start
72
+ EM.run do
73
+ ws = WebSocket::EventMachine::Client.connect(:uri => BitShares.node)
74
+ Thread.current[:ws] = ws
75
+
76
+ ws.onopen do
77
+ if @status == :start
78
+ puts "Connected"
79
+ @status = :connected
80
+ else
81
+ EventMachine.stop
82
+ end
83
+ end
84
+
85
+ ws.onmessage do |msg, type|
86
+ json = JSON.parse msg
87
+ id = json['id'] || json['method']
88
+ lock.synchronize do
89
+ @msgs[id] = json
90
+ end
91
+ end
92
+
93
+ ws.onclose do |code, reason|
94
+ puts "Disconnected with status code: #{code}"
95
+ @status = :disconnected
96
+ EventMachine.stop
97
+ end
98
+
99
+ ws.onpong do |msg|
100
+ #puts "Pong #{msg}"
101
+ end
102
+
103
+ end
104
+ rescue Exception => e
105
+ puts e.message
106
+ end
107
+
108
+ end
109
+
110
+ end
111
+ end
112
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitsharesws
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - scientistnik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
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: websocket-eventmachine-client
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.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: WebSocket client for BitShares API
84
+ email:
85
+ - nozdrin.plotnitsky@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - bitsharesws.gemspec
100
+ - lib/bitshares.rb
101
+ - lib/bitshares/account.rb
102
+ - lib/bitshares/asset.rb
103
+ - lib/bitshares/version.rb
104
+ - lib/bitshares/wallet.rb
105
+ - lib/bitshares/wsocket.rb
106
+ homepage: https://github.com/scientistnik/bitsharesws.git
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.5.2
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: WebSocket client for BitShares API
130
+ test_files: []