numerics 0.2.4 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,72 +5,72 @@ end
5
5
  require 'yajl'
6
6
 
7
7
  require 'numerics/connection'
8
+ require 'numerics/configuration'
8
9
 
9
10
  module Numerics
10
11
 
11
- VERSION = '0.2.4'
12
+ VERSION = '0.3.2'
12
13
 
13
- def self.connect(arg, env=nil)
14
- config = if arg.is_a?(Hash)
15
- arg
16
- else
17
- if arg.match(/\.json$/)
18
- File.open(arg){ |f| Yajl::Parser.parse(f) }
19
- elsif arg.match(/\.ya?ml$/)
20
- YAML::load_file(arg)
21
- else
22
- nil
23
- end
24
- end
14
+ def self.connect(arg, env=nil, key_set=nil)
15
+ configuration = if arg.is_a?(Numerics::Configuration)
16
+ if key_set.nil?
17
+ key_set = env
18
+ else
19
+ raise ArgumentError, 'no env required if you pass in a Configuration object'
20
+ end
21
+ arg
22
+ else
23
+ Numerics::Configuration.new(arg, env)
24
+ end
25
25
 
26
- if !config
27
- raise "Only json and yaml config files supported"
28
- end
29
-
30
- if env
31
- config = config[env.to_s] || config[env.to_sym]
32
- if !config
33
- raise "No #{env} found in #{arg}"
34
- end
35
- end
26
+ keys = if key_set.nil?
27
+ configuration.main
28
+ else
29
+ configuration.send(key_set)
30
+ end
36
31
 
37
- access_key = config[:access_key] || config['access_key']
38
- secret_key = config[:secret_key] || config['secret_key']
39
- host = config[:host] || config['host'] # nil means use the default
40
- port = config[:port] || config['port'] # nil means use the default
41
- disabled = config[:disabled] || config['disabled']
42
-
43
- if !access_key && !secret_key
44
- raise ArgumentError, 'Numerics.connect(config_file, env=nil) or Numerics.connect(:access_key => access_key, :secret_key => :secret_key)'
32
+ if !keys.access_key && !keys.secret_key
33
+ raise ArgumentError, 'Numerics.connect(config_file, env=nil, key_set=nil) or Numerics.connect(:access_key => access_key, :secret_key => :secret_key)'
45
34
  end
46
35
 
47
- Numerics::Connection.new(access_key, secret_key, host, port, disabled)
36
+ Numerics::Connection.new(keys.access_key, keys.secret_key, configuration.host, configuration.port, configuration.disabled?)
48
37
  end
49
38
 
50
- def self.config(arg, env=nil)
51
- @global_connection = self.connect(arg, env)
39
+ def self.configure(arg, env=nil, key_set=nil)
40
+ @global_configuration = Numerics::Configuration.new(arg, env)
41
+ @global_connection = self.connect(@global_configuration, key_set)
52
42
  end
53
43
 
54
- def self.global_connection
44
+ def self.connection
55
45
  @global_connection
56
46
  end
57
47
 
58
- def self.global_connection=(gc)
59
- @global_connection = gc
48
+ def self.configuration
49
+ @global_configuration
60
50
  end
61
51
 
62
52
  def self.respond_to?(method)
63
53
  !@global_connection.nil? && @global_connection.respond_to?(method)
64
54
  end
65
55
 
66
- ##@@ check syntax
67
56
  def self.method_missing(method, *args, &block)
68
57
  if self.respond_to?(method)
69
58
  @global_connection.send(method, *args, &block)
70
59
  else
71
60
  super
72
61
  end
62
+ end
73
63
 
64
+ JAVASCRIPT_URL = 'https://numerics.io/assets/numerics.js'
65
+ #CDN_JAVASCRIPT_URL = 'https://lib.numerics.io/assets/numerics.js'
66
+ CDN_JAVASCRIPT_URL = 'https://d19a9qlbf2kkmc.cloudfront.net/assets/numerics.js'
67
+
68
+ def self.javascript_url(cdn=true)
69
+ if cdn
70
+ self::CDN_JAVASCRIPT_URL
71
+ else
72
+ self::JAVASCRIPT_URL
73
+ end
74
74
  end
75
75
 
76
76
  end
@@ -0,0 +1,81 @@
1
+ require 'yajl'
2
+
3
+ module Numerics
4
+
5
+ class KeySet
6
+
7
+ attr_accessor :access_key, :secret_key
8
+
9
+ def initialize(keys)
10
+ @access_key = keys[:access_key] || keys['access_key'] || keys[:key] || keys['key']
11
+ @secret_key = keys[:secret_key] || keys['secret_key'] || keys[:secret] || keys['secret']
12
+ end
13
+
14
+ def to_hash
15
+ {:access_key => @access_key, :secret_key => @secret_key}
16
+ end
17
+
18
+ def to_json
19
+ Yajl::Encoder.encode(self.to_hash)
20
+ end
21
+
22
+ end
23
+
24
+ class Configuration
25
+
26
+ def initialize(arg, env=nil)
27
+ @config = if arg.is_a?(Hash)
28
+ arg
29
+ else
30
+ if arg.match(/\.json$/)
31
+ File.open(arg){ |f| Yajl::Parser.parse(f) }
32
+ elsif arg.match(/\.ya?ml$/)
33
+ YAML::load_file(arg)
34
+ else
35
+ nil
36
+ end
37
+ end
38
+
39
+ if !@config
40
+ raise "Only json and yaml config files supported"
41
+ end
42
+
43
+ if env
44
+ @config = @config[env.to_s] || @config[env.to_sym]
45
+ if !@config
46
+ raise "No #{env} found in #{arg}"
47
+ end
48
+ end
49
+ end
50
+
51
+ def main
52
+ KeySet.new(@config['main'] || @config[:main] || @config)
53
+ end
54
+
55
+ def disabled?
56
+ @config[:disabled] || @config['disabled']
57
+ end
58
+
59
+ def host
60
+ @config[:host] || @config['host']
61
+ end
62
+
63
+ def port
64
+ @config[:port] || @config['port']
65
+ end
66
+
67
+ def respond_to?(meth)
68
+ @config.has_key?(meth.to_s) || @config.has_key?(meth.to_sym) || super
69
+ end
70
+
71
+ def method_missing(meth)
72
+ keys = @config[meth.to_s] || @config[meth.to_sym]
73
+ if keys
74
+ KeySet.new(keys)
75
+ else
76
+ super
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -5,6 +5,8 @@ if RUBY_VERSION =~ /^1\.8/
5
5
  end
6
6
  require 'yajl'
7
7
 
8
+ require 'numerics/configuration'
9
+
8
10
  module Numerics
9
11
  class Connection
10
12
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: numerics
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.4
5
+ version: 0.3.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Lund
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-11-19 00:00:00 Z
13
+ date: 2012-03-06 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: yajl-ruby
@@ -34,6 +34,7 @@ extra_rdoc_files: []
34
34
  files:
35
35
  - lib/numerics.rb
36
36
  - lib/numerics/connection.rb
37
+ - lib/numerics/configuration.rb
37
38
  homepage: http://github.com/frequalize/numerics-ruby
38
39
  licenses: []
39
40