connection_url_resolver 0.0.0 → 1.0.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
  SHA256:
3
- metadata.gz: '08d73dbaa2242536ba547b2b95aa56213cb909c3544c7439b04f62673c5e1337'
4
- data.tar.gz: b5d64c818e01cd503e12d53b3a20d72b69fddd815ee48a97617ded090bb347e8
3
+ metadata.gz: 16b913895179d320a2a95e54e321589102aa947c796fc4dcf343b67045115cf7
4
+ data.tar.gz: 979bfb34de8c5878d16a8c3b274c7fb773739c6a73215690107d6c000c93309e
5
5
  SHA512:
6
- metadata.gz: 8252a34fb0a0fb68d6c848de706e07306959583f3782275e92fcd5dd678e1913634950d8fa1a209af3ab9bbe2760a6135853fef81ce992cc3d82f176d2aae8a3
7
- data.tar.gz: afd5fc8cbce2e13035dcd5a097faabd4e6d37289d468bf6f6ffdbcf6f02ced02744d8e6140d592615ec21d08814fc9b406ede2ba77ba3d4f27f5b15ad6f6612b
6
+ metadata.gz: 51d70d268d006fa299140da6bc5e419c169e17277fb2a7a81ed335002cc09ace0f853bae34157d83408a7376280c481d789d861fb20f6cc2e08b289c24f2db66
7
+ data.tar.gz: 3384bac2b355464140f559a2b7d627dd4bf90d9b53b82a6b622a7064067fc3fd989924c897522a035b3302ad4ef236dc9c707a2765a7e82d8b032c8a1e04f258
@@ -0,0 +1,7 @@
1
+ # Master
2
+
3
+ ## 1.0.0
4
+
5
+ 2018.02.05
6
+
7
+ - first implementation #2 @JuanitoFatas
data/Gemfile CHANGED
@@ -6,3 +6,4 @@ gemspec
6
6
  gem "bundler"
7
7
  gem "rake"
8
8
  gem "rspec", "~> 3.7"
9
+ gem "pry"
@@ -1,12 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- connection_url_resolver (0.0.0)
4
+ connection_url_resolver (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ coderay (1.1.2)
9
10
  diff-lcs (1.3)
11
+ method_source (0.9.0)
12
+ pry (0.11.3)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
10
15
  rake (12.3.0)
11
16
  rspec (3.7.0)
12
17
  rspec-core (~> 3.7.0)
@@ -28,6 +33,7 @@ PLATFORMS
28
33
  DEPENDENCIES
29
34
  bundler
30
35
  connection_url_resolver!
36
+ pry
31
37
  rake
32
38
  rspec (~> 3.7)
33
39
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # ConnectionUrlResolver
2
2
 
3
+ [![Build Status](https://travis-ci.org/JuanitoFatas/connection_url_resolver.svg?branch=master)](https://travis-ci.org/JuanitoFatas/connection_url_resolver)
4
+
3
5
  Resolve connection URL into Hash.
4
6
 
5
7
  ## Installation
@@ -20,8 +22,18 @@ Or install it yourself as:
20
22
 
21
23
  ## Usage
22
24
 
23
- ```
25
+ ```ruby
26
+ url = "mysql2://user:password@localhost:3306/app_dev"
24
27
  ConnectionUrlResolver.new(url).to_hash
28
+ # =>
29
+ {
30
+ "adapter" => "mysql2",
31
+ "username" => "user",
32
+ "password" => "password",
33
+ "port" => "3306",
34
+ "database" => "app_dev",
35
+ "host" => "localhost"
36
+ }
25
37
  ```
26
38
 
27
39
  ## License, Contributor's Guidelines
@@ -1,4 +1,20 @@
1
- require "connection_url_resolver/version"
1
+ require_relative "connection_url_resolver/version"
2
+ require_relative "connection_url_resolver/uri_decorator"
2
3
 
3
- module ConnectionUrlResolver
4
+ class ConnectionUrlResolver
5
+ def initialize(url)
6
+ raise NoURLError if url.empty?
7
+
8
+ @uri = URIDecorator.new(url)
9
+ end
10
+
11
+ def to_hash
12
+ uri.to_hash
13
+ end
14
+
15
+ private
16
+
17
+ NoURLError = Class.new(ArgumentError)
18
+
19
+ attr_reader :uri
4
20
  end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ class ConnectionUrlResolver
6
+ class URIDecorator
7
+ def initialize(url)
8
+ @uri = parser.parse(url)
9
+ end
10
+
11
+ def to_hash
12
+ connection_hash
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :uri
18
+
19
+ def parser
20
+ @_parser ||= URI::Parser.new
21
+ end
22
+
23
+ def connection_hash
24
+ unescape_values(connection_hash_with_values)
25
+ end
26
+
27
+ def connection_hash_with_values
28
+ query_hash.merge(
29
+ "adapter" => adapter,
30
+ "username" => user,
31
+ "password" => password,
32
+ "port" => port,
33
+ "database" => database,
34
+ "host" => host
35
+ ).reject { |_key, value| value.nil? || value.empty? }
36
+ end
37
+
38
+ def query_hash
39
+ query.split("&").map { |pair| pair.split("=") }.to_h
40
+ end
41
+
42
+ def query
43
+ uri.query || ""
44
+ end
45
+
46
+ def adapter
47
+ @_adapter ||= begin
48
+ adapter = uri.scheme && uri.scheme.tr("-", "_")
49
+
50
+ if adapter == "postgres"
51
+ "postgresql"
52
+ else
53
+ adapter
54
+ end
55
+ end
56
+ end
57
+
58
+ def user
59
+ uri.user
60
+ end
61
+
62
+ def password
63
+ uri.password
64
+ end
65
+
66
+ def port
67
+ uri.port.to_s
68
+ end
69
+
70
+ def database
71
+ if adapter == "sqlite3"
72
+ uri.path
73
+ else
74
+ uri.path.sub %r(^/), ""
75
+ end
76
+ end
77
+
78
+ def host
79
+ uri.hostname
80
+ end
81
+
82
+ def unescape_values(hash)
83
+ hash.transform_values { |value| unescape(value) }
84
+ end
85
+
86
+ def unescape(value)
87
+ if value.is_a?(String)
88
+ parser.unescape(value)
89
+ end
90
+ end
91
+ end
92
+ end
@@ -1,3 +1,5 @@
1
- module ConnectionUrlResolver
2
- VERSION = "0.0.0"
1
+ # frozen_string_literal: true
2
+
3
+ class ConnectionUrlResolver
4
+ VERSION = "1.0.0"
3
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: connection_url_resolver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juanito Fatas
@@ -20,6 +20,7 @@ files:
20
20
  - ".gitignore"
21
21
  - ".rspec"
22
22
  - ".travis.yml"
23
+ - CHANGELOG.md
23
24
  - Gemfile
24
25
  - Gemfile.lock
25
26
  - LICENSE.txt
@@ -29,6 +30,7 @@ files:
29
30
  - bin/setup
30
31
  - connection_url_resolver.gemspec
31
32
  - lib/connection_url_resolver.rb
33
+ - lib/connection_url_resolver/uri_decorator.rb
32
34
  - lib/connection_url_resolver/version.rb
33
35
  homepage: https://github.com/JuanitoFatas/connection_url_resolver
34
36
  licenses: