sf_commons 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -0
- data/Gemfile.lock +33 -0
- data/Rakefile +9 -0
- data/lib/sf_commons.rb +4 -0
- data/lib/sf_commons/cassandra_uri.rb +22 -0
- data/lib/sf_commons/mysql_uri.rb +23 -0
- data/lib/sf_commons/redis_uri.rb +14 -0
- data/lib/sf_commons/version.rb +3 -0
- data/sf_commons.gemspec +25 -0
- data/spec/lib/mysql_url_spec.rb +64 -0
- data/spec/lib/redis_uri_spec.rb +61 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +3 -0
- metadata +131 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sf_commons (0.0.1)
|
5
|
+
addressable
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.3.2)
|
11
|
+
diff-lcs (1.1.3)
|
12
|
+
metaclass (0.0.1)
|
13
|
+
mocha (0.13.2)
|
14
|
+
metaclass (~> 0.0.1)
|
15
|
+
rake (10.0.3)
|
16
|
+
rspec (2.12.0)
|
17
|
+
rspec-core (~> 2.12.0)
|
18
|
+
rspec-expectations (~> 2.12.0)
|
19
|
+
rspec-mocks (~> 2.12.0)
|
20
|
+
rspec-core (2.12.2)
|
21
|
+
rspec-expectations (2.12.1)
|
22
|
+
diff-lcs (~> 1.1.3)
|
23
|
+
rspec-mocks (2.12.2)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler
|
30
|
+
mocha
|
31
|
+
rake
|
32
|
+
rspec
|
33
|
+
sf_commons!
|
data/Rakefile
ADDED
data/lib/sf_commons.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class CassandraURI
|
2
|
+
def initialize(uri)
|
3
|
+
@uri = Addressable::URI.parse(uri)
|
4
|
+
raise "Are you sure this is a cassandra server" if @uri.scheme != 'cassandra'
|
5
|
+
@options = @uri.query_values || {}
|
6
|
+
@poolsize = @options.delete("pool") || 25
|
7
|
+
end
|
8
|
+
|
9
|
+
def keyspace
|
10
|
+
@uri.basename if @uri.basename
|
11
|
+
end
|
12
|
+
def pool_size
|
13
|
+
@poolsize.to_i
|
14
|
+
end
|
15
|
+
def server
|
16
|
+
@uri.authority
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_options
|
20
|
+
@options
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class MysqlURI
|
2
|
+
def initialize(uri)
|
3
|
+
@uri = Addressable::URI.parse(uri)
|
4
|
+
if @uri.scheme != 'mysql2' && @uri.scheme != 'mysql' && @uri.scheme != 'socket'
|
5
|
+
raise "Are you sure this is a mysql server, mysql2 and socket schemes are supported"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_options
|
10
|
+
defaults = {pool: 25, adapter: 'mysql2', encoding: 'utf8', reconnect: true, verify_connection: true}
|
11
|
+
if @uri.scheme == 'socket'
|
12
|
+
defaults.merge(socket: @uri.path, database: @uri.query_values['database'])
|
13
|
+
else
|
14
|
+
defaults.merge(host: @uri.host, port: @uri.port|| 3306).tap do |x|
|
15
|
+
x[:username]= @uri.user if @uri.user
|
16
|
+
x[:password]= @uri.password if @uri.password
|
17
|
+
x[:db] = x[:database] = @uri.path.gsub('/','') if @uri.path
|
18
|
+
x[:timeout] = @uri.query_values['timeout'].to_i if @uri.query_values && @uri.query_values.key?('timeout')
|
19
|
+
x[:pool] = @uri.query_values['pool'].to_i if @uri.query_values && @uri.query_values.key?('pool')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class RedisURI
|
2
|
+
def initialize(uri)
|
3
|
+
@uri = Addressable::URI.parse(uri)
|
4
|
+
raise "Are you sure this is a redis server" if @uri.scheme != 'redis'
|
5
|
+
end
|
6
|
+
|
7
|
+
def to_options
|
8
|
+
{host: @uri.host, port: @uri.port|| 6379}.tap do |x|
|
9
|
+
x[:password]= @uri.password if @uri.password
|
10
|
+
x[:db] = @uri.path.gsub('/','').to_i if @uri.path
|
11
|
+
x[:timeout] = @uri.query_values['timeout'].to_i if @uri.query_values && @uri.query_values.key?('timeout')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/sf_commons.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/sf_commons/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "sf_commons"
|
6
|
+
s.version = SfCommons::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Platform team"]
|
9
|
+
s.homepage = "http://github.com/althea/sf_commons.git"
|
10
|
+
s.description = s.summary = %q{commons extracted out...}
|
11
|
+
|
12
|
+
s.rubyforge_project = "sf_commons"
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_runtime_dependency(%q<addressable>)
|
21
|
+
|
22
|
+
s.add_development_dependency('rspec')
|
23
|
+
s.add_development_dependency('rake')
|
24
|
+
s.add_development_dependency('bundler')
|
25
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe MysqlURI do
|
4
|
+
it "parses simple urls" do
|
5
|
+
uri = MysqlURI.new('mysql://localhost')
|
6
|
+
options = uri.to_options
|
7
|
+
options[:host].should == 'localhost'
|
8
|
+
options[:port].should == 3306
|
9
|
+
end
|
10
|
+
|
11
|
+
it "raises error on a different adapter" do
|
12
|
+
begin
|
13
|
+
uri = MysqlURI.new('pg://localhost')
|
14
|
+
fail "Expected an error as the protocol is not supposed to be parsed"
|
15
|
+
rescue
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "defaults port to 3306 and allows mysq2" do
|
20
|
+
uri = MysqlURI.new('mysql2://localhost')
|
21
|
+
options = uri.to_options
|
22
|
+
options[:host].should == 'localhost'
|
23
|
+
options[:port].should == 3306
|
24
|
+
end
|
25
|
+
|
26
|
+
it "defaults standard things like poolsize and verify connection" do
|
27
|
+
uri = MysqlURI.new('mysql://localhost')
|
28
|
+
options = uri.to_options
|
29
|
+
options[:host].should == 'localhost'
|
30
|
+
options[:port].should == 3306
|
31
|
+
options[:pool].should == 25
|
32
|
+
options[:encoding].should == 'utf8'
|
33
|
+
options[:adapter].should == 'mysql2'
|
34
|
+
options[:verify_connection].should == true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "uses correct database" do
|
38
|
+
uri = MysqlURI.new('mysql2://localhost:4009/foobar')
|
39
|
+
options = uri.to_options
|
40
|
+
options[:host].should == 'localhost'
|
41
|
+
options[:port].should == 4009
|
42
|
+
options[:database].should == 'foobar'
|
43
|
+
|
44
|
+
end
|
45
|
+
#
|
46
|
+
it "uses correct username/password" do
|
47
|
+
uri = MysqlURI.new('mysql2://foo:bar@localhost/9')
|
48
|
+
options = uri.to_options
|
49
|
+
options[:host].should == 'localhost'
|
50
|
+
options[:password].should == 'bar'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "uses correct timeout" do
|
54
|
+
uri = MysqlURI.new('mysql2://localhost/foobar?timeout=3')
|
55
|
+
options = uri.to_options
|
56
|
+
options[:timeout].should == 3
|
57
|
+
end
|
58
|
+
|
59
|
+
it "uses correct connection poolsize" do
|
60
|
+
uri = MysqlURI.new('mysql2://localhost/foobar?timeout=3&pool=5')
|
61
|
+
options = uri.to_options
|
62
|
+
options[:pool].should == 5
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe RedisURI do
|
4
|
+
it "parses simple urls" do
|
5
|
+
uri = RedisURI.new('redis://localhost:6379')
|
6
|
+
options = uri.to_options
|
7
|
+
options[:host].should == 'localhost'
|
8
|
+
options[:port].should == 6379
|
9
|
+
end
|
10
|
+
|
11
|
+
it "defaults port to 6379" do
|
12
|
+
uri = RedisURI.new('redis://localhost')
|
13
|
+
options = uri.to_options
|
14
|
+
options[:host].should == 'localhost'
|
15
|
+
options[:port].should == 6379
|
16
|
+
end
|
17
|
+
|
18
|
+
it "defaults port to 6379" do
|
19
|
+
uri = RedisURI.new('redis://localhost')
|
20
|
+
options = uri.to_options
|
21
|
+
options[:host].should == 'localhost'
|
22
|
+
options[:port].should == 6379
|
23
|
+
end
|
24
|
+
|
25
|
+
it "defaults db to 0" do
|
26
|
+
uri = RedisURI.new('redis://localhost')
|
27
|
+
options = uri.to_options
|
28
|
+
options[:host].should == 'localhost'
|
29
|
+
options[:port].should == 6379
|
30
|
+
options[:db].should == 0
|
31
|
+
end
|
32
|
+
|
33
|
+
it "uses correct database" do
|
34
|
+
uri = RedisURI.new('redis://localhost/9')
|
35
|
+
options = uri.to_options
|
36
|
+
options[:host].should == 'localhost'
|
37
|
+
options[:port].should == 6379
|
38
|
+
options[:db].should == 9
|
39
|
+
end
|
40
|
+
|
41
|
+
it "uses correct password" do
|
42
|
+
uri = RedisURI.new('redis://foo:bar@localhost/9')
|
43
|
+
options = uri.to_options
|
44
|
+
options[:host].should == 'localhost'
|
45
|
+
options[:password].should == 'bar'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "uses correct timeout" do
|
49
|
+
uri = RedisURI.new('redis://localhost/9?timeout=3')
|
50
|
+
options = uri.to_options
|
51
|
+
options[:timeout].should == 3
|
52
|
+
end
|
53
|
+
|
54
|
+
it "ensures protocol is redis" do
|
55
|
+
begin
|
56
|
+
uri = RedisURI.new('foo://localhost')
|
57
|
+
fail
|
58
|
+
rescue
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sf_commons
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Platform team
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: addressable
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bundler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: commons extracted out...
|
79
|
+
email:
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files: []
|
83
|
+
files:
|
84
|
+
- Gemfile
|
85
|
+
- Gemfile.lock
|
86
|
+
- Rakefile
|
87
|
+
- lib/sf_commons.rb
|
88
|
+
- lib/sf_commons/cassandra_uri.rb
|
89
|
+
- lib/sf_commons/mysql_uri.rb
|
90
|
+
- lib/sf_commons/redis_uri.rb
|
91
|
+
- lib/sf_commons/version.rb
|
92
|
+
- sf_commons.gemspec
|
93
|
+
- spec/lib/mysql_url_spec.rb
|
94
|
+
- spec/lib/redis_uri_spec.rb
|
95
|
+
- spec/spec.opts
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
homepage: http://github.com/althea/sf_commons.git
|
98
|
+
licenses: []
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: 1065467839353441678
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
hash: 1065467839353441678
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project: sf_commons
|
123
|
+
rubygems_version: 1.8.24
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: commons extracted out...
|
127
|
+
test_files:
|
128
|
+
- spec/lib/mysql_url_spec.rb
|
129
|
+
- spec/lib/redis_uri_spec.rb
|
130
|
+
- spec/spec.opts
|
131
|
+
- spec/spec_helper.rb
|