lib/flapjack/gateways/jsonapi.rb in flapjack-0.8.4 vs lib/flapjack/gateways/jsonapi.rb in flapjack-0.8.5
- old
+ new
@@ -25,19 +25,26 @@
class JSONAPI < Sinatra::Base
include Flapjack::Utility
- JSON_REQUEST_MIME_TYPES = ['application/vnd.api+json', 'application/json']
+ JSON_REQUEST_MIME_TYPES = ['application/vnd.api+json', 'application/json', 'application/json-patch+json']
class ContactNotFound < RuntimeError
attr_reader :contact_id
def initialize(contact_id)
@contact_id = contact_id
end
end
+ class ContactsNotFound < RuntimeError
+ attr_reader :contact_ids
+ def initialize(contact_ids)
+ @contact_ids = contact_ids
+ end
+ end
+
class NotificationRuleNotFound < RuntimeError
attr_reader :rule_id
def initialize(rule_id)
@rule_id = rule_id
end
@@ -104,10 +111,12 @@
:query_string => env['QUERY_STRING']
}
case e
when Flapjack::Gateways::JSONAPI::ContactNotFound
rescue_error.call(404, e, request_info, "could not find contact '#{e.contact_id}'")
+ when Flapjack::Gateways::JSONAPI::ContactsNotFound
+ rescue_error.call(404, e, request_info, "could not find contacts '" + e.contact_ids.join(', ') + "'")
when Flapjack::Gateways::JSONAPI::NotificationRuleNotFound
rescue_error.call(404, e, request_info,"could not find notification rule '#{e.rule_id}'")
when Flapjack::Gateways::JSONAPI::EntityNotFound
rescue_error.call(404, e, request_info, "could not find entity '#{e.entity}'")
when Flapjack::Gateways::JSONAPI::EntityCheckNotFound
@@ -185,14 +194,22 @@
query_string = (request.query_string.respond_to?(:length) &&
request.query_string.length > 0) ? "?#{request.query_string}" : ""
if logger.debug?
logger.debug("Returning #{response.status} for #{request.request_method} " +
- "#{request.path_info}#{query_string}, body: #{response.body.join(', ')}")
+ "#{request.path_info}#{query_string}, body: #{response.body}")
elsif logger.info?
logger.info("Returning #{response.status} for #{request.request_method} " +
"#{request.path_info}#{query_string}")
end
+ end
+
+ def is_json_request?
+ Flapjack::Gateways::JSONAPI::JSON_REQUEST_MIME_TYPES.include?(request.content_type.split(/\s*[;,]\s*/, 2).first.downcase)
+ end
+
+ def is_jsonpatch_request?
+ 'application/json-patch+json'.eql?(request.content_type.split(/\s*[;,]\s*/, 2).first.downcase)
end
register Flapjack::Gateways::JSONAPI::EntityMethods
register Flapjack::Gateways::JSONAPI::ContactMethods