lib/minitel/client.rb in minitel-0.1.1 vs lib/minitel/client.rb in minitel-0.2.0

- old
+ new

@@ -15,50 +15,32 @@ } ) end def notify_app(args) - keywords = [:app_uuid, :body, :title] - app_uuid, body, title = args[:app_uuid], args[:body], args[:title] + StrictArgs.enforce(args, [:app_uuid, :body, :title], :app_uuid) + post_message('app', args[:app_uuid], args[:title], args[:body]) + end - ensure_strict_args(args.keys, keywords) - ensure_no_nils(args, keywords) - ensure_is_uuid(app_uuid) + def notify_user(args) + StrictArgs.enforce(args, [:user_uuid, :body, :title], :user_uuid) + post_message('user', args[:user_uuid], args[:title], args[:body]) + end + private + + def post_message(type, id, title, body) message = { title: title, body: body, - target: {type: 'app', id: app_uuid} + target: {type: type, id: id} } response = connection.post( path: "/producer/messages", body: MultiJson.dump(message), expects: 201) MultiJson.load(response.body) end - - private - - # A Ruby 2.1 required keyword argument sorta backport - def ensure_strict_args(keys, accept_keys) - if keys.sort != accept_keys.sort - delta = accept_keys - keys - raise ArgumentError, "missing or extra keywords: #{delta.join(', ')}" - end - end - - def ensure_no_nils(args, keys) - keys.each do |key| - raise ArgumentError, "keyword #{key} is nil" unless args[key] - end - end - - def ensure_is_uuid(uuid) - unless uuid =~ /\A[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}\z/i - raise ArgumentError, "not formated like a uuid" - end - end - end end