Module: GetUrl::Sources
Overview
The Sources represents the external, read-only sources.
Instance Method Summary collapse
-
#get_sources ⇒ Hash
Returns all the sources as a two dimensional Hash, with the id as key, and futher information as a child Hash.
-
#register_source(id, url, options = {}) ⇒ Integer
Registers the given sources.
-
#reload(id = nil, options = {}) ⇒ Object
Reloads the yaml file from the source, and caches it locally.
-
#unregister_source(id) ⇒ Integer
Unregisters the source with the given id.
Instance Method Details
#get_sources ⇒ Hash
Returns all the sources as a two dimensional Hash, with the id as key, and futher information as a child Hash.
18 19 20 21 |
# File 'lib/geturl/geturl-sources.rb', line 18 def get_sources data = File.read(@source_file) YAML.load(data).to_h rescue {} end |
#register_source(id, url, options = {}) ⇒ Integer
Registers the given sources. If the id already exists, it will be overwritten without warning.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/geturl/geturl-sources.rb', line 29 def register_source(id, url, = {}) id.downcase! raise ArgumentError.new('The source id \'local\' is reserved.') if (id == 'local') sources = get_sources || {} sources[id] = { 'url' => url, 'cert' => ['cert'], 'key' => ['key'], } File.write(@source_file, sources.to_yaml) FileManager.clear_all_items_cache return sources.size end |
#reload(id = nil, options = {}) ⇒ Object
Reloads the yaml file from the source, and caches it locally. If the id is nil, all sources will be reloaded.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/geturl/geturl-sources.rb', line 61 def reload(id = nil, = {}) if id.nil? get_sources.keys.each {|i| reload(i, ) unless i.nil? } else begin source_data = get_sources[id] print "Loading #{id} ...\r" if ([:verbose]) if source_data.include?('cert') && source_data.include?('key') curl_command = "curl -s #{source_data['url']} -E #{source_data['cert']} --key #{source_data['key']}" data = `#{curl_command}` else data = open(source_data['url']).read end raise Exception.new('File is empty') if data.empty? file = FileManager.get_local_source_file(id) FileManager.clean_and_save_items_to_yaml_file(data, file) puts "Loaded #{id}" + "".ljust(5) if ([:verbose]) rescue Exception => e puts "Failed to load #{id}: #{e.full_message}" if ([:verbose]) end end end |
#unregister_source(id) ⇒ Integer
Unregisters the source with the given id. It also removes the locally cached file.
47 48 49 50 51 52 53 54 |
# File 'lib/geturl/geturl-sources.rb', line 47 def unregister_source(id) sources = get_sources || {} sources.delete(id) File.write(@source_file, sources.to_yaml) File.unlink(FileManager.get_local_source_file(id)) rescue nil FileManager.clear_all_items_cache return sources.size end |