Class: GemMirror::Source

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

Overview

The Source class is used for storing information about an external source such as the name and the Gems to mirror.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, host, gems = []) ⇒ Source

Returns a new instance of Source.

Parameters:

  • name (String)
  • host (String)
  • gems (Array) (defaults to: [])


23
24
25
26
27
# File 'lib/gem_mirror/source.rb', line 23

def initialize(name, host, gems = [])
  @name = name.downcase.gsub(/\s+/, "_")
  @host = host.chomp("/")
  @gems = gems
end

Instance Attribute Details

#gemsArray (readonly)

Returns:

  • (Array)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gem_mirror/source.rb', line 15

class Source
  attr_reader :name, :host, :gems

  ##
  # @param [String] name
  # @param [String] host
  # @param [Array] gems
  #
  def initialize(name, host, gems = [])
    @name = name.downcase.gsub(/\s+/, "_")
    @host = host.chomp("/")
    @gems = gems
  end

  ##
  # Returns a new Source instance based on the current one.
  #
  # @param [Array] new_gems The gems to set, overwrites the current ones.
  # @return [Source]
  #
  def updated(new_gems)
    self.class.new(name, host, new_gems)
  end

  ##
  # Fetches a list of all the available Gems and their versions.
  #
  # @return [String]
  #
  def fetch_versions
    http_get("#{host}/#{Configuration.versions_file}").body
  end

  ##
  # Fetches the Gem specification of a Gem.
  #
  # @param [String] name
  # @param [String] version
  # @return [String]
  #
  def fetch_specification(name, version)
    url = host + "/quick/#{Configuration.marshal_identifier}" \
          "/#{name}-#{version}.gemspec.rz"

    http_get(url).body
  end

  ##
  # Fetches the `.gem` file of a given Gem and version.
  #
  # @param [String] name
  # @param [String] version
  # @return [String]
  #
  def fetch_gem(name, version)
    http_get(host + "/gems/#{name}-#{version}.gem").body
  end

  ##
  # Adds a new Gem to the source.
  #
  # @param [String] name
  # @param [String] requirement
  #
  def gem(name, requirement = nil)
    gems << Gem.new(name, requirement)
  end

  private

  ##
  # Requests the given HTTP resource.
  #
  # @param [String] url
  # @return [HTTP::Message]
  #
  def http_get(url)
    response = client.get(url, follow_redirect: true)

    raise HTTPClient::BadResponseError, response.reason unless HTTP::Status.successful?(response.status)

    response
  end

  ##
  # @return [HTTPClient]
  #
  def client
    @client ||= HTTPClient.new
  end
end

#hostString (readonly)

Returns:

  • (String)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gem_mirror/source.rb', line 15

class Source
  attr_reader :name, :host, :gems

  ##
  # @param [String] name
  # @param [String] host
  # @param [Array] gems
  #
  def initialize(name, host, gems = [])
    @name = name.downcase.gsub(/\s+/, "_")
    @host = host.chomp("/")
    @gems = gems
  end

  ##
  # Returns a new Source instance based on the current one.
  #
  # @param [Array] new_gems The gems to set, overwrites the current ones.
  # @return [Source]
  #
  def updated(new_gems)
    self.class.new(name, host, new_gems)
  end

  ##
  # Fetches a list of all the available Gems and their versions.
  #
  # @return [String]
  #
  def fetch_versions
    http_get("#{host}/#{Configuration.versions_file}").body
  end

  ##
  # Fetches the Gem specification of a Gem.
  #
  # @param [String] name
  # @param [String] version
  # @return [String]
  #
  def fetch_specification(name, version)
    url = host + "/quick/#{Configuration.marshal_identifier}" \
          "/#{name}-#{version}.gemspec.rz"

    http_get(url).body
  end

  ##
  # Fetches the `.gem` file of a given Gem and version.
  #
  # @param [String] name
  # @param [String] version
  # @return [String]
  #
  def fetch_gem(name, version)
    http_get(host + "/gems/#{name}-#{version}.gem").body
  end

  ##
  # Adds a new Gem to the source.
  #
  # @param [String] name
  # @param [String] requirement
  #
  def gem(name, requirement = nil)
    gems << Gem.new(name, requirement)
  end

  private

  ##
  # Requests the given HTTP resource.
  #
  # @param [String] url
  # @return [HTTP::Message]
  #
  def http_get(url)
    response = client.get(url, follow_redirect: true)

    raise HTTPClient::BadResponseError, response.reason unless HTTP::Status.successful?(response.status)

    response
  end

  ##
  # @return [HTTPClient]
  #
  def client
    @client ||= HTTPClient.new
  end
end

#nameString (readonly)

Returns:

  • (String)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gem_mirror/source.rb', line 15

class Source
  attr_reader :name, :host, :gems

  ##
  # @param [String] name
  # @param [String] host
  # @param [Array] gems
  #
  def initialize(name, host, gems = [])
    @name = name.downcase.gsub(/\s+/, "_")
    @host = host.chomp("/")
    @gems = gems
  end

  ##
  # Returns a new Source instance based on the current one.
  #
  # @param [Array] new_gems The gems to set, overwrites the current ones.
  # @return [Source]
  #
  def updated(new_gems)
    self.class.new(name, host, new_gems)
  end

  ##
  # Fetches a list of all the available Gems and their versions.
  #
  # @return [String]
  #
  def fetch_versions
    http_get("#{host}/#{Configuration.versions_file}").body
  end

  ##
  # Fetches the Gem specification of a Gem.
  #
  # @param [String] name
  # @param [String] version
  # @return [String]
  #
  def fetch_specification(name, version)
    url = host + "/quick/#{Configuration.marshal_identifier}" \
          "/#{name}-#{version}.gemspec.rz"

    http_get(url).body
  end

  ##
  # Fetches the `.gem` file of a given Gem and version.
  #
  # @param [String] name
  # @param [String] version
  # @return [String]
  #
  def fetch_gem(name, version)
    http_get(host + "/gems/#{name}-#{version}.gem").body
  end

  ##
  # Adds a new Gem to the source.
  #
  # @param [String] name
  # @param [String] requirement
  #
  def gem(name, requirement = nil)
    gems << Gem.new(name, requirement)
  end

  private

  ##
  # Requests the given HTTP resource.
  #
  # @param [String] url
  # @return [HTTP::Message]
  #
  def http_get(url)
    response = client.get(url, follow_redirect: true)

    raise HTTPClient::BadResponseError, response.reason unless HTTP::Status.successful?(response.status)

    response
  end

  ##
  # @return [HTTPClient]
  #
  def client
    @client ||= HTTPClient.new
  end
end

Instance Method Details

#fetch_gem(name, version) ⇒ String

Fetches the ‘.gem` file of a given Gem and version.

Parameters:

  • name (String)
  • version (String)

Returns:

  • (String)


69
70
71
# File 'lib/gem_mirror/source.rb', line 69

def fetch_gem(name, version)
  http_get(host + "/gems/#{name}-#{version}.gem").body
end

#fetch_specification(name, version) ⇒ String

Fetches the Gem specification of a Gem.

Parameters:

  • name (String)
  • version (String)

Returns:

  • (String)


55
56
57
58
59
60
# File 'lib/gem_mirror/source.rb', line 55

def fetch_specification(name, version)
  url = host + "/quick/#{Configuration.marshal_identifier}" \
        "/#{name}-#{version}.gemspec.rz"

  http_get(url).body
end

#fetch_versionsString

Fetches a list of all the available Gems and their versions.

Returns:

  • (String)


44
45
46
# File 'lib/gem_mirror/source.rb', line 44

def fetch_versions
  http_get("#{host}/#{Configuration.versions_file}").body
end

#gem(name, requirement = nil) ⇒ Object

Adds a new Gem to the source.

Parameters:

  • name (String)
  • requirement (String) (defaults to: nil)


79
80
81
# File 'lib/gem_mirror/source.rb', line 79

def gem(name, requirement = nil)
  gems << Gem.new(name, requirement)
end

#updated(new_gems) ⇒ Source

Returns a new Source instance based on the current one.

Parameters:

  • new_gems (Array)

    The gems to set, overwrites the current ones.

Returns:



35
36
37
# File 'lib/gem_mirror/source.rb', line 35

def updated(new_gems)
  self.class.new(name, host, new_gems)
end