Class: Faye::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/faye/transport.rb

Direct Known Subclasses

HttpTransport, LocalTransport

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, endpoint) ⇒ Transport

Returns a new instance of Transport.



7
8
9
10
# File 'lib/faye/transport.rb', line 7

def initialize(client, endpoint)
  @client   = client
  @endpoint = endpoint
end

Class Attribute Details

.connection_typeObject

Returns the value of attribute connection_type.



45
46
47
# File 'lib/faye/transport.rb', line 45

def connection_type
  @connection_type
end

Class Method Details

.get(client, connection_types = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/faye/transport.rb', line 47

def get(client, connection_types = nil)
  endpoint = client.endpoint
  connection_types ||= supported_connection_types
  
  candidate_class = @transports.find do |type, klass|
    connection_types.include?(type) and
    klass.usable?(endpoint)
  end
  
  unless candidate_class
    raise "Could not find a usable connection type for #{ endpoint }"
  end
  
  candidate_class.last.new(client, endpoint)
end

.register(type, klass) ⇒ Object



63
64
65
66
# File 'lib/faye/transport.rb', line 63

def register(type, klass)
  @transports[type] = klass
  klass.connection_type = type
end

.supported_connection_typesObject



68
69
70
# File 'lib/faye/transport.rb', line 68

def supported_connection_types
  @transports.keys
end

Instance Method Details

#connection_typeObject



12
13
14
# File 'lib/faye/transport.rb', line 12

def connection_type
  self.class.connection_type
end

#send(message, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/faye/transport.rb', line 16

def send(message, &block)
  if message.is_a?(Hash) and not message.has_key?('id')
    message['id'] = @client.namespace.generate
  end
  
  request(message) { |responses|
    if block_given?
      [responses].flatten.each do |response|
        
        if message.is_a?(Hash) and response['id'] == message['id']
          block.call(response) 
        end
        
        if response['advice']
          @client.handle_advice(response['advice'])
        end
        
        if response['data'] and response['channel']
          @client.send_to_subscribers(response)
        end
        
      end
    end
  }
end