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
@private The gRPC Google::Cloud::PubSub::V1::Schema object.
@private The Service
object.
Public Class Methods
@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
@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
@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
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
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
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
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
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
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
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
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
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
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
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
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
@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