Class: SubZero::Client

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.

Yields:



6
7
8
9
# File 'lib/sub_zero/client.rb', line 6

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



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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/sub_zero/client.rb', line 4

def config
  @config
end

#sidObject (readonly)

Returns the value of attribute sid.



4
5
6
# File 'lib/sub_zero/client.rb', line 4

def sid
  @sid
end

Instance Method Details

#[](verb) ⇒ Object



35
36
37
# File 'lib/sub_zero/client.rb', line 35

def [] verb
  call verb
end

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sub_zero/client.rb', line 15

def call verb, payload = {}, options = {}
  sz_verb = verb.to_s.upcase.gsub('_', '/')

  request = Message.new(type: 'REQ', subtype: sid, verb: sz_verb,
                        payload: payload, options: options)

  response = socket.call(request)

  if block_given?
    yield response
  else
    fail Error.new(response) if response.error?
    response.payload
  end
end

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



31
32
33
# File 'lib/sub_zero/client.rb', line 31

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

#socketObject



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

def socket
  Socket.new config
end