Class: Ap4r::MessageBuilder
Overview
This MessageBuilder
is the class for formatting message body. Current support formats are text, xml, json and yaml, and the formatted messages are sent over HTTP.
Using format
method, this class automatically changes the format of the given message body and adds appropriate Content-type
to http header. Or using body_as_*
methods, you can directly assign formatted message body.
Instance Attribute Summary collapse
-
#format(v) ⇒ Object
readonly
Sets format message serialization.
-
#message_body ⇒ Object
Returns the value of attribute message_body.
-
#message_headers ⇒ Object
Returns the value of attribute message_headers.
-
#queue_name ⇒ Object
Returns the value of attribute queue_name.
-
#to_xml_options ⇒ Object
readonly
Returns the value of attribute to_xml_options.
Instance Method Summary collapse
-
#body(k, v, options = { }) ⇒ Object
Sets message body in async_to block.
-
#body_as_json(json) ⇒ Object
Sets json format message.
-
#body_as_text(text) ⇒ Object
Sets text format message.
-
#body_as_xml(xml) ⇒ Object
Sets xml format message.
-
#body_as_yaml(yaml) ⇒ Object
Sets yaml format message.
-
#format_message_body ⇒ Object
Return converted message body, as to assigned format.
-
#header(k, v) ⇒ Object
Sets message header in async_to block.
-
#http_header(k, v) ⇒ Object
Sets http header in async_to block such as ‘Content_type’.
-
#initialize(queue_name, queue_message, queue_headers) ⇒ MessageBuilder
constructor
A new instance of MessageBuilder.
Constructor Details
#initialize(queue_name, queue_message, queue_headers) ⇒ MessageBuilder
Returns a new instance of MessageBuilder.
19 20 21 22 23 24 25 26 |
# File 'lib/ap4r/message_builder.rb', line 19 def initialize(queue_name, , queue_headers) @queue_name = queue_name @message_body = @message_headers = queue_headers @format = nil @message_body_with_format = nil @to_xml_options = {:root => "root"} end |
Instance Attribute Details
#format(v) ⇒ Object (readonly)
Sets format message serialization. As to the format, automatically sets content-type. Unless any format, content-type is defined as “application/x-www-form-urlencoded”.
76 77 78 |
# File 'lib/ap4r/message_builder.rb', line 76 def format @format end |
#message_body ⇒ Object
Returns the value of attribute message_body.
28 29 30 |
# File 'lib/ap4r/message_builder.rb', line 28 def @message_body end |
#message_headers ⇒ Object
Returns the value of attribute message_headers.
28 29 30 |
# File 'lib/ap4r/message_builder.rb', line 28 def @message_headers end |
#queue_name ⇒ Object
Returns the value of attribute queue_name.
28 29 30 |
# File 'lib/ap4r/message_builder.rb', line 28 def queue_name @queue_name end |
#to_xml_options ⇒ Object (readonly)
Returns the value of attribute to_xml_options.
29 30 31 |
# File 'lib/ap4r/message_builder.rb', line 29 def @to_xml_options end |
Instance Method Details
#body(k, v, options = { }) ⇒ Object
Sets message body in async_to block. The first argument is key and the second one is value.
options are for to_xml conversion on Array and Hash, ActiveRecord objects.
36 37 38 39 40 41 42 43 44 |
# File 'lib/ap4r/message_builder.rb', line 36 def body(k, v, = { }) k ||= v.class if v.kind_of? ActiveRecord::Base @message_body[k.to_sym] = v @to_xml_options = @to_xml_options.merge() else @message_body[k.to_sym] = v end end |
#body_as_json(json) ⇒ Object
Sets json format message. No need to use format
.
107 108 109 110 |
# File 'lib/ap4r/message_builder.rb', line 107 def body_as_json(json) @message_body_with_format = json format :json end |
#body_as_text(text) ⇒ Object
Sets text format message. No need to use format
.
93 94 95 96 |
# File 'lib/ap4r/message_builder.rb', line 93 def body_as_text(text) @message_body_with_format = text format :text end |
#body_as_xml(xml) ⇒ Object
Sets xml format message. No need to use format
.
100 101 102 103 |
# File 'lib/ap4r/message_builder.rb', line 100 def body_as_xml(xml) @message_body_with_format = xml format :xml end |
#body_as_yaml(yaml) ⇒ Object
Sets yaml format message. No need to use format
.
114 115 116 117 |
# File 'lib/ap4r/message_builder.rb', line 114 def body_as_yaml(yaml) @message_body_with_format = yaml format :yaml end |
#format_message_body ⇒ Object
Return converted message body, as to assigned format.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ap4r/message_builder.rb', line 121 def return @message_body_with_format if @message_body_with_format case @format when :text return @message_body.to_s when :xml return @message_body.to_xml(@to_xml_options) when :json return @message_body.to_json when :yaml return @message_body.to_yaml else @message_body.each do |k,v| if v.kind_of? ActiveRecord::Base @message_body[k] = v.attributes end end return query_string(@message_body) end end |
#header(k, v) ⇒ Object
Sets message header in async_to block. The first argument is key and the second one is value.
Now supports following keys:
:expire
:priority
:delivery
:max_deliveries
:dispatch_mode
:target_method
:target_url
:id
For details, please refer the reliable-msg.
61 62 63 |
# File 'lib/ap4r/message_builder.rb', line 61 def header(k, v) @message_headers[k.to_sym] = v end |
#http_header(k, v) ⇒ Object
Sets http header in async_to block such as ‘Content_type’. The first argument is key and the second one is value.
68 69 70 |
# File 'lib/ap4r/message_builder.rb', line 68 def http_header(k, v) @message_headers["http_header_#{k}".to_sym] = v end |