Navigation Menu

Skip to content

Commit

Permalink
Validate message before sent
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jan 15, 2015
1 parent 6e216e4 commit bfc34e4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bin/droonga-request
Expand Up @@ -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
Expand All @@ -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 = ""
Expand Down
62 changes: 62 additions & 0 deletions lib/droonga/client/message_validator.rb
@@ -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

0 comments on commit bfc34e4

Please sign in to comment.