Class: AvroTurf::SchemaRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/avro_turf/schema_registry.rb

Constant Summary collapse

CONTENT_TYPE =
"application/vnd.schemaregistry.v1+json".freeze

Instance Method Summary collapse

Constructor Details

#initialize(url, logger: Logger.new($stdout)) ⇒ SchemaRegistry

Returns a new instance of SchemaRegistry.



6
7
8
9
10
11
# File 'lib/avro_turf/schema_registry.rb', line 6

def initialize(url, logger: Logger.new($stdout))
  @logger = logger
  @connection = Excon.new(url, headers: {
    "Content-Type" => CONTENT_TYPE,
  })
end

Instance Method Details

#check(subject, schema) ⇒ Object

Check if a schema exists. Returns nil if not found.



47
48
49
50
51
52
# File 'lib/avro_turf/schema_registry.rb', line 47

def check(subject, schema)
  data = post("/subjects/#{subject}",
              expects: [200, 404],
              body: { schema: schema.to_s }.to_json)
  data unless data.has_key?("error_code")
end

#compatible?(subject, schema, version = 'latest') ⇒ Boolean

Check if a schema is compatible with the stored version. Returns true if compatible, false otherwise docs.confluent.io/2.0.0/schema-registry/docs/api.html#compatibility

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/avro_turf/schema_registry.rb', line 57

def compatible?(subject, schema, version = 'latest')
  data = post("/compatibility/subjects/#{subject}/versions/#{version}",
              expects: [200, 404],
              body: { schema: schema.to_s }.to_json)
  data.fetch('is_compatible', false) unless data.has_key?('error_code')
end

#fetch(id) ⇒ Object



13
14
15
16
17
# File 'lib/avro_turf/schema_registry.rb', line 13

def fetch(id)
  @logger.info "Fetching schema with id #{id}"
  data = get("/schemas/ids/#{id}")
  data.fetch("schema")
end

#register(subject, schema) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/avro_turf/schema_registry.rb', line 19

def register(subject, schema)
  data = post("/subjects/#{subject}/versions", body: {
    schema: schema.to_s
  }.to_json)

  id = data.fetch("id")

  @logger.info "Registered schema for subject `#{subject}`; id = #{id}"

  id
end

#subject_version(subject, version = 'latest') ⇒ Object

Get a specific version for a subject



42
43
44
# File 'lib/avro_turf/schema_registry.rb', line 42

def subject_version(subject, version = 'latest')
  get("/subjects/#{subject}/versions/#{version}")
end

#subject_versions(subject) ⇒ Object

List all versions for a subject



37
38
39
# File 'lib/avro_turf/schema_registry.rb', line 37

def subject_versions(subject)
  get("/subjects/#{subject}/versions")
end

#subjectsObject

List all subjects



32
33
34
# File 'lib/avro_turf/schema_registry.rb', line 32

def subjects
  get('/subjects')
end