Class: Azure::ServiceBus::BrokeredMessageSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/azure/service_bus/brokered_message_serializer.rb

Overview

BrokeredMessageSerializer

Constant Summary collapse

PROPERTIES =
{
  'ContentType'             => 'content_type',
  'CorrelationId'           => 'correlation_id',
  'SessionID'               => 'session_id',
  'DeliveryCount'           => 'delivery_count',
  'LockedUntilUtc'          => 'locked_until_utc',
  'LockToken'               => 'lock_token',
  'MessageId'               => 'message_id',
  'Label'                   => 'label',
  'ReplyTo'                 => 'reply_to',
  'EnqueuedTimeUtc'         => 'enqueued_time_utc',
  'SequenceNumber'          => 'sequence_number',
  'TimeToLive'              => 'time_to_live',
  'To'                      => 'to',
  'ScheduledEnqueueTimeUtc' => 'scheduled_enqueue_time_utc',
  'ReplyToSessionId'        => 'reply_to_session_id'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg) ⇒ BrokeredMessageSerializer

Returns a new instance of BrokeredMessageSerializer.



46
47
48
# File 'lib/azure/service_bus/brokered_message_serializer.rb', line 46

def initialize(msg)
  @message = msg
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



44
45
46
# File 'lib/azure/service_bus/brokered_message_serializer.rb', line 44

def message
  @message
end

Class Method Details

.get_from_http_response(response) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/azure/service_bus/brokered_message_serializer.rb', line 50

def self.get_from_http_response(response)
  props = JSON.parse(response.headers['brokerproperties'])
  BrokeredMessage.new(response.body) do |m|
    loc_header = response.headers['location']
    m.location = URI(loc_header) unless loc_header.nil?
    m.content_type = response.headers['content-type']

    # String based properties
    m.lock_token = props['LockToken']
    m.message_id = props['MessageId']
    m.label = props['Label']
    m.to = props['To']
    m.session_id = props['SessionID']
    m.correlation_id = props['CorrelationId']
    m.reply_to = props['ReplyTo']
    m.reply_to = props['ReplyTo']
    m.reply_to_session_id = props['ReplyToSessionId']

    # Time based properties
    utc_lock = props['LockedUntilUtc']
    m.locked_until_utc = Time.parse(utc_lock) unless utc_lock.nil?

    enqueued_time_utc = self.parse_dot_net_serialized_datetime(
      props['EnqueuedTimeUtc']
    ) unless props['EnqueuedTimeUtc'].nil?
    m.enqueued_time_utc = enqueued_time_utc unless enqueued_time_utc.nil?

    m.scheduled_enqueue_time_utc = Time.parse(
      props['ScheduledEnqueueTimeUtc']
    ) unless props['ScheduledEnqueueTimeUtc'].nil?

    # Numeric based properties
    m.delivery_count = props['DeliveryCount'].to_i
    m.sequence_number = props['SequenceNumber'].to_i
    m.time_to_live = props['TimeToLive'].to_f

    # Custom Properties
    header_names_black_list = %w(
      brokerproperties
      date
      transfer-encoding
      location
      server
      connection
      content-type
      content-length
    )

    props = response.headers.reject do |k, _|
      header_names_black_list.include?(k.downcase)
    end

    props.each do |prop_name, value|
      parsed = JSON.parse('{ "' + prop_name + '" : ' + value + '}')
      m.properties[prop_name] = parsed[prop_name]
    end
  end
end

Instance Method Details

#get_property_headersObject

Build a hash based on message properties and ensure the values are in a valid format for HTTP headers

Returns a Hash



126
127
128
129
130
131
132
133
134
135
# File 'lib/azure/service_bus/brokered_message_serializer.rb', line 126

def get_property_headers
  hash = {}
  @message.properties.each do |name, value|
    value = value.httpdate if !value.nil? && value.class == Time

    tmp = JSON.generate [value]
    hash[name] = tmp[1..(tmp.length - 2)]
  end
  hash
end

#to_jsonObject

Serialize the message’s attributes to JSON

Returns a JSON String



112
113
114
115
116
117
118
119
120
# File 'lib/azure/service_bus/brokered_message_serializer.rb', line 112

def to_json
  hash = {}
  PROPERTIES.each do |p, u|
    attr_name = u.encode('UTF-8')
    value = @message.send(attr_name)
    hash[p] = value.to_s.encode('UTF-8') unless value.nil?
  end
  hash.to_json
end