Class: Gemkeeper::GemUploader

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

Overview

Encapsulates Geminabox’s HTTP API so callers never construct multipart requests directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(geminabox_url) ⇒ GemUploader

Returns a new instance of GemUploader.



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

def initialize(geminabox_url)
  @geminabox_url = geminabox_url
end

Instance Attribute Details

#geminabox_urlObject (readonly)

Returns the value of attribute geminabox_url.



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

def geminabox_url
  @geminabox_url
end

Instance Method Details

#list_gemsObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gemkeeper/gem_uploader.rb', line 42

def list_gems
  response = connection.get("/api/v1/gems.json")

  body = response.body
  raise UploadError, "Failed to list gems: #{response.status} #{body}" unless response.success?

  JSON.parse(body)
rescue JSON::ParserError => parse_error
  raise UploadError, "Invalid JSON response: #{parse_error.message}"
rescue Faraday::Error => connection_error
  raise UploadError, "Connection error: #{connection_error.message}"
end

#reachable?Boolean

Returns:

  • (Boolean)


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

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

#upload(gem_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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
  raise ServerNotReachableError,
        "Geminabox server is not reachable at #{@geminabox_url}" \
        "run 'gemkeeper server start' or check 'gemkeeper server status'"
end