Class: MarchHare::BasicPropertiesBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/march_hare/metadata.rb

Class Method Summary collapse

Class Method Details

.build_properties_from(props = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/march_hare/metadata.rb', line 100

def self.build_properties_from(props = {})
  builder = AMQP::BasicProperties::Builder.new

  builder.content_type(props[:content_type]).
    content_encoding(props[:content_encoding]).
    headers(self.deep_stringify_keys(props[:headers])).
    delivery_mode(props[:persistent] ? 2 : 1).
    priority(props[:priority]).
    correlation_id(props[:correlation_id]).
    reply_to(props[:reply_to]).
    expiration(if props[:expiration] then props[:expiration].to_s end).
    message_id(props[:message_id]).
    timestamp(props[:timestamp]).
    type(props[:type]).
    user_id(props[:user_id]).
    app_id(props[:app_id]).
    cluster_id(props[:cluster_id]).
    build
end

.deep_stringify_keys(hash) ⇒ Object



143
144
145
146
147
# File 'lib/march_hare/metadata.rb', line 143

def self.deep_stringify_keys(hash)
  transform_hash(hash, :deep => true) do |hash, key, value|
    hash[key.to_s] = value
  end
end

.transform_hash(value, options = {}, &block) ⇒ Object

Deep hash transformation fn is courtesy of Avdi Grimm and Markus Kuhnt, with some modifications to support primitive and array values.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/march_hare/metadata.rb', line 123

def self.transform_hash(value, options = {}, &block)
  return nil if value.nil?
  return value if !value.is_a?(Hash) && !value.is_a?(Array)
  return value.map { |v| transform_hash(v, options, &block) } if value.is_a?(Array)

  value.inject({}) do |result, (key, value)|
    value = if (options[:deep] && value.is_a?(Hash))
              transform_hash(value, options, &block)
            else
              if value.is_a?(Array)
                value.map { |v| transform_hash(v, options, &block) }
              else
                value
              end
            end
    block.call(result, key, value)
    result
  end
end