socrates 0.1.18 → 0.1.19

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
- SHA1:
3
- metadata.gz: 67a86f6c23427a17a1271fc44b51b656b7e3e8c6
4
- data.tar.gz: 331e7f16f68d78ca66490fc985861418cc891bf0
2
+ SHA256:
3
+ metadata.gz: e648a9ac94de0a6fd27a051b478618c505f7027541eaa38e40677e555c22fc1a
4
+ data.tar.gz: '094d6680aefa6c2c5b64a667775c29069c9f95e9aaf953ebfcaefed5bbafef39'
5
5
  SHA512:
6
- metadata.gz: c8445176b8a001cbe6957e23f9e4f4909f376439d9cc0aa045a5f23ecfbeccd850331c1f31313f8a12987222ab8f70900d2bf0a2fd472b6b62b0c0c63a0aeee2
7
- data.tar.gz: ff3aeced1731694a2fe660658c83c24f2e21db08755531a64639046e66ab39ad2190c0796d27af612547a5793646ce0345f0441fb15c4d2070437f911c8b0907
6
+ metadata.gz: 2e7e0ee90d0486f5a325fce2889b99f9739b76fd45696577ca3e999f18250d2f4155bc46fb8d324eadeff0d986e65a6809c6baa31d0a2bd07fbe9938a0a148dc
7
+ data.tar.gz: f8b3c910975023e89e05216eea0b05eb24d17f2af4de933d736ab40f3662ed841e3fef53224076edac5f6e5f2a1c8d160290781e96890a620f1632e0ebb90f5d
data/.rubocop.yml CHANGED
@@ -3,3 +3,6 @@ inherit_from:
3
3
 
4
4
  AllCops:
5
5
  TargetRubyVersion: 2.4
6
+
7
+ Naming/UncommunicativeMethodParamName:
8
+ Enabled: false
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.4.2
1
+ 2.5.0
data/circle.yml CHANGED
@@ -1,3 +1,7 @@
1
+ dependencies:
2
+ pre:
3
+ - gem update --system
4
+
1
5
  test:
2
6
  post:
3
7
  - bundle exec rubocop
data/exe/socrates CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- File.expand_path("../../lib", __FILE__).tap do |lib|
3
+ File.expand_path("../lib", __dir__).tap do |lib|
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  end
6
6
 
@@ -17,12 +17,14 @@ module Socrates
17
17
  attr_accessor :error_message
18
18
  attr_accessor :expired_timeout # seconds
19
19
  attr_accessor :logger
20
+ attr_accessor :error_handler # a callable proc
20
21
 
21
22
  def initialize
22
23
  @storage = Storage::Memory.new
23
24
  @error_message = "Sorry, something went wrong. We'll have to start over..."
24
25
  @expired_timeout = 30.minutes
25
26
  @logger = Socrates::Logger.default
27
+ @error_handler = proc { |_message, _error| }
26
28
  end
27
29
  end
28
30
  end
@@ -18,6 +18,7 @@ module Socrates
18
18
  @storage = storage || Socrates.config.storage
19
19
 
20
20
  @logger = Socrates.config.logger
21
+ @error_handler = Socrates.config.error_handler
21
22
  @error_message = Socrates.config.error_message || DEFAULT_ERROR_MESSAGE
22
23
  end
23
24
 
@@ -41,7 +42,7 @@ module Socrates
41
42
  # return false unless conversation_state(user).nil?
42
43
 
43
44
  # Create state data to match the request.
44
- state_data = Socrates::Core::StateData.new(state_id: state_id, state_action: :ask)
45
+ state_data = StateData.new(state_id: state_id, state_action: :ask)
45
46
 
46
47
  persist_state_data(session.client_id, state_data)
47
48
 
@@ -57,15 +58,8 @@ module Socrates
57
58
 
58
59
  return nil unless @storage.has_key?(client_id)
59
60
 
60
- begin
61
- snapshot = @storage.get(client_id)
62
- state_data = StateData.deserialize(snapshot)
63
- state_data = nil if state_data.expired? || state_data.finished?
64
- rescue StandardError => e
65
- @logger.warn "Error while fetching state_data for client id '#{client_id}'."
66
- @logger.warn e
67
- state_data = nil
68
- end
61
+ state_data = @storage.fetch(client_id)
62
+ state_data = nil if state_data&.expired? || state_data&.finished?
69
63
 
70
64
  state_data
71
65
  end
@@ -119,17 +113,7 @@ module Socrates
119
113
 
120
114
  # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
121
115
  def fetch_state_data(client_id)
122
- if @storage.has_key?(client_id)
123
- begin
124
- snapshot = @storage.get(client_id)
125
- state_data = StateData.deserialize(snapshot)
126
- rescue StandardError => e
127
- @logger.warn "Error while fetching state_data for client id '#{client_id}', resetting state: #{e.message}"
128
- @logger.warn e
129
- end
130
- end
131
-
132
- state_data ||= StateData.new
116
+ state_data = @storage.fetch(client_id) || StateData.new
133
117
 
134
118
  # If the current state is nil or END_OF_CONVERSATION, set it to the default state, which is typically a state
135
119
  # that waits for an initial command or input from the user (e.g. help, start, etc).
@@ -152,8 +136,7 @@ module Socrates
152
136
  # rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
153
137
 
154
138
  def persist_state_data(client_id, state_data)
155
- state_data.reset_elapsed_time
156
- @storage.put(client_id, state_data.serialize)
139
+ @storage.persist(client_id, state_data)
157
140
  end
158
141
 
159
142
  def instantiate_state(session, state_data)
@@ -168,9 +151,12 @@ module Socrates
168
151
  state.data.state_id.nil? || state.data.state_id == StateData::END_OF_CONVERSATION
169
152
  end
170
153
 
171
- def handle_action_error(e, session, state)
172
- @logger.warn "Error while processing action #{state.data.state_id}/#{state.data.state_action}: #{e.message}"
173
- @logger.warn e
154
+ def handle_action_error(error, session, state)
155
+ msg = "Error while processing action #{state.data.state_id}/#{state.data.state_action}: #{error.message}"
156
+ @logger.warn msg
157
+ @logger.warn error
158
+
159
+ @error_handler.call(msg, error) if @error_handler.present?
174
160
 
175
161
  @adapter.queue_message(session, @error_message, send_now: true)
176
162
 
@@ -3,7 +3,7 @@ require "logger"
3
3
  module Socrates
4
4
  class Logger < ::Logger
5
5
  def self.default
6
- @logger ||= begin
6
+ @default ||= begin
7
7
  logger = new(STDOUT)
8
8
  logger.level = Logger::WARN
9
9
  logger
@@ -1,6 +1,10 @@
1
+ require "socrates/storage/storage"
2
+
1
3
  module Socrates
2
4
  module Storage
3
5
  class Memory
6
+ include Socrates::Storage::Storage
7
+
4
8
  def initialize
5
9
  @memory = {}
6
10
  end
@@ -1,8 +1,12 @@
1
1
  require "redis"
2
2
 
3
+ require "socrates/storage/storage"
4
+
3
5
  module Socrates
4
6
  module Storage
5
7
  class Redis
8
+ include Socrates::Storage::Storage
9
+
6
10
  def initialize(url: "redis://localhost")
7
11
  @redis = ::Redis.new(url: url)
8
12
  end
@@ -0,0 +1,60 @@
1
+ require "socrates/core/state_data"
2
+
3
+ module Socrates
4
+ module Storage
5
+ module Storage
6
+ def initialize
7
+ @logger = Socrates.config.logger
8
+ end
9
+
10
+ def fetch(client_id)
11
+ key = generate_key(client_id)
12
+
13
+ snapshot = get(key)
14
+
15
+ return if snapshot.nil?
16
+
17
+ begin
18
+ Socrates::Core::StateData.deserialize(snapshot)
19
+ rescue StandardError => e
20
+ @logger.warn "Error while fetching state_data for client id '#{client_id}', resetting state: #{e.message}"
21
+ @logger.warn e
22
+ end
23
+ end
24
+
25
+ def persist(client_id, state_data)
26
+ key = generate_key(client_id)
27
+ state_data.reset_elapsed_time
28
+ put(key, state_data.serialize)
29
+ end
30
+
31
+ def generate_key(client_id)
32
+ client_id
33
+ end
34
+
35
+ # rubocop:disable Lint/UnusedMethodArgument
36
+
37
+ def has_key?(key)
38
+ raise NotImplementedError
39
+ end
40
+
41
+ def clear(key)
42
+ raise NotImplementedError
43
+ end
44
+
45
+ def get(key)
46
+ raise NotImplementedError
47
+ end
48
+
49
+ def put(key, value)
50
+ raise NotImplementedError
51
+ end
52
+
53
+ def clear_all
54
+ raise NotImplementedError
55
+ end
56
+
57
+ # rubocop:enable Lint/UnusedMethodArgument
58
+ end
59
+ end
60
+ end
@@ -1,3 +1,3 @@
1
1
  module Socrates
2
- VERSION = "0.1.18"
2
+ VERSION = "0.1.19"
3
3
  end
data/socrates.gemspec CHANGED
@@ -1,4 +1,4 @@
1
- lib = File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path("lib", __dir__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  require "socrates/version"
@@ -21,17 +21,17 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.required_ruby_version = ">= 2.3.0"
24
+ spec.required_ruby_version = ">= 2.4.0"
25
25
 
26
- spec.add_development_dependency "bundler", "~> 1.15"
27
- spec.add_development_dependency "rake", "~> 12.1"
28
- spec.add_development_dependency "rspec", "~> 3.6"
29
- spec.add_development_dependency "rubocop", "~> 0.50.0"
30
- spec.add_development_dependency "timecop", "~> 0.9.1"
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 12.3"
28
+ spec.add_development_dependency "rspec", "~> 3.7"
29
+ spec.add_development_dependency "rubocop", "~> 0.52"
30
+ spec.add_development_dependency "timecop", "~> 0.9"
31
31
 
32
32
  spec.add_dependency "activesupport", ">= 5.1.4"
33
33
  spec.add_dependency "celluloid-io", ">= 0.17.3"
34
- spec.add_dependency "hashie", ">= 3.5.6"
35
- spec.add_dependency "redis", ">= 4.0.0"
36
- spec.add_dependency "slack-ruby-client", ">= 0.9.1"
34
+ spec.add_dependency "hashie", ">= 3.5.7"
35
+ spec.add_dependency "redis", ">= 4.0.1"
36
+ spec.add_dependency "slack-ruby-client", ">= 0.11.0"
37
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: socrates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Nelson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-19 00:00:00.000000000 Z
11
+ date: 2018-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,70 +16,70 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.15'
19
+ version: '1.16'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.15'
26
+ version: '1.16'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '12.1'
33
+ version: '12.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '12.1'
40
+ version: '12.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.6'
47
+ version: '3.7'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.6'
54
+ version: '3.7'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.50.0
61
+ version: '0.52'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.50.0
68
+ version: '0.52'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: timecop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.9.1
75
+ version: '0.9'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.9.1
82
+ version: '0.9'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: activesupport
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -114,42 +114,42 @@ dependencies:
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: 3.5.6
117
+ version: 3.5.7
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: 3.5.6
124
+ version: 3.5.7
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: redis
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: 4.0.0
131
+ version: 4.0.1
132
132
  type: :runtime
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: 4.0.0
138
+ version: 4.0.1
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: slack-ruby-client
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - ">="
144
144
  - !ruby/object:Gem::Version
145
- version: 0.9.1
145
+ version: 0.11.0
146
146
  type: :runtime
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
- version: 0.9.1
152
+ version: 0.11.0
153
153
  description:
154
154
  email:
155
155
  - christian@carbonfive.com
@@ -186,6 +186,7 @@ files:
186
186
  - lib/socrates/sample_states.rb
187
187
  - lib/socrates/storage/memory.rb
188
188
  - lib/socrates/storage/redis.rb
189
+ - lib/socrates/storage/storage.rb
189
190
  - lib/socrates/string_helpers.rb
190
191
  - lib/socrates/version.rb
191
192
  - sample-conversation-console.gif
@@ -202,7 +203,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
202
203
  requirements:
203
204
  - - ">="
204
205
  - !ruby/object:Gem::Version
205
- version: 2.3.0
206
+ version: 2.4.0
206
207
  required_rubygems_version: !ruby/object:Gem::Requirement
207
208
  requirements:
208
209
  - - ">="
@@ -210,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
211
  version: '0'
211
212
  requirements: []
212
213
  rubyforge_project:
213
- rubygems_version: 2.6.13
214
+ rubygems_version: 2.7.4
214
215
  signing_key:
215
216
  specification_version: 4
216
217
  summary: A micro-framework for building stateful conversational bots.