relax-rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: adff098dfab3b6235f2d925db88d2a1a7c626d0f
4
+ data.tar.gz: b7f2c5ad14d0bafacfdbabbd9aa8137b4f8ba7c6
5
+ SHA512:
6
+ metadata.gz: 639d3c3e543bc5959ef656143c130d15e10f0cb5840c0c6f0197bac3ea6af0465a30439ab9c024a75d808a6b69c52952b41fd7eb7d5353ffe8ca4affe7184a11
7
+ data.tar.gz: 0c0fa52320c7af2cbc79a4971b9424f251136e4c89b8c9f4d43b6269f46f64603570eacd087efc2040e7c4863337c3abdc2ab7309f9a810e720507a2528ab963
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
+
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ services:
5
+ - redis-server
6
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in relax-rb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 arunthampi
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,130 @@
1
+ # Relax
2
+
3
+ Relax is a Ruby client/consumer library for [relax](https://github.com/zerobotlabs/relax) – which is a multitenant message broker for Slack.
4
+
5
+ [![Travis Badge for relax-rb](https://api.travis-ci.org/zerobotlabs/relax-rb.svg?branch=master)](https://api.travis-ci.org/zerobotlabs/relax-rb)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'relax'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage (with Rails)
20
+
21
+ [Relax](https://github.com/zerobotlabs/relax) is meant to be used in
22
+ conjunction with a web app such as Rails. There are two primary
23
+ functions that this library lets you perform:
24
+
25
+ * Start Slack bots on Relax
26
+
27
+ * Listen for events generated by Relax.
28
+
29
+ ### Setup (Environment Variables)
30
+
31
+ The Relax Ruby client requires a few environment variables to be set up
32
+ (these same environment variables are also used to set up the relax
33
+ message broker).
34
+
35
+ `RELAX_BOTS_KEY`: This can be any string value and is used to store state
36
+ about all Slack clients currently controlled by Relax in Redis. (Used by
37
+ `Relax::Bot`)
38
+
39
+ `RELAX_BOTS_PUBSUB`: This can be any string value and is used to notify
40
+ Relax brokers that a new Slack bot has been started. (Used by
41
+ `Relax::Bot`)
42
+
43
+ `RELAX_EVENTS_QUEUE`: This can be any string value and is used by Relax
44
+ brokers to send events to the client. (Used by `Relax::EventListener`)
45
+
46
+ For a full list of events that are sent from Relax brokers, visit the
47
+ [Relax Github page](https://github.com/zerobotlabs/relax).
48
+
49
+ ### Starting Bots
50
+
51
+ To start a bot, or update a bot with a new token, call
52
+ `Relax::Bot.start!(team_uid, token)` where `team_uid` is the UID of the
53
+ team (generated by the Slack API) and `token` is the API token for the
54
+ bot (again generated by the Slack API).
55
+
56
+ If a token is invalid or a bot connection is unsuccesful, you will
57
+ receive a `disable_bot` event.
58
+
59
+ ### Listening for Events
60
+
61
+ Events are queued in Redis by Relax brokers in the `REDIS_EVENTS_QUEUE`
62
+ key and `Relax::EventListener.listen!` listens for events and invokes
63
+ the callback that is set by `Relax::EventListener.callback=`.
64
+
65
+ #### Setting the callback
66
+
67
+ The recommended way to set the callback method for events in Rails is an
68
+ initializer, for e.g. in a file `config/initializers/relax.rb`:
69
+
70
+ ```ruby
71
+ callback = Proc.new do |event|
72
+ Rails.logger.info "received event: #{event}"
73
+ # handle event
74
+ end
75
+
76
+ Relax::EventListener.callback = callback
77
+ ```
78
+
79
+ The callback can also be a class (or instance) method, in which case you should do
80
+ something like this:
81
+
82
+ ```ruby
83
+ class RelaxEventHandler
84
+ def self.handle_event(event)
85
+ Rails.logger.info "received event: #{event}"
86
+ # handle event
87
+ end
88
+ end
89
+
90
+ Relax::EventListener.callback = RelaxEventHandler.method(:handle_event)
91
+ ```
92
+
93
+ #### Starting the Listener
94
+
95
+ The recommended way to start the listener in a Rails app is to use
96
+ [Foreman](https://github.com/ddollar/foreman). In your Procfile, create
97
+ an entry like this:
98
+
99
+ `relax: bundle exec rake relax:listen_for_events`
100
+
101
+ And this will invoke `Relax::EventListener.listen!` as part your of your
102
+ Rails application.
103
+
104
+ This process can be scaled indepedently from the rest of your web
105
+ application, and so the more events you get, the more "relax" listener
106
+ processes you can have.
107
+
108
+ #### Events
109
+
110
+ An Event is an instance of the
111
+ `[Relax::Event](https://github.com/zerobotlabs/relax-rb/blob/master/lib/relax/event.rb)` class.
112
+
113
+ Read more about the different types of events generated by Relax
114
+ [here](https://github.com/zerobotlabs/relax#events)
115
+
116
+ ## Development
117
+
118
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
119
+
120
+ 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).
121
+
122
+ ## Contributing
123
+
124
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zerobotlabs/relax-rb. 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.
125
+
126
+
127
+ ## License
128
+
129
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
130
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "relax/rb"
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
data/bin/setup ADDED
@@ -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
data/lib/relax/base.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Relax
2
+ class Base
3
+ def self.redis
4
+ if uri = ENV['REDISTOGO_URL']
5
+ redis_uri = URI.parse(uri)
6
+ elsif uri = ENV['REDIS_URL']
7
+ redis_uri = URI.parse(uri)
8
+ else
9
+ redis_uri = URI.parse("redis://localhost:6379")
10
+ end
11
+
12
+ @@redis ||= Redis.new(url: redis_uri, db: 0)
13
+ end
14
+ end
15
+ end
data/lib/relax/bot.rb ADDED
@@ -0,0 +1,29 @@
1
+ module Relax
2
+ class BotsKeyNotSetError < StandardError; end
3
+ class BotsPubsubNotSetError < StandardError; end
4
+
5
+ class Bot < Base
6
+ def self.start!(team_uid, token)
7
+ if relax_bots_key.nil? || relax_bots_key == ""
8
+ raise BotsKeyNotSetError, "Environment Variable RELAX_BOTS_KEY is not set"
9
+ end
10
+
11
+ if relax_bots_pubsub.nil? || relax_bots_pubsub == ""
12
+ raise BotsPubsubNotSetError, "Environment Variable RELAX_BOTS_PUBSUB is not set"
13
+ end
14
+
15
+ redis.multi do
16
+ redis.hset(relax_bots_key, team_uid, {team_id: team_uid, token: token}.to_json)
17
+ redis.publish(relax_bots_pubsub, {type: 'team_added', team_id: team_uid}.to_json)
18
+ end
19
+ end
20
+
21
+ def self.relax_bots_pubsub
22
+ ENV['RELAX_BOTS_PUBSUB']
23
+ end
24
+
25
+ def self.relax_bots_key
26
+ ENV['RELAX_BOTS_KEY']
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ module Relax
2
+ class Event
3
+ attr_accessor :type, :user_uid, :channel_uid, :team_uid, :im, :text,
4
+ :relax_bot_uid, :timestamp, :provider, :event_timestamp
5
+
6
+ def initialize(opts = {})
7
+ opts.each { |k,v| self.send("#{k}=", v) if self.respond_to?("#{k}=") }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,52 @@
1
+ module Relax
2
+ class EventsQueueNotSetError < StandardError; end
3
+
4
+ class EventListener < Base
5
+ DEFAULT_LOG_LEVEL = 'info'
6
+
7
+ @@callback = nil
8
+ @@logger = nil
9
+
10
+ def self.listen!
11
+ if relax_events_queue.nil? || relax_events_queue == ""
12
+ raise EventsQueueNotSetError, "Environment Variable RELAX_EVENTS_QUEUE is not set"
13
+ end
14
+
15
+ self.log("Listening for Relax Events...")
16
+
17
+ while true do
18
+ queue_name, event_json = redis.blpop(relax_events_queue)
19
+
20
+ if queue_name == relax_events_queue
21
+ event = Event.new(JSON.parse(event_json))
22
+ callback.call(event) if callback
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.log(text, level = DEFAULT_LOG_LEVEL)
28
+ return if logger.nil?
29
+ logger.send(level, "#{Time.now.strftime('%FT%T%z')}: #{text}")
30
+ end
31
+
32
+ def self.callback=(cb)
33
+ @@callback = cb
34
+ end
35
+
36
+ def self.callback
37
+ @@callback
38
+ end
39
+
40
+ def self.logger=(logger)
41
+ @@logger = logger
42
+ end
43
+
44
+ def self.logger
45
+ @@logger
46
+ end
47
+
48
+ def self.relax_events_queue
49
+ ENV['RELAX_EVENTS_QUEUE']
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,18 @@
1
+ require 'relax'
2
+ require 'rails'
3
+
4
+ module Relax
5
+ class Railtie < Rails::Railtie
6
+ initializer :after_initialize do
7
+ Relax::EventListener.logger ||= if defined?(Rails)
8
+ Rails.logger
9
+ elsif defined?(RAILS_DEFAULT_LOGGER)
10
+ RAILS_DEFAULT_LOGGER
11
+ end
12
+ end
13
+
14
+ rake_tasks do
15
+ load 'relax/tasks.rb'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ namespace :relax do
2
+ desc 'Start Listening for Events'
3
+ task listen_for_events: :environment do
4
+ Relax::EventListener.listen!
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Relax
2
+ VERSION = "0.1.0"
3
+ end
data/lib/relax.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "redis"
2
+ require "json"
3
+
4
+ require "relax/version"
5
+ require "relax/base"
6
+ require "relax/event"
7
+ require "relax/event_listener"
8
+ require "relax/bot"
9
+ require 'relax/railtie' if defined?(Rails::Railtie)
data/relax-rb.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'relax/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "relax-rb"
8
+ spec.version = Relax::VERSION
9
+ spec.authors = ["arunthampi"]
10
+ spec.email = ["arun@zerobotlabs.com"]
11
+
12
+ spec.summary = %q{relax-rb is a Ruby Client for Relax, which is a message broker for Slack's Real-Time API}
13
+ spec.description = %q{relax-rb is a Ruby Client for Relax, which is a message broker for Slack's Real-Time API}
14
+ spec.homepage = "http://relax.zerobotlabs.com"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "bin"
27
+ spec.executables = []
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "redis", "~> 3.2.1"
31
+ spec.add_dependency "json", "~> 1.8.3"
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.10"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.3.0"
36
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: relax-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - arunthampi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.3
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: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.3.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.3.0
83
+ description: relax-rb is a Ruby Client for Relax, which is a message broker for Slack's
84
+ Real-Time API
85
+ email:
86
+ - arun@zerobotlabs.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/relax.rb
102
+ - lib/relax/base.rb
103
+ - lib/relax/bot.rb
104
+ - lib/relax/event.rb
105
+ - lib/relax/event_listener.rb
106
+ - lib/relax/railtie.rb
107
+ - lib/relax/tasks.rb
108
+ - lib/relax/version.rb
109
+ - relax-rb.gemspec
110
+ homepage: http://relax.zerobotlabs.com
111
+ licenses:
112
+ - MIT
113
+ metadata:
114
+ allowed_push_host: https://rubygems.org
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.8
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: relax-rb is a Ruby Client for Relax, which is a message broker for Slack's
135
+ Real-Time API
136
+ test_files: []