Class: BelliteJsonRpcApi Abstract
- Inherits:
-
Object
- Object
- BelliteJsonRpcApi
- Defined in:
- lib/bellite.rb
Overview
This is common interface of Bellite json-rpc API
Direct Known Subclasses
Instance Method Summary collapse
- #_connect(cred, f_ready) ⇒ Object abstract
- #_invoke(method, params = nil) ⇒ Object abstract
-
#auth(token) ⇒ Promise
Authenticates with server using token.
-
#bindEvent(selfId = 0, evtType = '*', res = -1,, ctx = false) ⇒ Promise
binds Event on some evtType.
-
#connect(cred = nil) ⇒ Object
Connecting to JSON-RPC server.
-
#findCredentials(cred = false) ⇒ Hash
Finds credentials in environment variable BELLITE_SERVER or in passed parameter.
-
#initialize(cred = nil) ⇒ BelliteJsonRpcApi
constructor
Constructor.
-
#perform(selfId, cmd, *args) ⇒ Promise
perform JSON-RPC request.
-
#ping ⇒ Promise
Ping server.
-
#ready ⇒ Object
Return promise for ready event.
-
#respondsTo(selfId, cmd) ⇒ Promise
invokes respondsTo.
-
#unbindEvent(selfId, evtType = false) ⇒ Promise
Unbins Event on some evtType.
-
#version ⇒ Promise
Server version.
Constructor Details
#initialize(cred = nil) ⇒ BelliteJsonRpcApi
Constructor. Connects to server with cred credentials
84 85 86 |
# File 'lib/bellite.rb', line 84 def initialize(cred=nil) connect(cred) end |
Instance Method Details
#_connect(cred, f_ready) ⇒ Object
Connecting to JSON-RPC server
211 212 213 |
# File 'lib/bellite.rb', line 211 def _connect(cred, f_ready) raise NotImplementedError, "Subclass Responsibility" end |
#_invoke(method, params = nil) ⇒ Object
Invokes method
218 219 220 |
# File 'lib/bellite.rb', line 218 def _invoke(method, params=nil) raise NotImplementedError, "Subclass Responsibility" end |
#auth(token) ⇒ Promise
Authenticates with server using token
91 92 93 |
# File 'lib/bellite.rb', line 91 def auth(token) return _invoke('auth', [token]) end |
#bindEvent(selfId = 0, evtType = '*', res = -1,, ctx = false) ⇒ Promise
binds Event on some evtType
148 149 150 151 152 153 |
# File 'lib/bellite.rb', line 148 def bindEvent(selfId=0, evtType='*', res = -1, ctx=false) if not selfId selfId = 0 end return _invoke('bindEvent',[selfId, evtType, res, ctx]) end |
#connect(cred = nil) ⇒ Object
Connecting to JSON-RPC server
198 199 200 201 202 203 204 205 206 |
# File 'lib/bellite.rb', line 198 def connect(cred=nil) cred = findCredentials(cred) if cred f_ready = deferred() @ready = f_ready.promise _connect(cred, f_ready) return @ready end end |
#findCredentials(cred = false) ⇒ Hash
Finds credentials in environment variable BELLITE_SERVER or in passed parameter
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/bellite.rb', line 170 def findCredentials(cred=false) if not cred cred = ENV['BELLITE_SERVER'] if not cred cred = '127.0.0.1:3099/bellite-demo-host'; $stderr.puts 'BELLITE_SERVER environment variable not found, using "'+cred+'"' end elsif not cred.instance_of String return cred end begin host, token = cred.split('/', 2) host, port = host.split(':', 2) return ({"credentials" => cred, "token" => token, "host" => host, "port" => Integer(port)}) rescue return false end end |
#perform(selfId, cmd, *args) ⇒ Promise
perform JSON-RPC request
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/bellite.rb', line 122 def perform(selfId, cmd, *args) if args.size > 1 args.each do |arg| if arg.instance_of(Hash) raise ArgumentError, "Cannot specify both positional and keyword arguments" end end end if args.size == 1 and (args[0].instance_of? Hash or args[0].instance_of? Array) args = args[0] end if args == [] args = nil end if not selfId selfId = 0 end return _invoke('perform',[selfId, cmd, args]) end |
#ping ⇒ Promise
Ping server
103 104 105 |
# File 'lib/bellite.rb', line 103 def ping return _invoke('ping') end |
#ready ⇒ Object
Return promise for ready event
191 192 193 |
# File 'lib/bellite.rb', line 191 def ready() return @ready end |
#respondsTo(selfId, cmd) ⇒ Promise
invokes respondsTo
111 112 113 114 115 116 |
# File 'lib/bellite.rb', line 111 def respondsTo(selfId, cmd) if not selfId selfId = 0 end return _invoke('respondsTo', [selfId, cmd]) end |
#unbindEvent(selfId, evtType = false) ⇒ Promise
Unbins Event on some evtType
159 160 161 162 163 164 |
# File 'lib/bellite.rb', line 159 def unbindEvent(selfId, evtType=false) if not selfId selfId = 0 end return _invoke('unbindEvent',[selfId, evtType]) end |
#version ⇒ Promise
Server version
97 98 99 |
# File 'lib/bellite.rb', line 97 def version return _invoke('version') end |