Skip to content

Commit a19da9d

Browse files
committed
API docs + better error message
1 parent 84e8bf4 commit a19da9d

4 files changed

Lines changed: 103 additions & 8 deletions

File tree

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v3.1
2+
3+
- allows for custom logging
4+
- better error message
5+
16
## v3.0
27

38
Bot Engine integration

README.md

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,111 @@ gem install wit-*.gem
2020

2121
See the `examples` folder for examples.
2222

23+
## API
24+
25+
### Overview
26+
27+
`wit-ruby` provides a Wit class with the following methods:
28+
* `message` - the Wit [message API](https://wit.ai/docs/http/20160330#get-intent-via-text-link)
29+
* `converse` - the low-level Wit [converse API](https://wit.ai/docs/http/20160330#converse-link)
30+
* `run_actions` - a higher-level method to the Wit converse API
31+
32+
### Wit class
33+
34+
The Wit constructor takes the following parameters:
35+
* `access_token` - the access token of your Wit instance
36+
* `actions` - the `Hash` with your actions
37+
38+
The `actions` `Hash` has action names as keys, and action implementations as values.
39+
Action names are symbols, and action implementations are lambda functions (not `Proc`).
40+
You need to provide at least an implementation for the special actions `:say`, `:merge` and `:error`.
41+
42+
A minimal `actions` `Hash` looks like this:
43+
```ruby
44+
actions = {
45+
:say => -> (session_id, msg) {
46+
p msg
47+
},
48+
:merge => -> (context, entities) {
49+
return context
50+
},
51+
:error => -> (session_id, msg) {
52+
p 'Oops I don\'t know what to do.'
53+
},
54+
}
55+
```
56+
57+
A custom action takes one parameter:
58+
* `context` - the `Hash` representing the session state
59+
60+
Example:
61+
```ruby
62+
require 'wit'
63+
client = Wit.new access_token, actions
64+
```
65+
2366
### Logging
2467

25-
Default logging is to `STDOUT` with `INFO` level. Silence logger as follows.
68+
Default logging is to `STDOUT` with `INFO` level.
2669

70+
You can setup your logging level as follows:
2771
```ruby
2872
Wit.logger.level = Logger::WARN
2973
```
74+
See the [Logger class](http://ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html) docs for more information.
3075

31-
## API
76+
### message
77+
78+
The Wit [message API](https://wit.ai/docs/http/20160330#get-intent-via-text-link).
79+
80+
Takes the following parameters:
81+
* `msg` - the text you want Wit.ai to extract the information from
82+
83+
Example:
84+
```ruby
85+
resp = client.message 'what is the weather in London?'
86+
p "Yay, got Wit.ai response: #{resp}"
87+
```
88+
89+
### run_actions
90+
91+
A higher-level method to the Wit converse API.
92+
93+
Takes the following parameters:
94+
* `session_id` - a unique identifier describing the user session
95+
* `message` - the text received from the user
96+
* `context` - the `Hash` representing the session state
97+
* `max_steps` - (optional) the maximum number of actions to execute (defaults to 5)
98+
99+
Example:
100+
```ruby
101+
session = 'my-user-session-42'
102+
context0 = {}
103+
context1 = client.run_actions session, 'what is the weather in London?', context0
104+
p "The session state is now: #{context1}"
105+
context2 = client.run_actions session, 'and in Brussels?', context1
106+
p "The session state is now: #{context2}"
107+
```
108+
109+
### converse
110+
111+
The low-level Wit [converse API](https://wit.ai/docs/http/20160330#converse-link).
112+
113+
Takes the following parameters:
114+
* `session_id` - a unique identifier describing the user session
115+
* `msg` - the text received from the user
116+
* `context` - the `Hash` representing the session state
117+
118+
Example:
119+
```ruby
120+
resp = client.converse 'my-user-session-42', 'what is the weather in London?', {}
121+
p "Yay, got Wit.ai response: #{resp}"
122+
```
32123

33-
`wit-ruby` provides a Wit class with the following methods:
34-
* `message` - the Wit message API
35-
* `converse` - the low-level Wit converse API
36-
* `run_actions` - a higher-level method to the Wit converse API
37124

38125
See the [docs](https://wit.ai/docs) for more information.
39126

127+
40128
## Thanks
41129

42130
Thanks to [Justin Workman](http://github.com/xtagon) for releasing a first version in October 2013. We really appreciate!

lib/wit.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def req(access_token, meth_class, path, params={}, payload={})
2121
Net::HTTP.start uri.host, uri.port, {:use_ssl => uri.scheme == 'https'} do |http|
2222
rsp = http.request request
2323
raise WitException.new "HTTP error code=#{rsp.code}" unless rsp.code.to_i <= 200
24-
JSON.parse rsp.body
24+
json = JSON.parse rsp.body
25+
raise WitException.new "Wit responded with an error: #{json['error']}" if json.has_key? 'error'
26+
json
2527
end
2628
end
2729

wit.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'wit'
3-
s.version = '3.0.0'
3+
s.version = '3.1.0'
44
s.date = '2014-12-05'
55
s.summary = 'Ruby SDK for Wit.ai'
66
s.description = 'Ruby SDK for Wit.ai'

0 commit comments

Comments
 (0)