Class: Iglu::Resolver
- Inherits:
-
Object
- Object
- Iglu::Resolver
- Defined in:
- lib/iglu-client/resolver.rb
Overview
Iglu Client. Able to fetch schemas only from Iglu Central
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#cacheTtl ⇒ Object
readonly
Returns the value of attribute cacheTtl.
-
#registries ⇒ Object
readonly
Returns the value of attribute registries.
Class Method Summary collapse
-
.btoi(b) ⇒ Object
Convert boolean to int.
- .get_data(json) ⇒ Object
- .get_schema_key(json) ⇒ Object
- .parse(json) ⇒ Object
- .parse_registry(config) ⇒ Object
Instance Method Summary collapse
-
#initialize(registries, cacheTtl = nil) ⇒ Resolver
constructor
A new instance of Resolver.
-
#lookup_schema(schema_key) ⇒ Object
Lookup schema in cache or try to fetch.
- #prioritize_repos(schema_key, repository_refs) ⇒ Object
-
#validate(json) ⇒ Object
Return true or throw exception.
Constructor Details
#initialize(registries, cacheTtl = nil) ⇒ Resolver
Returns a new instance of Resolver.
21 22 23 24 25 |
# File 'lib/iglu-client/resolver.rb', line 21 def initialize(registries, cacheTtl=nil) @registries = registries.unshift(Registries.bootstrap) @cache = Hash.new @cacheTtl = cacheTtl end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
19 20 21 |
# File 'lib/iglu-client/resolver.rb', line 19 def cache @cache end |
#cacheTtl ⇒ Object (readonly)
Returns the value of attribute cacheTtl.
19 20 21 |
# File 'lib/iglu-client/resolver.rb', line 19 def cacheTtl @cacheTtl end |
#registries ⇒ Object (readonly)
Returns the value of attribute registries.
19 20 21 |
# File 'lib/iglu-client/resolver.rb', line 19 def registries @registries end |
Class Method Details
.btoi(b) ⇒ Object
Convert boolean to int
133 134 135 |
# File 'lib/iglu-client/resolver.rb', line 133 def self.btoi(b) if b then 1 else 0 end end |
.get_data(json) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/iglu-client/resolver.rb', line 109 def self.get_data(json) data = json[:data] || json["data"] if data.nil? raise IgluError.new "JSON instance is not self-describing (data proprty is absent):\n #{json.to_json}" else data end end |
.get_schema_key(json) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/iglu-client/resolver.rb', line 100 def self.get_schema_key(json) schema_uri = json[:schema] || json["schema"] if schema_uri.nil? raise IgluError.new "JSON instance is not self-describing (schema property is absent):\n #{json.to_json}" else SchemaKey.parse_key schema_uri end end |
.parse(json) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/iglu-client/resolver.rb', line 76 def self.parse(json) schema_key = Resolver.get_schema_key(json) schema = Registries.bootstrap.lookup_schema(schema_key) data = get_data(json) if JSON::Validator.validate!(schema, data) registries = data[:repositories].map do |registry| parse_registry(registry) end cacheTtl = json[:data][:cacheTtl] Resolver.new(registries, cacheTtl) else throw IgluError.new "Invalid resolver configuration" end end |
.parse_registry(config) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/iglu-client/resolver.rb', line 89 def self.parse_registry(config) ref_config = Registries::RegistryRefConfig.parse(config) if not config[:connection][:embedded].nil? Registries::EmbeddedRegistryRef.new(ref_config, config[:connection][:embedded][:path]) elsif not config[:connection][:http].nil? Registries::HttpRegistryRef.new(ref_config, config[:connection][:http][:uri]) else raise IgluError.new "Incorrect RegistryRef" end end |
Instance Method Details
#lookup_schema(schema_key) ⇒ Object
Lookup schema in cache or try to fetch
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/iglu-client/resolver.rb', line 28 def lookup_schema(schema_key) lookup_time = Time.now.getutc if schema_key.is_a?(String) schema_key = SchemaKey.parse_key(schema_key) end failures = [] cache_result = @cache[schema_key] if not cache_result.nil? if not @cacheTtl.nil? store_time = cache_result[1] time_diff = (lookup_time - store_time).round if time_diff >= @cacheTtl @cache.delete(schema_key) cache_result = nil else return cache_result[0] end else return cache_result[0] end end if cache_result.nil? # Fetch from every registry for registry in prioritize_repos(schema_key, @registries) do begin lookup_result = registry.lookup_schema(schema_key) rescue StandardError => e failures.push(Registries::LookupFailure.new(registry.config.name, e)) else if lookup_result.nil? failures.push(Registries::NotFound.new(registry.config.name)) else break end end end if lookup_result.nil? raise Registries::ResolverError.new(failures, schema_key) else store_time = Time.now.getutc @cache[schema_key] = [lookup_result, store_time] lookup_result end end end |
#prioritize_repos(schema_key, repository_refs) ⇒ Object
126 127 128 129 130 |
# File 'lib/iglu-client/resolver.rb', line 126 def prioritize_repos(schema_key, repository_refs) repository_refs.sort_by do |ref| [Resolver.btoi(!ref.vendor_matched(schema_key)), ref.class_priority, ref.config.priority] end end |
#validate(json) ⇒ Object
Return true or throw exception
119 120 121 122 123 124 |
# File 'lib/iglu-client/resolver.rb', line 119 def validate(json) schema_key = Resolver.get_schema_key json data = Resolver.get_data json schema = lookup_schema schema_key JSON::Validator.validate!(schema, data) end |