module TwirpRails

Constants

VERSION

Public Class Methods

client(klass, url) click to toggle source
# File lib/twirp_rails.rb, line 137
def self.client(klass, url)
  client = klass.new(url)

  TwirpRails::ErrorHandling.wrap_client(client)
end
configuration() click to toggle source
# File lib/twirp_rails.rb, line 69
def self.configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/twirp_rails.rb, line 73
def self.configure
  yield configuration if block_given?
  setup
end
handle_dev_error(msg) { || ... } click to toggle source
# File lib/twirp_rails.rb, line 95
def self.handle_dev_error(msg, &_)
  if Rails.env.development? && !ENV['TWIRP_RAILS_RAISE']
    begin
      yield
    rescue StandardError => e
      warn("#{msg} #{e.message}")
      warn('twirp_rails error raised but control flow will resume for development environment. Define env TWIRP_RAILS_RAISE to raise error.')
    end
  else
    yield
  end
end
log_twirp_calls!(&log_writer) click to toggle source
# File lib/twirp_rails.rb, line 88
def self.log_twirp_calls!(&log_writer)
  TwirpRails::LoggingAdapter.install

  TwirpRails::LogSubscriber.log_writer = log_writer if block_given?
  TwirpRails::LogSubscriber.attach_to(:twirp)
end
setup() click to toggle source
# File lib/twirp_rails.rb, line 78
def self.setup
  if configuration.log_twirp_calls
    if configuration.log_twirp_calls.is_a?(Proc)
      log_twirp_calls!(&configuration.log_twirp_calls)
    else
      log_twirp_calls!
    end
  end
end
time_to_timestamp(time) click to toggle source

Utility for converting a Ruby Time instance to a Google::Protobuf::Timestamp.

@param time [Time] The Time to be converted.

@return [Google::Protobuf::Timestamp] The converted

Google::Protobuf::Timestamp.
# File lib/twirp_rails.rb, line 133
def self.time_to_timestamp(time)
  Google::Protobuf::Timestamp.new(seconds: time.to_i, nanos: time.nsec)
end
timestamp_to_time(proto_timestamp) click to toggle source

Convert google.protobuf.Timestamp or Google::Protobuf::Timestampp hash to [Time] @param [Hash|Google::Protobuf::Timestamp] proto_timestamp

# File lib/twirp_rails.rb, line 111
def self.timestamp_to_time(proto_timestamp)
  return nil unless proto_timestamp

  proto_timestamp = proto_timestamp.to_h unless proto_timestamp.is_a?(Hash)

  seconds = proto_timestamp[:seconds]
  raise "invalid timestamp #{proto_timestamp.inspect}" unless seconds

  nanos = proto_timestamp[:nanos]

  seconds += nanos * 1e-9 unless nanos.nil? || nanos.zero?

  Time.zone.at seconds
end