wit 3.3.1 → 3.4.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: fd594fc892e82bfd4d1a23bff6eb062a3b31a117
4
- data.tar.gz: a385c71e0165ab8ca866effc5e099fbe48547834
3
+ metadata.gz: b70a97f9ec0b2c9b01f49983870ae7981cd0ca9a
4
+ data.tar.gz: a575cf16cc6fd9ae10ce374617364c6edc6ea80a
5
5
  SHA512:
6
- metadata.gz: 059661c6167529bc21f5700f7a93db866629cd20bb103b479b625f31982e84a3d97995990eaa27e6ebaf1596aced8df7c85cc5b4126f1a23da1e8446c55eca6d
7
- data.tar.gz: e934ef82f3b4178b5fe36d681708e0a40a7df88695b5441b6df777abe103c0d9d51f06b5d4459fc9505372667c00c97f0a101dd43dcf7fc77cbe9d905727f91c
6
+ metadata.gz: 2f8a5583c0e72f940c9b7eff2c2cbcf31103b309ba040049b6f68bef599df4b14264fe99a5c18314c5202db928408157ad7d1bb38cef47bedce0bd46830bac09
7
+ data.tar.gz: 58496e8a8bcb1f0c3183e00a611f713740932b2afbb2418534e244596dc303a1265a5681dbcb711907265283681e460c42094ba474573a27351d95369ba422e3
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
2
  *.swo
3
3
  *.swp
4
+ .idea/
4
5
  Gemfile.lock
data/CHANGES.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## v3.4
2
+
3
+ - allows for overriding API version, by setting `WIT_API_VERSION`
4
+ - warns instead of throwing when validating actions
5
+
6
+ ### breaking
7
+
8
+ - bumped default API version from `20160330` to `20160516`
9
+
1
10
  ## v3.3.1
2
11
 
3
12
  - fixed recursive call when running actions
data/README.md CHANGED
@@ -16,9 +16,15 @@ gem build wit.gemspec
16
16
  gem install wit-*.gem
17
17
  ```
18
18
 
19
- ## Usage
19
+ ## Quickstart
20
20
 
21
- See the `examples` folder for examples.
21
+ Run in your terminal:
22
+
23
+ ```bash
24
+ ruby examples/quickstart.rb <your_token>
25
+ ```
26
+
27
+ See the `examples` folder for more examples.
22
28
 
23
29
  ## API
24
30
 
@@ -28,6 +34,7 @@ See the `examples` folder for examples.
28
34
  * `message` - the Wit [message API](https://wit.ai/docs/http/20160330#get-intent-via-text-link)
29
35
  * `converse` - the low-level Wit [converse API](https://wit.ai/docs/http/20160330#converse-link)
30
36
  * `run_actions` - a higher-level method to the Wit converse API
37
+ * `interactive` - starts an interactive conversation with your bot
31
38
 
32
39
  ### Wit class
33
40
 
@@ -123,6 +130,15 @@ p "Yay, got Wit.ai response: #{resp}"
123
130
  ```
124
131
 
125
132
 
133
+ ### interactive
134
+
135
+ Starts an interactive conversation with your bot.
136
+
137
+ Example:
138
+ ```ruby
139
+ client.interactive
140
+ ```
141
+
126
142
  See the [docs](https://wit.ai/docs) for more information.
127
143
 
128
144
 
@@ -3,7 +3,11 @@ require 'wit'
3
3
  # Quickstart example
4
4
  # See https://wit.ai/l5t/Quickstart
5
5
 
6
- access_token = 'YOUR_ACCESS_TOKEN'
6
+ access_token = ARGV.shift
7
+ unless access_token
8
+ puts 'usage: ruby examples/quickstart.rb <access-token>'
9
+ exit
10
+ end
7
11
 
8
12
  def first_entity_value(entities, entity)
9
13
  return nil unless entities.has_key? entity
@@ -30,6 +34,4 @@ actions = {
30
34
  },
31
35
  }
32
36
  client = Wit.new access_token, actions
33
-
34
- session_id = 'my-user-id-42'
35
- client.run_actions session_id, 'weather in London', {}
37
+ client.interactive
data/lib/wit.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  require 'json'
2
2
  require 'logger'
3
3
  require 'net/http'
4
+ require 'securerandom'
4
5
 
5
6
  WIT_API_HOST = ENV['WIT_URL'] || 'https://api.wit.ai'
7
+ WIT_API_VERSION = ENV['WIT_API_VERSION'] || '20160516'
6
8
  DEFAULT_MAX_STEPS = 5
7
9
 
8
10
  class WitException < Exception
@@ -14,7 +16,7 @@ def req(access_token, meth_class, path, params={}, payload={})
14
16
 
15
17
  request = meth_class.new uri
16
18
  request['authorization'] = 'Bearer ' + access_token
17
- request['accept'] = 'application/vnd.wit.20160330+json'
19
+ request['accept'] = 'application/vnd.wit.' + WIT_API_VERSION + '+json'
18
20
  request.add_field 'Content-Type', 'application/json'
19
21
  request.body = payload.to_json
20
22
 
@@ -29,17 +31,17 @@ end
29
31
 
30
32
  def validate_actions(actions)
31
33
  learn_more = 'Learn more at https://wit.ai/docs/quickstart'
32
- raise WitException.new 'The second parameter should be a Hash' unless actions.is_a? Hash
34
+ Wit.logger.warn 'The second parameter should be a Hash' unless actions.is_a? Hash
33
35
  [:say, :merge, :error].each do |action|
34
- raise WitException.new "The #{action} action is missing. #{learn_more}" unless actions.has_key? action
36
+ Wit.logger.warn "The #{action} action is missing. #{learn_more}" unless actions.has_key? action
35
37
  end
36
38
  actions.each_pair do |k, v|
37
- raise WitException.new "The '#{k}' action name should be a symbol" unless k.is_a? Symbol
38
- raise WitException.new "The '#{k}' action should be a lambda function" unless v.respond_to? :call and v.lambda?
39
- raise WitException.new "The \'say\' action should take 3 arguments: session_id, context, msg. #{learn_more}" if k == :say and v.arity != 3
40
- raise WitException.new "The \'merge\' action should take 4 arguments: session_id, context, entities, msg. #{learn_more}" if k == :merge and v.arity != 4
41
- raise WitException.new "The \'error\' action should take 3 arguments: session_id, context, error. #{learn_more}" if k == :error and v.arity != 3
42
- raise WitException.new "The '#{k}' action should take 2 arguments: session_id, context. #{learn_more}" if k != :say and k != :merge and k != :error and v.arity != 2
39
+ Wit.logger.warn "The '#{k}' action name should be a symbol" unless k.is_a? Symbol
40
+ Wit.logger.warn "The '#{k}' action should be a lambda function" unless v.respond_to? :call and v.lambda?
41
+ Wit.logger.warn "The \'say\' action should take 3 arguments: session_id, context, msg. #{learn_more}" if k == :say and v.arity != 3
42
+ Wit.logger.warn "The \'merge\' action should take 4 arguments: session_id, context, entities, msg. #{learn_more}" if k == :merge and v.arity != 4
43
+ Wit.logger.warn "The \'error\' action should take 3 arguments: session_id, context, error. #{learn_more}" if k == :error and v.arity != 3
44
+ Wit.logger.warn "The '#{k}' action should take 2 arguments: session_id, context. #{learn_more}" if k != :say and k != :merge and k != :error and v.arity != 2
43
45
  end
44
46
  return actions
45
47
  end
@@ -66,17 +68,23 @@ class Wit
66
68
  end
67
69
 
68
70
  def message(msg)
71
+ logger.debug "Message request: msg=#{msg}"
69
72
  params = {}
70
73
  params[:q] = msg unless msg.nil?
71
- req @access_token, Net::HTTP::Get, '/message', params
74
+ res = req @access_token, Net::HTTP::Get, '/message', params
75
+ logger.debug "Message response: #{res}"
76
+ return res
72
77
  end
73
78
 
74
79
  def converse(session_id, msg, context={})
80
+ logger.debug "Converse request: session_id=#{session_id} msg=#{msg} context=#{context}"
75
81
  raise WitException.new 'context should be a Hash' unless context.is_a? Hash
76
82
  params = {}
77
83
  params[:q] = msg unless msg.nil?
78
84
  params[:session_id] = session_id
79
- req @access_token, Net::HTTP::Post, '/converse', params, context
85
+ res = req @access_token, Net::HTTP::Post, '/converse', params, context
86
+ logger.debug "Converse response: #{res}"
87
+ return res
80
88
  end
81
89
 
82
90
  def run_actions_(session_id, message, context, max_steps, user_message)
@@ -125,5 +133,22 @@ class Wit
125
133
  return run_actions_ session_id, message, context, max_steps, message
126
134
  end
127
135
 
136
+ def interactive(context={}, max_steps=DEFAULT_MAX_STEPS)
137
+ session_id = SecureRandom.uuid
138
+
139
+ while true
140
+ print '> '
141
+ msg = gets.strip
142
+
143
+ begin
144
+ context = run_actions(session_id, msg, context, max_steps)
145
+ rescue WitException => exp
146
+ p exp.message
147
+ end
148
+ end
149
+ rescue Interrupt => _exp
150
+ puts
151
+ end
152
+
128
153
  private :run_actions_
129
154
  end
data/wit.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'wit'
3
- s.version = '3.3.1'
3
+ s.version = '3.4.0'
4
4
  s.date = Date.today.to_s
5
5
  s.summary = 'Ruby SDK for Wit.ai'
6
6
  s.description = 'Ruby SDK for Wit.ai'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wit
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.1
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Wit Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-18 00:00:00.000000000 Z
11
+ date: 2016-05-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby SDK for Wit.ai
14
14
  email: help@wit.ai