Module: GetUrl::Sources

Extended by:
Sources
Included in:
Sources
Defined in:
lib/geturl/geturl-sources.rb

Overview

The Sources represents the external, read-only sources.

Instance Method Summary collapse

Instance Method Details

#get_sourcesHash

Returns all the sources as a two dimensional Hash, with the id as key, and futher information as a child Hash.

Returns:

  • (Hash)

    The sources.



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.

Parameters:

  • id (String)

    The id of the source.

  • url (String)

    The url of the source.

  • options (Hash) (defaults to: {})

    The options.

Returns:

  • (Integer)

    The size of the sources.

Raises:

  • (ArgumentError)


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, options = {})
  id.downcase!
  raise ArgumentError.new('The source id \'local\' is reserved.') if (id == 'local')
  sources = get_sources || {}
  sources[id] = {
      'url' => url,
      'cert' => options['cert'],
      'key' => options['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.

Parameters:

  • id (Object) (defaults to: nil)

    The id of the source to reload.

  • options (Object) (defaults to: {})

    The options.



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, options = {})
  if id.nil?
    get_sources.keys.each {|i|
      reload(i, options) unless i.nil?
    }
  else
    begin
      source_data = get_sources[id]
      print "Loading #{id} ...\r" if (options[: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 (options[:verbose])
    rescue Exception => e
      puts "Failed to load #{id}: #{e.full_message}" if (options[:verbose])
    end
  end
end

#unregister_source(id) ⇒ Integer

Unregisters the source with the given id. It also removes the locally cached file.

Parameters:

  • id (String)

    The id of the source.

Returns:

  • (Integer)

    The size of the sources.



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