Class: Metybur::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
# File 'lib/metybur/client.rb', line 8

def initialize(credentials)
  @credentials = credentials.freeze
  @subscription_messages = []
  @collections = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/metybur/client.rb', line 39

def method_missing(method, *params, &block)
  method = method.to_s.camelize(:lower)
  params.map! do |param|
    case param
    when Hash
      param_array = param.map { |k, v| [k.to_s.camelize(:lower), v] }
      Hash[param_array]
    else
      param
    end
  end
  call(method, params, &block)
end

Instance Attribute Details

#websocket=(value) ⇒ Object (writeonly)

Sets the attribute websocket

Parameters:

  • value

    the value to set the attribute websocket to.



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

def websocket=(value)
  @websocket = value
end

Instance Method Details

#call(method_name, params, &block) ⇒ Object



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

def call(method_name, params, &block)
  method(method_name).call(params, &block)
end

#collection(name) ⇒ Object



25
26
27
28
29
# File 'lib/metybur/client.rb', line 25

def collection(name)
  Metybur::Collection.new(name, @websocket).tap do |collection|
    @collections << collection
  end
end

#connectObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/metybur/client.rb', line 58

def connect
  connect_message = {
    msg: 'connect',
    version: '1',
    support: ['1']
  }
  @websocket.send(connect_message.to_json)

  credentials = @credentials.dup
  password = credentials.delete(:password)
  if password
    this = self
    (user: credentials, password: password) { this.resubscribe }
  else
    resubscribe
  end
end

#method(name) ⇒ Object



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

def method(name)
  Metybur::Method.new(name, @websocket)
end

#resubscribeObject



53
54
55
56
# File 'lib/metybur/client.rb', line 53

def resubscribe
  @subscription_messages.each { |message| @websocket.send(message) }
  @collections.each { |collection| collection.websocket = @websocket }
end

#subscribe(record_set_name, *params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/metybur/client.rb', line 14

def subscribe(record_set_name, *params)
  message = {
    msg: 'sub',
    id: 'cde',
    name: record_set_name,
    params: params
  }.to_json
  @subscription_messages << message
  @websocket.send message
end