Module: TwirpRails

Defined in:
lib/twirp_rails.rb,
lib/twirp_rails/twirp.rb,
lib/twirp_rails/engine.rb,
lib/twirp_rails/routes.rb,
lib/twirp_rails/version.rb,
lib/twirp_rails/rspec/helper.rb,
lib/twirp_rails/raven_adapter.rb,
lib/twirp_rails/log_subscriber.rb,
lib/twirp_rails/logging_adapter.rb,
lib/twirp_rails/error_handling/base.rb,
lib/twirp_rails/active_record_extension.rb,
lib/twirp_rails/error_handling/error_handling_factory.rb

Defined Under Namespace

Modules: ActiveRecordExtension, ErrorHandling, LoggingAdapter, RSpec, RavenAdapter, Routes, Twirp Classes: Configuration, Error, ErrorHandlingFactory, LogSubscriber

Constant Summary collapse

VERSION =
'0.4.9'.freeze

Class Method Summary collapse

Class Method Details

.client(klass, url) ⇒ Object



137
138
139
140
141
# File 'lib/twirp_rails.rb', line 137

def self.client(klass, url)
  client = klass.new(url)

  TwirpRails::ErrorHandling.wrap_client(client)
end

.configurationObject



69
70
71
# File 'lib/twirp_rails.rb', line 69

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



73
74
75
76
# File 'lib/twirp_rails.rb', line 73

def self.configure
  yield configuration if block_given?
  setup
end

.handle_dev_error(msg, &_) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



88
89
90
91
92
93
# 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

.setupObject



78
79
80
81
82
83
84
85
86
# 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) ⇒ Google::Protobuf::Timestamp

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

Parameters:

  • time (Time)

    The Time to be converted.

Returns:

  • (Google::Protobuf::Timestamp)

    The converted Google::Protobuf::Timestamp.



133
134
135
# 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) ⇒ Object

Convert google.protobuf.Timestamp or Google::Protobuf::Timestampp hash to [Time]

Parameters:

  • proto_timestamp (Hash|Google::Protobuf::Timestamp)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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