Class: GripPubControl

Inherits:
PubControl
  • Object
show all
Defined in:
lib/grippubcontrol.rb

Overview

The GripPubControl class allows consumers to easily publish HTTP response and HTTP stream format messages to GRIP proxies. Configuring GripPubControl is slightly different from configuring PubControl in that the ‘uri’ and ‘iss’ keys in each config entry should have a ‘control_’ prefix. GripPubControl inherits from PubControl and therefore also provides all of the same functionality.

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ GripPubControl

Initialize with or without a configuration. A configuration can be applied after initialization via the apply_grip_config method.



23
24
25
26
27
28
# File 'lib/grippubcontrol.rb', line 23

def initialize(config=nil)
  @clients = Array.new
  if !config.nil?
    apply_grip_config(config)
  end
end

Instance Method Details

#apply_grip_config(config) ⇒ Object

Apply the specified configuration to this GripPubControl instance. The configuration object can either be a hash or an array of hashes where each hash corresponds to a single PubControlClient instance. Each hash will be parsed and a PubControlClient will be created either using just a URI or a URI and JWT authentication information.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/grippubcontrol.rb', line 35

def apply_grip_config(config)
  if !config.is_a?(Array)
    config = [config]
  end
  config.each do |entry|
    if !entry.key?('control_uri')
      next
    end
    client = PubControlClient.new(entry['control_uri'])
    if entry.key?('control_iss')
      client.set_auth_jwt({'iss' => entry['control_iss']}, entry['key'])
    end
    super_add_client(client)
  end
end

#publish_http_response(channel, http_response, id = nil, prev_id = nil) ⇒ Object

Synchronously publish an HTTP response format message to all of the configured PubControlClients with a specified channel, message, and optional ID and previous ID. Note that the ‘http_response’ parameter can be provided as either an HttpResponseFormat instance or a string (in which case an HttpResponseFormat instance will automatically be created and have the ‘body’ field set to the specified string).



57
58
59
60
61
62
63
# File 'lib/grippubcontrol.rb', line 57

def publish_http_response(channel, http_response, id=nil, prev_id=nil)
  if http_response.is_a?(String)
    http_response = HttpResponseFormat.new(nil, nil, nil, http_response)
  end
  item = Item.new(http_response, id, prev_id)
  super_publish(channel, item)
end

#publish_http_response_async(channel, http_response, id = nil, prev_id = nil, callback = nil) ⇒ Object

Asynchronously publish an HTTP response format message to all of the configured PubControlClients with a specified channel, message, and optional ID, previous ID, and callback. Note that the ‘http_response’ parameter can be provided as either an HttpResponseFormat instance or a string (in which case an HttpResponseFormat instance will automatically be created and have the ‘body’ field set to the specified string). When specified, the callback method will be called after publishing is complete and passed a result and error message (if an error was encountered).



73
74
75
76
77
78
79
80
# File 'lib/grippubcontrol.rb', line 73

def publish_http_response_async(channel, http_response, id=nil,
    prev_id=nil, callback=nil)
  if http_response.is_a?(String)
    http_response = HttpResponseFormat.new(nil, nil, nil, http_response)
  end
  item = Item.new(http_response, id, prev_id)
  super_publish_async(channel, item, callback)
end

#publish_http_stream(channel, http_stream, id = nil, prev_id = nil) ⇒ Object

Synchronously publish an HTTP stream format message to all of the configured PubControlClients with a specified channel, message, and optional ID and previous ID. Note that the ‘http_stream’ parameter can be provided as either an HttpStreamFormat instance or a string (in which case an HttStreamFormat instance will automatically be created and have the ‘content’ field set to the specified string).



88
89
90
91
92
93
94
# File 'lib/grippubcontrol.rb', line 88

def publish_http_stream(channel, http_stream, id=nil, prev_id=nil)
  if http_stream.is_a?(String)
    http_stream = HttpStreamFormat.new(http_stream)
  end
  item = Item.new(http_stream, id, prev_id)
  super_publish(channel, item)
end

#publish_http_stream_async(channel, http_stream, id = nil, prev_id = nil, callback = nil) ⇒ Object

Asynchronously publish an HTTP stream format message to all of the configured PubControlClients with a specified channel, message, and optional ID, previous ID, and callback. Note that the ‘http_stream’ parameter can be provided as either an HttpStreamFormat instance or a string (in which case an HttpStreamFormat instance will automatically be created and have the ‘content’ field set to the specified string). When specified, the callback method will be called after publishing is complete and passed a result and error message (if an error was encountered).



104
105
106
107
108
109
110
111
# File 'lib/grippubcontrol.rb', line 104

def publish_http_stream_async(channel, http_stream, id=nil,
    prev_id=nil, callback=nil)
  if http_stream.is_a?(String)
    http_stream = HttpStreamFormat.new(http_stream)
  end
  item = Item.new(http_stream, id, prev_id)  
  super_publish_async(channel, item, callback)
end