Class: SubZero::Client

Inherits:
Object
  • Object
show all
Includes:
Socket
Defined in:
lib/sub_zero/client.rb,
lib/sub_zero/client/socket.rb,
lib/sub_zero/client/configuration.rb

Defined Under Namespace

Modules: Configuration, Socket Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sid) {|@config| ... } ⇒ Client

Returns a new instance of Client.

Yields:



12
13
14
15
# File 'lib/sub_zero/client.rb', line 12

def initialize sid
  @sid, @config = sid.to_s.upcase, Configuration.default
  yield @config if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, payload = {}, options = {}, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/sub_zero/client.rb', line 32

def method_missing method, payload = {}, options = {}, &block
  if method.to_s.end_with? '!'
    call! method.delete('!'), payload, options, &block
  else
    call method, payload, options, &block
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/sub_zero/client.rb', line 10

def config
  @config
end

#sidObject (readonly)

Returns the value of attribute sid.



10
11
12
# File 'lib/sub_zero/client.rb', line 10

def sid
  @sid
end

Instance Method Details

#[](verb) ⇒ Object



28
29
30
# File 'lib/sub_zero/client.rb', line 28

def [] verb
  call verb
end

#call(verb, payload = {}, options = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/sub_zero/client.rb', line 17

def call verb, payload = {}, options = {}
  sz_verb = verb.to_s.upcase.gsub('_', '/')
  status, result = zeromq_fetch(sz_verb, payload, options)
  result = handle_result(status, result)
  block_given? ? yield(result) : result
end

#call!(verb, payload = {}, options = {}, &block) ⇒ Object



24
25
26
# File 'lib/sub_zero/client.rb', line 24

def call! verb, payload = {}, options = {}, &block
  call verb, payload, options, &block
end

#handle_result(status, result) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/sub_zero/client.rb', line 40

def handle_result status, result
  case status
  when :ok
    result
  when :error
    fail Error.new(result['error'])
  end
end