redshift-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 67c83c57a7e840e5aedd9f7e898e19e29c686fd7
4
+ data.tar.gz: 83d397b157a2e917f76b985d70d776f7ddcade7b
5
+ SHA512:
6
+ metadata.gz: e17ca257c5596dac375318526f7d636b6b560f21cca0c4f015d555bea3691abed47ec57a1988dd18db78eeb5db702d1ea184802e201234bff6eb3114eb96f77f
7
+ data.tar.gz: 737debee56140d5b3190179f3be78ed2f43017a6249b15d3e6590349d39705cc9d205111a240355480723d1ea9da6e72a642873588c7e6f3dff0f3ab2ad875d3
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ addons:
5
+ postgresql: "9.4"
6
+ cache: bundler
7
+ before_script:
8
+ - createdb travis_ci_test
9
+ env:
10
+ - REDSHIFT_URL="redshift://postgres@localhost:5432/travis_ci_test"
11
+ script: bundle exec rake spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redshift-client.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Dai Akatsuka
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # Redshift::Client [![Build Status](https://travis-ci.org/dakatsuka/redshift-client.svg)](https://travis-ci.org/dakatsuka/redshift-client)
2
+
3
+ The ruby client for AWS Redshift.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'redshift-client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install redshift-client
20
+
21
+ ## Usage
22
+
23
+ Please set `REDSHIFT_URL` Env.
24
+
25
+ ```
26
+ $ export REDSHIFT_URL="redshift://user:password@*****.region.redshift.amazonaws.com:5439/dbname"
27
+ ```
28
+
29
+ ```ruby
30
+ require 'redshift/client'
31
+
32
+ Redshift::Client.establish_connection
33
+ Redshift::Client.connection # instance of PG::Connection
34
+
35
+ Redshift::Client.connection.exec("SELECT GETDATE()").first # => {"getdate"=>"2015-10-08 05:17:40"}
36
+ ```
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/redshift-client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
47
+
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
52
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redshift/client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ require "redshift/client/version"
2
+ require "redshift/client/errors"
3
+ require "redshift/client/configuration"
4
+ require "redshift/client/connection_handling"
5
+
6
+ module Redshift
7
+ module Client
8
+ extend ConnectionHandling
9
+ end
10
+ end
@@ -0,0 +1,36 @@
1
+ module Redshift
2
+ module Client
3
+ class Configuration
4
+ attr_reader :host, :port, :user, :password, :dbname
5
+
6
+ def self.parse(config)
7
+ if config.empty?
8
+ url = URI.parse(ENV["REDSHIFT_URL"])
9
+ self.new(url.host, url.port, url.user, url.password, url.path[1..-1])
10
+ else
11
+ self.new(config[:host], config[:port], config[:user], config[:password], config[:dbname])
12
+ end
13
+ rescue => e
14
+ raise ConfigurationError.new(e.message)
15
+ end
16
+
17
+ def initialize(host, port, user, password, dbname)
18
+ @host = host
19
+ @port = port || 5439
20
+ @user = user
21
+ @password = password
22
+ @dbname = dbname
23
+ end
24
+
25
+ def to_hash
26
+ {
27
+ host: @host,
28
+ port: @port,
29
+ user: @user,
30
+ password: @password,
31
+ dbname: @dbname
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,58 @@
1
+ require 'pg'
2
+ require 'uri'
3
+
4
+ module Redshift
5
+ module Client
6
+ module ConnectionHandling
7
+ def establish_connection(config = {})
8
+ disconnect
9
+ initialize_thread!
10
+ configure!(config)
11
+ end
12
+
13
+ def disconnect
14
+ connection.finish if connected?
15
+ cleanup_thread!
16
+ end
17
+
18
+ def connected?
19
+ !!(thread && thread[:connection])
20
+ end
21
+
22
+ def established?
23
+ !!(thread && thread[:configurations])
24
+ end
25
+
26
+ def connection
27
+ raise ConnectionNotEstablished.new unless established?
28
+
29
+ if connected?
30
+ thread[:connection]
31
+ else
32
+ connect!
33
+ end
34
+ end
35
+
36
+ private
37
+ def initialize_thread!
38
+ Thread.current[:redshift] = {}
39
+ end
40
+
41
+ def cleanup_thread!
42
+ Thread.current[:redshift] = nil
43
+ end
44
+
45
+ def thread
46
+ Thread.current[:redshift]
47
+ end
48
+
49
+ def connect!
50
+ thread[:connection] = PG.connect(thread[:configurations].to_hash)
51
+ end
52
+
53
+ def configure!(config)
54
+ thread[:configurations] = Configuration.parse(config)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,12 @@
1
+ module Redshift
2
+ module Client
3
+ class RedshiftClientError < StandardError
4
+ end
5
+
6
+ class ConfigurationError < RedshiftClientError
7
+ end
8
+
9
+ class ConnectionNotEstablished < RedshiftClientError
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Redshift
2
+ module Client
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'redshift/client/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "redshift-client"
7
+ spec.version = Redshift::Client::VERSION
8
+ spec.authors = ["Dai Akatsuka"]
9
+ spec.email = ["d.akatsuka@gmail.com"]
10
+
11
+ spec.summary = %q{The ruby client for AWS Redshift.}
12
+ spec.description = %q{The ruby client for AWS Redshift.}
13
+ spec.homepage = "https://github.com/dakatsuka/redshift-client"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "pg"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe Redshift::Client do
4
+ it 'has a version number' do
5
+ expect(Redshift::Client::VERSION).not_to be nil
6
+ end
7
+
8
+ describe '#establish_connection' do
9
+ it 'establishes connection' do
10
+ expect {
11
+ Redshift::Client.establish_connection
12
+ }.to change {
13
+ Redshift::Client.established?
14
+ }.from(false).to(true)
15
+ end
16
+ end
17
+
18
+ describe '#disconnect' do
19
+ before do
20
+ Redshift::Client.establish_connection
21
+ Redshift::Client.connection
22
+ end
23
+
24
+ it 'disconnects from database' do
25
+ expect {
26
+ Redshift::Client.disconnect
27
+ }.to change {
28
+ Redshift::Client.connected?
29
+ }.from(true).to(false)
30
+ end
31
+ end
32
+
33
+ describe '#connected?' do
34
+ context "when already connected" do
35
+ before do
36
+ Redshift::Client.establish_connection
37
+ Redshift::Client.connection
38
+ end
39
+
40
+ after do
41
+ Redshift::Client.disconnect
42
+ end
43
+
44
+ it "returns true" do
45
+ expect(Redshift::Client).to be_connected
46
+ end
47
+ end
48
+
49
+ context "when not yet connected" do
50
+ it "returns false" do
51
+ expect(Redshift::Client).not_to be_connected
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "#established?" do
57
+ context "when already established" do
58
+ before do
59
+ Redshift::Client.establish_connection
60
+ end
61
+
62
+ after do
63
+ Redshift::Client.disconnect
64
+ end
65
+
66
+ it "returns true" do
67
+ expect(Redshift::Client).to be_established
68
+ end
69
+ end
70
+
71
+ context "when not yet established" do
72
+ it "returns false" do
73
+ expect(Redshift::Client).not_to be_established
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'redshift/client'
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redshift-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dai Akatsuka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: The ruby client for AWS Redshift.
70
+ email:
71
+ - d.akatsuka@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/redshift/client.rb
86
+ - lib/redshift/client/configuration.rb
87
+ - lib/redshift/client/connection_handling.rb
88
+ - lib/redshift/client/errors.rb
89
+ - lib/redshift/client/version.rb
90
+ - redshift-client.gemspec
91
+ - spec/redshift/client_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: https://github.com/dakatsuka/redshift-client
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.5
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: The ruby client for AWS Redshift.
117
+ test_files:
118
+ - spec/redshift/client_spec.rb
119
+ - spec/spec_helper.rb