ruboty-postgres 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 11e4de665e99eed7b510e88ace66499b0ce0e653
4
+ data.tar.gz: 2623c04f9300a4744d97bf52a3aded42f5f265b5
5
+ SHA512:
6
+ metadata.gz: 973d9d438a2c7d0b34852b7312568bd79c5ea1ea6481e66f0c5af8009694edf621b95488bc298cdad6094e1254b17b9a3d19e6f28f807d63697d128b6edd2553
7
+ data.tar.gz: 1c3b792badeb2546cdbf932bf18e2563529e798b5284fca7a1bc26b500690909460e5bf207b04bb13a5a4035ad553d72b9a9b8a47a4af245b96e32f1c146690c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle/
11
+
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 SHIOYA, Hiromu
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.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Ruboty::Postgres
2
+
3
+ store memory of [ruboty](https://github.com/r7kamura/ruboty) in postgresql
4
+
5
+ ## ENV
6
+
7
+ - `POSTGRES_HOST`
8
+ - host of postgres (default: localhost)
9
+ - optional
10
+ - `POSTGRES_PORT`
11
+ - port number of postgres (default: 5432)
12
+ - optional
13
+ - `POSTGRES_USER`
14
+ - user name of postgres
15
+ - required
16
+ - `POSTGRES_PASSWORD`
17
+ - password of postgres
18
+ - required
19
+ - `POSTGRES_DBNAME`
20
+ - database name of postgres
21
+ - required
22
+ - `POSTGRES_NAMESPACE`
23
+ - relation name of postgres (default: ruboty)
24
+ - optional
25
+ - `POSTGRES_BOTNAME`
26
+ - name of your ruboty (default: ruboty)
27
+ - optional
28
+ - max length: 240
29
+ - `POSTGRES_SAVE_INTERVAL`
30
+ - sleep duration between each save (default: 10)
31
+ - optional
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
36
+
37
+ core of implementation refers to [r7kamura/ruboty-redis](https://github.com/r7kamura/ruboty-redis) with :sushi: .
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task default: :test
5
+
6
+ desc 'Run test_unit based test'
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'test'
9
+ t.test_files = Dir['test/**/test_*.rb']
10
+ t.verbose = true
11
+ end
@@ -0,0 +1,134 @@
1
+ require 'pg'
2
+
3
+ module Ruboty
4
+ module Brains
5
+ class Postgres < Base
6
+ attr_reader :thread
7
+
8
+ env :POSTGRES_HOST, 'host of postgres (default: localhost)', optional: true
9
+ env :POSTGRES_PORT, 'port number of postgres (default: 5432)', optional: true
10
+ env :POSTGRES_USER, 'user name of postgres', optional: false
11
+ env :POSTGRES_PASSWORD, 'password of postgres', optional: false
12
+ env :POSTGRES_DBNAME, 'database name of postgres', optional: false
13
+ env :POSTGRES_NAMESPACE, 'relation name of postgres (default: ruboty)', optional: true
14
+ env :POSTGRES_BOTNAME, 'name of your ruboty (default: ruboty)', optional: true
15
+ env :POSTGRES_SAVE_INTERVAL, 'sleep duration between each save (default: 10)', optional: true
16
+
17
+ def initialize
18
+ super
19
+ @thread = Thread.new { sync }
20
+ @thread.abort_on_exception = true
21
+ end
22
+
23
+ def data
24
+ @data ||= load || {}
25
+ end
26
+
27
+ # Override.
28
+ def validate!
29
+ super
30
+ Ruboty.die("#{self.class.usage}") unless host
31
+ end
32
+
33
+ private
34
+
35
+ def save
36
+ str = connection.escape_bytea(Marshal.dump(data))
37
+ connection.exec_prepared('save_statement', [str, botname])
38
+ end
39
+
40
+ def load
41
+ result = connection.exec_prepared('load_statement', [botname])
42
+ if result.count > 0
43
+ Marshal.load(connection.unescape_bytea(result.first['marshal']))
44
+ else
45
+ str = connection.escape_bytea(Marshal.dump({}))
46
+ sql = "INSERT INTO #{namespace} (botname, marshal) VALUES ($1, $2);"
47
+ connection.prepare('insert_statement', sql)
48
+ connection.exec_prepared('insert_statement', [botname, str])
49
+ {}
50
+ end
51
+ end
52
+
53
+ def sync
54
+ loop do
55
+ wait
56
+ save
57
+ end
58
+ end
59
+
60
+ def wait
61
+ sleep(interval)
62
+ end
63
+
64
+ def connection
65
+ if @connection.nil?
66
+ @connection = PG::connect(
67
+ host: host,
68
+ user: user,
69
+ password: password,
70
+ dbname: dbname,
71
+ port: port
72
+ )
73
+ migrate(@connection)
74
+ prepare_save_and_load_statement(@connection)
75
+ end
76
+ @connection
77
+ end
78
+
79
+ def host
80
+ ENV['POSTGRES_HOST'] || 'localhost'
81
+ end
82
+
83
+ def port
84
+ ENV['POSTGRES_PORT'] || 5432
85
+ end
86
+
87
+ def user
88
+ ENV['POSTGRES_USER'] || raise('ENV["POSTGRES_USER"] is required.')
89
+ end
90
+
91
+ def password
92
+ ENV['POSTGRES_PASSWORD'] || raise('ENV["POSTGRES_PASSWORD"] is required.')
93
+ end
94
+
95
+ def dbname
96
+ ENV['POSTGRES_DBNAME'] || raise('ENV["POSTGRES_DBNAME"] is required.')
97
+ end
98
+
99
+ def namespace
100
+ ENV['POSTGRES_NAMESPACE'] || 'ruboty'
101
+ end
102
+
103
+ def botname
104
+ ENV['POSTGRES_BOTNAME'] || 'ruboty'
105
+ end
106
+
107
+ def interval
108
+ ENV['POSTGRES_SAVE_INTERVAL'].to_i || 10
109
+ end
110
+
111
+ def migrate(conn)
112
+ unless relation_exists?(conn)
113
+ sql = "CREATE TABLE #{namespace} (botname VARCHAR(240) PRIMARY KEY, marshal BYTEA);"
114
+ conn.exec(sql)
115
+ load
116
+ end
117
+ end
118
+
119
+ def relation_exists?(conn)
120
+ sql = "SELECT relname FROM pg_class WHERE relkind = 'r' AND relname = $1;"
121
+ conn.prepare('exist_statement', sql)
122
+ result = conn.exec_prepared('exist_statement', [namespace])
123
+ return false if result.count == 0
124
+ result.first['relname'] == namespace
125
+ end
126
+
127
+ # only this statment is called repeatedly
128
+ def prepare_save_and_load_statement(conn)
129
+ conn.prepare('save_statement', "UPDATE #{namespace} SET marshal = $1 WHERE botname = $2;")
130
+ conn.prepare('load_statement', "SELECT marshal FROM #{namespace} WHERE botname = $1;")
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,2 @@
1
+ require 'ruboty/postgres/version'
2
+ require 'ruboty/brains/postgres'
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module Postgres
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'ruboty/postgres/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ruboty-postgres'
7
+ spec.version = Ruboty::Postgres::VERSION
8
+ spec.authors = ['SHIOYA, Hiromu']
9
+ spec.email = ['kwappa.856@gmail.com']
10
+
11
+ spec.summary = 'store memory of ruboty in postgresql'
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/kwappa/ruboty-postgres'
14
+ spec.license = 'MIT'
15
+
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
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_dependency 'ruboty'
23
+ spec.add_dependency 'pg'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.10'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'test-unit'
29
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-postgres
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - SHIOYA, Hiromu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruboty
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: pg
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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: store memory of ruboty in postgresql
98
+ email:
99
+ - kwappa.856@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/ruboty/brains/postgres.rb
110
+ - lib/ruboty/postgres.rb
111
+ - lib/ruboty/postgres/version.rb
112
+ - ruboty-postgres.gemspec
113
+ homepage: https://github.com/kwappa/ruboty-postgres
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.5.1
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: store memory of ruboty in postgresql
137
+ test_files: []