Class: SocketCake::BaseSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/socketcake/base_socket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_list, url, open_message, cookie, verbose = false) ⇒ BaseSocket

Returns a new instance of BaseSocket.



9
10
11
12
13
14
15
16
17
# File 'lib/socketcake/base_socket.rb', line 9

def initialize task_list, url, open_message, cookie, verbose = false
  self.url = url
  self.open_message = open_message
  self.task_list = task_list
  self.cookie = cookie
  self.completed_tasks = Array.new
  self.local_storage = Hash.new
  self.verbose = verbose
end

Instance Attribute Details

#completed_tasksObject

Returns the value of attribute completed_tasks.



3
4
5
# File 'lib/socketcake/base_socket.rb', line 3

def completed_tasks
  @completed_tasks
end

Returns the value of attribute cookie.



3
4
5
# File 'lib/socketcake/base_socket.rb', line 3

def cookie
  @cookie
end

#current_taskObject

Returns the value of attribute current_task.



3
4
5
# File 'lib/socketcake/base_socket.rb', line 3

def current_task
  @current_task
end

#local_storageObject

Returns the value of attribute local_storage.



3
4
5
# File 'lib/socketcake/base_socket.rb', line 3

def local_storage
  @local_storage
end

#open_messageObject

Returns the value of attribute open_message.



3
4
5
# File 'lib/socketcake/base_socket.rb', line 3

def open_message
  @open_message
end

#task_listObject

Returns the value of attribute task_list.



3
4
5
# File 'lib/socketcake/base_socket.rb', line 3

def task_list
  @task_list
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/socketcake/base_socket.rb', line 3

def url
  @url
end

#verboseObject

Returns the value of attribute verbose.



3
4
5
# File 'lib/socketcake/base_socket.rb', line 3

def verbose
  @verbose
end

Class Method Details

.get_sequenceObject



5
6
7
# File 'lib/socketcake/base_socket.rb', line 5

def self.get_sequence
    ObjectSpace.each_object(self)
end

Instance Method Details

#handle(msg) ⇒ Object



49
50
51
52
53
54
# File 'lib/socketcake/base_socket.rb', line 49

def handle msg
    @message = JSON.parse(msg)
    unless self.current_task[:completed]
            self.send(self.current_task[:name], self.current_task[:args])    
    end
end

#start(arg = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/socketcake/base_socket.rb', line 19

def start(arg = nil)


  @socket = WebSocket::EventMachine::Client.connect(:uri => self.url,
                                                    headers: {
                                                        'Cookie': self.cookie})


  @socket.onopen do
    puts "#{Time.now} Connected"
    get_task
    
    @socket.send self.open_message
  end

  @socket.onmessage do |msg|
    puts msg if @verbose
    handle(msg)
  end


  @socket.onerror do |error|
    puts error
  end

  @socket.onclose do |code, reason|
    puts "#{Time.now} Disconnected with status code: #{code} and reason: #{reason}"
    yield(local_storage)
  end

  def handle msg
      @message = JSON.parse(msg)
      unless self.current_task[:completed]
              self.send(self.current_task[:name], self.current_task[:args])    
      end
  end

end