Module: Bugzilla::Utils

Defined in:
lib/bugzilla/utils.rb

Instance Method Summary collapse

Instance Method Details

#get_proxy(info) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/bugzilla/utils.rb', line 3

def get_proxy(info)
  uri = info[:Proxy] || ENV['http_proxy']
  proxy_uri = uri.nil? ? nil : URI.parse(uri)
  proxy_host = proxy_uri.nil? ? nil : proxy_uri.host
  proxy_port = proxy_uri.nil? ? nil : proxy_uri.port
  [proxy_host, proxy_port]
end

#get_xmlrpc(conf = {}, opts = {}) {|host| ... } ⇒ Object

Yields:

  • (host)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bugzilla/utils.rb', line 11

def get_xmlrpc(conf = {}, opts = {})
  info = conf
  uri = URI.parse(info[:URL])
  host = uri.host
  port = uri.port
  path = uri.path.empty? ? nil : uri.path
  proxy_host, proxy_port = get_proxy(info)
  timeout = opts[:timeout].nil? ? 60 : opts[:timeout]
  yield host if block_given? # if you want to run some pre hook
  xmlrpc = XMLRPC.new(host, port: port, path: path, proxy_host:
                                proxy_host, proxy_port: proxy_port, timeout:
                                timeout, http_basic_auth_user: uri.user,
                            http_basic_auth_pass: uri.password, debug: opts[:debug])
  [xmlrpc, host]
end

#read_config(opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bugzilla/utils.rb', line 26

def read_config(opts)
  fname = opts[:config].nil? ? @defaultyamlfile : opts[:config]
  begin
    # TODO: fix config file
    # Psych doesnt allow Symbol as class
    # conf = YAML.safe_load(File.open(fname).read)
    conf = YAML.load(File.open(fname).read)
  rescue Errno::ENOENT
    conf = {}
  end
  conf.each do |_k, v|
    load(v[:Plugin]) if v.is_a?(Hash) && v.include?(:Plugin)
  end
  conf
end

#save_config(opts, conf) ⇒ Object

def read_config



42
43
44
45
46
47
48
49
50
51
# File 'lib/bugzilla/utils.rb', line 42

def save_config(opts, conf)
  fname = opts[:config].nil? ? @defaultyamlfile : opts[:config]
  if File.exist?(fname)
    st = File.lstat(fname)
    if st.mode & 0o600 != 0o600
      raise format('The permissions of %s has to be 0600', fname)
    end
  end
  File.open(fname, 'w') { |f| f.chmod(0o600); f.write(conf.to_yaml) }
end