frecon 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53470115c1a7a01b3b7850dd9f803951bd3cb407
4
- data.tar.gz: 820d7bc082d1bad50aac2aed9ea3c9b422fb8508
3
+ metadata.gz: 3695507c98a8055222ee241b01a4811d02f0aa96
4
+ data.tar.gz: 152e3d581b3f941762b1cba4b6dfb871408b042d
5
5
  SHA512:
6
- metadata.gz: edc0bebe5fbda796d7e4689db9cceff2eb4f91869e5ec38bf441eed9237ab51709cd065775cb6dd95d6729d8e28e87c55f20d46a03c2cec75a53e3fde86f8476
7
- data.tar.gz: 82e6c63b5d4b6650bc85ba1119ea565c7b82f7c1c24acc7ba5eb00dde28d18fcc4eb1b6b25690e656c834fbc1e3f02adfd804a2a346bf3982000b6d243da661c
6
+ metadata.gz: e8da7107dd168433d62a49c08104e3c4f675543e0837c6c3041b67edcfd78710ea2b9ec3c0608f46ac4fc2c39269c005e4536258bd6a17be324cea4f6502757b
7
+ data.tar.gz: f9811d2b31a5c30fc0597a9357be77122df2d052686857e506a7945b0e85b8635e7626e14b4b1f7312e3774c464508b23c2b6bdc62056609905d35251aa9ca7c
data/bin/frecon CHANGED
@@ -15,8 +15,85 @@ $LOAD_PATH.unshift(lib_directory) unless $LOAD_PATH.map { |directory| File.expan
15
15
 
16
16
  require "frecon"
17
17
 
18
- unless ARGV.include?("c") || ARGV.include?("console")
19
- FReCon::Server.start
20
- else
21
- FReCon::Console.start
18
+ require "optparse"
19
+
20
+ options = {
21
+ mode: :server,
22
+ configuration: {}
23
+ }
24
+
25
+ defaults = FReCon::ConfigurationFile.default.read
26
+
27
+ optparse = OptionParser.new do |opts|
28
+ opts.banner = <<EOF
29
+ Usage: #{$0} [OPTIONS] [MODE]
30
+
31
+ If MODE is not present, FReCon starts the Server. If MODE is present, starts
32
+ with "c", and contains any of the letters of the word "console" in order, FReCon
33
+ instead starts the Console.
34
+ EOF
35
+
36
+ opts.separator ""
37
+ opts.separator "Server OPTIONS:"
38
+
39
+ opts.on("-o", "--host HOST", "Bind to HOST (default: #{defaults["frecon"]["server"]["host"]})") do |host|
40
+ options[:configuration] ||= {}
41
+ options[:configuration]["frecon"] ||= {}
42
+ options[:configuration]["frecon"]["server"] ||= {}
43
+ options[:configuration]["frecon"]["server"]["host"] = host
44
+ end
45
+
46
+ opts.on("-p", "--port PORT", "Bind to port PORT (default: #{defaults["frecon"]["server"]["port"]})") do |port|
47
+ options[:configuration] ||= {}
48
+ options[:configuration]["frecon"] ||= {}
49
+ options[:configuration]["frecon"]["server"] ||= {}
50
+ options[:configuration]["frecon"]["server"]["port"] = port
51
+ end
52
+
53
+ opts.separator ""
54
+ opts.separator "General OPTIONS:"
55
+
56
+ server_environment = defaults["frecon"]["server"]["environment"]
57
+ console_environment = defaults["frecon"]["console"]["environment"]
58
+ default_environment_string = server_environment == console_environment ? server_environment : "server: #{server_environment}, console: #{console_environment}"
59
+
60
+ opts.on("-E", "--env ENVIRONMENT", "Run in the ENVIRONMENT environment (default: #{default_environment_string})") do |environment|
61
+ options[:configuration] ||= {}
62
+ options[:configuration]["frecon"] ||= {}
63
+ options[:configuration]["frecon"]["server"] ||= {}
64
+ options[:configuration]["frecon"]["server"]["environment"] = environment
65
+ options[:configuration]["frecon"]["console"] ||= {}
66
+ options[:configuration]["frecon"]["console"]["environment"] = environment
67
+ end
68
+
69
+ # opts.on("-v", "--verbose", "Run more verbosely.") do
70
+ # options[:output_level] = :verbose
71
+ # end
72
+
73
+ # opts.on("-d", "--debug", "Print debugging messages.") do
74
+ # options[:output_level] = :debug
75
+ # end
76
+
77
+ opts.on("-h", "--help", "Print this usage message.") do
78
+ puts opts
79
+ exit
80
+ end
81
+ end
82
+
83
+ optparse.parse!
84
+
85
+ ARGV.select do |arg|
86
+ case arg
87
+ when /^co?n?s?o?l?e?$/i
88
+ options[:mode] = :console
89
+ end
90
+ end
91
+
92
+ configuration = FReCon::Configuration.construct!(argument_configuration: options[:configuration])
93
+
94
+ case options[:mode]
95
+ when :server
96
+ FReCon::Server.start(configuration: configuration)
97
+ when :console
98
+ FReCon::Console.start(configuration: configuration)
22
99
  end
@@ -0,0 +1,32 @@
1
+ frecon:
2
+ server:
3
+ host: "localhost"
4
+ port: 4567
5
+ environment: "development"
6
+
7
+ console:
8
+ environment: "development"
9
+
10
+ database:
11
+ mongoid:
12
+ development:
13
+ sessions:
14
+ default:
15
+ database: frecon
16
+ hosts:
17
+ - localhost:27017
18
+
19
+ options:
20
+ use_utc: true
21
+ raise_not_found_error: false
22
+
23
+ production:
24
+ sessions:
25
+ default:
26
+ database: frecon
27
+ hosts:
28
+ - localhost:27017
29
+
30
+ options:
31
+ use_utc: true
32
+ raise_not_found_error: false
@@ -8,7 +8,7 @@
8
8
  # <http://opensource.org/licenses/MIT>.
9
9
 
10
10
  module FReCon
11
- VERSION = "0.4.0"
11
+ VERSION ||= "0.5.0"
12
12
 
13
13
  @environment_variable = :development
14
14
 
@@ -0,0 +1,67 @@
1
+ # lib/frecon/configuration.rb
2
+ #
3
+ # Copyright (C) 2015 Christopher Cooper, Sam Craig, Tiger Huang, Vincent Mai, Sam Mercier, and Kristofer Rye
4
+ #
5
+ # This file is part of FReCon, an API for scouting at FRC Competitions, which is
6
+ # licensed under the MIT license. You should have received a copy of the MIT
7
+ # license with this program. If not, please see
8
+ # <http://opensource.org/licenses/MIT>.
9
+
10
+ require "frecon/configuration_file"
11
+
12
+ module FReCon
13
+ class Configuration < Hash
14
+ def initialize(data)
15
+ data.each do |key, value|
16
+ self[key] = value
17
+ end
18
+ end
19
+
20
+ def to_h
21
+ hash = {}
22
+
23
+ self.each do |key, value|
24
+ case value
25
+ when Configuration
26
+ hash[key] = value.to_h
27
+ else
28
+ hash[key] = value
29
+ end
30
+ end
31
+
32
+ hash
33
+ end
34
+
35
+ def merge(other)
36
+ case other
37
+ when Configuration, Hash
38
+ other.each do |key, value|
39
+ case value
40
+ when Configuration, Hash
41
+ me = Configuration.new(self[key] || {})
42
+ me.merge(Configuration.new(value))
43
+ self[key] = me
44
+ else
45
+ self[key] = value
46
+ end
47
+ end
48
+ when nil
49
+ end
50
+ end
51
+
52
+ def self.construct!(default_configuration: ConfigurationFile.default.read,
53
+ system_configuration: ConfigurationFile.system.read,
54
+ user_configuration: ConfigurationFile.user.read,
55
+ argument_configuration: nil)
56
+
57
+ configuration_hierarchy = [default_configuration, system_configuration, user_configuration, argument_configuration]
58
+
59
+ configuration = Configuration.new({})
60
+ configuration_hierarchy.each do |other_configuration|
61
+ configuration.merge(other_configuration)
62
+ end
63
+
64
+ configuration
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,45 @@
1
+ # lib/frecon/configuration_file.rb
2
+ #
3
+ # Copyright (C) 2015 Christopher Cooper, Sam Craig, Tiger Huang, Vincent Mai, Sam Mercier, and Kristofer Rye
4
+ #
5
+ # This file is part of FReCon, an API for scouting at FRC Competitions, which is
6
+ # licensed under the MIT license. You should have received a copy of the MIT
7
+ # license with this program. If not, please see
8
+ # <http://opensource.org/licenses/MIT>.
9
+
10
+ require "yaml"
11
+ require "frecon/configuration"
12
+
13
+ module FReCon
14
+ class ConfigurationFile
15
+ attr_accessor :filename
16
+
17
+ def initialize(filename)
18
+ @filename = filename
19
+ end
20
+
21
+ def read
22
+ begin
23
+ data = open(@filename, "rb") do |io|
24
+ io.read
25
+ end
26
+
27
+ Configuration.new(YAML.load(data))
28
+ rescue Errno::ENOENT
29
+ nil
30
+ end
31
+ end
32
+
33
+ def self.default
34
+ self.new(File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "default.yml")))
35
+ end
36
+
37
+ def self.system
38
+ self.new(File.join("", "etc", "frecon", "config.yml"))
39
+ end
40
+
41
+ def self.user
42
+ self.new(File.join(Dir.home, "config", "frecon.yml"))
43
+ end
44
+ end
45
+ end
@@ -7,13 +7,17 @@
7
7
  # license with this program. If not, please see
8
8
  # <http://opensource.org/licenses/MIT>.
9
9
 
10
+ require "frecon/base/variables"
11
+ require "frecon/configuration"
10
12
  require "frecon/database"
11
13
  require "frecon/server"
12
14
 
13
15
  module FReCon
14
16
  class Console
15
- def self.start
16
- Database.setup(FReCon.environment)
17
+ def self.start(configuration: Configuration.construct!)
18
+ environment = configuration["frecon"]["console"]["environment"]
19
+ mongoid = configuration["frecon"]["database"]["mongoid"]
20
+ Database.setup(environment: environment, mongoid: mongoid)
17
21
 
18
22
  require "pry"
19
23
 
@@ -19,10 +19,11 @@ require "frecon/models"
19
19
 
20
20
  module FReCon
21
21
  class Database
22
- def self.setup(environment, mongoid_hash = nil)
23
- if mongoid_hash.is_a?(Hash)
22
+ def self.setup(environment: FReCon.environment, mongoid: nil)
23
+ if mongoid.is_a?(Hash)
24
24
  mongoid_tempfile = Tempfile.new("FReCon")
25
- mongoid_tempfile.write(mongoid_hash.to_yaml)
25
+
26
+ mongoid_tempfile.write(mongoid.to_h.to_yaml)
26
27
  mongoid_tempfile.rewind
27
28
 
28
29
  Mongoid.load!(mongoid_tempfile.path, environment)
data/lib/frecon/server.rb CHANGED
@@ -9,6 +9,8 @@
9
9
 
10
10
  require "sinatra/base"
11
11
 
12
+ require "frecon/base/variables"
13
+ require "frecon/configuration"
12
14
  require "frecon/database"
13
15
  require "frecon/routes"
14
16
  require "frecon/controllers"
@@ -21,15 +23,27 @@ module FReCon
21
23
  content_type "application/json"
22
24
  end
23
25
 
24
- def self.run!
25
- set :environment, FReCon.environment
26
- Database.setup(FReCon.environment)
26
+ def self.start(**keyword_arguments)
27
+ run!(**keyword_arguments)
28
+ end
27
29
 
28
- super
30
+ protected
31
+
32
+ def self.setup!(configuration: Configuration.construct!)
33
+ set :server, %w[thin HTTP webrick]
34
+ set :bind, configuration["frecon"]["server"]["host"]
35
+ set :port, configuration["frecon"]["server"]["port"]
36
+ set :environment, configuration["frecon"]["server"]["environment"]
37
+
38
+ mongoid = configuration["frecon"]["database"]["mongoid"]
39
+
40
+ Database.setup(environment: environment, mongoid: mongoid)
29
41
  end
30
42
 
31
- def self.start
32
- run!
43
+ def self.run!(**keyword_arguments)
44
+ setup!(**keyword_arguments)
45
+
46
+ super
33
47
  end
34
48
  end
35
49
  end
data/lib/frecon.rb CHANGED
@@ -11,6 +11,8 @@ require "mongoid"
11
11
 
12
12
  require "frecon/base"
13
13
 
14
+ require "frecon/configuration"
15
+ require "frecon/configuration_file"
14
16
  require "frecon/database"
15
17
  require "frecon/server"
16
18
  require "frecon/console"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frecon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Craig
@@ -80,11 +80,14 @@ extensions: []
80
80
  extra_rdoc_files: []
81
81
  files:
82
82
  - bin/frecon
83
+ - config/default.yml
83
84
  - lib/frecon.rb
84
85
  - lib/frecon/base.rb
85
86
  - lib/frecon/base/bson.rb
86
87
  - lib/frecon/base/object.rb
87
88
  - lib/frecon/base/variables.rb
89
+ - lib/frecon/configuration.rb
90
+ - lib/frecon/configuration_file.rb
88
91
  - lib/frecon/console.rb
89
92
  - lib/frecon/controller.rb
90
93
  - lib/frecon/controllers.rb