little-dutch 0.0.1 → 0.0.3

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: 1f92b130feef056e373873258e6ea3bfef4a64da
4
- data.tar.gz: 7424c2e441ac3a9123723831c2fff2fb4c0f64c3
3
+ metadata.gz: 860c1ed9521b517a2f15da7e28a7b330a500046d
4
+ data.tar.gz: 4971d6c1436fc43a6991466b2f29e4a30a9f7ec4
5
5
  SHA512:
6
- metadata.gz: 12eb019369dc23b4acab5e131acc59d30d673f8658ad46f49a55928517a9611378f64335bc2f18368fb653eb9fb6c53a82f3f1d65d2a2db7e72b195f80d86475
7
- data.tar.gz: d00f8753f5e070bcffc10676d5414dc527317cb8f627214bda910f25b15ec9173f918e3ef4f3aac5e98414eefde8770a37ee9d604a5958caf80ecb9ff104bcef
6
+ metadata.gz: e88cd0e48e293413f7bb445f5b9a29ce433cc1acd92c7c6fc2002fdf7688aa9284906f0c377f72d273559e91fd557088ec1f7b161efae02cc1dec19ee10500fe
7
+ data.tar.gz: ccc4df27eec8bcb347daed3c2008f252139e5906b85918a0c34f3c3de3d953351336ecfe896141399087d36fa3cd410d1987d90f96ad317887f874cab862da92
data/lib/little-dutch.rb CHANGED
@@ -1,25 +1,17 @@
1
- require "little-dutch/version"
2
- require 'securerandom'
1
+ require 'active_support/all'
2
+ require 'faraday'
3
3
  require 'objspace'
4
+ require 'little-dutch/version'
5
+ require 'little-dutch/configuration'
4
6
 
5
7
  module LittleDutch
6
8
  extend self
7
9
 
8
- # Configuration
9
- INSTANCE_UUID = SecureRandom.uuid
10
- LITTLE_DUTCH_ENV = defined?(Rails) ? Rails.env : ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'
11
- LITTLE_DUTCH_CONFIG_FILE = File.expand_path File.join 'config', 'little-dutch.yml'
12
- CONFIG = YAML.load(ERB.new(File.read LITTLE_DUTCH_CONFIG_FILE).result)[LITTLE_DUTCH_ENV]
13
- INSTANCE_ID = [CONFIG['instance_name'] || 'web', INSTANCE_UUID].join('.')
14
- BATCH_SIZE = CONFIG['batch_size'] || 500
15
- ENDPOINT = CONFIG['endpoint'] || 'https://little-dutch.herokuapp.com'
16
- APP_NAME = (CONFIG['app_name'] || 'app').downcase
17
- INTERVAL = CONFIG['interval'] || 60
18
- TOKEN = CONFIG['token']
19
- ENABLED = !!CONFIG['enabled']
20
- LOG_FILE = File.expand_path File.join 'log', 'little-dutch.log'
21
- FileUtils.mkdir_p File.dirname LOG_FILE
22
- LOGGER = Logger.new LOG_FILE
10
+ CONFIG_FILE = File.expand_path File.join 'config', 'little-dutch.yml'
11
+
12
+ def config
13
+ @config ||= Configuration.new(CONFIG_FILE)
14
+ end
23
15
 
24
16
  def run
25
17
  stop
@@ -27,7 +19,7 @@ module LittleDutch
27
19
  end
28
20
 
29
21
  def start
30
- ENABLED ? run : :disabled
22
+ config.enabled ? run : :disabled
31
23
  end
32
24
 
33
25
  def stop
@@ -41,12 +33,12 @@ module LittleDutch
41
33
  loop do
42
34
  @runcount += 1
43
35
  post_json ObjectSpace.dump_all.tap(&:rewind)
44
- sleep INTERVAL
36
+ sleep config.interval
45
37
  end
46
38
  end
47
39
 
48
40
  def connection
49
- @connection ||= Faraday.new ENDPOINT do |conn|
41
+ @connection ||= Faraday.new config.endpoint do |conn|
50
42
  conn.request :multipart
51
43
  conn.request :url_encoded
52
44
  conn.adapter :net_http
@@ -54,10 +46,10 @@ module LittleDutch
54
46
  end
55
47
 
56
48
  def post_json(file)
57
- url = URI.parse(ENDPOINT)
58
- url.path = "/r/#{APP_NAME}/#{INSTANCE_ID}/#{@runcount}"
59
- url.query = { token: TOKEN }.to_param
60
- LOGGER.info url
49
+ url = URI.parse(config.endpoint)
50
+ url.path = "/r/#{config.app_name}/#{config.instance_id}/#{@runcount}"
51
+ url.query = { token: config.token }.to_param
52
+ config.logger.info url
61
53
  connection.post url, file: Faraday::UploadIO.new(file, 'application/json')
62
54
  file.close
63
55
  end
@@ -0,0 +1,73 @@
1
+ require 'securerandom'
2
+ require 'active_support/all'
3
+
4
+ module LittleDutch
5
+ module Configuration
6
+
7
+ DEFAULTS = {
8
+ 'enabled' => false,
9
+ 'endpoint' => 'https://littledutch.herokuapp.com',
10
+ 'interval' => 60,
11
+ 'appname' => 'app',
12
+ 'instance_name' => 'dev',
13
+ 'logfile' => File.expand_path File.join 'log', 'little-dutch.log'
14
+ }
15
+
16
+ class << self
17
+
18
+ def option_reader(*names)
19
+ names.each { |n| define_hash_reader n }
20
+ end
21
+
22
+ private
23
+
24
+ def define_option_reader(name)
25
+ define_method name do
26
+ @options[name.to_s]
27
+ end
28
+ end
29
+
30
+ def env
31
+ if defined?(Rails)
32
+ Rails.env
33
+ else
34
+ ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ option_reader :endpoint, :token, :enabled, :interval, :app_name,
41
+ :instance_name, :logfile
42
+ delegate :env, to: :class
43
+
44
+ def initialize(file)
45
+ @options = load(file)
46
+ FileUtils.mkdir_p File.dirname logfile
47
+ end
48
+
49
+ def instance_id
50
+ @instance_id ||= [instance_name, uuid].join '.'
51
+ end
52
+
53
+ def logger
54
+ @logger ||= Logger.new logfile
55
+ end
56
+
57
+ private
58
+
59
+ def uuid
60
+ @uuid ||= SecureRandom.uuid
61
+ end
62
+
63
+ def load(file)
64
+ return DEFAULTS unless File.exists?(file)
65
+ parse_yaml(File.read file).fetch(env) { {} }.reverse_merge DEFAULTS
66
+ end
67
+
68
+ def parse_yaml(contents)
69
+ YAML.load ERB.new(contents).result
70
+ end
71
+
72
+ end
73
+ end
@@ -1,5 +1,4 @@
1
1
  module LittleDutch
2
2
  class Engine < ::Rails::Engine
3
-
4
3
  end
5
4
  end
@@ -1,5 +1,5 @@
1
1
  module Little
2
2
  module Dutch
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/little-dutch.gemspec CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency 'faraday'
22
+ spec.add_dependency 'activesupport'
21
23
  spec.add_development_dependency "bundler", "~> 1.7"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
25
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: little-dutch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Waldrip
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-10 00:00:00.000000000 Z
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
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: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +80,7 @@ files:
52
80
  - Rakefile
53
81
  - config/initializers/little-dutch-init.rb
54
82
  - lib/little-dutch.rb
83
+ - lib/little-dutch/configuration.rb
55
84
  - lib/little-dutch/engine.rb
56
85
  - lib/little-dutch/version.rb
57
86
  - little-dutch.gemspec