slack-ruby-bot-server 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +11 -9
  3. data/CHANGELOG.md +7 -1
  4. data/Gemfile +16 -0
  5. data/README.md +31 -4
  6. data/UPGRADING.md +25 -0
  7. data/lib/slack-ruby-bot-server.rb +6 -5
  8. data/lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb +1 -1
  9. data/lib/slack-ruby-bot-server/api/helpers/cursor_helpers.rb +31 -18
  10. data/lib/slack-ruby-bot-server/api/helpers/error_helpers.rb +12 -10
  11. data/lib/slack-ruby-bot-server/api/helpers/sort_helpers.rb +5 -1
  12. data/lib/slack-ruby-bot-server/api/middleware.rb +6 -1
  13. data/lib/slack-ruby-bot-server/api/presenters/status_presenter.rb +7 -3
  14. data/lib/slack-ruby-bot-server/app.rb +4 -23
  15. data/lib/slack-ruby-bot-server/config.rb +16 -0
  16. data/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb +27 -0
  17. data/lib/slack-ruby-bot-server/config/database_adapters/mongoid.rb +24 -0
  18. data/lib/slack-ruby-bot-server/models/team/activerecord.rb +13 -0
  19. data/lib/slack-ruby-bot-server/models/team/methods.rb +55 -0
  20. data/lib/slack-ruby-bot-server/models/team/mongoid.rb +22 -0
  21. data/lib/slack-ruby-bot-server/version.rb +1 -1
  22. data/sample_apps/README.md +11 -0
  23. data/{sample_app → sample_apps/sample_app_activerecord}/.rspec +0 -0
  24. data/sample_apps/sample_app_activerecord/.standalone_migrations +3 -0
  25. data/sample_apps/sample_app_activerecord/Gemfile +23 -0
  26. data/{sample_app → sample_apps/sample_app_activerecord}/Procfile +0 -0
  27. data/sample_apps/sample_app_activerecord/README.md +11 -0
  28. data/sample_apps/sample_app_activerecord/Rakefile +13 -0
  29. data/{sample_app → sample_apps/sample_app_activerecord}/commands.rb +0 -0
  30. data/{sample_app → sample_apps/sample_app_activerecord}/commands/help.rb +0 -0
  31. data/{sample_app → sample_apps/sample_app_activerecord}/commands/whoami.rb +0 -0
  32. data/sample_apps/sample_app_activerecord/config.ru +21 -0
  33. data/{sample_app → sample_apps/sample_app_activerecord}/config/newrelic.yml +0 -0
  34. data/sample_apps/sample_app_activerecord/config/postgresql.yml +17 -0
  35. data/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb +13 -0
  36. data/sample_apps/sample_app_activerecord/db/schema.rb +26 -0
  37. data/sample_apps/sample_app_activerecord/spec/api/root_spec.rb +16 -0
  38. data/{sample_app → sample_apps/sample_app_activerecord}/spec/commands/help_spec.rb +0 -0
  39. data/{sample_app → sample_apps/sample_app_activerecord}/spec/commands/whoami_spec.rb +0 -0
  40. data/sample_apps/sample_app_activerecord/spec/spec_helper.rb +19 -0
  41. data/sample_apps/sample_app_mongoid/.rspec +3 -0
  42. data/{sample_app → sample_apps/sample_app_mongoid}/Gemfile +6 -1
  43. data/sample_apps/sample_app_mongoid/Procfile +1 -0
  44. data/{sample_app → sample_apps/sample_app_mongoid}/README.md +13 -0
  45. data/{sample_app → sample_apps/sample_app_mongoid}/Rakefile +0 -0
  46. data/sample_apps/sample_app_mongoid/commands.rb +2 -0
  47. data/sample_apps/sample_app_mongoid/commands/help.rb +19 -0
  48. data/sample_apps/sample_app_mongoid/commands/whoami.rb +6 -0
  49. data/{sample_app → sample_apps/sample_app_mongoid}/config.ru +0 -0
  50. data/{sample_app → sample_apps/sample_app_mongoid}/config/mongoid.yml +0 -0
  51. data/sample_apps/sample_app_mongoid/config/newrelic.yml +217 -0
  52. data/sample_apps/sample_app_mongoid/spec/api/root_spec.rb +16 -0
  53. data/sample_apps/sample_app_mongoid/spec/commands/help_spec.rb +14 -0
  54. data/sample_apps/sample_app_mongoid/spec/commands/whoami_spec.rb +14 -0
  55. data/{sample_app → sample_apps/sample_app_mongoid}/spec/spec_helper.rb +2 -0
  56. data/slack-ruby-bot-server.gemspec +1 -5
  57. metadata +43 -76
  58. data/lib/slack-ruby-bot-server/models/team.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 405235dc495bea8024cf67229a77d93511c5b88e
4
- data.tar.gz: 0034902639a9d5e9bcc9bffb6cf8b1c62c007873
3
+ metadata.gz: 5b91c82bc0a49452c0b3871786fb739b763c8a25
4
+ data.tar.gz: 12e73c3db13dcaba4da39f499f0acb797e2db122
5
5
  SHA512:
6
- metadata.gz: 93bf915299dd2309ce6b2a817273f95483c1a4b37a3624cf843b22646890eff29fa0e26365baf8f9326a740d9e8e2302d87fbc5a92dc8cd953d0af02e1203e6d
7
- data.tar.gz: d69d616b93a479aee8401cdc24dd790405ef48b21d8ae986bd1b01d3e2b5c1931747b7ed2b17fa7935da172e30b5257b7a7f39d0335ac0ccd45988659a652afb
6
+ metadata.gz: bbf49bff1a3f7723468cc5841c68df08de3598451f1868ba4084262faa89a5a044ccbd1ea5a6a57cbbc8aadce2841681fb4e31c82a3d25c151af4099e13c3a5c
7
+ data.tar.gz: d9a0fa731ec1b9da48f4ef0e73189ebd6acf473e2f3bb5416e39acc5c1c0385aeea922b1e6d99d137cf2d8101a32356100822741f835b50df7a67a9e0062ba21
@@ -2,22 +2,24 @@ language: ruby
2
2
 
3
3
  cache: bundler
4
4
 
5
- services:
6
- - mongodb
7
-
8
5
  matrix:
9
6
  include:
10
7
  - rvm: 2.3.1
11
8
  script:
12
9
  - bundle exec danger
13
10
  - rvm: 2.3.1
14
- - rvm: 2.3.0
15
- - rvm: ruby-head
16
- - rvm: jruby-head
17
- allow_failures:
18
- - rvm: ruby-head
19
- - rvm: jruby-head
11
+ env: DATABASE_ADAPTER=activerecord
12
+ services:
13
+ - postgresql
14
+ - rvm: 2.3.1
15
+ env: DATABASE_ADAPTER=mongoid
16
+ services:
17
+ - mongodb
20
18
 
21
19
  before_install:
22
20
  - "export DISPLAY=:99.0"
23
21
  - "sh -e /etc/init.d/xvfb start"
22
+
23
+ script:
24
+ - bundle exec rake
25
+ - BUNDLE_GEMFILE=Gemfile && cd sample_apps/sample_app_$DATABASE_ADAPTER && bundle && bundle exec rake
@@ -1,9 +1,15 @@
1
1
  ### Changelog
2
2
 
3
+ #### 0.6.0 (3/12/2017)
4
+
5
+ * [#38](https://github.com/slack-ruby/slack-ruby-bot-server/issues/7): Added ActiveRecord support - [@zachfeldman](https://github.com/zachfeldman), [@spencerldixon](https://github.com/spencerldixon), [@dblock](https://github.com/dblock).
6
+ * [#45](https://github.com/slack-ruby/slack-ruby-bot-server/pull/45): Updated grape-roar to 0.4.0 - [@swalberg](https://github.com/swalberg).
7
+ * [#49](https://github.com/slack-ruby/slack-ruby-bot-server/pull/49): Run tests in the sample apps part of CI - [@dblock](https://github.com/dblock).
8
+
3
9
  #### 0.5.0 (2/9/2017)
4
10
 
5
11
  * [#43](https://github.com/slack-ruby/slack-ruby-bot-server/pull/43): Fix: `cannot load kaminari/grape` - [@dblock](https://github.com/dblock).
6
- * [#43](https://github.com/slack-ruby/slack-ruby-bot-server/pull/43): Fix: `undefined method `[]' for nil:NilClass` in APIs - [@dblock](https://github.com/dblock).
12
+ * [#43](https://github.com/slack-ruby/slack-ruby-bot-server/pull/43): Fix: `undefined method '[]' for nil:NilClass` in APIs - [@dblock](https://github.com/dblock).
7
13
  * [#28](https://github.com/slack-ruby/slack-ruby-bot-server/pull/28): Use slack-ruby-danger gem - [@dblock](https://github.com/dblock).
8
14
  * [#31](https://github.com/slack-ruby/slack-ruby-bot-server/pull/31): Adds MONGODB_URI as environment variable for MongoDB - [@corprew](https://github.com/corprew).
9
15
 
data/Gemfile CHANGED
@@ -1,5 +1,21 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ case ENV['DATABASE_ADAPTER']
4
+ when 'mongoid' then
5
+ gem 'mongoid'
6
+ gem 'kaminari-mongoid'
7
+ gem 'mongoid-scroll'
8
+ when 'activerecord' then
9
+ gem 'pg'
10
+ gem 'activerecord'
11
+ gem 'otr-activerecord'
12
+ gem 'cursor_pagination'
13
+ when nil then
14
+ warn "Missing ENV['DATABASE_ADAPTER']."
15
+ else
16
+ warn "Invalid ENV['DATABASE_ADAPTER']: #{ENV['DATABASE_ADAPTER']}."
17
+ end
18
+
3
19
  gemspec
4
20
 
5
21
  group :development, :test do
data/README.md CHANGED
@@ -13,11 +13,11 @@ A library that contains a [Grape](http://github.com/ruby-grape/grape) API servin
13
13
 
14
14
  ### Stable Release
15
15
 
16
- You're reading the documentation for the **stable** release of slack-ruby-bot-server, 0.5.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
16
+ You're reading the documentation for the **stable** release of slack-ruby-bot-server, 0.6.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
17
17
 
18
18
  ### Try Me
19
19
 
20
- A demo version of the [sample app](sample_app) is running on Heroku at [slack-ruby-bot-server.herokuapp.com](https://slack-ruby-bot-server.herokuapp.com). Use the _Add to Slack_ button. The bot will join your team as _@slackbotserver_.
20
+ A demo version of the [sample app with mongoid](sample_apps/sample_app_mongoid) is running on Heroku at [slack-ruby-bot-server.herokuapp.com](https://slack-ruby-bot-server.herokuapp.com). Use the _Add to Slack_ button. The bot will join your team as _@slackbotserver_.
21
21
 
22
22
  ![](images/slackbutton.gif)
23
23
 
@@ -27,9 +27,36 @@ Once a bot is registered, you can invite to a channel with `/invite @slackbotser
27
27
 
28
28
  ### Run Your Own
29
29
 
30
- You can use the [sample application](sample_app) to bootstrap your project and start adding slack command handlers on top of this code.
30
+ You can use one of the [sample applications](sample_apps) to bootstrap your project and start adding slack command handlers on top of this code. A database is required to store teams.
31
31
 
32
- Install [MongoDB](https://www.mongodb.org/downloads), required to store teams. We would like your help with [supporting other databases](https://github.com/slack-ruby/slack-ruby-bot-server/issues/12).
32
+ ### MongoDB
33
+
34
+ Use MongoDB with [Mongoid](https://github.com/mongodb/mongoid) as ODM. Configure the database connection in `mongoid.yml`. Add the `mongoid` gem in your Gemfile.
35
+
36
+ ```
37
+ gem 'mongoid'
38
+ gem 'kaminari-mongoid'
39
+ gem 'mongoid-scroll'
40
+ gem 'slack-ruby-bot-server'
41
+ ```
42
+
43
+ See the [sample app using Mongoid](sample_apps/sample_app_mongoid) for more information.
44
+
45
+ ### ActiveRecord
46
+
47
+ Use ActiveRecord with, for example, PostgreSQL via [pg](https://github.com/ged/ruby-pg). Configure the database connection in `postgresql.yml`. Add the `activerecord`, `pg`, `otr-activerecord` and `cursor_pagination` gems to your Gemfile.
48
+
49
+ ```
50
+ gem 'pg'
51
+ gem 'activerecord', require: 'active_record'
52
+ gem 'slack-ruby-bot-server'
53
+ gem 'otr-activerecord'
54
+ gem 'cursor_pagination'
55
+ ```
56
+
57
+ See the [sample app using ActiveRecord](sample_apps/sample_app_activerecord) for more information.
58
+
59
+ ### Usage
33
60
 
34
61
  [Create a New Application](https://api.slack.com/applications/new) on Slack.
35
62
 
@@ -1,6 +1,31 @@
1
1
  Upgrading Slack-Ruby-Bot-Server
2
2
  ===============================
3
3
 
4
+ ### Upgrading to >= 0.6.0
5
+
6
+ #### Mongoid and ActiveRecord support
7
+
8
+ Version 0.6.0 supports both Mongoid and ActiveRecord. The `mongoid` gem is no longer a dependency, so you must manually add the gems in your Gemfile.
9
+
10
+ ##### Mongoid
11
+
12
+ ```
13
+ gem 'mongoid'
14
+ gem 'slack-ruby-bot-server'
15
+ ```
16
+
17
+ ##### ActiveRecord (with PostgreSQL)
18
+
19
+ ```
20
+ gem 'pg'
21
+ gem 'activerecord', require: 'active_record'
22
+ gem 'slack-ruby-bot-server'
23
+ ```
24
+
25
+ The order matters, and the driver is required _first_, otherwise you will get a `One of "mongoid" or "activerecord" is required.` error.
26
+
27
+ See [#48](https://github.com/slack-ruby/slack-ruby-bot-server/pull/48) for more information.
28
+
4
29
  ### Upgrading to >= 0.4.0
5
30
 
6
31
  #### Add giphy to your Gemfile for GIF support
@@ -1,15 +1,16 @@
1
1
  require 'celluloid/current'
2
- require 'kaminari/grape'
3
- require 'mongoid-scroll'
2
+
4
3
  require 'grape-swagger'
5
4
  require 'slack-ruby-bot'
5
+ require 'slack-ruby-bot-server/server'
6
+ require 'slack-ruby-bot-server/config'
6
7
 
7
8
  require 'slack-ruby-bot-server/ext'
8
9
  require 'slack-ruby-bot-server/version'
9
10
  require 'slack-ruby-bot-server/info'
10
- require 'slack-ruby-bot-server/models'
11
+
12
+ require "slack-ruby-bot-server/config/database_adapters/#{SlackRubyBotServer::Config.database_adapter}.rb"
13
+
11
14
  require 'slack-ruby-bot-server/api'
12
15
  require 'slack-ruby-bot-server/app'
13
- require 'slack-ruby-bot-server/server'
14
- require 'slack-ruby-bot-server/config'
15
16
  require 'slack-ruby-bot-server/service'
@@ -26,7 +26,7 @@ module SlackRubyBotServer
26
26
  get do
27
27
  teams = Team.all
28
28
  teams = teams.active if params[:active]
29
- teams = paginate_and_sort_by_cursor(teams, default_sort_order: '-_id')
29
+ teams = paginate_and_sort_by_cursor(teams, default_sort_order: '-id')
30
30
  present teams, with: Presenters::TeamsPresenter
31
31
  end
32
32
 
@@ -8,28 +8,41 @@ module SlackRubyBotServer
8
8
  # returns a hash:
9
9
  # results: (paginated collection subset)
10
10
  # next: (cursor to the next page)
11
- def paginate_by_cursor(coll, &_block)
12
- raise 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor)
13
- results = { results: [], next: nil }
14
- size = (params[:size] || 10).to_i
15
- if params.key?(:offset)
16
- skip = params[:offset].to_i
17
- coll = coll.skip(skip)
11
+ if SlackRubyBotServer::Config.mongoid?
12
+ def paginate_by_cursor(coll, _options)
13
+ raise 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor)
14
+ results = { results: [], next: nil }
15
+ coll = coll.skip(params[:offset].to_i) if params.key?(:offset)
16
+ size = (params[:size] || 10).to_i
17
+ coll = coll.limit(size)
18
+ coll.scroll(params[:cursor]) do |record, next_cursor|
19
+ results[:results] << record if record
20
+ results[:next] = next_cursor.to_s
21
+ break if results[:results].count >= size
22
+ end
23
+ results[:total_count] = coll.count if params[:total_count] && coll.respond_to?(:count)
24
+ results
18
25
  end
19
- # some items may be skipped with a block
20
- query = block_given? ? coll : coll.limit(size)
21
- query.scroll(params[:cursor]) do |record, next_cursor|
22
- record = yield(record) if block_given?
23
- results[:results] << record if record
24
- results[:next] = next_cursor.to_s
25
- break if results[:results].count >= size
26
+ elsif SlackRubyBotServer::Config.activerecord?
27
+ def paginate_by_cursor(coll, options)
28
+ raise 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor)
29
+ results = { results: [], next: nil }
30
+ size = (params[:size] || 10).to_i
31
+ results[:total_count] = coll.count(:all) if params[:total_count]
32
+ coll = coll.offset(params[:offset].to_i) if params.key?(:offset)
33
+ sort_options = {}
34
+ sort_order(options).each do |order|
35
+ sort_options[order[:column]] = { reverse: true } if order[:direction] == :desc
36
+ end
37
+ coll = coll.cursor(params[:cursor], columns: sort_options).per(size)
38
+ results[:results] = coll.to_a
39
+ results[:next] = coll.next_cursor.to_s unless coll.last_page?
40
+ results
26
41
  end
27
- results[:total_count] = coll.count if params[:total_count] && coll.respond_to?(:count)
28
- results
29
42
  end
30
43
 
31
- def paginate_and_sort_by_cursor(coll, options = {}, &block)
32
- Hashie::Mash.new(paginate_by_cursor(sort(coll, options), &block))
44
+ def paginate_and_sort_by_cursor(coll, options = {})
45
+ Hashie::Mash.new(paginate_by_cursor(sort(coll, options), options))
33
46
  end
34
47
  end
35
48
  end
@@ -13,16 +13,18 @@ module SlackRubyBotServer
13
13
  rack_response(error.to_json, 400)
14
14
  end
15
15
  # rescue document validation errors into detail json
16
- rescue_from Mongoid::Errors::Validations do |e|
17
- backtrace = e.backtrace[0..5].join("\n ")
18
- Middleware.logger.warn "#{e.class.name}: #{e.message}\n #{backtrace}"
19
- rack_response({
20
- type: 'param_error',
21
- message: e.document.errors.full_messages.uniq.join(', ') + '.',
22
- detail: e.document.errors.messages.each_with_object({}) do |(k, v), h|
23
- h[k] = v.uniq
24
- end
25
- }.to_json, 400)
16
+ if SlackRubyBotServer::Config.mongoid?
17
+ rescue_from Mongoid::Errors::Validations do |e|
18
+ backtrace = e.backtrace[0..5].join("\n ")
19
+ Middleware.logger.warn "#{e.class.name}: #{e.message}\n #{backtrace}"
20
+ rack_response({
21
+ type: 'param_error',
22
+ message: e.document.errors.full_messages.uniq.join(', ') + '.',
23
+ detail: e.document.errors.messages.each_with_object({}) do |(k, v), h|
24
+ h[k] = v.uniq
25
+ end
26
+ }.to_json, 400)
27
+ end
26
28
  end
27
29
  rescue_from Grape::Exceptions::Validation do |e|
28
30
  backtrace = e.backtrace[0..5].join("\n ")
@@ -37,7 +37,11 @@ module SlackRubyBotServer
37
37
  def sort(coll, options = {})
38
38
  sort_order = sort_order(options)
39
39
  unless sort_order.empty?
40
- if coll.respond_to?(:asc) && coll.respond_to?(:desc)
40
+ if coll.respond_to?(:order)
41
+ sort_order.each do |s|
42
+ coll = coll.order(s[:column] => s[:direction])
43
+ end
44
+ elsif coll.respond_to?(:asc) && coll.respond_to?(:desc)
41
45
  sort_order.each do |s|
42
46
  coll = coll.send(s[:direction], s[:column])
43
47
  end
@@ -1,4 +1,7 @@
1
- %w(rack/cors rack-rewrite rack-server-pages).each { |l| require l }
1
+ require 'rack/cors'
2
+ require 'rack-rewrite'
3
+ require 'rack-server-pages'
4
+ require 'otr-activerecord' if SlackRubyBotServer::Config.activerecord?
2
5
 
3
6
  module SlackRubyBotServer
4
7
  module Api
@@ -12,6 +15,8 @@ module SlackRubyBotServer
12
15
 
13
16
  def self.instance
14
17
  @instance ||= Rack::Builder.new do
18
+ use OTR::ActiveRecord::ConnectionManagement if SlackRubyBotServer::Config.activerecord?
19
+
15
20
  use Rack::Cors do
16
21
  allow do
17
22
  origins '*'
@@ -14,10 +14,14 @@ module SlackRubyBotServer
14
14
  property :active_teams_count
15
15
  property :ping
16
16
 
17
- private
18
-
19
17
  def ping
20
- team = Team.asc(:_id).first
18
+ if SlackRubyBotServer::Config.mongoid?
19
+ team = Team.asc(:_id).first
20
+ elsif SlackRubyBotServer::Config.activerecord?
21
+ team = Team.last
22
+ else
23
+ raise 'Unsupported database driver.'
24
+ end
21
25
  return unless team
22
26
  team.ping!
23
27
  end
@@ -1,10 +1,8 @@
1
1
  module SlackRubyBotServer
2
2
  class App
3
3
  def prepare!
4
- silence_loggers!
5
- check_mongodb_provider!
6
4
  check_database!
7
- create_indexes!
5
+ init_database!
8
6
  mark_teams_active!
9
7
  migrate_from_single_team!
10
8
  update_team_name_and_id!
@@ -25,29 +23,12 @@ module SlackRubyBotServer
25
23
  end
26
24
  end
27
25
 
28
- def silence_loggers!
29
- Mongoid.logger.level = Logger::INFO
30
- Mongo::Logger.logger.level = Logger::INFO
31
- end
32
-
33
- def check_mongodb_provider!
34
- return unless ENV['RACK_ENV'] == 'production'
35
- unless ENV['MONGO_URL'] || ENV['MONGOHQ_URI'] || ENV['MONGODB_URI'] || ENV['MONGOLAB_URI']
36
- raise "Missing ENV['MONGO_URL'], ENV['MONGOHQ_URI'], ENV['MONGODB_URI'], or ENV['MONGOLAB_URI']."
37
- end
38
- end
39
-
40
26
  def check_database!
41
- rc = Mongoid.default_client.command(ping: 1)
42
- return if rc && rc.ok?
43
- raise rc.documents.first['error'] || 'Unexpected error.'
44
- rescue Exception => e
45
- warn "Error connecting to MongoDB: #{e.message}"
46
- raise e
27
+ SlackRubyBotServer::DatabaseAdapter.check!
47
28
  end
48
29
 
49
- def create_indexes!
50
- ::Mongoid::Tasks::Database.create_indexes
30
+ def init_database!
31
+ SlackRubyBotServer::DatabaseAdapter.init!
51
32
  end
52
33
 
53
34
  def mark_teams_active!
@@ -3,9 +3,25 @@ module SlackRubyBotServer
3
3
  extend self
4
4
 
5
5
  attr_accessor :server_class
6
+ attr_accessor :database_adapter
6
7
 
7
8
  def reset!
8
9
  self.server_class = SlackRubyBotServer::Server
10
+ self.database_adapter = if defined?(::Mongoid)
11
+ :mongoid
12
+ elsif defined?(::ActiveRecord)
13
+ :activerecord
14
+ else
15
+ raise 'One of "mongoid" or "activerecord" is required.'
16
+ end
17
+ end
18
+
19
+ def activerecord?
20
+ database_adapter == :activerecord
21
+ end
22
+
23
+ def mongoid?
24
+ database_adapter == :mongoid
9
25
  end
10
26
 
11
27
  reset!
@@ -0,0 +1,27 @@
1
+ require 'slack-ruby-bot-server/models/team/activerecord.rb'
2
+
3
+ module SlackRubyBotServer
4
+ module DatabaseAdapter
5
+ def self.check!
6
+ ActiveRecord::Base.connection_pool.with_connection(&:active?)
7
+ raise 'Unexpected error.' unless ActiveRecord::Base.connected?
8
+ rescue StandardError => e
9
+ warn "Error connecting to PostgreSQL: #{e.message}"
10
+ raise e
11
+ end
12
+
13
+ def self.init!
14
+ return if ActiveRecord::Base.connection.tables.include?('teams')
15
+ ActiveRecord::Base.connection.create_table :teams do |t|
16
+ t.string :team_id
17
+ t.string :name
18
+ t.string :domain
19
+ t.string :token
20
+ t.boolean :active, default: true
21
+ t.timestamps
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ ::Boolean = Virtus::Attribute::Boolean
@@ -0,0 +1,24 @@
1
+ require 'slack-ruby-bot-server/models/team/mongoid.rb'
2
+ require 'kaminari/grape'
3
+ require 'mongoid-scroll'
4
+
5
+ module SlackRubyBotServer
6
+ module DatabaseAdapter
7
+ def self.check!
8
+ rc = Mongoid.default_client.command(ping: 1)
9
+ return if rc && rc.ok?
10
+ raise rc.documents.first['error'] || 'Unexpected error.'
11
+ rescue StandardError => e
12
+ warn "Error connecting to MongoDB: #{e.message}"
13
+ raise e
14
+ end
15
+
16
+ def self.init!
17
+ # create indexes
18
+ ::Mongoid::Tasks::Database.create_indexes
19
+ # silence loggers
20
+ Mongoid.logger.level = Logger::INFO
21
+ Mongo::Logger.logger.level = Logger::INFO
22
+ end
23
+ end
24
+ end