Cloud Pub/Sub API - Class Google::Cloud::PubSub::Schema (v2.18.1)

Reference documentation and code samples for the Cloud Pub/Sub API class Google::Cloud::PubSub::Schema.

Schema

A schema resource.

Inherits

  • Object

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

Methods

#definition

def definition() -> String, nil

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.

Returns
  • (String, nil) — The schema definition.

#delete

def delete() -> Boolean

Removes the schema, if it exists.

Returns
  • (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

#exists?

def exists?() -> Boolean

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

Returns
  • (Boolean)
Example
require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

schema = pubsub.schema "my-schema"
schema.exists? #=> true

#name

def name() -> String

The name of the schema.

Returns
  • (String) — A fully-qualified schema name in the form projects/{project_id}/schemas/{schema_id}.

#reference?

def reference?() -> Boolean

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

Returns
  • (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

#refresh!

def refresh!(view: nil) -> Google::Cloud::PubSub::Schema
Alias Of: #reload!

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

Parameter
  • view (Symbol, String, nil) (defaults to: 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.

Returns
Examples

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!

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

#reload!

def reload!(view: nil) -> Google::Cloud::PubSub::Schema
Aliases

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

Parameter
  • view (Symbol, String, nil) (defaults to: 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.

Returns
Examples

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!

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

#resource?

def resource?() -> Boolean

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

Returns
  • (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

#resource_full?

def resource_full?() -> Boolean

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

Returns
  • (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

#resource_partial?

def resource_partial?() -> Boolean

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

Returns
  • (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

#type

def type() -> String, nil

The type of the schema. Possible values include:

  • PROTOCOL_BUFFER - A Protocol Buffer schema definition.
  • AVRO - An Avro schema definition.
Returns
  • (String, nil) — The upper-case type name.

#validate_message

def validate_message(message_data, message_encoding) -> Boolean

Validates a message against a schema.

Parameters
  • message_data (String) — Message to validate against the provided schema_spec.
  • 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.
Returns
  • (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