changeset 47:e5082c4c234d

Also allow config-file-based configuration.
author nanaya <me@myconan.net>
date Thu, 23 Apr 2015 16:05:08 +0900
parents 92b22027d0ae
children 8983c426e256
files app/models/tweet.rb config/application.rb config/config_init.rb
diffstat 3 files changed, 18 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/app/models/tweet.rb	Thu Apr 23 15:32:56 2015 +0900
+++ b/app/models/tweet.rb	Thu Apr 23 16:05:08 2015 +0900
@@ -1,11 +1,4 @@
 class Tweet
-  CONFIG = {
-    :consumer_key => ENV["RT_CONSUMER_KEY"],
-    :consumer_secret => ENV["RT_CONSUMER_SECRET"],
-    :access_token => ENV["RT_ACCESS_TOKEN"],
-    :access_token_secret => ENV["RT_ACCESS_TOKEN_SECRET"]
-  }
-
   def timeline
     @timeline ||=
       Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => 5.minutes) do
@@ -15,7 +8,7 @@
 
   def initialize(twitter_id)
     @client = Twitter::REST::Client.new do |config|
-      CONFIG.each do |cfg_key, cfg_value|
+      CFG[:twitter].each do |cfg_key, cfg_value|
         config.public_send("#{cfg_key}=", cfg_value)
       end
     end
--- a/config/application.rb	Thu Apr 23 15:32:56 2015 +0900
+++ b/config/application.rb	Thu Apr 23 16:05:08 2015 +0900
@@ -9,6 +9,8 @@
 # require "sprockets/railtie"
 require "rails/test_unit/railtie"
 
+require File.expand_path("../config_init", __FILE__)
+
 # Require the gems listed in Gemfile, including any gems
 # you've limited to :test, :development, or :production.
 Bundler.require(*Rails.groups)
@@ -32,8 +34,8 @@
     config.secret_key_base = "herp a derp"
     config.session_store :disabled
 
-    if ENV["RT_MEMCACHED_SERVERS"]
-      config.cache_store = :mem_cache_store, ENV["RT_MEMCACHED_SERVERS"].split(","), { :namespace => "rsstweet" }
+    if CFG[:memcached_servers]
+      config.cache_store = :mem_cache_store, CFG[:memcached_servers], { :namespace => "rsstweet" }
     end
   end
 end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config/config_init.rb	Thu Apr 23 16:05:08 2015 +0900
@@ -0,0 +1,13 @@
+CFG = {
+  :twitter => {
+    :consumer_key => ENV["RT_CONSUMER_KEY"],
+    :consumer_secret => ENV["RT_CONSUMER_SECRET"],
+    :access_token => ENV["RT_ACCESS_TOKEN"],
+    :access_token_secret => ENV["RT_ACCESS_TOKEN_SECRET"]
+  },
+  :memcached_servers => ENV["RT_MEMCACHED_SERVERS"].try(:split, ",")
+}
+
+config_local = File.expand_path("../config_local_#{Rails.env}.rb", __FILE__)
+
+require config_local if File.exist? config_local