Class: ClientSqueak

Inherits:
Object
  • Object
show all
Includes:
XmlHelpers
Defined in:
lib/mapsqueak.rb

Overview

Note that if I am not the owner of a squeak, I will not know the user

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from XmlHelpers

#xml_element

Constructor Details

#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

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def created_at
  @created_at
end

#durationObject

Returns the value of attribute duration.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def duration
  @duration
end

#expiresObject

Returns the value of attribute expires.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def expires
  @expires
end

#gmapsObject

Returns the value of attribute gmaps.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def gmaps
  @gmaps
end

#has_imageObject

Returns the value of attribute has_image.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def has_image
  @has_image
end

#idObject

Returns the value of attribute id.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def id
  @id
end

#latitudeObject

Returns the value of attribute latitude.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def longitude
  @longitude
end

#textObject Also known as: description

Returns the value of attribute text.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def text
  @text
end

#time_utcObject

Returns the value of attribute time_utc.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def time_utc
  @time_utc
end

#updated_atObject

Returns the value of attribute updated_at.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def updated_at
  @updated_at
end

#user_emailObject

Returns the value of attribute user_email.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def user_email
  @user_email
end

#usernameObject

Returns the value of attribute username.



185
186
187
# File 'lib/mapsqueak.rb', line 185

def username
  @username
end

Instance Method Details

#merge(other) ⇒ Object

merge another Squeak’s parameters by letting the other’s parameters overwrite the current Squeak’s parms. Returns a copy Hash



280
281
282
# File 'lib/mapsqueak.rb', line 280

def merge(other)
  self.to_hash.merge!(other)
end

#merge!(other) ⇒ Object

merge another Squeak’s parameters by letting the other’s parameters overwrite the current Squeak’s parms. Modifies the current Squeak



286
287
288
289
290
291
292
# File 'lib/mapsqueak.rb', line 286

def merge!(other)
  t = other.to_hash
  t.keys.each do | k |
    # this will set and validate the parameters
    self.send("#{k.to_s.gsub('-','_')}=",t[k])
  end
end

#to_hashObject

convert the squeak to a Hash



267
268
269
270
271
272
273
274
275
276
# File 'lib/mapsqueak.rb', line 267

def to_hash
  {
    :squeak=> {
	:latitude => self.latitude.to_s,
	:longitude => self.longitude.to_s,
	:duration => self.duration.to_s,
	:text => self.text
    }
  }
end

#to_jsonObject

convert the squeak to JSON format

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


262
263
264
# File 'lib/mapsqueak.rb', line 262

def to_json
  self.to_hash.to_json
end

#to_sObject

simply use to_s on the Hash representation



329
330
331
# File 'lib/mapsqueak.rb', line 329

def to_s
  self.to_hash.to_s
end

#to_xmlObject

convert the squeak to XML format

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


250
251
252
253
254
255
256
257
258
# File 'lib/mapsqueak.rb', line 250

def to_xml
  params_xml = Document.new
  params_xml.add_element('squeak')
  params_xml.root << xml_element('latitude',self.latitude.to_s)
  params_xml.root << xml_element('longitude',self.longitude.to_s)
  params_xml.root << xml_element('text',self.text)
  params_xml.root << xml_element('duration',self.duration.to_s)
  params_xml
end