lib/flapjack/gateways/jsonapi/medium_methods.rb in flapjack-1.1.0 vs lib/flapjack/gateways/jsonapi/medium_methods.rb in flapjack-1.2.0rc1
- old
+ new
@@ -40,12 +40,12 @@
media_ids.split(',').uniq.collect do |m_id|
m_id =~ /\A(.+)_(#{known_media_identifiers})\z/
contact_id = $1
media_type = $2
- halt err(422, "Could not get contact_id from media_id") if contact_id.nil?
- halt err(422, "Could not get media type from media_id") if media_type.nil?
+ halt err(422, "Could not get contact_id from media_id '#{m_id}'") if contact_id.nil?
+ halt err(422, "Could not get media type from media_id '#{m_id}'") if media_type.nil?
contact_cache[contact_id] ||= find_contact(contact_id)
{:contact => contact_cache[contact_id], :type => media_type}
end
@@ -63,19 +63,21 @@
if media_data.nil? || !media_data.is_a?(Enumerable)
halt err(422, "No valid media were submitted")
end
- unless media_data.all? {|m| m['id'].nil? }
- halt err(422, "Media creation cannot include IDs")
+ media_id_re = /^#{params[:contact_id]}_(?:#{Flapjack::Data::Contact::ALL_MEDIA.join('|')}$)/
+
+ unless media_data.all? {|m| m['id'].nil? || media_id_re === m['id'] }
+ halt err(422, "Media creation cannot include non-conformant IDs")
end
semaphore = obtain_semaphore(SEMAPHORE_CONTACT_MASS_UPDATE)
contact = Flapjack::Data::Contact.find_by_id(params[:contact_id], :redis => redis)
if contact.nil?
semaphore.release
- halt err(422, "Contact id:'#{params[:contact_id]}' could not be loaded")
+ halt err(422, "Contact id: '#{params[:contact_id]}' could not be loaded")
end
media_data.each do |medium_data|
type = medium_data['type']
contact.set_address_for_media(type, medium_data['address'])
@@ -88,11 +90,11 @@
media_ids = media_data.collect {|md| md['id']}
status 201
response.headers['Location'] = "#{base_url}/media/#{media_ids.join(',')}"
- media_ids.to_json
+ Flapjack.dump_json(media_ids)
end
# get one or more media records; media ids are, for Flapjack
# v1, composed of "#{contact.id}_#{media_type}"
app.get %r{^/media(?:/)?([^/]+)?$} do
@@ -113,22 +115,24 @@
media_type = contact_media_type[:type]
media_list_cache[contact.id] ||= contact.media_list
if media_list_cache[contact.id].include?(media_type)
medium_id = "#{contact.id}_#{media_type}"
+ int = contact.media_intervals[media_type]
+ rut = contact.media_rollup_thresholds[media_type]
memo <<
{:id => medium_id,
:type => media_type,
:address => contact.media[media_type],
- :interval => contact.media_intervals[media_type],
- :rollup_threshold => contact.media_rollup_thresholds[media_type],
+ :interval => int.nil? ? nil : int.to_i,
+ :rollup_threshold => rut.nil? ? nil : rut.to_i,
:links => {:contacts => [contact.id]}}
end
memo
end
- '{"media":' + media_data.to_json + '}'
+ '{"media":' + Flapjack.dump_json(media_data) + '}'
end
# update one or more media records; media ids are, for Flapjack
# v1, composed of "#{contact.id}_#{media_type}"
app.patch '/media/:id' do