Add Google Map of voters
[selectricity-live] / vendor / plugins / ym4r_gm / lib / gm_plugin / key.rb
diff --git a/vendor/plugins/ym4r_gm/lib/gm_plugin/key.rb b/vendor/plugins/ym4r_gm/lib/gm_plugin/key.rb
new file mode 100644 (file)
index 0000000..0de9c18
--- /dev/null
@@ -0,0 +1,37 @@
+module Ym4r\r
+  module GmPlugin\r
+    class GMapsAPIKeyConfigFileNotFoundException < StandardError\r
+    end\r
+        \r
+    class AmbiguousGMapsAPIKeyException < StandardError\r
+    end\r
+    \r
+    #Class fo the manipulation of the API key\r
+    class ApiKey\r
+      #Read the API key config for the current ENV\r
+      unless File.exist?(RAILS_ROOT + '/config/gmaps_api_key.yml')\r
+        raise GMapsAPIKeyConfigFileNotFoundException.new("File RAILS_ROOT/config/gmaps_api_key.yml not found")\r
+      else\r
+        env = ENV['RAILS_ENV'] || RAILS_ENV\r
+        GMAPS_API_KEY = YAML.load_file(RAILS_ROOT + '/config/gmaps_api_key.yml')[env]\r
+      end\r
+      \r
+      def self.get(options = {})\r
+        if options.has_key?(:key)\r
+          options[:key]\r
+        elsif GMAPS_API_KEY.is_a?(Hash)\r
+          #For this environment, multiple hosts are possible.\r
+          #:host must have been passed as option\r
+          if options.has_key?(:host)\r
+            GMAPS_API_KEY[options[:host]]\r
+          else\r
+            raise AmbiguousGMapsAPIKeyException.new(GMAPS_API_KEY.keys.join(","))\r
+          end\r
+        else\r
+          #Only one possible key: take it and ignore the :host option if it is there\r
+          GMAPS_API_KEY\r
+        end\r
+      end\r
+    end\r
+  end\r
+end\r

Benjamin Mako Hill || Want to submit a patch?