YUKI Hiroshi
null+****@clear*****
Thu Jan 15 14:32:49 JST 2015
YUKI Hiroshi 2015-01-15 14:32:49 +0900 (Thu, 15 Jan 2015) New Revision: bfc34e4698c28f1fdd557a9c5bd64d57152a32a2 https://github.com/droonga/droonga-client-ruby/commit/bfc34e4698c28f1fdd557a9c5bd64d57152a32a2 Message: Validate message before sent Added files: lib/droonga/client/message_validator.rb Modified files: bin/droonga-request Modified: bin/droonga-request (+2 -0) =================================================================== --- bin/droonga-request 2015-01-15 14:32:12 +0900 (f8a3134) +++ bin/droonga-request 2015-01-15 14:32:49 +0900 (269db84) @@ -101,6 +101,7 @@ end request_json_files = parser.parse!(ARGV) perfector = Droonga::Client::MessagePerfector.new +validator = Droonga::Client::MessageValidator.new client = Droonga::Client.new(options) json_parser = Yajl::Parser.new @@ -116,6 +117,7 @@ json_parser.on_parse_complete = lambda do |request_message| print(message) end request_message = perfector.perfect(request_message) + validator.validate(request_message) start = Time.now request = client.request(request_message) do |response| message = "" Added: lib/droonga/client/message_validator.rb (+62 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/client/message_validator.rb 2015-01-15 14:32:49 +0900 (0cfcfce) @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2015 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +require "time" + +module Droonga + class Client + class MessageValidator + class MissingId < ArgumentError + end + + class MissingDataset < ArgumentError + end + + class InvalidDate < ArgumentError + end + + def initialize(options={}) + @options = options + end + + def validate(message) + validate_id(message) + validate_dataset(message) + validate_date(message) + end + + private + def validate_id(message) + unless message["id"] + raise MissingId.new(message["id"]) + end + end + + def validate_dataset(message) + unless message["dataset"] + raise MissingDataset.new(message["dataset"]) + end + end + + def validate_date(message) + Time.parse(message["date"]) + rescue ArgumentError => error + raise InvalidDate.new(message["date"]) + end + end + end +end -------------- next part -------------- HTML����������������������������...Download