Class: Mosca::Client

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
# File 'lib/mosca/client.rb', line 18

def initialize args = {}
  options = default.merge(args)
  attributes.each do |attribute|
    send "#{attribute}=".to_sym, options[attribute]
  end
end

Class Attribute Details

.default_brokerObject

Returns the value of attribute default_broker.



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

def default_broker
  @default_broker
end

.default_timeoutObject

Returns the value of attribute default_timeout.



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

def default_timeout
  @default_timeout
end

Instance Attribute Details

#brokerObject

Returns the value of attribute broker.



16
17
18
# File 'lib/mosca/client.rb', line 16

def broker
  @broker
end

#clientObject

Returns the value of attribute client.



16
17
18
# File 'lib/mosca/client.rb', line 16

def client
  @client
end

#passObject

Returns the value of attribute pass.



16
17
18
# File 'lib/mosca/client.rb', line 16

def pass
  @pass
end

#topic_baseObject

Returns the value of attribute topic_base.



16
17
18
# File 'lib/mosca/client.rb', line 16

def topic_base
  @topic_base
end

#topic_inObject

Returns the value of attribute topic_in.



16
17
18
# File 'lib/mosca/client.rb', line 16

def topic_in
  @topic_in
end

#topic_outObject

Returns the value of attribute topic_out.



16
17
18
# File 'lib/mosca/client.rb', line 16

def topic_out
  @topic_out
end

#userObject

Returns the value of attribute user.



16
17
18
# File 'lib/mosca/client.rb', line 16

def user
  @user
end

Instance Method Details

#full_topic(topic_name) ⇒ Object



48
49
50
# File 'lib/mosca/client.rb', line 48

def full_topic topic_name
  topic_base + topic_name
end

#get(params = {}) ⇒ Object



42
43
44
45
46
# File 'lib/mosca/client.rb', line 42

def get params = {}
  get! params
rescue Timeout::Error
  nil
end

#get!(params = {}) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/mosca/client.rb', line 33

def get! params = {}
  timeout(params) do
    topic = params[:topic_in] || params[:topic] || @topic_in || Exceptions.raise_missing_topic
    connection.get(full_topic topic) do |topic, message|
      return parse_response message
    end
  end
end

#publish(json, params = {}) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/mosca/client.rb', line 25

def publish json, params = {}
  topic_out = params[:topic_out] || params[:topic] || @topic_out || Exceptions.raise_missing_topic
  topic_in = params[:topic_in] || @topic_in
  connection.subscribe full_topic(topic_in) if params[:response]
  connection.publish full_topic(topic_out), json
  get(params) if params[:response]
end