Class: Gemkeeper::GemUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/gemkeeper/gem_uploader.rb

Overview

Encapsulates the Gemkeeper server’s upload HTTP API so callers never construct multipart requests directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url) ⇒ GemUploader

Returns a new instance of GemUploader.



11
12
13
# File 'lib/gemkeeper/gem_uploader.rb', line 11

def initialize(server_url)
  @server_url = server_url
end

Instance Attribute Details

#server_urlObject (readonly)

Returns the value of attribute server_url.



9
10
11
# File 'lib/gemkeeper/gem_uploader.rb', line 9

def server_url
  @server_url
end

Instance Method Details

#list_gemsObject

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/gemkeeper/gem_uploader.rb', line 53

def list_gems
  raise NotImplementedError, "list_gems is not supported by CompactIndexServer; use gemkeeper list instead"
end

#reachable?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/gemkeeper/gem_uploader.rb', line 46

def reachable?
  connection.get("/")
  true
rescue Faraday::ConnectionFailed, Faraday::TimeoutError
  false
end

#serves?(name, version) ⇒ Boolean

True when the running server’s private store already serves name@version. Hits the private-store endpoint, never /info, so a public gem can’t fool it.

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/gemkeeper/gem_uploader.rb', line 35

def serves?(name, version)
  status = connection.get("/gemkeeper/has/#{name}/#{version}").status
  case status
  when 200 then true
  when 404 then false
  else raise ServerError, "Unexpected status #{status} checking #{name} #{version} on #{@server_url}"
  end
rescue Faraday::ConnectionFailed, Faraday::TimeoutError
  not_reachable!
end

#upload(gem_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gemkeeper/gem_uploader.rb', line 15

def upload(gem_path)
  raise UploadError, "Gem file not found: #{gem_path}" unless File.exist?(gem_path)

  response = connection.post("/upload") do |req|
    req.body = {
      file: Faraday::Multipart::FilePart.new(
        gem_path,
        "application/octet-stream",
        File.basename(gem_path)
      )
    }
  end

  handle_response(response, gem_path)
rescue Faraday::ConnectionFailed, Faraday::TimeoutError
  not_reachable!
end