class Gemstash::GemPusher

Class that supports pushing a new gem to the private repository of gems.

Public Class Methods

new(auth, content) click to toggle source
# File lib/gemstash/gem_pusher.rb, line 26
def initialize(auth, content)
  @auth = auth
  @content = content
end
serve(app) click to toggle source
# File lib/gemstash/gem_pusher.rb, line 21
def self.serve(app)
  gem = app.request.body.read
  new(app.auth, gem).serve
end

Public Instance Methods

serve() click to toggle source
# File lib/gemstash/gem_pusher.rb, line 31
def serve
  check_auth
  store_gem
  store_gemspec
  save_to_database
  invalidate_cache
end

Private Instance Methods

check_auth() click to toggle source
# File lib/gemstash/gem_pusher.rb, line 53
def check_auth
  @auth.check("push")
end
full_name() click to toggle source
# File lib/gemstash/gem_pusher.rb, line 49
def full_name
  @full_name ||= gem.spec.full_name
end
gem() click to toggle source
# File lib/gemstash/gem_pusher.rb, line 41
def gem
  @gem ||= Gem::Package.new(StringIO.new(@content))
end
invalidate_cache() click to toggle source
# File lib/gemstash/gem_pusher.rb, line 87
def invalidate_cache
  gemstash_env.cache.invalidate_gem("private", gem.spec.name)
end
save_to_database() click to toggle source
# File lib/gemstash/gem_pusher.rb, line 73
def save_to_database
  spec = gem.spec

  gemstash_env.db.transaction do
    gem_id = Gemstash::DB::Rubygem.find_or_insert(spec)
    existing = Gemstash::DB::Version.find_by_spec(gem_id, spec)
    raise ExistingVersionError, "Cannot push to an existing version!" if existing && existing.indexed
    raise YankedVersionError, "Cannot push to a yanked version!" if existing && !existing.indexed

    version_id = Gemstash::DB::Version.insert_by_spec(gem_id, spec)
    Gemstash::DB::Dependency.insert_by_spec(version_id, spec)
  end
end
storage() click to toggle source
# File lib/gemstash/gem_pusher.rb, line 45
def storage
  @storage ||= Gemstash::Storage.for("private").for("gems")
end
store_gem() click to toggle source
# File lib/gemstash/gem_pusher.rb, line 57
def store_gem
  resource_exist = storage.resource(full_name).exist?
  resource_is_indexed = storage.resource(full_name).properties[:indexed] if resource_exist

  raise ExistingVersionError, "Cannot push to an existing version!" if resource_exist && resource_is_indexed

  storage.resource(full_name).save({ gem: @content }, indexed: true)
end
store_gemspec() click to toggle source
# File lib/gemstash/gem_pusher.rb, line 66
def store_gemspec
  spec = gem.spec
  spec = Marshal.dump(spec)
  spec = Zlib::Deflate.deflate(spec)
  storage.resource(full_name).save(spec: spec)
end