cognition 1.0.3 → 1.1.0

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: 0e50eb1ebaaaa6fdfc1c8d760cf3bad97359bf14
4
- data.tar.gz: 15e38a680a5cba0aa9652a4d47597afffdeee25a
3
+ metadata.gz: fc7111ee4e33d5d94ad94ae8ee6e8e6c5466e539
4
+ data.tar.gz: d1f63422b1a9c8e672b1b4f024fcf4f0e77a5853
5
5
  SHA512:
6
- metadata.gz: f67e1f05ff1a2862dbb4fb501b8cb31be1fe62b741083fbffff997c2310849ed5ce44e8f4376eabc5725e45c0063a1db372b5660ad7dfc20318f691fe56d772d
7
- data.tar.gz: d4f667e367fef56e5b43168de85346bcc918ba1f69ed575f71cd823eeaa0231744c9b31c621e4e87c3ef6a9f9387a141292aa77efeae996d65280c1ed5104886
6
+ metadata.gz: 5ea98203f67db1f9f1bc18b3133e1f342ad90b11f76af75cde294ecf7ac54e85067f61a52db318551e732f93abec06c0a411d9661864dfc677d8f2f92e0a705e
7
+ data.tar.gz: ba51ff97c11f54b57b15747d48b89ba707d2e231ea4d6613dcd576b807d8a63d7ce16527d75cfac9563a4415a2fc7bfd78ddfb52160798afc0c58cf34396298c
data/cognition.gemspec CHANGED
@@ -18,7 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "httparty", "~> 0.13.3"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.7"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
25
  spec.add_development_dependency "minitest", "~> 5.0.0"
26
+ spec.add_development_dependency "webmock", "~> 1.20.4"
24
27
  end
@@ -1,6 +1,6 @@
1
1
  module Cognition
2
2
  class Matcher
3
- attr_accessor :trigger, :action, :response, :help
3
+ attr_reader :trigger, :action, :response, :help, :match_data
4
4
 
5
5
  def initialize(trigger, help = 'Undocumented', &action)
6
6
  raise ArgumentError, 'matcher must have a trigger' unless trigger
@@ -17,7 +17,7 @@ module Cognition
17
17
  end
18
18
 
19
19
  def run(msg)
20
- @response = action.call(msg)
20
+ @response = action.call(msg, match_data)
21
21
  end
22
22
 
23
23
  def matches?(msg)
@@ -25,7 +25,7 @@ module Cognition
25
25
  when String
26
26
  trigger == msg.command
27
27
  when Regexp
28
- msg.matches = trigger.match msg.command
28
+ @match_data = trigger.match msg.command
29
29
  end
30
30
  end
31
31
  end
@@ -1,11 +1,16 @@
1
1
  module Cognition
2
2
  class Message
3
- attr_reader :command, :metadata
4
- attr_accessor :matches
3
+ attr_reader :command, :metadata, :responder
5
4
 
6
5
  def initialize(command, metadata = {})
7
6
  @command = command
8
7
  @metadata = metadata
8
+ @responder = Cognition::Responder.new(metadata[:callback_url]) if metadata[:callback_url]
9
+ end
10
+
11
+ def reply(text)
12
+ return "No Callback URL provided" unless @responder
13
+ @responder.reply(text)
9
14
  end
10
15
  end
11
16
  end
@@ -4,12 +4,12 @@ module Cognition
4
4
  match(/^ping/i, 'ping: Returns "PONG"', :pong)
5
5
  match(/^help\s*(?<command>.*)/i, 'help: Returns help text for registered plugins', :help)
6
6
 
7
- def pong(*)
7
+ def pong(msg, match_data = nil)
8
8
  'PONG'
9
9
  end
10
10
 
11
- def help(msg)
12
- if msg.matches["command"].empty?
11
+ def help(msg, match_data = nil)
12
+ if match_data["command"].empty?
13
13
  Cognition.help.join("\n")
14
14
  else
15
15
  Cognition.help.find_all { |text| text.match msg.matches[:command] }.join("\n")
@@ -0,0 +1,18 @@
1
+ require 'httparty'
2
+ module Cognition
3
+ class Responder
4
+ include HTTParty
5
+ attr_reader :url
6
+
7
+ def initialize(uri)
8
+ @options = { timeout: 5 }
9
+ @uri = uri
10
+ end
11
+
12
+ def reply(text)
13
+ self.class.post(@uri, @options.merge({ content: text }))
14
+ rescue Timeout::Error => e
15
+ "Request to #{@uri} timed out."
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module Cognition
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/cognition.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'cognition/version'
2
2
  require 'cognition/message'
3
3
  require 'cognition/matcher'
4
+ require 'cognition/responder'
4
5
  require 'cognition/plugins/base'
5
6
  require 'cognition/plugins/default'
6
7
 
@@ -1,7 +1,7 @@
1
1
  class Hello < Cognition::Plugins::Base
2
2
  match 'hello', 'hello: Returns Hello World', :hello
3
3
 
4
- def hello(*)
4
+ def hello(msg)
5
5
  'Hello World'
6
6
  end
7
7
  end
@@ -0,0 +1,5 @@
1
+ require 'webmock/test_unit'
2
+
3
+ class Minitest::Test
4
+ include WebMock
5
+ end
data/test/test_matcher.rb CHANGED
@@ -51,10 +51,21 @@ class MatcherTest < Minitest::Test
51
51
  refute matcher.attempt(msg)
52
52
  end
53
53
 
54
- def test_sets_matches_on_message
54
+ def test_sets_match_data
55
55
  msg = Cognition::Message.new('hello john')
56
56
  matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, 'test', &Proc.new {'PONG'})
57
+ matcher.matches?(msg)
58
+ assert_equal "john", matcher.match_data[:name]
59
+ end
60
+
61
+ def test_captures_response
62
+ msg = Cognition::Message.new('hello john')
63
+ matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, 'test', &Proc.new(&method(:dummy_method)))
57
64
  matcher.attempt(msg)
58
- assert_equal "john", msg.matches[:name]
65
+ assert_equal "Hello john", matcher.response
59
66
  end
60
67
  end
68
+
69
+ def dummy_method(msg, match_data)
70
+ "Hello #{match_data['name']}"
71
+ end
data/test/test_message.rb CHANGED
@@ -7,4 +7,14 @@ class CognitionTest < Minitest::Test
7
7
  assert_equal 15, msg.metadata[:user_id]
8
8
  assert_equal 'bar', msg.metadata[:foo]
9
9
  end
10
+
11
+ def test_sets_responder_if_callback_url
12
+ msg = Cognition::Message.new('ping', callback_url: 'http://foo.bar/')
13
+ assert_kind_of Cognition::Responder, msg.responder
14
+ end
15
+
16
+ def test_no_responder_if_no_callback_url
17
+ msg = Cognition::Message.new('ping', user: { name: 'foo', id: 123456 })
18
+ refute msg.responder
19
+ end
10
20
  end
@@ -0,0 +1,20 @@
1
+ require 'minitest/autorun'
2
+ require 'cognition'
3
+ require 'test_helper'
4
+
5
+ class ResponderTest < Minitest::Test
6
+ def test_sends_reply
7
+ stub_request(:any, "http://foo.bar/path").
8
+ to_return(status: 200)
9
+ responder = Cognition::Responder.new('http://foo.bar/path')
10
+
11
+ assert_equal 200, responder.reply('foobar').code
12
+ end
13
+
14
+ def test_handles_timeouts
15
+ stub_request(:any, "http://foo.bar/path").to_timeout
16
+ responder = Cognition::Responder.new('http://foo.bar/path')
17
+
18
+ assert_equal 'Request to http://foo.bar/path timed out.', responder.reply('foobar')
19
+ end
20
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cognition
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Anderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-22 00:00:00.000000000 Z
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.3
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: 5.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.20.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.20.4
55
83
  description: Match text; run commands. Works great for building a chatbot!
56
84
  email:
57
85
  - andnat@gmail.com
@@ -70,12 +98,15 @@ files:
70
98
  - lib/cognition/message.rb
71
99
  - lib/cognition/plugins/base.rb
72
100
  - lib/cognition/plugins/default.rb
101
+ - lib/cognition/responder.rb
73
102
  - lib/cognition/version.rb
74
103
  - test/fixtures/hello.rb
75
104
  - test/test_cognition.rb
105
+ - test/test_helper.rb
76
106
  - test/test_matcher.rb
77
107
  - test/test_message.rb
78
108
  - test/test_plugin.rb
109
+ - test/test_responder.rb
79
110
  homepage: https://github.com/anoldguy/Cognition
80
111
  licenses:
81
112
  - MIT
@@ -103,6 +134,8 @@ summary: A rules engine for running commands.
103
134
  test_files:
104
135
  - test/fixtures/hello.rb
105
136
  - test/test_cognition.rb
137
+ - test/test_helper.rb
106
138
  - test/test_matcher.rb
107
139
  - test/test_message.rb
108
140
  - test/test_plugin.rb
141
+ - test/test_responder.rb