Skip to content

Commit

Permalink
Use config_for for rubygems config
Browse files Browse the repository at this point in the history
  • Loading branch information
spk committed Jan 19, 2015
1 parent 7de0f88 commit 06f4a5f
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 32 deletions.
3 changes: 2 additions & 1 deletion app/jobs/indexer.rb
Expand Up @@ -23,7 +23,8 @@ def write_gem(body, spec)
end

def directory
fog.directories.get($rubygems_config[:s3_bucket]) || fog.directories.create(:key => $rubygems_config[:s3_bucket])
fog.directories.get(Gemcutter.config['s3_bucket']) ||
fog.directories.create(key: Gemcutter.config['s3_bucket'])
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/mailer.rb
@@ -1,5 +1,5 @@
class Mailer < ActionMailer::Base
default_url_options[:host] = HOST
default_url_options[:host] = Gemcutter::HOST

def email_reset(user)
@user = user
Expand Down
4 changes: 2 additions & 2 deletions app/middleware/hostess.rb
Expand Up @@ -20,13 +20,13 @@ def serve

def serve_via_s3
serve do
redirect "http://#{$rubygems_config[:s3_domain]}#{request.path_info}"
redirect "http://#{Gemcutter.config['s3_domain']}#{request.path_info}"
end
end

def serve_via_cf
serve do
redirect "http://#{$rubygems_config[:cf_domain]}#{request.path_info}"
redirect "http://#{Gemcutter.config['cf_domain']}#{request.path_info}"
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/middleware/redirector.rb
Expand Up @@ -6,8 +6,8 @@ def initialize(app)
def call(env)
request = Rack::Request.new(env)

if request.host != HOST && request.path !~ %r{^/api}
fake_request = Rack::Request.new(env.merge("HTTP_HOST" => HOST))
if request.host != Gemcutter::HOST && request.path !~ %r{^/api}
fake_request = Rack::Request.new(env.merge("HTTP_HOST" => Gemcutter::HOST))
redirect_to(fake_request.url)
elsif request.path =~ %r{^/(book|chapter|export|read|shelf|syndicate)}
redirect_to("http://docs.rubygems.org#{request.path}")
Expand Down
2 changes: 1 addition & 1 deletion app/models/rubygem.rb
Expand Up @@ -147,7 +147,7 @@ def downloads_today
versions.to_a.sum {|v| Download.today(v) }
end

def payload(version=versions.most_recent, host_with_port=HOST)
def payload(version=versions.most_recent, host_with_port=Gemcutter::HOST)
{
'name' => name,
'downloads' => downloads,
Expand Down
25 changes: 17 additions & 8 deletions config/application.rb
Expand Up @@ -11,30 +11,39 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

$rubygems_config = YAML.load_file("config/rubygems.yml")[Rails.env].symbolize_keys
HOST = $rubygems_config[:host]

module Gemcutter
class Application < Rails::Application
def config_for(name, env = Rails.env)
YAML.load_file(Rails.root.join("config/#{name}.yml"))[env]
end
config.rubygems = Application.config_for :rubygems

config.time_zone = "UTC"
config.encoding = "utf-8"

config.middleware.use "Hostess"
config.middleware.insert_after "Hostess", "Redirector" if $rubygems_config[:redirector] && ENV["LOCAL"].nil?
if config.rubygems['redirector'] && ENV["LOCAL"].nil?
config.middleware.insert_after "Hostess", "Redirector"
end

unless Rails.env.maintenance?
config.action_mailer.default_url_options = { :host => HOST }
config.action_mailer.delivery_method = $rubygems_config[:delivery_method]
config.action_mailer.delivery_method = config.rubygems['delivery_method']
config.active_record.include_root_in_json = false
end

config.after_initialize do
Hostess.local = $rubygems_config[:local_storage]
Hostess.local = config.rubygems['local_storage']
end

config.plugins = [:dynamic_form]
config.plugins << :heroku_asset_cacher if $rubygems_config[:asset_cacher]
config.plugins << :heroku_asset_cacher if config.rubygems['asset_cacher']

config.autoload_paths << "./app/jobs"
end

def self.config
Rails.application.config.rubygems
end

HOST = config['host']
end
1 change: 1 addition & 0 deletions config/environments/development.rb
Expand Up @@ -15,6 +15,7 @@

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { host: Gemcutter::HOST }

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Expand Up @@ -58,7 +58,7 @@
# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { host: "rubygems.org", protocol: "https" }
config.action_mailer.default_url_options = { host: Gemcutter::HOST, protocol: "https" }

# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
Expand Down
2 changes: 1 addition & 1 deletion config/environments/recovery.rb
Expand Up @@ -14,7 +14,7 @@
config.action_controller.perform_caching = true
config.action_dispatch.x_sendfile_header = "X-Sendfile"
config.active_support.deprecation = :notify
config.serve_static_assets = $rubygems_config[:asset_cacher]
config.serve_static_assets = Gemcutter.config['asset_cacher']
config.i18n.fallbacks = true

config.action_dispatch.session = {
Expand Down
2 changes: 1 addition & 1 deletion config/environments/staging.rb
Expand Up @@ -58,7 +58,7 @@
# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { host: "rubygems.org", protocol: "https" }
config.action_mailer.default_url_options = { host: Gemcutter::HOST, protocol: "https" }

# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
Expand Down
1 change: 1 addition & 0 deletions config/environments/test.rb
Expand Up @@ -30,6 +30,7 @@
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { host: Gemcutter::HOST, protocol: "https" }

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
Expand Down
2 changes: 1 addition & 1 deletion script/add_sizes_to_versions
Expand Up @@ -11,7 +11,7 @@ end
Version.find_in_batches do |versions|
versions.each do |version|
print "sizing #{version.full_name}: "
file = fog.directories.get($rubygems_config[:s3_bucket] + '/gems') \
file = fog.directories.get(Gemcutter.config['s3_bucket'] + '/gems') \
.files.get("#{version.full_name}.gem")
if file
begin
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -26,7 +26,7 @@ class MiniTest::Test
def setup
RR.reset
Redis.current.flushdb
$fog.directories.create(:key => $rubygems_config[:s3_bucket], :public => true)
$fog.directories.create(key: Gemcutter.config['s3_bucket'], public: true)
end

def page
Expand Down
2 changes: 1 addition & 1 deletion test/unit/hostess_test.rb
Expand Up @@ -67,7 +67,7 @@ def touch(path, local = true)
should "redirect to cdn for a gem" do
get @file

assert_equal "http://#{$rubygems_config[:cf_domain]}#{@file}", last_response.headers["Location"]
assert_equal "http://#{Gemcutter.config['cf_domain']}#{@file}", last_response.headers["Location"]
assert_equal 302, last_response.status
end
end
Expand Down
14 changes: 7 additions & 7 deletions test/unit/redirector_test.rb
Expand Up @@ -9,7 +9,7 @@ def app
end

should "forward requests that don't match" do
get "/specs.4.8.gz", {}, {"HTTP_HOST" => HOST}
get "/specs.4.8.gz", {}, {"HTTP_HOST" => Gemcutter::HOST}
assert last_response.ok?
end

Expand All @@ -18,15 +18,15 @@ def app
get path, {}, {"HTTP_HOST" => "gems.rubyforge.org"}

assert_equal 301, last_response.status
assert_equal "http://#{HOST}#{path}", last_response.headers["Location"]
assert_equal "http://#{Gemcutter::HOST}#{path}", last_response.headers["Location"]
end

should "redirect requests from a non-HOST domain with query string" do
path = "/search"
get path, {"query" => "rush"}, {"HTTP_HOST" => "gems.rubyforge.org"}

assert_equal 301, last_response.status
assert_equal "http://#{HOST}#{path}?query=rush", last_response.headers["Location"]
assert_equal "http://#{Gemcutter::HOST}#{path}?query=rush", last_response.headers["Location"]
end

should "not redirect requests to the API from a non-HOST domain" do
Expand All @@ -44,29 +44,29 @@ def app
/shelf/9000
/syndicate.xml].each do |uri|
should "redirect to docs.rubygems.org when #{uri} is hit" do
get uri, {}, {"HTTP_HOST" => HOST}
get uri, {}, {"HTTP_HOST" => Gemcutter::HOST}

assert_equal 301, last_response.status
assert_equal "http://docs.rubygems.org#{uri}", last_response.headers["Location"]
end
end

should "redirect request to docs to guides " do
get "/pages/docs", {}, {"HTTP_HOST" => HOST}
get "/pages/docs", {}, {"HTTP_HOST" => Gemcutter::HOST}

assert_equal 301, last_response.status
assert_equal "http://guides.rubygems.org", last_response.headers["Location"]
end

should "redirect request to gem docs to guides " do
get "/pages/gem_docs", {}, {"HTTP_HOST" => HOST}
get "/pages/gem_docs", {}, {"HTTP_HOST" => Gemcutter::HOST}

assert_equal 301, last_response.status
assert_equal "http://guides.rubygems.org/command-reference", last_response.headers["Location"]
end

should "redirect request to api docs to guides " do
get "/pages/api_docs", {}, {"HTTP_HOST" => HOST}
get "/pages/api_docs", {}, {"HTTP_HOST" => Gemcutter::HOST}

assert_equal 301, last_response.status
assert_equal "http://guides.rubygems.org/rubygems-org-api", last_response.headers["Location"]
Expand Down
8 changes: 4 additions & 4 deletions test/unit/rubygem_test.rb
Expand Up @@ -342,8 +342,8 @@ class RubygemTest < ActiveSupport::TestCase
assert_equal @rubygem.versions.most_recent.platform, hash["platform"]
assert_equal @rubygem.versions.most_recent.authors, hash["authors"]
assert_equal @rubygem.versions.most_recent.info, hash["info"]
assert_equal "http://#{HOST}/gems/#{@rubygem.name}", hash["project_uri"]
assert_equal "http://#{HOST}/gems/#{@rubygem.versions.most_recent.full_name}.gem", hash["gem_uri"]
assert_equal "http://#{Gemcutter::HOST}/gems/#{@rubygem.name}", hash["project_uri"]
assert_equal "http://#{Gemcutter::HOST}/gems/#{@rubygem.versions.most_recent.full_name}.gem", hash["gem_uri"]

assert_equal MultiJson.load(dev_dep.to_json), hash["dependencies"]["development"].first
assert_equal MultiJson.load(run_dep.to_json), hash["dependencies"]["runtime"].first
Expand All @@ -363,8 +363,8 @@ class RubygemTest < ActiveSupport::TestCase
assert_equal @rubygem.versions.most_recent.downloads_count.to_s, doc.at_css("version-downloads").content
assert_equal @rubygem.versions.most_recent.authors, doc.at_css("authors").content
assert_equal @rubygem.versions.most_recent.info, doc.at_css("info").content
assert_equal "http://#{HOST}/gems/#{@rubygem.name}", doc.at_css("project-uri").content
assert_equal "http://#{HOST}/gems/#{@rubygem.versions.most_recent.full_name}.gem", doc.at_css("gem-uri").content
assert_equal "http://#{Gemcutter::HOST}/gems/#{@rubygem.name}", doc.at_css("project-uri").content
assert_equal "http://#{Gemcutter::HOST}/gems/#{@rubygem.versions.most_recent.full_name}.gem", doc.at_css("gem-uri").content

assert_equal dev_dep.name, doc.at_css("dependencies development dependency name").content
assert_equal run_dep.name, doc.at_css("dependencies runtime dependency name").content
Expand Down

0 comments on commit 06f4a5f

Please sign in to comment.