class Icalia::Event::Publisher

Attributes

client[R]
logger[R]

Public Class Methods

instance() click to toggle source
# File lib/icalia/event/publisher.rb, line 22
def instance
  @instance ||= new
end
new() click to toggle source
# File lib/icalia/event/publisher.rb, line 33
def initialize
  initialize_pubsub_client
  initialize_logger
end

Public Instance Methods

publish(topic_name, data, attributes = {}) click to toggle source
# File lib/icalia/event/publisher.rb, line 38
def publish(topic_name, data, attributes = {})
  logger.debug "Publishing to '#{topic_name}': #{data.inspect}"
  get_or_create_topic(topic_name).publish message_pack(data), attributes
end

Protected Instance Methods

get_or_create_topic(topic_name) click to toggle source
# File lib/icalia/event/publisher.rb, line 45
def get_or_create_topic(topic_name)
  client.topic(topic_name) || client.new_topic(topic_name)
end

Private Instance Methods

initialize_logger() click to toggle source
# File lib/icalia/event/publisher.rb, line 69
def initialize_logger
  return use_rails_logger if rails_logger_exist?
  use_default_logger
end
initialize_pubsub_client() click to toggle source

See googleapis.dev/ruby/google-cloud-pubsub/latest/file.AUTHENTICATION.html#Environment_Variables

The environment variables that google-cloud-pubsub checks for project ID are:

  1. PUBSUB_PROJECT

  2. GOOGLE_CLOUD_PROJECT

The environment variables that google-cloud-pubsub checks for credentials are configured on Google::Cloud::PubSub::V1::Credentials:

  1. PUBSUB_CREDENTIALS - Path to JSON file, or JSON contents

  2. PUBSUB_KEYFILE - Path to JSON file, or JSON contents

  3. GOOGLE_CLOUD_CREDENTIALS - Path to JSON file, or JSON contents

  4. GOOGLE_CLOUD_KEYFILE - Path to JSON file, or JSON contents

  5. GOOGLE_APPLICATION_CREDENTIALS - Path to JSON file

# File lib/icalia/event/publisher.rb, line 65
def initialize_pubsub_client
  @client = Google::Cloud::Pubsub.new
end
rails_logger_exist?() click to toggle source
# File lib/icalia/event/publisher.rb, line 74
def rails_logger_exist?
  defined?(Rails) && Rails.logger.present?
end
use_default_logger() click to toggle source
# File lib/icalia/event/publisher.rb, line 83
def use_default_logger
  @logger = ActiveSupport::Logger.new(STDOUT)
  GRPCStdoutLogger.send :extend, GRPCStdoutLogger
end
use_rails_logger() click to toggle source
# File lib/icalia/event/publisher.rb, line 78
def use_rails_logger
  @logger = Rails.logger
  GRPCRailsLogger.send :extend, GRPCRailsLogger
end