Class: Google::Cloud::PubSub::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/pubsub/schema.rb,
lib/google/cloud/pubsub/schema/list.rb

Overview

Schema

A schema resource.

Examples:

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

Defined Under Namespace

Classes: List

Lifecycle collapse

Instance Method Summary collapse

Instance Method Details

#definitionString?

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.



82
83
84
85
# File 'lib/google/cloud/pubsub/schema.rb', line 82

def definition
  return nil if reference?
  @grpc.definition if @grpc.definition && !@grpc.definition.empty?
end

#deleteBoolean

Removes the schema, if it exists.

Examples:

require "google/cloud/pubsub"

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

schema.delete

Returns:

  • (Boolean)

    Returns true if the schema was deleted.



130
131
132
133
134
# File 'lib/google/cloud/pubsub/schema.rb', line 130

def delete
  ensure_service!
  service.delete_schema name
  true
end

#exists?Boolean

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

Examples:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

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

Returns:

  • (Boolean)


189
190
191
192
193
194
195
196
197
198
# 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

#nameString

The name of the schema.

Returns:

  • (String)

    A fully-qualified schema name in the form projects/{project_id}/schemas/{schema_id}.



59
60
61
# File 'lib/google/cloud/pubsub/schema.rb', line 59

def name
  @grpc.name
end

#reference?Boolean

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

Examples:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

schema = pubsub.schema "my-schema", skip_lookup: true
schema.reference? #=> true

Returns:

  • (Boolean)

    true when the schema was created without a resource representation, false otherwise.



215
216
217
# File 'lib/google/cloud/pubsub/schema.rb', line 215

def reference?
  @grpc.type.nil? || @grpc.type == :TYPE_UNSPECIFIED
end

#reload!(view: nil) ⇒ Google::Cloud::PubSub::Schema Also known as: refresh!

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

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

Parameters:

  • 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:



168
169
170
171
172
173
174
175
# 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

#resource?Boolean

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

Examples:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

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

Returns:

  • (Boolean)

    true when the schema was created with a resource representation, false otherwise.



234
235
236
# File 'lib/google/cloud/pubsub/schema.rb', line 234

def resource?
  !reference?
end

#resource_full?Boolean

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

Examples:

require "google/cloud/pubsub"

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

schema.resource_full? #=> true

Returns:

  • (Boolean)

    true when the schema was created with a full resource representation, false otherwise.



274
275
276
# File 'lib/google/cloud/pubsub/schema.rb', line 274

def resource_full?
  resource? && @grpc.definition && !@grpc.definition.empty?
end

#resource_partial?Boolean

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

Examples:

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

Returns:

  • (Boolean)

    true when the schema was created with a partial resource representation, false otherwise.



255
256
257
# File 'lib/google/cloud/pubsub/schema.rb', line 255

def resource_partial?
  resource? && !resource_full?
end

#typeString?

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.



71
72
73
74
# File 'lib/google/cloud/pubsub/schema.rb', line 71

def type
  return nil if reference?
  @grpc.type
end

#validate_message(message_data, message_encoding) ⇒ Boolean

Validates a message against a schema.

Examples:

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

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.



109
110
111
112
113
114
115
# 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