Class: Azure::ServiceBus::BrokeredMessageSerializer

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

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
# 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|
    m.location                    = URI(response.headers['location']) unless response.headers['location'].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
    m.locked_until_utc            = Time.parse(props['LockedUntilUtc']) unless props['LockedUntilUtc'].nil?
    m.enqueued_time_utc           = Time.parse(props['EnqueuedTimeUtc']) unless props['EnqueuedTimeUtc'].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 = [
      'brokerproperties',
      'date',
      'transfer-encoding',
      'location',
      'server',
      'connection',
      'content-type',
      'content-length'
    ]
    props = response.headers.reject {|k,_| header_names_black_list.include?(k.downcase) }
    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



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

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

    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



99
100
101
102
103
104
105
106
107
# File 'lib/azure/service_bus/brokered_message_serializer.rb', line 99

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