Class: AndSon::CallRunner

Inherits:
OpenStruct
  • Object
show all
Includes:
CallRunnerMethods
Defined in:
lib/and-son/client.rb

Instance Method Summary collapse

Methods included from CallRunnerMethods

#logger, #params, #timeout

Instance Method Details

#call(name, params = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/and-son/client.rb', line 76

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
  client_response = nil
  benchmark = Benchmark.measure do
    client_response = self.responses.find(name, params) if ENV['ANDSON_TEST_MODE']
    client_response ||= self.call!(name, params)
  end

  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!(name, params) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/and-son/client.rb', line 103

def call!(name, params)
  call_params = self.params_value.merge(params)
  AndSon::Connection.new(host, port).open do |connection|
    connection.write(Sanford::Protocol::Request.new(name, call_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

#call_runnerObject

chain runner methods by returning itself



74
# File 'lib/and-son/client.rb', line 74

def call_runner; self; end