Class: DependencyTimelineAudit::API

Inherits:
Object
  • Object
show all
Defined in:
lib/dependency-timeline-audit/api.rb

Constant Summary collapse

API_URL =
'https://rubygems.org/api/v1/versions/'
CACHE_DIRECTORY =
"#{Dir.pwd}/.dependency-timeline-audit/cache/ruby/"
EXCEPTIONS_DIRECTORY =
"#{Dir.pwd}/.dependency-timeline-audit/exceptions/ruby/"
@@gem_cache =
{}

Class Method Summary collapse

Class Method Details

.cache_outdated?(gem_name) ⇒ Boolean

TODO: Implement cache_outdated? method using config.cache_expires_after

Returns:

  • (Boolean)


24
25
26
# File 'lib/dependency-timeline-audit/api.rb', line 24

def self.cache_outdated?(gem_name)
  false
end

.cached?(gem_name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/dependency-timeline-audit/api.rb', line 19

def self.cached?(gem_name)
  File.exist?(gem_cache_file(gem_name))
end

.fetch_gem_info(gem_name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/dependency-timeline-audit/api.rb', line 11

def self.fetch_gem_info(gem_name)
  if !cached?(gem_name) || cache_outdated?(gem_name)
    update_cache(gem_name)
  end

  gem_cache(gem_name)
end

.gem_cache(gem_name) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/dependency-timeline-audit/api.rb', line 49

def self.gem_cache(gem_name)
  if @@gem_cache[gem_name].nil? && cached?(gem_name)
    @@gem_cache[gem_name] = GemCache.new(JSON.parse(File.read(gem_cache_file(gem_name))))
  end

  @@gem_cache[gem_name]
end

.gem_cache_file(gem_name) ⇒ Object



28
29
30
# File 'lib/dependency-timeline-audit/api.rb', line 28

def self.gem_cache_file(gem_name)
  File.join(CACHE_DIRECTORY, "#{gem_name}.json")
end

.rubygems_api_response(gem_name) ⇒ Object



44
45
46
47
# File 'lib/dependency-timeline-audit/api.rb', line 44

def self.rubygems_api_response(gem_name)
  url = URI("#{API_URL}#{gem_name}.json")
  Net::HTTP.get(url)
end

.update_cache(gem_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dependency-timeline-audit/api.rb', line 32

def self.update_cache(gem_name)
  response = rubygems_api_response(gem_name)
  gem_cache = GemCache.new(JSON.parse(response))

  FileUtils.mkdir_p(CACHE_DIRECTORY) unless File.directory?(CACHE_DIRECTORY)
  File.open(gem_cache_file(gem_name), 'w') do |file|
    file.write(JSON.pretty_generate(gem_cache.to_h))
  end

  gem_cache(gem_name)
end