class AndSon::CallRunner

Constants

DEFAULT_TIMEOUT

Attributes

after_call_procs[R]
before_call_procs[R]
host[R]
logger_value[RW]
params_value[RW]
port[R]
timeout_value[RW]

Public Class Methods

new(host, port) click to toggle source
# File lib/and-son/call_runner.rb, line 17
def initialize(host, port)
  @host = host
  @port = port
  @params_value  = {}
  @timeout_value = (ENV['ANDSON_TIMEOUT'] || DEFAULT_TIMEOUT).to_f
  @logger_value  = NullLogger.new
  @before_call_procs = []
  @after_call_procs  = []
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/and-son/call_runner.rb, line 69
def ==(other)
  other.kind_of?(self.class) ? self.hash == other.hash : super
end
Also aliased as: eql?
call(name, params = nil) { |protocol_response| ... } click to toggle source
# File lib/and-son/call_runner.rb, line 30
def call(name, params = nil)
  params ||= {}
  if !params.kind_of?(Hash)
    raise ArgumentError, "expected params to be a Hash instead of a #{params.class}"
  end
  call_params = self.params_value.merge(params)

  self.before_call_procs.each{ |p| p.call(name, call_params, self) }
  client_response = nil
  benchmark = Benchmark.measure do
    client_response = call!(name, call_params)
  end
  self.after_call_procs.each{ |p| p.call(name, call_params, self) }

  summary_line = SummaryLine.new({
    'time'    => RoundedTime.new(benchmark.real),
    'status'  => client_response.protocol_response.code,
    'host'    => "#{self.host}:#{self.port}",
    'service' => name,
    'params'  => params
  })
  self.logger_value.info("[AndSon] #{summary_line}")

  if block_given?
    yield client_response.protocol_response
  else
    client_response.data
  end
end
call_runner() click to toggle source

chain runner methods by returning itself

# File lib/and-son/call_runner.rb, line 28
def call_runner; self; end
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/and-son/call_runner.rb, line 60
def hash
  [ self.host,
    self.port,
    self.timeout_value,
    self.params_value,
    self.logger_value
  ].hash
end

Private Instance Methods

call!(name, params) click to toggle source
# File lib/and-son/call_runner.rb, line 76
def call!(name, params)
  AndSon::Connection.new(host, port).open do |connection|
    connection.write(Sanford::Protocol::Request.new(name, params).to_hash)
    connection.close_write
    if !connection.peek(timeout_value).empty?
      AndSon::Response.parse(connection.read(timeout_value))
    else
      raise AndSon::ConnectionClosedError.new
    end
  end
end