Class: Pls::Pls

Inherits:
Object
  • Object
show all
Defined in:
lib/pls/pls.rb

Overview

Data structure is hash, key - package name, val - array of hashes:

aaa => [
         bbb => [],
         ccc => [
                  ddd => [],
                  eee => []
                ]
       ]

Instance Method Summary collapse

Constructor Details

#initializePls

Returns a new instance of Pls.



23
24
25
26
27
28
# File 'lib/pls/pls.rb', line 23

def initialize
  @cfg = Configurator.new
  @rep = Reporter.new
  @dat = {}
  @mut_dat = Mutex.new
end

Instance Method Details

#build(pac) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pls/pls.rb', line 63

def build(pac)
  arr = read_cache(pac)
  if arr.to_a.empty?
    str = read_http(pac)
    doc = JSON.parse(str)
    dep = doc['dependencies']
    arr = dep.to_a.empty? ? [] : build_dep(dep)
    write_cache(pac, arr)
  end
  { pac => arr }
end

#build_dep(dep) ⇒ Object

rubocop:disable Metrics/MethodLength



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pls/pls.rb', line 38

def build_dep(dep) # rubocop:disable Metrics/MethodLength
  arr = []
  threads = []
  mut = Mutex.new
  dep.each_key do |pac|
    threads << Thread.new(pac, arr) do |p, a|
      dat = build(p)
      mut.synchronize { a << dat }
    end
  end
  threads.each(&:join)
  arr
end

#doObject



75
76
77
# File 'lib/pls/pls.rb', line 75

def do
  @rep.do(build(@cfg.pac))
end

#read_cache(pac) ⇒ Object

Consider time validation for cache data.



53
54
55
56
57
# File 'lib/pls/pls.rb', line 53

def read_cache(pac)
  arr = []
  @mut_dat.synchronize { arr = @dat[pac] }
  arr
end

#read_http(pac) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/pls/pls.rb', line 30

def read_http(pac)
  url = "#{@cfg.url}/#{pac}/latest"
  res = HTTParty.get(url)
  raise "Unable to continue with #{url}." unless res.code == 200

  res.body
end

#write_cache(pac, arr) ⇒ Object



59
60
61
# File 'lib/pls/pls.rb', line 59

def write_cache(pac, arr)
  @mut_dat.synchronize { @dat[pac] = arr }
end