Method: ClientSqueak#initialize

Defined in:
lib/mapsqueak.rb

#initialize(squeak) ⇒ ClientSqueak

Initialize a new squeak which must be in an allowable format Must set: latitude, longitude, text, and duration json - a String representation of a JSON object

e.g. {\"squeak\":{\"latitude\":\"54\",\"longitude\":\"-1.5\",\"duration\":\"2\",\"text\":\"Another squeak!\"}}

xml - a String representation of an XML blob

e.g. <squeak><latitude>54</latitude><longitude>-1.5</longitude><text>Another squeak!</text><duration>2</duration></squeak>

hash - a hash representation of a squeak

e.g. {:squeak => {:latitude=> 54, :longitude=>-1.69, :text => "Another squeak!", :duration => 2}}
OR if you are lazy (like me) you can just specify the inner hash:
e.g. {:latitude=> 54, :longitude=>-1.69, :text => "Another squeak!", :duration => 2}


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/mapsqueak.rb', line 198

def initialize(squeak)
  @original_squeak = squeak
  temp_hash = {:squeak => {}}
  case squeak
  when Hash
    # just to be nice, we won't force the user to specify the outer wrapping
    if squeak.has_key? 'squeak'
	temp_hash = squeak.dup
    else
	temp_hash = {:squeak => squeak}
    end
    
  when String 
    begin
	temp_hash = JSON.parse(squeak)
    rescue Exception => e
	begin
 doc = Document.new(squeak)
 # a nice flexible way to parse the XML. as long as the XML 
 # is only one level deep
 doc.elements.each('squeak/') do | el |
   el.elements.each do |child|
     temp_hash[:squeak][child.name.to_sym] = child.text
   end
 end
	rescue Exception => e2
 raise "Can't parse squeak text: #{squeak}\n" + e2.backtrace.join("\n")
	end
    end
  end
  
  t = temp_hash['squeak'] || temp_hash[:squeak]
  self.merge!(t)

  # but we need to make sure that all of the mandatory parameters are defined
  unless (%w[latitude longitude text duration] - self.to_hash[:squeak].keys.map {|k| k.to_s}).empty?
    raise "Must have duration, lat and long and text"
  end
end