Class: BelliteJsonRpcApi Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/bellite.rb

Overview

This class is abstract.

This is common interface of Bellite json-rpc API

Direct Known Subclasses

BelliteJsonRpc

Instance Method Summary collapse

Constructor Details

#initialize(cred = nil) ⇒ BelliteJsonRpcApi

Constructor. Connects to server with cred credentials

Parameters:

  • cred (String) (defaults to: nil)

    Credentials in format: 'host:port/token';



84
85
86
# File 'lib/bellite.rb', line 84

def initialize(cred=nil)
    connect(cred)
end

Instance Method Details

#_connect(cred, f_ready) ⇒ Object

This method is abstract.

Connecting to JSON-RPC server

Parameters:

  • cred (String)

    Credentials in format: 'host:port/token';

  • f_ready (Future)

    Ready future instance

Raises:

  • (NotImplementedError)


211
212
213
# File 'lib/bellite.rb', line 211

def _connect(cred, f_ready)
    raise NotImplementedError, "Subclass Responsibility"
end

#_invoke(method, params = nil) ⇒ Object

This method is abstract.

Invokes method

Parameters:

  • method (String)
  • params (Hash) (defaults to: nil)

    Something JSONable to pass as ethod params

Raises:

  • (NotImplementedError)


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

Parameters:

  • token (String)

    Token to access server

Returns:



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

Parameters:

  • selfId (Fixnum) (defaults to: 0)

    internal id

  • evtType (String) (defaults to: '*')

    type of event

  • res (Fixnum) (defaults to: -1,)
  • ctx (Hash) (defaults to: false)
    • context, passed to event handler

Returns:



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

Parameters:

  • host (String)

    Host

  • port (Fixnum)

    Port



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

Parameters:

  • cred (String) (defaults to: false)

    server credentials in format host:port/token

Returns:

  • (Hash)

    with credentials or false if failed



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

Parameters:

  • cmd (String)

    command

  • *args (Array)

    Variable-length arguments, passed with cmd. :key => value ruby Hash parameter sugar also works

Returns:



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

#pingPromise

Ping server

Returns:



103
104
105
# File 'lib/bellite.rb', line 103

def ping
    return _invoke('ping')
end

#readyObject

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

Parameters:

  • selfId (Fixnum)

    Id for internal use (events, for example)

  • cmd (Hash)
    • some data, which can be encoded as JSON to pass to server

Returns:



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

Parameters:

  • selfId (Fixnum)

    internal id

  • evtType (String) (defaults to: false)

    type of event

Returns:



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

#versionPromise

Server version

Returns:



97
98
99
# File 'lib/bellite.rb', line 97

def version
    return _invoke('version')
end