Navigation Menu

Skip to content

Commit

Permalink
Fixup some fields before a message is sent
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jan 15, 2015
1 parent 7f66c80 commit 6e216e4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion bin/droonga-request
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
#
# Copyright (C) 2014 Droonga Project
# Copyright (C) 2014-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
Expand All @@ -20,6 +20,7 @@ require "yajl"
require "json"

require "droonga/client"
require "droonga/client/message_perfector"

options = {
:host => "localhost",
Expand Down Expand Up @@ -99,6 +100,8 @@ parser.on("--[no-]report-elapsed-time",
end
request_json_files = parser.parse!(ARGV)

perfector = Droonga::Client::MessagePerfector.new

client = Droonga::Client.new(options)
json_parser = Yajl::Parser.new
json_parser.on_parse_complete = lambda do |request_message|
Expand All @@ -112,6 +115,7 @@ json_parser.on_parse_complete = lambda do |request_message|
message << "\n"
print(message)
end
request_message = perfector.perfect(request_message)
start = Time.now
request = client.request(request_message) do |response|
message = ""
Expand Down
44 changes: 44 additions & 0 deletions lib/droonga/client/message_perfector.rb
@@ -0,0 +1,44 @@
# -*- 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 MessagePerfector
def initialize(options={})
@options = options
@date ||= @options["date"]
end

def perfect(message)
message["id"] ||= generate_id
message["date"] ||= date
message
end

private
def generate_id
"#{Time.now} / #{Random.rand}"
end

def date
@date ||= Time.now.iso8601
end
end
end
end

0 comments on commit 6e216e4

Please sign in to comment.