Class: Gem::Resolver::APISet

Inherits:
Set
  • Object
show all
Defined in:
lib/rubygems/resolver/api_set.rb

Overview

The global rubygems pool, available via the rubygems.org API. Returns instances of APISpecification.

Defined Under Namespace

Classes: GemParser

Instance Attribute Summary collapse

Attributes inherited from Set

#errors, #prerelease, #remote

Instance Method Summary collapse

Methods inherited from Set

#remote?

Constructor Details

#initialize(dep_uri = "https://index.rubygems.org/info/") ⇒ APISet

Creates a new APISet that will retrieve gems from uri using the RubyGems API URL dep_uri which is described at guides.rubygems.org/rubygems-org-api



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubygems/resolver/api_set.rb', line 30

def initialize(dep_uri = "https://index.rubygems.org/info/")
  super()

  dep_uri = Gem::URI dep_uri unless Gem::URI === dep_uri

  @dep_uri = dep_uri
  @uri     = dep_uri + ".."

  @data   = Hash.new {|h,k| h[k] = [] }
  @source = Gem::Source.new @uri

  @to_fetch = []
end

Instance Attribute Details

#dep_uriObject (readonly)

The URI for the dependency API this APISet uses.



13
14
15
# File 'lib/rubygems/resolver/api_set.rb', line 13

def dep_uri
  @dep_uri
end

#sourceObject (readonly)

The Gem::Source that gems are fetched from



18
19
20
# File 'lib/rubygems/resolver/api_set.rb', line 18

def source
  @source
end

#uriObject (readonly)

The corresponding place to fetch gems.



23
24
25
# File 'lib/rubygems/resolver/api_set.rb', line 23

def uri
  @uri
end

Instance Method Details

#find_all(req) ⇒ Object

Return an array of APISpecification objects matching DependencyRequest req.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubygems/resolver/api_set.rb', line 48

def find_all(req)
  res = []

  return res unless @remote

  if @to_fetch.include?(req.name)
    prefetch_now
  end

  versions(req.name).each do |ver|
    if req.dependency.match? req.name, ver[:number], @prerelease
      res << Gem::Resolver::APISpecification.new(self, ver)
    end
  end

  res
end

#prefetch(reqs) ⇒ Object

A hint run by the resolver to allow the Set to fetch data for DependencyRequests reqs.



70
71
72
73
74
75
76
# File 'lib/rubygems/resolver/api_set.rb', line 70

def prefetch(reqs)
  return unless @remote
  names = reqs.map {|r| r.dependency.name }
  needed = names - @data.keys - @to_fetch

  @to_fetch += needed
end

#prefetch_nowObject

:nodoc:



78
79
80
81
82
83
84
85
# File 'lib/rubygems/resolver/api_set.rb', line 78

def prefetch_now # :nodoc:
  needed = @to_fetch
  @to_fetch = []

  needed.sort.each do |name|
    versions(name)
  end
end

#pretty_print(q) ⇒ Object

:nodoc:



87
88
89
90
91
92
93
94
95
96
# File 'lib/rubygems/resolver/api_set.rb', line 87

def pretty_print(q) # :nodoc:
  q.group 2, "[APISet", "]" do
    q.breakable
    q.text "URI: #{@dep_uri}"

    q.breakable
    q.text "gem names:"
    q.pp @data.keys
  end
end

#versions(name) ⇒ Object

Return data for all versions of the gem name.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rubygems/resolver/api_set.rb', line 101

def versions(name) # :nodoc:
  if @data.key?(name)
    return @data[name]
  end

  uri = @dep_uri + name

  begin
    str = Gem::RemoteFetcher.fetcher.fetch_path uri
  rescue Gem::RemoteFetcher::FetchError
    @data[name] = []
  else
    lines(str).each do |ver|
      number, platform, dependencies, requirements = parse_gem(ver)

      platform ||= "ruby"
      dependencies = dependencies.map {|dep_name, reqs| [dep_name, reqs.join(", ")] }
      requirements = requirements.map {|req_name, reqs| [req_name.to_sym, reqs] }.to_h

      @data[name] << { name: name, number: number, platform: platform, dependencies: dependencies, requirements: requirements }
    end
  end

  @data[name]
end