slappy 0.2.0 → 0.2.1

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
  SHA1:
3
- metadata.gz: 26e15337ee96df3976f78672ee576f6eb247e475
4
- data.tar.gz: 6fd8714fcf6a87aa094b11db890d511bb1aab457
3
+ metadata.gz: a38d4528b56aac0bc940bc257f66da45dc0d904f
4
+ data.tar.gz: a476d2ae9b9da850a08d5fdba7342d1ae019817d
5
5
  SHA512:
6
- metadata.gz: f73b384121b7fa2d7f48a53e9e265ac5206ddee17184f659a546db34f7f710e13ac026444e9fbbd6a28c5d8101aca185391956bec7cf686a8ef6142560ca9b5a
7
- data.tar.gz: 50b4a56ec88f0d5ab1158d14e57f451768ce4d1e4134bf4aa256238200e268f3aca15d4a37f600ffde2cef7f85fbffa52a5d3e4b413d470758897d47d3a0dddf
6
+ metadata.gz: 84194d504736578922db692aabf5649bbb395bc15a3dc4ea936405ffdf021ff67a9341f72064a783cf7707f84e1f6a5d5e7f1da5fc0e5ea338c6708f1b7f528f
7
+ data.tar.gz: 1915ef726371fa9e4bda4e8642b430054e955f60809c4e3fc7790e9c4c95510e4546c11a02342e83d923e9168cddfc5a6a9bb5619028f269e92cdfd5ef42b131
data/.hound.yml CHANGED
@@ -5,6 +5,7 @@ AllCops:
5
5
  - "Guardfile"
6
6
  - "slappy.gemspec"
7
7
  DisplayCopNames: true
8
+ RunRailsCops: false
8
9
 
9
10
  ##################### Style ##################################
10
11
 
data/.travis.yml CHANGED
@@ -1,17 +1,20 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0
4
- - 2.1
5
- - 2.2
6
- - ruby-head
3
+ - 2.0
4
+ - 2.1
5
+ - 2.2
6
+ - ruby-head
7
7
  before_install: gem install bundler -v "~> 1.10"
8
- bundler_args: --jobs=2
8
+ bundler_args: "--jobs=2"
9
9
  cache: bundler
10
10
  before_script:
11
- - export CODECLIMATE_REPO_TOKEN=46e09f028aadd0d0f7e8aa4d917dc6f053c0a3d9ab0f82aab41f0918acc0384a
11
+ - export CODECLIMATE_REPO_TOKEN=46e09f028aadd0d0f7e8aa4d917dc6f053c0a3d9ab0f82aab41f0918acc0384a
12
12
  script:
13
- - ruby --version
14
- - bundle exec rspec
13
+ - ruby --version
14
+ - bundle exec rspec
15
15
  branches:
16
16
  only:
17
- - master
17
+ - master
18
+ notifications:
19
+ slack:
20
+ secure: T6QhA+PdJTpGYjOk6d9uwt/p02OLcfieVUuNOY2po6KUsw9erMXCCuCGn1TXrRwFfnSVbuA7L3zjECMvaeDlsUURLVaZZduYHCpn6VXFfKlqzFxWr51Pk1qyE1feZCDPNNkr6PbgtFPHNWzZ/NUMrG0Z8Lo91PAr/RDolAa44MvLGq1ZDfAunITBRvGz8DhAu1Uf3EMPOqKtkN8NdEHp8pkns0XhmcqNLXwohdqXJY9R/TQ1gGJBc1hx3wZAqYCy0Eda44VBuzCg72QPUVQ3seGOts+2rgksNjnNLedB4F7bL7YZUjsam5OXKppnWt5HyE5bqkG1xrh6RguGVULBUTLRG5rY+FFtgJqgg2Qp4Ttu49Iyzh2DYhYleY6lsh1B/hsxzIjP+ojD4OXFKnb93IWxRvLrvPZGT2Q9V7zTo04Xa4cl7Zz33aPk59mZ/9ZmOlUCBtUFmYMslghOyIBSdqJHvHgXiVtheu231MuWdK7jg2U/aGnPKVh0jah9aEqNBmSmDbW2xfYEA+WINJmIKEktpQLyj8S3ajsdvZUuFcKdvZ0SEPoYuJGfaxiCKvPydm5yJFstlIYmgDlpFJh7Jh+LJy1trAvWGPcQMXs1MrZsc+c/UFs7zfrr/Uh74IxfdkIztNT2ZANYQ6zfXN2ZCT2ax9iE63gSgcCRGnEJ/R0=
data/Guardfile CHANGED
@@ -26,7 +26,8 @@
26
26
 
27
27
  guard :rspec, cmd: 'bundle exec rspec' do
28
28
  watch(%r{^spec/.+_spec\.rb$})
29
- watch(%r{^lib/(.+)/(.+)\.rb$}) { |m| "spec/#{m[1]}/#{m[2]}_spec.rb" }
29
+ watch(%r{^lib/(.+)/(.+)\.rb$}) { |m| "spec/#{m[1]}/#{m[2]}_spec.rb" }
30
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
30
31
  watch('spec/spec_helper.rb') { 'spec' }
31
32
  end
32
33
 
data/README.md CHANGED
@@ -6,8 +6,35 @@
6
6
  [![Test Coverage](https://codeclimate.com/repos/563cbaad1787d72930000582/badges/9753daa4ecd1a303b6ae/coverage.svg)](https://codeclimate.com/repos/563cbaad1787d72930000582/coverage)
7
7
  [![Dependency Status](https://gemnasium.com/yuemori/slappy.svg)](https://gemnasium.com/yuemori/slappy)
8
8
 
9
- This gem support to make slack bot with hubot like interface.
10
- Use the Slack Realtime API(see the [official-documentation](https://api.slack.com/rtm)).
9
+ This gem support to make slack bot, inspire from [hubot](https://github.com/github/hubot) and [sinatra](https://github.com/sinatra/sinatra).
10
+
11
+ Use the Slack Realtime API and Web API(see the [official-documentation](https://api.slack.com)).
12
+
13
+ ## Quick Start
14
+
15
+ ### 1. Generate Slack API Token
16
+
17
+ Slack API Token generate from [official page](https://api.slack.com/web).
18
+
19
+ ### 2. Set environment variable to Slack API Token
20
+
21
+ Slappy references `ENV['SLACK_TOKEN']` when use token.
22
+
23
+ (If you want to setting in code, we offer way to you).
24
+
25
+ ### 3. Written Code
26
+
27
+ ```ruby
28
+ require 'slappy'
29
+ require 'slappy/dsl'
30
+
31
+ # catch pattern
32
+ hear '^hello, slappy!' do |event|
33
+ say 'hello!!', channel: event.channel #=> respond message to channel
34
+ end
35
+
36
+ start
37
+ ```
11
38
 
12
39
  ## Installation
13
40
 
@@ -33,8 +60,6 @@ Store configuration value in environment variables. They are easy to change betw
33
60
  SLACK_TOKEN - required (when not configured)
34
61
  ```
35
62
 
36
- Slack API Token generated in [this page](https://api.slack.com/web).
37
-
38
63
  ### Configure
39
64
  Configure default settings.
40
65
  There configrations effect on send message to slack when use `say` method and should override when option given.
@@ -64,7 +89,8 @@ channel - default: '#general'
64
89
  icon_url - default: nil
65
90
  ```
66
91
 
67
- ### Example Code
92
+ ### Example
93
+ #### Basic
68
94
 
69
95
  ```ruby
70
96
  require 'slappy'
@@ -75,45 +101,45 @@ Slappy.hello do
75
101
  end
76
102
 
77
103
  # called when match message
78
- Slappy.hear(/foo/) do
104
+ Slappy.hear 'foo' do
79
105
  puts 'foo'
80
106
  end
81
107
 
82
- # called when match message with pattern match
83
- Slappy.hear(/bar (.*)/) do |event|
108
+ # use regexp in string literal
109
+ Slappy.hear 'bar (.*)' do |event|
84
110
  puts event.matches[1] #=> Event#matches return MatchData object
85
111
  end
86
112
 
87
- # event object is slack event JSON (convert to [hashie](https://github.com/intridea/hashie))
88
- Slappy.hear(/bar (.*)/) do |event|
113
+ # event object is slack event JSON (convert to Hashie::Mash)
114
+ Slappy.hear '^bar (.*)' do |event|
89
115
  puts event.channel #=> channel id
90
116
  Slappy.say 'slappy!', channel: event.channel #=> to received message channel
91
117
  Slappy.say 'slappy!', channel: '#general'
92
118
  Slappy.say 'slappy!', username: 'slappy!', icon_emoji: ':slappy:'
93
119
  end
94
120
 
121
+ # use regexp literal
122
+ Slappy.hear /^foobar/ do
123
+ puts 'slappppy!'
124
+ end
125
+
95
126
  Slappy.start
96
127
  ```
97
128
 
98
- ## How to run tests
99
-
100
- Please create a file named .env on the root of this repository. You can use .env.example file as a template
101
-
102
- ```
103
- cp .env.example .env
104
- ```
129
+ #### Use Slappy DSL
105
130
 
106
- and edit `.env` file properly.
131
+ Please require `'slappy/dsl'`, if you want short code
107
132
 
108
- ```
109
- SLACK_TOKEN=abcd-1234567890-1234567890-1234567890
110
- ```
133
+ ```ruby
134
+ require 'slappy'
135
+ require 'slappy/dsl'
111
136
 
112
- Then run tests.
137
+ # Omit 'Slappy' keyward, when delegator required
138
+ hear 'foo' do
139
+ say 'slappy!'
140
+ end
113
141
 
114
- ```
115
- bundle install
116
- bundle exec rspec
142
+ start
117
143
  ```
118
144
 
119
145
  ## Feature
@@ -123,11 +149,11 @@ bundle exec rspec
123
149
  - [ ] Generate template settings
124
150
  - [ ] CLI commands
125
151
  - [ ] Add bot name
126
- - [ ] client#respond (hubot#respond)
152
+ - [ ] client#respond (hubot#respond like)
127
153
 
128
154
  ## Contributing
129
155
 
130
- 1. Fork it ( http://github.com/aki017/slack-ruby-gem/fork )
156
+ 1. Fork it ( http://github.com/yuemori/slappy/fork )
131
157
  2. Create your feature branch (`git checkout -b my-new-feature`)
132
158
  3. Commit your changes (`git commit -am 'Add some feature'`)
133
159
  4. Push to the branch (`git push origin my-new-feature`)
data/lib/slappy/client.rb CHANGED
@@ -1,13 +1,10 @@
1
- require 'slack'
2
- require 'slappy/event'
3
- require 'slappy/listener'
4
- require 'slappy/configuration'
5
-
6
1
  module Slappy
7
2
  class Client
3
+ attr_reader :start_time
4
+
8
5
  def initialize
9
6
  Slack.configure { |slack| slack.token = config.token }
10
- @listeners = {}
7
+ @callbacks = {}
11
8
  end
12
9
 
13
10
  def client
@@ -15,25 +12,22 @@ module Slappy
15
12
  end
16
13
 
17
14
  def start
18
- @listeners.each do |key, array|
19
- client.on key do |data|
20
- array.each do |listener|
21
- event = Event.new(data, listener.pattern) if key == :message
22
- listener.call(event)
23
- end
24
- end
15
+ @start_time = Time.now
16
+ @callbacks.each do |event_name, listeners|
17
+ register_event event_name, listeners
25
18
  end
19
+ set_signal_trap
26
20
  client.start
27
21
  end
28
22
 
29
23
  def hello(&block)
30
- @listeners[:hello] ||= []
31
- @listeners[:hello].push block
24
+ @callbacks[:hello] ||= []
25
+ @callbacks[:hello].push block
32
26
  end
33
27
 
34
- def hear(regexp, &block)
35
- @listeners[:message] ||= []
36
- @listeners[:message].push Listener.new(regexp, block)
28
+ def hear(pattern, &block)
29
+ @callbacks[:message] ||= []
30
+ @callbacks[:message].push Listener.new(pattern, block)
37
31
  end
38
32
 
39
33
  def say(text, options = {})
@@ -44,6 +38,28 @@ module Slappy
44
38
 
45
39
  private
46
40
 
41
+ def set_signal_trap
42
+ [:TERM, :INT].each do |signal|
43
+ Signal.trap(signal) do
44
+ EventMachine.stop
45
+ end
46
+ end
47
+ end
48
+
49
+ def register_event(event_name, listeners)
50
+ client.on event_name do |data|
51
+ listeners.each do |listener|
52
+ case event_name
53
+ when :hello
54
+ listener.call
55
+ when :message
56
+ event = Event.new(data, listener.pattern)
57
+ listener.call(event)
58
+ end
59
+ end
60
+ end
61
+ end
62
+
47
63
  def config
48
64
  Slappy.configuration
49
65
  end
data/lib/slappy/dsl.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Slappy
2
+ module DSL
3
+ extend Forwardable
4
+
5
+ def_delegators :Slappy, :hello, :hear, :say, :start
6
+ end
7
+ end
8
+
9
+ extend Slappy::DSL
data/lib/slappy/event.rb CHANGED
@@ -1,6 +1,3 @@
1
- require 'hashie'
2
- require 'forwardable'
3
-
4
1
  module Slappy
5
2
  class Event
6
3
  extend Forwardable
@@ -1,11 +1,13 @@
1
1
  module Slappy
2
2
  class Listener
3
- def initialize(regexp, callback)
4
- @regexp = regexp
3
+ def initialize(pattern, callback)
4
+ pattern = /#{pattern}/ if pattern.is_a? String
5
+ @regexp = pattern
5
6
  @callback = callback
6
7
  end
7
8
 
8
9
  def call(event)
10
+ return unless Time.at(event.ts.to_f) > Slappy.start_time
9
11
  return unless event.text.match @regexp
10
12
  @callback.call(event)
11
13
  end
@@ -1,3 +1,3 @@
1
1
  module Slappy
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
data/lib/slappy.rb CHANGED
@@ -1,6 +1,6 @@
1
- require 'slappy/client'
2
- require 'slappy/configuration'
3
- require 'slappy/version'
1
+ require 'forwardable'
2
+ require 'hashie'
3
+ require 'slack'
4
4
 
5
5
  module Slappy
6
6
  class << self
@@ -18,10 +18,6 @@ module Slappy
18
18
  @configuration || configure
19
19
  end
20
20
 
21
- def client
22
- @client ||= Client.new
23
- end
24
-
25
21
  def method_missing(method, *args, &block)
26
22
  return super unless client.respond_to?(method)
27
23
  client.send(method, *args, &block)
@@ -30,5 +26,17 @@ module Slappy
30
26
  def respond_to?(method)
31
27
  client.respond_to?(method) || super
32
28
  end
29
+
30
+ private
31
+
32
+ def client
33
+ @client ||= Client.new
34
+ end
33
35
  end
34
36
  end
37
+
38
+ require 'slappy/client'
39
+ require 'slappy/configuration'
40
+ require 'slappy/event'
41
+ require 'slappy/listener'
42
+ require 'slappy/version'
data/slappy.gemspec CHANGED
@@ -10,13 +10,12 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["yuemori@aiming-inc.com"]
11
11
 
12
12
  spec.summary = %q{Simple Slack Bot Framework}
13
- spec.description = %q{Simple Slack Bot Framework. Use Slack RealTime API}
13
+ spec.description = %q{Simple Slack Bot Framework. Use Slack RealTime API and Web API.}
14
14
  spec.homepage = "https://github.com/yuemori/slappy"
15
15
  spec.license = "MIT"
16
16
 
17
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) }
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
19
  spec.require_paths = ["lib"]
21
20
 
22
21
  spec.add_dependency 'slack-api'
@@ -28,6 +27,7 @@ Gem::Specification.new do |spec|
28
27
  spec.add_development_dependency "guard-rspec"
29
28
  spec.add_development_dependency "rubocop"
30
29
  spec.add_development_dependency "guard-rubocop"
31
- spec.add_development_dependency "dotenv"
32
30
  spec.add_development_dependency "codeclimate-test-reporter"
31
+ spec.add_development_dependency "timecop"
32
+ spec.add_development_dependency "activesupport"
33
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slappy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - yuemori
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-07 00:00:00.000000000 Z
11
+ date: 2015-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-api
@@ -137,7 +137,7 @@ dependencies:
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
- name: dotenv
140
+ name: codeclimate-test-reporter
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - ">="
@@ -151,7 +151,21 @@ dependencies:
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
153
  - !ruby/object:Gem::Dependency
154
- name: codeclimate-test-reporter
154
+ name: timecop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: activesupport
155
169
  requirement: !ruby/object:Gem::Requirement
156
170
  requirements:
157
171
  - - ">="
@@ -164,7 +178,7 @@ dependencies:
164
178
  - - ">="
165
179
  - !ruby/object:Gem::Version
166
180
  version: '0'
167
- description: Simple Slack Bot Framework. Use Slack RealTime API
181
+ description: Simple Slack Bot Framework. Use Slack RealTime API and Web API.
168
182
  email:
169
183
  - yuemori@aiming-inc.com
170
184
  executables: []
@@ -183,11 +197,10 @@ files:
183
197
  - LICENSE.txt
184
198
  - README.md
185
199
  - Rakefile
186
- - bin/console
187
- - bin/setup
188
200
  - lib/slappy.rb
189
201
  - lib/slappy/client.rb
190
202
  - lib/slappy/configuration.rb
203
+ - lib/slappy/dsl.rb
191
204
  - lib/slappy/event.rb
192
205
  - lib/slappy/listener.rb
193
206
  - lib/slappy/version.rb
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'slappy'
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 DELETED
@@ -1,7 +0,0 @@
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