conflux 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0c5540b770d89a463b4daae814ded06fad60bb36
4
+ data.tar.gz: 1d33b3c82346b50c158c4fd8786db7ebdd52a1b5
5
+ SHA512:
6
+ metadata.gz: 309f765210426259abd625aba293e4e268e89a59cdb43f58601f9fd1c8a38e575a2b7356ea029c7a5a907295b9b8668a4a8d9bd7082e6c9c6de1a929375255dc
7
+ data.tar.gz: 0e23eb9ef81a73b8721850099ed57ad889f68eb06db4e0a20a668c975b2a5f00da4d5e8a0bcf485f2207ed07b1034a40d8b78512c5d7b5ab91364ea6a9041ea9
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
11
+ /configs.yml
12
+ /release.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in conflux.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 GoConflux
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ben Whittle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # Conflux Ruby gem
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'conflux'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself with:
16
+
17
+ $ gem install conflux
18
+
19
+ ## License
20
+
21
+ [MIT License](http://opensource.org/licenses/MIT).
22
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "conflux"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/conflux.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'conflux/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "conflux"
8
+ spec.version = Conflux::VERSION
9
+ spec.authors = ["Ben Whittle"]
10
+ spec.email = ["benwhittle31@gmail.com"]
11
+ spec.summary = "Gem to fetch and make available Conflux configs on Rails boot."
12
+ spec.homepage = "https://www.github.com/GoConflux/conflux-rb"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "rest-client"
21
+
22
+ spec.add_development_dependency "rails", "~> 4.0"
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
data/lib/conflux.rb ADDED
@@ -0,0 +1,130 @@
1
+ require 'conflux/version'
2
+ require 'rest-client'
3
+ require 'json'
4
+
5
+ module Conflux
6
+ require 'conflux/railtie' if defined?(Rails)
7
+ extend self
8
+
9
+ config_vars_path = File.join(Dir.pwd, 'configs.yml')
10
+
11
+ if File.exists?(config_vars_path)
12
+ require 'yaml'
13
+ configs = YAML::load_file(config_vars_path) rescue {}
14
+ configs.each { |key, val|
15
+ ENV[key] = val if !ENV.key?(key)
16
+ }
17
+ end
18
+
19
+ APPLICATION_YAML_PATH = File.join(Dir.pwd, 'config', 'application.yml')
20
+ MANIFEST_PATH = File.join(Dir.pwd, '.conflux', 'manifest.json')
21
+ CONFLUX_YAML_PATH = File.join(Dir.pwd, '.conflux', 'conflux.yml')
22
+ CONFLUX_USER = 'CONFLUX_USER'
23
+ CONFLUX_APP = 'CONFLUX_APP'
24
+
25
+ def start!
26
+ fetch_configs if configured_for_conflux
27
+ end
28
+
29
+ private
30
+
31
+ def configured_for_conflux
32
+ if ENV[CONFLUX_USER] && ENV[CONFLUX_APP]
33
+ @creds_preset = true
34
+
35
+ @credentials = {
36
+ 'CONFLUX_USER' => ENV[CONFLUX_USER],
37
+ 'CONFLUX_APP' => ENV[CONFLUX_APP]
38
+ }
39
+ else
40
+ @credentials = File.exists?(MANIFEST_PATH) ? manifest_creds : {}
41
+ end
42
+
43
+ @credentials[CONFLUX_USER] && @credentials[CONFLUX_APP]
44
+ end
45
+
46
+ def manifest_creds
47
+ manifest = JSON.parse(File.read(MANIFEST_PATH)) rescue {}
48
+ configs = manifest['configs'] || {}
49
+
50
+ {
51
+ 'CONFLUX_USER' => configs[CONFLUX_USER],
52
+ 'CONFLUX_APP' => configs[CONFLUX_APP]
53
+ }
54
+ end
55
+
56
+ def fetch_configs
57
+ RestClient.get("#{conflux_url}/api/keys", headers) do |response|
58
+ if response.code == 200
59
+ configs = JSON.parse(response.body) rescue []
60
+ set_configs(configs, !@creds_preset)
61
+ end
62
+ end
63
+ end
64
+
65
+ def set_configs(configs_map, add_to_yml)
66
+ # Get application.yml file to make sure config vars aren't already there
67
+ @app_configs = YAML::load_file(APPLICATION_YAML_PATH) rescue {}
68
+
69
+ if add_to_yml
70
+ File.open(CONFLUX_YAML_PATH, 'w+') do |f|
71
+ f.write(yaml_header)
72
+
73
+ configs_map.each { |addon_name, configs|
74
+ f.write("\n\n# #{addon_name}") if !configs.empty?
75
+
76
+ configs.each { |config|
77
+ key = config['name']
78
+ value = config['value']
79
+ description = config['description']
80
+
81
+ # if the key already exists, let the developer know that it has
82
+ # been overwritten and to what value
83
+ if key_already_set?(key)
84
+ description = description.nil? ? '' : " ... #{description}"
85
+ line = "\n# #{key} <-- (Overwritten) #{description}"
86
+ else
87
+ description = description.nil? ? '' : " # #{description}"
88
+ line = "\n#{key}#{description}"
89
+ ENV[key] = value
90
+ end
91
+
92
+ f.write(line)
93
+ }
94
+ }
95
+ end
96
+ else
97
+ configs_map.each { |addon_name, configs|
98
+ configs.each { |config|
99
+ key = config['name']
100
+ value = config['value']
101
+
102
+ ENV[key] = value if !key_already_set?(key)
103
+ }
104
+ }
105
+ end
106
+ end
107
+
108
+ def key_already_set?(key)
109
+ ENV.key?(key) || @app_configs.key?(key) || (@app_configs[Rails.env] || {}).key?(key)
110
+ end
111
+
112
+ def headers
113
+ {
114
+ 'Conflux-User' => @credentials[CONFLUX_USER],
115
+ 'Conflux-App' => @credentials[CONFLUX_APP]
116
+ }
117
+ end
118
+
119
+ def conflux_url
120
+ ENV['CONFLUX_HOST'] || 'http://api.goconflux.com'
121
+ end
122
+
123
+ def yaml_header
124
+ "\n# CONFLUX CONFIG VARS:\n\n" \
125
+ "# All config vars seen here are in use and pulled from Conflux.\n" \
126
+ "# If any are ever overwritten, they will be marked with \"Overwritten\"\n" \
127
+ "# If you ever wish to overwrite any of these, do so inside of a config/application.yml file."
128
+ end
129
+
130
+ end
@@ -0,0 +1,154 @@
1
+ module Conflux
2
+ module Helpers
3
+ extend self
4
+
5
+ def ask_for_basic_creds
6
+ # Ask for Conflux Credentials
7
+ puts 'Enter your Conflux credentials.'
8
+
9
+ # Email:
10
+ print 'Email: '
11
+ email = allow_user_response
12
+
13
+ # Password
14
+ print 'Password (typing will be hidden): '
15
+
16
+ password = running_on_windows? ? ask_for_password_on_windows : ask_for_password
17
+
18
+ { email: email, password: password }
19
+ end
20
+
21
+ def ask_for_password_on_windows
22
+ require 'Win32API'
23
+ char = nil
24
+ password = ''
25
+
26
+ while char = Win32API.new('msvcrt', '_getch', [ ], 'L').Call do
27
+ break if char == 10 || char == 13 # received carriage return or newline
28
+ if char == 127 || char == 8 # backspace and delete
29
+ password.slice!(-1, 1)
30
+ else
31
+ # windows might throw a -1 at us so make sure to handle RangeError
32
+ (password << char.chr) rescue RangeError
33
+ end
34
+ end
35
+
36
+ puts
37
+ password
38
+ end
39
+
40
+ def ask_for_password
41
+ begin
42
+ echo_off # make the password input hidden
43
+ password = allow_user_response
44
+ puts
45
+ ensure
46
+ echo_on # flip input visibility back on
47
+ end
48
+
49
+ password
50
+ end
51
+
52
+ # Hide user input
53
+ def echo_off
54
+ with_tty do
55
+ system 'stty -echo'
56
+ end
57
+ end
58
+
59
+ # Show user input
60
+ def echo_on
61
+ with_tty do
62
+ system 'stty echo'
63
+ end
64
+ end
65
+
66
+ def with_tty(&block)
67
+ return unless $stdin.isatty
68
+ begin
69
+ yield
70
+ rescue
71
+ # fails on windows
72
+ end
73
+ end
74
+
75
+ def allow_user_response
76
+ $stdin.gets.to_s.strip
77
+ end
78
+
79
+ def running_on_windows?
80
+ RUBY_PLATFORM =~ /mswin32|mingw32/
81
+ end
82
+
83
+ def url(route)
84
+ "#{host_url}/api#{route}"
85
+ end
86
+
87
+ def host_url
88
+ ENV['CONFLUX_HOST'] || 'http://api.goconflux.com'
89
+ end
90
+
91
+ def prompt_user_to_select_app(apps_map)
92
+ answer = nil
93
+ question = "\nWhich Conflux app does this project belong to?\n"
94
+
95
+ # Keep asking until the user responds with one of the possible answers
96
+ until !answer.nil?
97
+ count = 0
98
+ app_slugs = []
99
+
100
+ puts question
101
+
102
+ apps_map.each { |team, apps|
103
+ puts "\n#{team}:\n\n" # separate apps out by team for easier selection
104
+
105
+ apps.each { |slug|
106
+ count += 1
107
+ puts "(#{count}) #{slug}"
108
+ app_slugs << slug
109
+ }
110
+ }
111
+
112
+ puts "\n"
113
+
114
+ response = allow_user_response
115
+
116
+ # it's fine if the user responds with an exact app slug
117
+ if app_slugs.include?(response)
118
+ answer = response
119
+
120
+ # otherwise, they can just respond with the number next to the app they wish to choose
121
+ else
122
+ response_int = response.to_i rescue 0
123
+ answer = app_slugs[response_int - 1 ]if response_int > 0
124
+ end
125
+
126
+ question = "\nSorry I didn't catch that. Can you respond with the number that appears next to your answer?"
127
+ end
128
+
129
+ answer
130
+ end
131
+
132
+ def error(msg = '')
133
+ $stderr.puts(msg)
134
+ exit(1)
135
+ end
136
+
137
+ def handle_json_response(response, error_message)
138
+ if response.code == 200
139
+ JSON.parse(response.body) rescue {}
140
+ else
141
+ error(error_message)
142
+ end
143
+ end
144
+
145
+ def conflux_folder_path
146
+ "#{Dir.pwd}/.conflux/"
147
+ end
148
+
149
+ def conflux_manifest_path
150
+ File.join(conflux_folder_path, 'manifest.json')
151
+ end
152
+
153
+ end
154
+ end
@@ -0,0 +1,20 @@
1
+ require 'conflux'
2
+ require 'rails'
3
+
4
+ module Conflux
5
+ class Railtie < ::Rails::Railtie
6
+ railtie_name :conflux
7
+ # Hooking into `to_prepare` because it runs BEFORE the config/initializers
8
+ # files but AFTER any other railties hooking into `before_configuration` - which
9
+ # is what the popular gem "figaro" uses. This allows for Conflux config vars to be
10
+ # more easily overwritten if desired, by simply using figaro and manually specifying
11
+ # config vars in your config/application.yml file.
12
+ config.before_configuration do
13
+ Conflux.start!
14
+ end
15
+
16
+ rake_tasks do
17
+ load 'tasks/conflux.rake'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Conflux
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+ require 'fileutils'
4
+ require 'conflux/helpers'
5
+
6
+ namespace :conflux do
7
+
8
+ desc 'Set which conflux app to use for the current directory'
9
+ task :set_app do
10
+ helpers = Conflux::Helpers
11
+ creds = helpers.ask_for_basic_creds
12
+
13
+ RestClient.post(helpers.url('/users/apps_basic_auth'), creds) do |response|
14
+ body = helpers.handle_json_response(response, 'Authentication failed.')
15
+
16
+ # Ask which app user wants to use:
17
+ app_slug = helpers.prompt_user_to_select_app(body['apps_map'])
18
+
19
+ RestClient.get(helpers.url("/apps/manifest?app_slug=#{app_slug}"), { 'Conflux-User' => body['token'] }) do |response|
20
+ resp = helpers.handle_json_response(response, 'Request failed.')
21
+ manifest_json = resp['manifest']
22
+
23
+ # Create .conflux/ folder if doesn't already exist
24
+ FileUtils.mkdir_p(helpers.conflux_folder_path) if !File.exists?(helpers.conflux_folder_path)
25
+
26
+ puts 'Configuring manifest.json...'
27
+
28
+ # Write this app info to a new manifest.json file for the user
29
+ File.open(helpers.conflux_manifest_path, 'w+') do |f|
30
+ f.write(JSON.pretty_generate(manifest_json))
31
+ end
32
+
33
+ puts "Successfully connected project to conflux app: #{app_slug}"
34
+ puts "The 'conflux' ruby gem wasn't automatically installed...Make sure it's installed if you haven't already done so."
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conflux
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Whittle
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - benwhittle31@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - conflux.gemspec
101
+ - lib/conflux.rb
102
+ - lib/conflux/helpers.rb
103
+ - lib/conflux/railtie.rb
104
+ - lib/conflux/version.rb
105
+ - lib/tasks/conflux.rake
106
+ homepage: https://www.github.com/GoConflux/conflux-rb
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.5.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Gem to fetch and make available Conflux configs on Rails boot.
130
+ test_files: []