Class: GemStats::GemInfo

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

Constant Summary collapse

API_URL =
"https://rubygems.org/api/v1/gems/"
REQUEST_FORMAT =
'json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ GemInfo

Returns a new instance of GemInfo.



11
12
13
# File 'lib/gem_stats/gem_info.rb', line 11

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/gem_stats/gem_info.rb', line 9

def name
  @name
end

Instance Method Details

#clients(env = 'runtime') ⇒ Object



29
30
31
# File 'lib/gem_stats/gem_info.rb', line 29

def clients(env = 'runtime')
  Gem.where("dependencies.#{env}.name" => @name).all
end

#dependencies(env = 'runtime') ⇒ Object



24
25
26
27
# File 'lib/gem_stats/gem_info.rb', line 24

def dependencies(env = 'runtime')
  @dependencies ||= {}
  @dependencies[env.to_s] ||= info['dependencies'][env.to_s].inject([]) { |list, gem| list << gem['name'] }
end

#infoObject



15
16
17
18
19
20
21
22
# File 'lib/gem_stats/gem_info.rb', line 15

def info
  return @info if defined?(@info)
  begin
    @info ||= JSON.load open("#{API_URL}#{@name}.#{REQUEST_FORMAT}").read
  rescue OpenURI::HTTPError => e
    @info = nil
  end
end

#saveObject



33
34
35
36
37
38
39
40
41
# File 'lib/gem_stats/gem_info.rb', line 33

def save
  gem = Gem.find_by_name(@name) || Gem.new
  if info
    gem.attributes = info
    gem.save
  else
    false
  end
end