jarvis-cli 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd5864b01fafb85e866cd02e13297b059ec71c4e
4
- data.tar.gz: 3ea72ef6d38c31edd5d73890cd4597e8e937aa25
3
+ metadata.gz: c26b44a300bbc904164ecdbc1cd144df389f805c
4
+ data.tar.gz: 08857899933477e2adb38afca39c835d481d7bdb
5
5
  SHA512:
6
- metadata.gz: 26e0cb3e4a1d584e15979474559b737da87224a7f1ae955a84d0eb63808d62ad519d5578daa31a4022621d4f8f4bc5c64e730cf60ff1e9359ff40083b40b2133
7
- data.tar.gz: ac63c5acf2a562c52575e1edb95f87ba3532412c7a5177b6b086b07d38a8e05a8fc5cad3b89ea5448231020775f978672340a47bbf5b001441aa74ddce0d2978
6
+ metadata.gz: b0974d434a5b97a70a03d8541beed904b3c45657f75574f8ddf0e91ff6b39eea320408e395ce90963f7a1fa71a34eb42b76078a4886419b16288cc09db3f73cc
7
+ data.tar.gz: c66896af2af9037b620aa5fc9425fa7e8c735ac01579b864b3b5349eb7c6980834ecc1ec257823277dd38895b3183bd4728df6b491d6ae2b1cfe4f3276060d69
data/README.md CHANGED
@@ -58,11 +58,11 @@ There are two ways to tell Jarvis to send a given message to a particular servic
58
58
 
59
59
  ```ruby
60
60
  class ImgFlip < Jarvis::Service
61
- phrases = "success kid", "overly attached girlfriend"
61
+ phrases "success kid", "overly attached girlfriend"
62
62
  # will converted to a pattern that looks like /success kid|overly attached girlfriend/i
63
63
  end
64
64
  class SortingHat < Jarvis::Service
65
- interpreter_pattern = /sorting hat|where do I belong/i
65
+ interpreter_pattern /sorting hat|where do I belong/i
66
66
  end
67
67
  ```
68
68
 
@@ -1,3 +1,3 @@
1
1
  Given(/^a server is running with the (.+) service enabled$/) do |service|
2
- Jarvis.register_services service.to_sym
2
+ Server.register_services service.to_sym
3
3
  end
@@ -17,7 +17,7 @@ private
17
17
  Jarvis.services.each do |service|
18
18
  constant = service.constantize
19
19
  determine_service += <<-code
20
- when #{constant.interpreter_pattern.inspect} then #{constant}
20
+ when #{constant.pattern.inspect} then #{constant}
21
21
  code
22
22
  end
23
23
  determine_service += <<-code
data/lib/jarvis/server.rb CHANGED
@@ -1,15 +1,17 @@
1
1
  require 'sinatra'
2
2
  require "sinatra/json"
3
+ require "sinatra/multi_route"
3
4
  require 'json'
4
5
 
5
6
  module Jarvis
6
7
  class Server < Sinatra::Base
8
+ register Sinatra::MultiRoute
7
9
 
8
10
  get "/" do
9
11
  json text: "Hello, I'm Jarvis"
10
12
  end
11
13
 
12
- post "/jarvis" do
14
+ route :get, :post, "/jarvis" do
13
15
  message = Slack::Message.new(params)
14
16
  service = Jarvis::Interpreter.new(message).determine_service.new(message)
15
17
  begin
@@ -21,6 +23,10 @@ module Jarvis
21
23
  end
22
24
  end
23
25
 
26
+ def self.register_services(*args)
27
+ Jarvis.register_services *args
28
+ end
29
+
24
30
  private
25
31
 
26
32
  def run_service(service)
@@ -25,58 +25,64 @@ module Jarvis
25
25
 
26
26
  # Class Methods
27
27
  class << self
28
- attr_accessor :required_environment_variables, :interpreter_pattern, :phrases, :invoker
28
+ attr_accessor :required_environment_variables, :pattern, :interpreter_phrases, :invoker
29
+ end
29
30
 
30
- def phrases=(*args)
31
- @phrases = args
32
- reset_interpreter_pattern
33
- end
31
+ def self.phrases(*args)
32
+ @interpreter_phrases = args
33
+ reset_interpreter_pattern
34
+ end
34
35
 
35
- def required_environment_variables
36
- @required_environment_variables ||= []
37
- end
36
+ def self.required_environment_variables
37
+ @required_environment_variables ||= []
38
+ end
38
39
 
39
- def environment(*args)
40
- args.each do |sym|
41
- str = sym.to_s.upcase
42
- send :define_method, sym do
43
- ENV[str] || ENV[str.downcase]
44
- end
45
- required_environment_variables << str
40
+ def self.environment(*args)
41
+ args.each do |sym|
42
+ str = sym.to_s.upcase
43
+ send :define_method, sym do
44
+ ENV[str] || ENV[str.downcase]
46
45
  end
46
+ required_environment_variables << str
47
47
  end
48
+ end
48
49
 
49
- def interpreter_pattern
50
- @interpreter_pattern ||= concatenate_phrases_into_regex
51
- end
52
- # class MyService < Jarvis::Service
53
- # invoke_with :post_tweet
54
- #
55
- # end
56
- def invoke_with(method_name)
57
- send :define_method, :invoke do
58
- run_hook :before_invoke
59
- response = send method_name
60
- run_hook :after_invoke
61
- response
62
- end
63
- end
64
50
 
51
+ def self.interpreter_pattern(pattern)
52
+ @pattern = pattern
53
+ end
65
54
 
66
- private
55
+ def self.pattern
56
+ @pattern ||= concatenate_phrases_into_regex
57
+ end
67
58
 
68
- def concatenate_phrases_into_regex
69
- case self.phrases
70
- when Array then /#{self.phrases.join("|")}/i
71
- when String then /#{self.phrases}/i
72
- end
59
+ # class MyService < Jarvis::Service
60
+ # invoke_with :post_tweet
61
+ #
62
+ # end
63
+ def self.invoke_with(method_name)
64
+ send :define_method, :invoke do
65
+ run_hook :before_invoke
66
+ response = send method_name
67
+ run_hook :after_invoke
68
+ response
73
69
  end
70
+ end
74
71
 
75
- def reset_interpreter_pattern
76
- @interpreter_pattern = nil
72
+
73
+ private
74
+
75
+ def self.concatenate_phrases_into_regex
76
+ case self.interpreter_phrases
77
+ when Array then /#{self.interpreter_phrases.join("|")}/i
78
+ when String then /#{self.interpreter_phrases}/i
77
79
  end
78
80
  end
79
81
 
82
+ def self.reset_interpreter_pattern
83
+ @pattern = nil
84
+ end
85
+
80
86
  invoke_with :run
81
87
  define_hooks :before_invoke, :after_invoke
82
88
  end
@@ -1,6 +1,6 @@
1
1
  class Dice < Jarvis::Service
2
2
  attr_accessor :dice_result
3
- phrases = "roll", "dice"
3
+ phrases "roll", "dice"
4
4
  before_invoke :perform_roll
5
5
 
6
6
  def run
@@ -1,6 +1,6 @@
1
1
  class Eightball < Jarvis::Service
2
2
 
3
- interpreter_pattern = /\?$/
3
+ interpreter_pattern /\?$/
4
4
 
5
5
  def run
6
6
  responses.sample
@@ -1,5 +1,5 @@
1
1
  class Fact < Jarvis::Service
2
- self.phrases = "fact"
2
+ phrases "fact"
3
3
 
4
4
  def run
5
5
  Jarvis.get("http://numbersapi.com/random").parsed_response
@@ -1,3 +1,3 @@
1
1
  module Jarvis
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -12,7 +12,7 @@ RSpec.describe Jarvis::Interpreter do
12
12
  end
13
13
 
14
14
  describe "Routes Services based on the registered services phrases" do
15
- before { service.phrases = "success kid" }
15
+ before { service.phrases "success kid" }
16
16
  before { Jarvis.register_services :test_service }
17
17
  context "A Match" do
18
18
  subject { described_class.new(message text: "success kid") }
@@ -27,7 +27,7 @@ RSpec.describe Jarvis::Interpreter do
27
27
  it { expect(subject.determine_service).to eq service}
28
28
  end
29
29
  context "Multiple phrases route correctly" do
30
- before(:each) { other_service.phrases = "overly attached bot", "hello world" }
30
+ before(:each) { other_service.phrases "overly attached bot", "hello world" }
31
31
  before { Jarvis.register_services :other_test_service }
32
32
  it { expect(described_class.new(message text: "hello world").determine_service).to eq other_service }
33
33
  it { expect(described_class.new(message text: "overly attached bot").determine_service).to eq other_service }
@@ -35,7 +35,7 @@ RSpec.describe Jarvis::Interpreter do
35
35
  end
36
36
 
37
37
  describe "Routes services based on the registered interpreter pattern" do
38
- before { service.interpreter_pattern = /success kid/i }
38
+ before { service.interpreter_pattern /success kid/i }
39
39
  before { Jarvis.register_services :test_service }
40
40
  context "A Match" do
41
41
  subject { described_class.new(message text: "success kid") }
@@ -50,8 +50,8 @@ RSpec.describe Jarvis::Interpreter do
50
50
  describe "Routes to the correct service" do
51
51
  let(:correct_service) { service }
52
52
  let(:incorrect_service) { other_service }
53
- before { correct_service.interpreter_pattern = /success kid/i }
54
- before { incorrect_service.interpreter_pattern = /overly attached chatbot/i }
53
+ before { correct_service.interpreter_pattern /success kid/i }
54
+ before { incorrect_service.interpreter_pattern /overly attached chatbot/i }
55
55
  before { Jarvis.register_services :test_service, :other_test_service }
56
56
  context "Routes to Test Service" do
57
57
  subject { described_class.new(message text: "success kid hello, world") }
@@ -12,7 +12,7 @@ RSpec.describe Jarvis::Server do
12
12
  describe "unfit environment" do
13
13
  let(:slack_message) { slack_outgoing_message(text: "Jarvis, success kid blah blah blah") }
14
14
  before { service.environment :blah }
15
- before { service.phrases = "success kid" }
15
+ before { service.phrases "success kid" }
16
16
  before { Jarvis.register_services :test_service }
17
17
  before { post "/jarvis", slack_message }
18
18
  it { expect(message).to eq "I'm really sorry, but that sevice needs to be configured" }
@@ -29,19 +29,19 @@ RSpec.describe Jarvis::Service do
29
29
  end
30
30
 
31
31
  describe "Expose Pattern For the Service to be Interpreted" do
32
- before { service.interpreter_pattern = /are you up/i }
33
- it { expect(service.interpreter_pattern).to eq /are you up/i }
32
+ before { service.interpreter_pattern /are you up/i }
33
+ it { expect(service.pattern).to eq /are you up/i }
34
34
  end
35
35
 
36
36
  describe "Allow the Developer to make a list of phrases to be concatenated into the interpreter pattern" do
37
37
  context "Accepts a single phrase" do
38
- before { service.phrases = "success kid" }
39
- it { expect(service.interpreter_pattern ).to eq /success kid/i }
38
+ before { service.phrases "success kid" }
39
+ it { expect(service.pattern ).to eq /success kid/i }
40
40
  end
41
41
 
42
42
  context "Multiple Phrases" do
43
- before { service.phrases = "success kid", "hello world" }
44
- it { expect(service.interpreter_pattern).to eq /success kid|hello world/i }
43
+ before { service.phrases "success kid", "hello world" }
44
+ it { expect(service.pattern).to eq /success kid|hello world/i }
45
45
 
46
46
  end
47
47
  end
@@ -1,2 +1,6 @@
1
1
  class Server < Jarvis::Server
2
+ # Use `register_services` to enable services in your Jarvis Bot. Available built-in services are:
3
+ # :dice, :eightball, :fact
4
+ # You can make your own services with `jarvis generate service my_service`
5
+ register_services :fact
2
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jarvis-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - DVG