Class: NchanTools::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/nchan_tools/pubsub.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, last_modified = nil, etag = nil) ⇒ Message

Returns a new instance of Message.



52
53
54
55
56
# File 'lib/nchan_tools/pubsub.rb', line 52

def initialize(msg, last_modified=nil, etag=nil)
  @times_seen=1
  @message, @last_modified, @etag = msg, last_modified, etag
  @idhist = []
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



51
52
53
# File 'lib/nchan_tools/pubsub.rb', line 51

def content_type
  @content_type
end

#etagObject

Returns the value of attribute etag.



51
52
53
# File 'lib/nchan_tools/pubsub.rb', line 51

def etag
  @etag
end

#eventsource_eventObject

Returns the value of attribute eventsource_event.



51
52
53
# File 'lib/nchan_tools/pubsub.rb', line 51

def eventsource_event
  @eventsource_event
end

#last_modifiedObject

Returns the value of attribute last_modified.



51
52
53
# File 'lib/nchan_tools/pubsub.rb', line 51

def last_modified
  @last_modified
end

#messageObject

Returns the value of attribute message.



51
52
53
# File 'lib/nchan_tools/pubsub.rb', line 51

def message
  @message
end

#times_seenObject

Returns the value of attribute times_seen.



51
52
53
# File 'lib/nchan_tools/pubsub.rb', line 51

def times_seen
  @times_seen
end

Class Method Details

.each_multipart_message(content_type, body) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nchan_tools/pubsub.rb', line 91

def self.each_multipart_message(content_type, body)
  content_type = content_type.last if Array === content_type 
  matches=/^multipart\/mixed; boundary=(?<boundary>.*)/.match content_type
  
  if matches
    splat = body.split(/^--#{Regexp.escape matches[:boundary]}-?-?\r?\n?/)
    splat.shift
    
    splat.each do |v|
      mm=(/(Content-Type:\s(?<content_type>.*?)\r\n)?\r\n(?<body>.*)\r\n/m).match v
      yield mm[:content_type], mm[:body], true
    end
    
  else
    yield content_type, body
  end
end

Instance Method Details

#==(msg) ⇒ Object



87
88
89
# File 'lib/nchan_tools/pubsub.rb', line 87

def ==(msg)
  @message == (msg.respond_to?(:message) ? msg.message : msg)
end

#idObject



69
70
71
# File 'lib/nchan_tools/pubsub.rb', line 69

def id
  @id||=serverside_id
end

#id=(val) ⇒ Object



66
67
68
# File 'lib/nchan_tools/pubsub.rb', line 66

def id=(val)
  @id=val.dup
end

#lengthObject



84
85
86
# File 'lib/nchan_tools/pubsub.rb', line 84

def length
  self.to_s.length
end

#serverside_idObject



57
58
59
60
61
62
63
64
65
# File 'lib/nchan_tools/pubsub.rb', line 57

def serverside_id
  timestamp=nil
  if last_modified
    timestamp = DateTime.httpdate(last_modified).to_time.utc.to_i
  end
  if last_modified || etag
    "#{timestamp}:#{etag}"
  end
end

#to_sObject



81
82
83
# File 'lib/nchan_tools/pubsub.rb', line 81

def to_s
  @message
end

#unique_idObject



72
73
74
75
76
77
78
79
80
# File 'lib/nchan_tools/pubsub.rb', line 72

def unique_id
  if id && id.include?(",")
    time, etag = id.split ":"
    etag = etag.split(",").map{|x| x[0] == "[" ? x : "?"}.join "," #]
    [time, etag].join ":"
  else
    id
  end
end