Class: SpeakingApps::MessageQueue

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment = nil) ⇒ MessageQueue

Returns a new instance of MessageQueue.



10
11
12
# File 'lib/speaking_apps/message_queue.rb', line 10

def initialize(environment = nil)
  @environment = environment
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



8
9
10
# File 'lib/speaking_apps/message_queue.rb', line 8

def environment
  @environment
end

#remote_queueObject

Returns the value of attribute remote_queue.



7
8
9
# File 'lib/speaking_apps/message_queue.rb', line 7

def remote_queue
  @remote_queue
end

Instance Method Details

#add(message, *attr) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/speaking_apps/message_queue.rb', line 14

def add(message, *attr)
  if environment_valid?
    store_in_remote_service_queue({
      :message => message, 
      :tags => attr, 
      :environment => @environment,
      :token => SpeakingApps::Config.token,
      :host => SpeakingApps::Config.host,
      :port => SpeakingApps::Config.port
    })
  end
end

#environment_valid?Boolean

:nodoc#

Returns:

  • (Boolean)


53
54
55
# File 'lib/speaking_apps/message_queue.rb', line 53

def environment_valid? #:nodoc#
  @environment.nil? || ::SpeakingApps::Config.environments.include?(@environment)
end

#pingObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/speaking_apps/message_queue.rb', line 27

def ping
  begin
    Timeout::timeout(2) do
      hub_url = URI.parse('http://127.0.0.1:28128/ping')
      response = Net::HTTP.start(hub_url.host, hub_url.port) { |http| http.get(hub_url.path) }
      return response.body == 'pong'
    end
  rescue => error
    return false
  end
end

#store_in_remote_service_queue(message_with_options = {}) ⇒ Object



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

def store_in_remote_service_queue(message_with_options = {})
#      begin
    Timeout::timeout(2) do
      hub_url = URI.parse('http://127.0.0.1:28128/messages')
      response = Net::HTTP.start(hub_url.host, hub_url.port) do |http| 
        http.post(hub_url.path, message_with_options.to_json)
      end
      return response == Net::HTTPCreated
   end        
  # rescue => error
  #   return false
  # end
end