Module: Droid::Utils
- Defined in:
- lib/droid/utils.rb,
lib/droid/heroku.rb
Class Method Summary collapse
- .create_event_hash ⇒ Object
- .data_summary(json) ⇒ Object
- .extract_custom_headers(hash, opts = {}, popts = {}) ⇒ Object
- .format_data_summary(data, headers) ⇒ Object
- .format_publish(data, opts = {}, popts = {}) ⇒ Object
- .generate_name_for_instance(name) ⇒ Object
- .generate_queue(exchange_name, second_name = nil) ⇒ Object
- .generate_reply_to(name) ⇒ Object
- .generate_sym ⇒ Object
- .parse_custom_headers(headers) ⇒ Object
- .parse_message(raw) ⇒ Object
Class Method Details
.create_event_hash ⇒ Object
28 29 30 31 |
# File 'lib/droid/utils.rb', line 28 def self.create_event_hash s = Time.now.to_s + self.object_id.to_s + rand(100).to_s 'd' + Digest::MD5.hexdigest(s) end |
.data_summary(json) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/droid/utils.rb', line 90 def self.data_summary(json) return '-> (empty)' if json.empty? summary = json.map do |k,v| v = v.to_s v = v[0..37] + '...' if v.size > 40 "#{k}=#{v}" end.join(', ') "-> #{summary}" end |
.extract_custom_headers(hash, opts = {}, popts = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/droid/utils.rb', line 33 def self.extract_custom_headers(hash, opts={}, popts={}) popts[:headers] ||= {} headers = popts[:headers] headers[:published_on] ||= hash.delete('published_on') || opts[:published_on] || Time.now.getgm.to_i headers[:ttl] ||= hash.delete('ttl') || (opts[:ttl] || Droid::DEFAULT_TTL).to_i headers[:reply_to] ||= opts[:reply_to] if opts[:reply_to] # this is the event hash that gets transferred through various publish/reply actions headers[:event_hash] ||= hash.delete('event_hash') || opts[:event_hash] || create_event_hash # this value should be unique for each published/received message pair headers[:message_id] ||= create_event_hash # some strange behavior with integers makes it better to # convert all amqp headers to strings to avoid any problems headers.each { |k,v| headers[k] = v.to_s } [hash, headers] end |
.format_data_summary(data, headers) ⇒ Object
100 101 102 |
# File 'lib/droid/utils.rb', line 100 def self.format_data_summary(data, headers) "(data) " + Droid::Utils.data_summary(data) + " (headers) " + Droid::Utils.data_summary(headers) end |
.format_publish(data, opts = {}, popts = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/droid/utils.rb', line 54 def self.format_publish(data, opts={}, popts={}) raise Droid::BadPayload unless data.is_a?(Hash) hash, headers = extract_custom_headers(data, opts, popts) popts[:content_type] ||= 'application/json' [hash.to_json, popts] end |
.generate_name_for_instance(name) ⇒ Object
69 70 71 |
# File 'lib/droid/utils.rb', line 69 def self.generate_name_for_instance(name) "#{name}.#{Socket.gethostname}" end |
.generate_queue(exchange_name, second_name = nil) ⇒ Object
64 65 66 67 |
# File 'lib/droid/utils.rb', line 64 def self.generate_queue(exchange_name, second_name=nil) second_name ||= $$ "#{generate_name_for_instance(exchange_name)}.#{second_name}" end |
.generate_reply_to(name) ⇒ Object
73 74 75 |
# File 'lib/droid/utils.rb', line 73 def self.generate_reply_to(name) "temp.reply.#{name}.#{self.generate_sym}" end |
.generate_sym ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/droid/utils.rb', line 77 def self.generate_sym values = [ rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x1000000), rand(0x1000000), ] "%04x%04x%04x%04x%04x%06x%06x" % values end |
.parse_custom_headers(headers) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/droid/utils.rb', line 15 def self.parse_custom_headers(headers) return { } unless headers h = headers.dup h[:published_on] = h[:published_on].to_i h[:ttl] = h[:ttl].to_i rescue -1 h[:ttl] = -1 if h[:ttl] == 0 h end |
.parse_message(raw) ⇒ Object
10 11 12 13 |
# File 'lib/droid/utils.rb', line 10 def self.(raw) return { } unless raw JSON.parse(raw) end |