Class: Groem::Client

Inherits:
EM::Connection
  • Object
show all
Includes:
EM::Deferrable, Constants
Defined in:
lib/groem/client.rb

Constant Summary collapse

DEFAULT_HOST =
'localhost'
DEFAULT_PORT =
23053

Constants included from Constants

Groem::Constants::ENVIRONMENT_KEY, Groem::Constants::GNTP_APPLICATION_ICON_KEY, Groem::Constants::GNTP_APPLICATION_NAME_KEY, Groem::Constants::GNTP_CALLBACK_RESPONSE, Groem::Constants::GNTP_CLICK_CALLBACK_RESULT, Groem::Constants::GNTP_CLOSE_CALLBACK_RESULT, Groem::Constants::GNTP_DEFAULT_ENVIRONMENT, Groem::Constants::GNTP_ENCRYPTION_ID_KEY, Groem::Constants::GNTP_ERROR_CODE_KEY, Groem::Constants::GNTP_ERROR_CODE_OK, Groem::Constants::GNTP_ERROR_RESPONSE, Groem::Constants::GNTP_NOTIFICATION_CALLBACK_CONTEXT_KEY, Groem::Constants::GNTP_NOTIFICATION_CALLBACK_CONTEXT_TYPE_KEY, Groem::Constants::GNTP_NOTIFICATION_CALLBACK_RESULT_KEY, Groem::Constants::GNTP_NOTIFICATION_CALLBACK_TARGET_KEY, Groem::Constants::GNTP_NOTIFICATION_CALLBACK_TIMESTAMP_KEY, Groem::Constants::GNTP_NOTIFICATION_COUNT_KEY, Groem::Constants::GNTP_NOTIFICATION_ICON_KEY, Groem::Constants::GNTP_NOTIFICATION_ID_KEY, Groem::Constants::GNTP_NOTIFICATION_NAME_KEY, Groem::Constants::GNTP_NOTIFY_METHOD, Groem::Constants::GNTP_OK_RESPONSE, Groem::Constants::GNTP_PROTOCOL_KEY, Groem::Constants::GNTP_REGISTER_METHOD, Groem::Constants::GNTP_REQUEST_METHOD_KEY, Groem::Constants::GNTP_RESPONSE_ACTION_KEY, Groem::Constants::GNTP_RESPONSE_METHOD_KEY, Groem::Constants::GNTP_SUBSCRIBE_METHOD, Groem::Constants::GNTP_TIMEDOUT_CALLBACK_RESULT, Groem::Constants::GNTP_VERSION_KEY, Groem::Constants::HEADERS_KEY, Groem::Constants::NOTIFICATIONS_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Constants

#growlify_action, #growlify_key, included

Constructor Details

#initialize(req) ⇒ Client

deprecated

def each_ok_response(&blk)
  @cb_each_response = blk
end

def each_callback_response(&blk)
  @cb_each_callback = blk
end

def each_error_response(&blk)
  @cb_each_errback = blk
end


83
84
85
86
87
88
89
90
91
92
# File 'lib/groem/client.rb', line 83

def initialize(req)
  super
  @req = req
  @req_action = req[ENVIRONMENT_KEY][GNTP_REQUEST_METHOD_KEY] 
  cb_context = req[HEADERS_KEY][GNTP_NOTIFICATION_CALLBACK_CONTEXT_KEY]
  cb_context_type = req[HEADERS_KEY][GNTP_NOTIFICATION_CALLBACK_CONTEXT_TYPE_KEY]
  cb_target = req[HEADERS_KEY][GNTP_NOTIFICATION_CALLBACK_TARGET_KEY]
  @wait_for_callback = @req_action == GNTP_NOTIFY_METHOD &&
                       cb_context && cb_context_type && !cb_target
end

Class Method Details

.anonymous_request_classObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/groem/client.rb', line 42

def anonymous_request_class
  @klass_req ||= \
    Class.new { 
      include(Groem::Marshal::Request) 
      require 'forwardable'
      extend Forwardable
      def_delegators :@raw, :[], :[]=
      def raw; @raw ||= {}; end
      def initialize(input = {})
        @raw = input
      end
    }
end

.anonymous_response_classObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/groem/client.rb', line 28

def anonymous_response_class
  @klass_resp ||= \
    Class.new { 
      include(Groem::Marshal::Response) 
      require 'forwardable'
      extend Forwardable
      def_delegators :@raw, :[], :[]=
      def raw; @raw ||= {}; end
      def initialize(input = {})
        @raw = input
      end
    }
end

.request(request, host = DEFAULT_HOST, port = DEFAULT_PORT) ⇒ Object Also known as: register, notify



21
22
23
24
# File 'lib/groem/client.rb', line 21

def request(request, host = DEFAULT_HOST, port = DEFAULT_PORT)
  connection = EM.connect host, port, self, request
  connection
end

.response_classObject



12
13
14
# File 'lib/groem/client.rb', line 12

def response_class
  @response_class ||= anonymous_response_class 
end

.response_class=(klass) ⇒ Object Also known as: load_response_as



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

def response_class=(klass)
  @response_class = klass
end

Instance Method Details

#post_initObject



94
95
96
97
98
# File 'lib/groem/client.rb', line 94

def post_init
  reset_state!
  #puts @req.dump.inspect

  send_data @req.dump
end

#receive_data(data) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/groem/client.rb', line 100

def receive_data data
  @buffer.extract(data).each do |line|
    #print "#{line.inspect}"

    @lines << line
  end
  if eof?
    receive_message @lines.join("\r\n") + "\r\n"
    reset_message_buffer!
  end
end

#response_classObject



58
59
60
# File 'lib/groem/client.rb', line 58

def response_class
  self.class.response_class
end

#when_callback(&blk) ⇒ Object



66
67
68
# File 'lib/groem/client.rb', line 66

def when_callback(&blk)
  @cb_callback = blk
end

#when_ok(&blk) ⇒ Object



62
63
64
# File 'lib/groem/client.rb', line 62

def when_ok(&blk)
  @cb_response = blk
end