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


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/march_hare/metadata.rb', line 78

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


121
122
123
124
125
# File 'lib/march_hare/metadata.rb', line 121

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.


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

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