spore-rails 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 985a93a4a301bda8d488e2ab9ede61d18fc2728c
4
+ data.tar.gz: efd1c8554eb8368dbc2cd48709afedf77c4070a4
5
+ SHA512:
6
+ metadata.gz: dec469cbed1e48a7d592dffb505700a23e433d829e2c0ac72dac2260cb585348820f86b7d8f38ca14f6b73f07d3ea032385a6447b7ba78bfb2c2bad6653a8929
7
+ data.tar.gz: c664cef26ddb8778a04ad230857ba9d67916db2ef6b60f2a56486fa06bd54e0728c7a4483a0b980497fe99fd81d7268aec3c49da29f7a727348a95430989d008
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brandon Keepers
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ spore-rails
2
+ -----------
3
+
4
+ Ruby/Rails gem to load Spore environment variables. See the [Spore Website](https://spore.sh) for more information.
5
+
6
+
7
+ ## Installation
8
+
9
+ ### Rails
10
+
11
+ Add this line to the top of your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'spore-rails'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```shell
20
+ $ bundle
21
+ ```
22
+
23
+ #### Note on load order
24
+
25
+ Spore is initialized in your Rails app during the `before_configuration` callback, which is fired when the `Application` constant is defined in `config/application.rb` with `class Application < Rails::Application`. If you need it to be initialized sooner, you can manually call `Spore::Railtie.load`.
26
+
27
+ ```ruby
28
+ # config/application.rb
29
+ Bundler.require(*Rails.groups)
30
+
31
+ Spore::Railtie.load
32
+
33
+ HOSTNAME = ENV['HOSTNAME']
34
+ ```
35
+
36
+ If you use gems that require environment variables to be set before they are loaded, then list `spore-rails` in the `Gemfile` before those other gems and require `spore/rails-now`.
37
+
38
+ ```ruby
39
+ gem 'spore-rails', :require => 'spore/rails-now'
40
+ gem 'gem-that-requires-env-variables'
41
+ ```
42
+
43
+ ### Sinatra or Plain ol' Ruby
44
+
45
+ Install the gem:
46
+
47
+ ```shell
48
+ $ gem install spore
49
+ ```
50
+
51
+ As early as possible in your application bootstrap process, load Spore:
52
+
53
+ ```ruby
54
+ require 'spore'
55
+ Spore.load
56
+ ```
57
+ To ensure Spore is loaded in rake, load the tasks:
58
+
59
+ ```ruby
60
+ require 'spore/tasks'
61
+
62
+ task :mytask => :spore do
63
+ # things that require environment variables
64
+ end
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
74
+
75
+ ## Notes
76
+
77
+ The `spore-rails` gem was based on [`dotenv`](https://github.com/bkeepers/dotenv) by Brandon Keepers.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ desc "Open an irb session preloaded with this library"
2
+ task :console do
3
+ sh "irb -rubygems -I lib -r spore.rb"
4
+ end
@@ -0,0 +1,102 @@
1
+ require 'spore-api'
2
+ require 'netrc'
3
+
4
+ module Spore
5
+ class Config
6
+ attr_reader :environment
7
+ attr_reader :api
8
+
9
+ DEPLOYMENT_VAR = "SPORE_DEPLOYMENT"
10
+ CONFIG_FILE = "config.json"
11
+ DEFAULT_CONFIG = File.expand_path(
12
+ "../config/default.json", File.dirname(__FILE__))
13
+
14
+ class << self
15
+ def is_deployment?
16
+ !ENV[DEPLOYMENT_VAR].nil?
17
+ end
18
+
19
+ def load
20
+ if is_deployment?
21
+ Spore::DeploymentConfig.new
22
+ else
23
+ Spore::LocalConfig.new
24
+ end
25
+ end
26
+ end
27
+
28
+ def config
29
+ @config ||= begin
30
+ file = File.read DEFAULT_CONFIG
31
+ hash = JSON.parse file
32
+ config_file = File.join(ENV["SPORE_HOME"] || "~/.spore", CONFIG_FILE)
33
+ begin
34
+ hash.update(JSON.parse(File.read(File.expand_path(config_file))))
35
+ rescue Errno::ENOENT
36
+ # ~/.spore/config.json doesn't exist, so just use the default config
37
+ hash
38
+ end
39
+ end
40
+ end
41
+
42
+ def fetch(app_id, cell_id)
43
+ api.get_cell(app_id, environment, cell_id)["value"]
44
+ end
45
+
46
+ def spore_file
47
+ File.expand_path(config["sporeFile"])
48
+ end
49
+ end
50
+ end
51
+
52
+ module Spore
53
+ class LocalConfig < Config
54
+ def initialize
55
+ email, key = load_credentials
56
+ @api = Spore::Client.new
57
+ @api.key = key
58
+ @api.name = email
59
+ @api.api_endpoint = server
60
+ end
61
+
62
+ def environment
63
+ ENV["SPORE_ENV"] || @config["defaultEnv"]
64
+ end
65
+
66
+ def server
67
+ config["useProxy"] ? "http://127.0.0.1:#{config["proxy"]["port"]}" : config["host"]
68
+ end
69
+
70
+ private
71
+
72
+ def load_credentials
73
+ n = Netrc.read(File.expand_path config["netrc"])
74
+ hostname = URI.parse(config["host"]).hostname
75
+ n[hostname]
76
+ end
77
+ end
78
+ end
79
+
80
+ module Spore
81
+ class DeploymentConfig < Config
82
+ ENV_FORMAT = /(http|https):\/\/([a-zA-Z0-9-]+)\+([a-zA-Z0-9-]+)\+([a-f0-9-]+):([^@]+)@(.+)/
83
+
84
+ def initialize
85
+ ENV_FORMAT.match(ENV[Config::DEPLOYMENT_VAR]) do |m|
86
+ name = m[2]
87
+ key = m[5]
88
+ host = "#{m[1]}://#{m[6]}"
89
+ @environment = m[3]
90
+ @api = Spore::Client.new
91
+ @api.key = key
92
+ @api.name = name
93
+ @api.api_endpoint = host
94
+ end
95
+ raise "SPORE_DEPLOYMENT has an unexpected format" if @environment.nil?
96
+ end
97
+
98
+ def environment
99
+ ENV["SPORE_ENV"] || @environment
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,27 @@
1
+ module Spore
2
+ # This class inherits from Hash and represents the environemnt into which
3
+ # Dotenv will load key value pairs from a file.
4
+ class Environment < Hash
5
+ attr_reader :filename
6
+
7
+ def initialize
8
+ load
9
+ end
10
+
11
+ def load
12
+ update Parser.call
13
+ end
14
+
15
+ def read
16
+ File.read(@filename)
17
+ end
18
+
19
+ def apply
20
+ each { |k, v| ENV[k] ||= v }
21
+ end
22
+
23
+ def apply!
24
+ each { |k, v| ENV[k] = v }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+
3
+ module Spore
4
+ class Parser
5
+ class << self
6
+ def call
7
+ config = Spore::Config.load
8
+ new(config).hash
9
+ end
10
+ end
11
+
12
+ attr_reader :hash
13
+ def initialize(config)
14
+ @config = config
15
+ spore = JSON.parse(File.read config.spore_file)
16
+ translate(spore["id"], spore["envs"][config.environment])
17
+ end
18
+
19
+ def translate(app_id, hash)
20
+ @hash = {}
21
+ hash.each do |key, value|
22
+ # Translate APP_ENV to RAILS_ENV
23
+ key = "RAILS_ENV" if key == "APP_ENV"
24
+ @hash[key] = @config.fetch(app_id, value)
25
+ end
26
+ @hash
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ # If you use gems that require environment variables to be set before they are
2
+ # loaded, then list `spore-rails` in the `Gemfile` before those other gems and
3
+ # require `spore/rails-now`.
4
+ #
5
+ # gem "spore-rails", :require => "spore/rails-now"
6
+ # gem "gem-that-requires-env-variables"
7
+ #
8
+
9
+ require "spore/rails"
10
+ Spore::Railtie.load
@@ -0,0 +1,43 @@
1
+ require "spore"
2
+
3
+ Spore.instrumenter = ActiveSupport::Notifications
4
+
5
+ # Watch all loaded env files with Spring
6
+ begin
7
+ require "spring/watcher"
8
+ ActiveSupport::Notifications.subscribe(/^spore/) do |*args|
9
+ event = ActiveSupport::Notifications::Event.new(*args)
10
+ Spring.watch event.payload[:env].filename if Rails.application
11
+ end
12
+ rescue LoadError
13
+ # Spring is not available
14
+ end
15
+
16
+ module Spore
17
+ # Spore Railtie for using Spore to load environment from a file into
18
+ # Rails applications
19
+ class Railtie < Rails::Railtie
20
+ config.before_configuration { load }
21
+
22
+ # Public: Load spore
23
+ #
24
+ # This will get called during the `before_configuration` callback, but you
25
+ # can manually call `Spore::Railtie.load` if you needed it sooner.
26
+ def load
27
+ Spore.load
28
+ end
29
+
30
+ # Internal: `Rails.root` is nil in Rails 4.1 before the application is
31
+ # initialized, so this falls back to the `RAILS_ROOT` environment variable,
32
+ # or the current working directory.
33
+ def root
34
+ Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd)
35
+ end
36
+
37
+ # Rails uses `#method_missing` to delegate all class methods to the
38
+ # instance, which means `Kernel#load` gets called here. We don't want that.
39
+ def self.load
40
+ instance.load
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,13 @@
1
+ module Spore
2
+ class Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 3
6
+
7
+ class << self
8
+ def to_s
9
+ [MAJOR, MINOR, PATCH].join('.')
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/spore.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "spore/parser"
2
+ require "spore/environment"
3
+ require "spore/config"
4
+
5
+ # The top level Spore module. The entrypoint for the application logic.
6
+ module Spore
7
+ class << self
8
+ attr_accessor :instrumenter
9
+ end
10
+
11
+ module_function
12
+
13
+ def load(*filenames)
14
+ env = Environment.new
15
+ instrument("spore.load", :env => env) { env.apply }
16
+ end
17
+
18
+ # same as `load`, but will override existing values in `ENV`
19
+ def overload(*filenames)
20
+ env = Environment.new
21
+ instrument("spore.overload", :env => env) { env.apply! }
22
+ end
23
+
24
+ def instrument(name, payload = {}, &block)
25
+ if instrumenter
26
+ instrumenter.instrument(name, payload, &block)
27
+ else
28
+ block.call
29
+ end
30
+ end
31
+ end
data/spore.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ require File.expand_path("../lib/spore/version", __FILE__)
2
+ require "English"
3
+
4
+ Gem::Specification.new "spore-rails", Spore::Version do |gem|
5
+ gem.authors = ["Shayan Guha"]
6
+ gem.email = ["shayan@teleborder.com"]
7
+ gem.description = gem.summary = "Loads environment variables from spore."
8
+ gem.homepage = "https://github.com/spore-sh/spore-rails"
9
+ gem.license = "MIT"
10
+
11
+ gem.files = %w(LICENSE README.md Rakefile spore.gemspec)
12
+ gem.files += Dir.glob("lib/**/*.rb")
13
+ gem.require_paths = ["lib"]
14
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
15
+
16
+ gem.add_dependency 'spore-api', "~> 0.0"
17
+ gem.add_dependency 'netrc', "~> 0.10"
18
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spore-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Shayan Guha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: spore-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: netrc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.10'
41
+ description: Loads environment variables from spore.
42
+ email:
43
+ - shayan@teleborder.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/spore.rb
52
+ - lib/spore/config.rb
53
+ - lib/spore/environment.rb
54
+ - lib/spore/parser.rb
55
+ - lib/spore/rails-now.rb
56
+ - lib/spore/rails.rb
57
+ - lib/spore/version.rb
58
+ - spore.gemspec
59
+ homepage: https://github.com/spore-sh/spore-rails
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Loads environment variables from spore.
83
+ test_files: []
84
+ has_rdoc: