class Google::Cloud::PubSub::Schema

# Schema

A schema resource.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

schema = pubsub.schema "my-schema"
schema.name #=> "projects/my-project/schemas/my-schema"
schema.type #=> :PROTOCOL_BUFFER

Attributes

grpc[RW]

@private The gRPC Google::Cloud::PubSub::V1::Schema object.

service[RW]

@private The Service object.

Public Class Methods

from_grpc(grpc, service, view: nil) click to toggle source

@private New Schema from a Google::Cloud::PubSub::V1::Schema object.

# File lib/google/cloud/pubsub/schema.rb, line 280
def self.from_grpc grpc, service, view: nil
  new grpc, service, view: view
end
from_name(name, view, service, options = {}) click to toggle source

@private New reference Schema object without making an HTTP request.

# File lib/google/cloud/pubsub/schema.rb, line 286
def self.from_name name, view, service, options = {}
  grpc = Google::Cloud::PubSub::V1::Schema.new name: service.schema_path(name, options)
  from_grpc grpc, service, view: view
end
new(grpc, service, view: nil) click to toggle source

@private Create a new Schema instance.

# File lib/google/cloud/pubsub/schema.rb, line 47
def initialize grpc, service, view: nil
  @grpc = grpc
  @service = service
  @exists = nil
  @view = view || :FULL
end

Public Instance Methods

definition() click to toggle source

The definition of the schema. This should be a string representing the full definition of the schema that is a valid schema definition of the type specified in {#type}.

@return [String, nil] The schema definition.

# File lib/google/cloud/pubsub/schema.rb, line 82
def definition
  return nil if reference?
  @grpc.definition if @grpc.definition && !@grpc.definition.empty?
end
delete() click to toggle source

Removes the schema, if it exists.

@return [Boolean] Returns `true` if the schema was deleted.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
schema = pubsub.schema "my-schema"

schema.delete
# File lib/google/cloud/pubsub/schema.rb, line 130
def delete
  ensure_service!
  service.delete_schema name
  true
end
exists?() click to toggle source

Determines whether the schema exists in the Pub/Sub service.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

schema = pubsub.schema "my-schema"
schema.exists? #=> true
# File lib/google/cloud/pubsub/schema.rb, line 189
def exists?
  # Always true if the object is not set as reference
  return true unless reference?
  # If we have a value, return it
  return @exists unless @exists.nil?
  ensure_grpc!
  @exists = true
rescue Google::Cloud::NotFoundError
  @exists = false
end
name() click to toggle source

The name of the schema.

@return [String] A fully-qualified schema name in the form `projects/{project_id}/schemas/{schema_id}`.

# File lib/google/cloud/pubsub/schema.rb, line 59
def name
  @grpc.name
end
reference?() click to toggle source

Determines whether the schema object was created without retrieving the resource representation from the Pub/Sub service.

@return [Boolean] `true` when the schema was created without a resource

representation, `false` otherwise.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

schema = pubsub.schema "my-schema", skip_lookup: true
schema.reference? #=> true
# File lib/google/cloud/pubsub/schema.rb, line 215
def reference?
  @grpc.type.nil? || @grpc.type == :TYPE_UNSPECIFIED
end
refresh!(view: nil)
Alias for: reload!
reload!(view: nil) click to toggle source

Reloads the schema with current data from the Pub/Sub service.

@param view [Symbol, String, nil] The set of fields to return in the response. Possible values:

* `BASIC` - Include the `name` and `type` of the schema, but not the `definition`.
* `FULL` - Include all Schema object fields.

Optional. If not provided or `nil`, the last non-nil `view` argument to this method will be used if one has
been given, othewise `FULL` will be used.

@return [Google::Cloud::PubSub::Schema] Returns the reloaded schema.

@example Skip retrieving the schema from the service, then load it:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
schema = pubsub.schema "my-schema", skip_lookup: true

schema.reload!

@example Use the `view` option to load the basic or full resource:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
schema = pubsub.schema "my-schema", view: :basic
schema.resource_partial? #=> true

schema.reload! view: :full
schema.resource_partial? #=> false

@!group Lifecycle

# File lib/google/cloud/pubsub/schema.rb, line 168
def reload! view: nil
  ensure_service!
  @view = view || @view
  @grpc = service.get_schema name, @view
  @reference = nil
  @exists = nil
  self
end
Also aliased as: refresh!
resource?() click to toggle source

Determines whether the schema object was created with a resource representation from the Pub/Sub service.

@return [Boolean] `true` when the schema was created with a resource

representation, `false` otherwise.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

schema = pubsub.schema "my-schema"
schema.resource? #=> true
# File lib/google/cloud/pubsub/schema.rb, line 234
def resource?
  !reference?
end
resource_full?() click to toggle source

Whether the schema was created with a full resource representation from the Pub/Sub service.

@return [Boolean] `true` when the schema was created with a full

resource representation, `false` otherwise.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
schema = pubsub.schema "my-schema"

schema.resource_full? #=> true
# File lib/google/cloud/pubsub/schema.rb, line 274
def resource_full?
  resource? && @grpc.definition && !@grpc.definition.empty?
end
resource_partial?() click to toggle source

Whether the schema was created with a partial resource representation from the Pub/Sub service.

@return [Boolean] `true` when the schema was created with a partial

resource representation, `false` otherwise.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
schema = pubsub.schema "my-schema", view: :basic

schema.resource_partial? #=> true
schema.reload! view: :full # Loads the full resource.
schema.resource_partial? #=> false
# File lib/google/cloud/pubsub/schema.rb, line 255
def resource_partial?
  resource? && !resource_full?
end
type() click to toggle source

The type of the schema. Possible values include:

* `PROTOCOL_BUFFER` - A Protocol Buffer schema definition.
* `AVRO` - An Avro schema definition.

@return [String, nil] The upper-case type name.

# File lib/google/cloud/pubsub/schema.rb, line 71
def type
  return nil if reference?
  @grpc.type
end
validate_message(message_data, message_encoding) click to toggle source

Validates a message against a schema.

@param message_data [String] Message to validate against the provided `schema_spec`. @param message_encoding [Symbol, String] The encoding of the message validated against the schema. Values

include:

* `JSON` - JSON encoding.
* `BINARY` - Binary encoding, as defined by the schema type. For some schema types, binary encoding may not
  be available.

@return [Boolean] Returns `true` if the message validiation succeeds, `false` otherwise.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
schema = pubsub.schema "my-schema"

message_data = { "name" => "Alaska", "post_abbr" => "AK" }.to_json
schema.validate_message message_data, :json
# File lib/google/cloud/pubsub/schema.rb, line 109
def validate_message message_data, message_encoding
  message_encoding = message_encoding.to_s.upcase
  service.validate_message message_data, message_encoding, schema_name: name
  true
rescue Google::Cloud::InvalidArgumentError
  false
end

Protected Instance Methods

ensure_grpc!() click to toggle source

Ensures a Google::Cloud::PubSub::V1::Schema object exists.

# File lib/google/cloud/pubsub/schema.rb, line 301
def ensure_grpc!
  ensure_service!
  reload! if reference?
end
ensure_service!() click to toggle source

@private Raise an error unless an active connection to the service is available.

# File lib/google/cloud/pubsub/schema.rb, line 295
def ensure_service!
  raise "Must have active connection to service" unless service
end