class Snowly::SchemaCache
Constants
- SNOWPLOW_IGLU_RESOLVER
Public Instance Methods
[](location)
click to toggle source
Provides easy access to the schema cache based on its registered key @param location [String] Location provided in the schema @return [String] Json for schema
# File lib/snowly/schema_cache.rb, line 13 def [](location) @@schema_cache[location] || save_in_cache(location) end
cache()
click to toggle source
Accessor to the global cache
# File lib/snowly/schema_cache.rb, line 23 def cache @@schema_cache end
reset_cache()
click to toggle source
Resets the schema cache
# File lib/snowly/schema_cache.rb, line 18 def reset_cache @@schema_cache = {} end
Private Instance Methods
external?(location)
click to toggle source
# File lib/snowly/schema_cache.rb, line 29 def external?(location) location.match(/^(http|https):\/\//) end
resolve(location, resolver)
click to toggle source
Translate an iglu address to an actual local or remote location @param location [String] @param resolver [String] local or remote path to look for the schema @return [String] Schema's actual location
# File lib/snowly/schema_cache.rb, line 37 def resolve(location, resolver) path = location.sub(/^iglu\:/, '') File.join resolver, path end
save_in_cache(location)
click to toggle source
Caches the schema content under its original location name @param location [String] @return [String] schema content
# File lib/snowly/schema_cache.rb, line 45 def save_in_cache(location) content = begin full_path = resolve(location, (Snowly.development_iglu_resolver_path || SNOWPLOW_IGLU_RESOLVER) ) external?(full_path) ? Net::HTTP.get(URI(full_path)) : File.read(full_path) rescue Snowly.logger.warn "Could't locate #{location} in development resolver. Attemping IgluCentral Server..." full_path = resolve(location, SNOWPLOW_IGLU_RESOLVER) begin result = Net::HTTP.get(URI(full_path)) JSON.load(result) && result rescue Snowly.logger.error "#{location} schema is not available in any resolver" return nil end end @@schema_cache[location] = content end