Class: Gigawatt::Cache

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

Instance Method Summary collapse

Constructor Details

#initialize(settings, access_key) ⇒ Cache

Returns a new instance of Cache.



3
4
5
6
# File 'lib/gigawatt/cache.rb', line 3

def initialize(settings, access_key)
  @access_key = access_key
  @settings = settings
end

Instance Method Details

#companies(indexed = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gigawatt/cache.rb', line 32

def companies(indexed = false)
  if @settings.companies.nil?
    @settings.companies = fetch_companies
    @settings.write(:companies)
  end
  return @settings.companies unless indexed
  companies = {}
  @settings.companies.each do |c|
    companies[c["uuid"]] = c
  end
  companies
end

#fetch_companiesObject



8
9
10
11
# File 'lib/gigawatt/cache.rb', line 8

def fetch_companies
  companies = JSON.parse(@access_key.get('/api/1/companies.json').body)
  companies["response"]
end

#fetch_projectsObject



13
14
15
16
# File 'lib/gigawatt/cache.rb', line 13

def fetch_projects
  projects = JSON.parse(@access_key.get("/api/1/projects.json?where=#{URI::encode('active="true"')}").body)
  projects["response"]
end

#fetch_staffObject



18
19
20
21
# File 'lib/gigawatt/cache.rb', line 18

def fetch_staff
  staff = JSON.parse(@access_key.get("/api/1/staff.json").body)
  staff["response"]
end

#projects(indexed = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gigawatt/cache.rb', line 45

def projects(indexed = false)
  if @settings.projects.nil?
    @settings.projects = fetch_projects
    @settings.write(:projects)
  end
  return @settings.projects unless indexed
  projects = {}
  @settings.projects.each do |p|
    projects[p["uuid"]] = p
  end
  projects
end

#refresh!Object



23
24
25
26
27
28
29
30
# File 'lib/gigawatt/cache.rb', line 23

def refresh!
  @settings.companies = nil
  @settings.projects = nil
  @settings.staff = nil
  companies
  projects
  staff
end

#staff(indexed = false) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gigawatt/cache.rb', line 58

def staff(indexed = false)
  if @settings.staff.nil?
    @settings.staff = fetch_staff
    @settings.write(:staff)
  end
  return @settings.staff unless indexed
  staff = {}
  @settings.staff.each do |s|
    staff[s["uuid"]] = s
  end
  staff
end