lib/mxit_api/client.rb in mxit-rails-0.4.0 vs lib/mxit_api/client.rb in mxit-rails-0.4.1
- old
+ new
@@ -196,11 +196,15 @@
end
handle_response(response)
end
- def batch_notify_users(mxit_ids, message, contains_markup)
+ # When the Mxit API returns 400::Bad Request due to a non-existent Mxit ID in the batch the rest
+ # of the IDs still receive the message.
+ def batch_notify_users(mxit_ids, message, contains_markup, options={ spool: true,
+ spool_timeout: 60*60*24*7 })
+
Rails.logger.info('Requesting MXit API auth...')
request_app_auth(["message/send"])
Rails.logger.info('Finished MXit API auth.')
batch_size = 50
@@ -209,11 +213,17 @@
while i < mxit_ids.count
current_batch = mxit_ids[i, batch_size]
i += current_batch.count
to = current_batch.join(',')
- send_message(@app_name, to, message, contains_markup)
+ begin
+ send_message(@app_name, to, message, contains_markup, options)
+ rescue Exception => exception
+ Rails.logger.error("The following batch caused an exception during send message:" +
+ "\n\t#{to}")
+ Rails.logger.error(exception.message)
+ end
Rails.logger.info("Total users notified: " + i.to_s)
end
Rails.logger.info('Finished notifying!')
end
@@ -257,24 +267,24 @@
def handle_response(response)
begin
data = JSON.parse(response.body)
rescue JSON::ParserError
- data = {}
+ body = response.body
+ # Unescape unicode characters.
+ body = body.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
+
+ data = { "error" => "Error", "error_description" => body }
end
case response
when Net::HTTPSuccess then
data
- when Net::HTTPBadRequest, Net::HTTPUnauthorized, Net::HTTPForbidden then
+ else
error_message = "#{response.code}::#{response.message}"
error_message += " - #{data["error"]}: #{data["error_description"]}" if not data.empty?
raise MxitApi::RequestException.new(error_message, response.code), error_message
-
- else
- raise MxitApi::RequestException.new(response.message, response.code),
- "#{response.code}::#{response.message}"
end
end
end