Class: YolodiceClient
- Inherits:
-
Object
- Object
- YolodiceClient
- Defined in:
- lib/yolodice_client.rb
Overview
YolodiceClient is a simple JSON-RPC 2.0 client that connects to YOLOdice.com.
Defined Under Namespace
Classes: ConnectionClosedError, Error, RemoteError
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
OpenSSL::SSL::SSLSocket
object created by theconnect
method. -
#notification_proc ⇒ Object
Proc for handling notifications from the server.
Class Method Summary collapse
-
.btc_to_satoshi(v) ⇒ Object
Converts amount from bitcoins (float) to satoshi (integer).
-
.satoshi_to_btc(v) ⇒ Object
Converts amount from satoshi (integer) to amount in bitcoins (float).
-
.target_from_multiplier(m) ⇒ Object
Returns bet terget given the required multiplier.
-
.target_from_probability(p) ⇒ Object
Returns bet target given the required win probability.
Instance Method Summary collapse
-
#authenticate(auth_key) ⇒ Object
Authenticates the connection by requesting a challenge message, signing it and sending the response back.
-
#call(method, *arguments, &blk) ⇒ Object
Calls an arbitrary method on the server.
- #check_connection ⇒ Object
-
#close ⇒ Object
Closes connection to the host.
-
#connect ⇒ Object
Connects to the host.
-
#initialize(opts = {}) ⇒ YolodiceClient
constructor
Initializes the client object.
-
#logger=(logger) ⇒ Object
Sets a logger for the object.
-
#method_missing(method, *args, &blk) ⇒ Object
Overloading the
method_missing
gives a convenience way to call server-side methods.
Constructor Details
#initialize(opts = {}) ⇒ YolodiceClient
Initializes the client object. The method accepts an option hash with the following keys:
-
:host
– defaults toapi.yolodice.com
-
:port
– defaults to4444
-
:ssl
– if SSL should be used, defaults totrue
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/yolodice_client.rb', line 29 def initialize opts={} @opts = { host: 'api.yolodice.com', port: 4444, ssl: true }.merge opts @req_id_seq = 0 @current_requests = {} @receive_queues = {} @thread_semaphore = Mutex.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
Overloading the method_missing
gives a convenience way to call server-side methods. This method calls the call
with the same set of arguments.
240 241 242 |
# File 'lib/yolodice_client.rb', line 240 def method_missing method, *args, &blk call method, *args, &blk end |
Instance Attribute Details
#connection ⇒ Object (readonly)
OpenSSL::SSL::SSLSocket
object created by the connect
method.
14 15 16 |
# File 'lib/yolodice_client.rb', line 14 def connection @connection end |
#notification_proc ⇒ Object
Proc for handling notifications from the server. This proc (if set) will be called each time the server sends a notification, i.e. a message that is not a response to any client call. The proc is given a single argument: the message hash. The proc is called in a separate thread.
21 22 23 |
# File 'lib/yolodice_client.rb', line 21 def notification_proc @notification_proc end |
Class Method Details
.btc_to_satoshi(v) ⇒ Object
Converts amount from bitcoins (float) to satoshi (integer).
310 311 312 |
# File 'lib/yolodice_client.rb', line 310 def btc_to_satoshi v (v * 100_000_000).round end |
.satoshi_to_btc(v) ⇒ Object
Converts amount from satoshi (integer) to amount in bitcoins (float).
305 306 307 |
# File 'lib/yolodice_client.rb', line 305 def satoshi_to_btc v (v.to_f / 100_000_000).round(8) end |
.target_from_multiplier(m) ⇒ Object
Returns bet terget given the required multiplier.
290 291 292 293 |
# File 'lib/yolodice_client.rb', line 290 def target_from_multiplier m edge = 0.01 (1_000_000.0 * (1.0 - edge) / m).round end |
.target_from_probability(p) ⇒ Object
Returns bet target given the required win probability.
298 299 300 |
# File 'lib/yolodice_client.rb', line 298 def target_from_probability p (p * 1_000_000.0).round end |
Instance Method Details
#authenticate(auth_key) ⇒ Object
Authenticates the connection by requesting a challenge message, signing it and sending the response back.
Parameters:
-
auth_key
– Base58 encoded private key for the API key
Returns
-
false
if authentication fails, -
user object (Hash with public user attributes) when authentication succeeds.
173 174 175 176 177 178 179 180 |
# File 'lib/yolodice_client.rb', line 173 def authenticate auth_key auth_key = Bitcoin::Key.from_base58(auth_key) unless auth_key.is_a?(Bitcoin::Key) challenge = generate_auth_challenge user = auth_by_address address: auth_key.addr, signature: auth_key.(challenge) raise Error, "Authentication failed" unless user log.debug "Authenticated as user #{user['name']}(#{user['id']})" user end |
#call(method, *arguments, &blk) ⇒ Object
Calls an arbitrary method on the server.
Parameters:
-
method
– method name, -
*arguments
– any arguments for the method, will be passed as theparams
object (optional), -
&blk
– a callback (optional) to be called upon receiving a response for async calls. The callback will receive the response object.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/yolodice_client.rb', line 191 def call method, *arguments, &blk raise ConnectionClosedError, "Not connected" unless @connection && !@connection.closed? params = if arguments.count == 0 nil elsif arguments.is_a?(Array) && arguments[0].is_a?(Hash) arguments[0] else arguments end id = @thread_semaphore.synchronize{ @req_id_seq += 1 } request = { id: id, method: method } request[:params] = params if params != nil if blk @thread_semaphore.synchronize{ @current_requests[id] = blk } log.debug{ "Calling remote method #{method}(#{params.inspect if params != nil}) with an async callback" } log.debug{ ">>> #{request.to_json}" } @connection.puts request.to_json nil else # a regular blocking request @thread_semaphore.synchronize{ @current_requests[id] = Thread.current.object_id } queue = (@receive_queues[Thread.current.object_id] ||= Queue.new) queue.clear log.debug{ "Calling remote method #{method}(#{params.inspect if params != nil})" } log.debug{ ">>> #{request.to_json}" } @connection.puts request.to_json response = queue.pop if response.has_key? 'result' response['result'] elsif response['error'] raise RemoteError.new response['error'] elsif response['client_error'] && response['client_error'] == 'Connection closed' raise ConnectionClosedError end end end |
#check_connection ⇒ Object
232 233 234 |
# File 'lib/yolodice_client.rb', line 232 def check_connection raise ConnectionClosedError if @connection.closed? end |
#close ⇒ Object
Closes connection to the host.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/yolodice_client.rb', line 138 def close log.debug "Closing connection" # Stop threads @connection.close # Send error to all pending requests = { 'client_error' => 'Connection closed' } while(callback = @thread_semaphore.synchronize{ @current_requests.shift }) do callback = callback[1] if callback.is_a? Integer # it's a thread @receive_queues[callback] << elsif callback.is_a? Proc # A thread pool would be better. Thread.new do callback.call end end end # Thread.stop @pinging_thread.kill @listening_thread.kill true end |
#connect ⇒ Object
Connects to the host.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/yolodice_client.rb', line 54 def connect if @connection && !@connection.closed? raise Error, 'Connection already open' end @connection = if @opts[:ssl] log.debug "Connecting to #{@opts[:host]}:#{@opts[:port]} over SSL" socket = TCPSocket.open @opts[:host], @opts[:port] ssl_socket = OpenSSL::SSL::SSLSocket.new socket ssl_socket.sync_close = true ssl_socket.connect ssl_socket else log.debug "Connecting to #{@opts[:host]}:#{@opts[:port]}" TCPSocket.open @opts[:host], @opts[:port] end log.info "Connected to #{@opts[:host]}:#{@opts[:port]}" # Start a thread that keeps listening on the socket @listening_thread = Thread.new do log.debug 'Listening thread started' loop do begin if @connection.closed? # keep the thread alive for a while sleep 1 end msg = @connection.gets if msg == nil # Connection is closed upstream. close end log.debug{ "<<< #{msg}" } = JSON.parse msg if ['id'] && (.has_key?('result') || .has_key?('error')) # definitealy a response callback = @thread_semaphore.synchronize{ @current_requests.delete ['id'] } raise Error, "Unknown id in response" unless callback if callback.is_a? Integer # it's a thread @receive_queues[callback] << elsif callback.is_a? Proc # A thread pool would be better. Thread.new do callback.call end end else if ['id'] # It must be a request from the server. We do not support it yet. else # No id, it must be a notification then. if notification_proc Thread.new do notification_proc.call end end end end rescue StandardError => e log.error e end end end # Start a thread that pings the server @pinging_thread = Thread.new do log.debug 'Pinging thread started' loop do begin sleep 30 call :ping unless @connection.closed? rescue StandardError => e log.error e end end end true end |
#logger=(logger) ⇒ Object
Sets a logger for the object.
46 47 48 |
# File 'lib/yolodice_client.rb', line 46 def logger=(logger) @log = logger end |