class Gemstash::GemFetcher

Public Class Methods

new(http_client) click to toggle source
# File lib/gemstash/gem_fetcher.rb, line 9
def initialize(http_client)
  @http_client = http_client
  @valid_headers = Set.new(%w[etag content-type content-length last-modified])
end

Public Instance Methods

fetch(gem_id, type) { |body, properties| ... } click to toggle source
# File lib/gemstash/gem_fetcher.rb, line 14
def fetch(gem_id, type, &block)
  @http_client.get(path_for(gem_id, type)) do |body, headers|
    properties = filter_headers(headers)
    validate_download(body, properties)
    yield body, properties
  end
end

Private Instance Methods

content_length(headers) click to toggle source
# File lib/gemstash/gem_fetcher.rb, line 48
def content_length(headers)
  headers["content-length"].to_i
end
filter_headers(headers) click to toggle source
# File lib/gemstash/gem_fetcher.rb, line 35
def filter_headers(headers)
  headers.inject({}) do |properties, (key, value)|
    properties[key.downcase] = value if @valid_headers.include?(key.downcase)
    properties
  end
end
path_for(gem_id, type) click to toggle source
# File lib/gemstash/gem_fetcher.rb, line 24
def path_for(gem_id, type)
  case type
  when :gem
    "gems/#{gem_id}"
  when :spec
    "quick/Marshal.4.8/#{gem_id}"
  else
    raise "Invalid type #{type.inspect}"
  end
end
validate_download(content, headers) click to toggle source
# File lib/gemstash/gem_fetcher.rb, line 42
def validate_download(content, headers)
  expected_size = content_length(headers)
  raise "Incomplete download, only #{body.length} was downloaded out of #{expected_size}" \
    if content.length < expected_size
end