wit 7.0.0 → 7.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +3 -0
  3. data/README.md +38 -0
  4. data/lib/wit.rb +60 -18
  5. data/wit.gemspec +1 -1
  6. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a15b6f521d5b67b3c2d512e1ea9262ef4b1e39109e22eb9822a10aabe8e7b34
4
- data.tar.gz: 6fd43d27a1edc8ccba793c45ca0fac4e90f9c27d265b3a105135e1bfb85b707e
3
+ metadata.gz: 94c89f68746b71c5af51cd31f5479780820a01ec01401adc6234dfcab54460d6
4
+ data.tar.gz: e364ac9ca54797c85bd098279e4add1360b4c3e1a63e1f5ddf2f7ac94dd1411d
5
5
  SHA512:
6
- metadata.gz: eed12472514c8efdcbf6716b637aed58820dd98c437703566c027a10b57dca54877011d02dbc173e326aad8f7fa6a7d6c28ca499161070a73b913e73d840b650
7
- data.tar.gz: 6d6f64a64117a9fd39ba9eb6074e3b7dc17bad80e48382930ea21f4584b2a1c96b5289df4ffdc6623ab8691d10c4af515f112551fd8e787184dec83e8fa01091
6
+ metadata.gz: fbe3df4df740ddbdae3bb8c021de7d4caf4d280454a85ac2ad07e45e49018b265e86e9a94f0da7c7b69760e5942d2f7fd47103dd58ea5a7430bc43ab93ab0121
7
+ data.tar.gz: c934ff92d4d252af1dcf57b6582612b0ea229d9d441a4507efeffa1d7a7749af07819ee36ccd7dbe55f85906c7973f30aecf08bad5120c8a7fa05293e873717b
data/CHANGES.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## v7.0.1
2
+ - Updating encode function to encode more special characters that could be passed in when constructing a request URL.
3
+
1
4
  ## v7.0.0
2
5
  - Updated API version to latest: `20200513`. Browse the latest HTTP API documentation [here](https://wit.ai/docs/http/20200513#get__message_link).
3
6
  - Added intents and traits CRUD methods.
data/README.md CHANGED
@@ -47,6 +47,44 @@ client = Wit.new(access_token: access_token)
47
47
  client.message('set an alarm tomorrow at 7am')
48
48
  ```
49
49
 
50
+ ### Create new App Using the Access Token
51
+
52
+ Creates new app using the server token.
53
+ See [POST /apps](https://wit.ai/docs/http/20200513#post__apps_link).
54
+
55
+ ```ruby
56
+ new_app_payload = {name: "new-app-1", lang: "en", private: true}
57
+ # set_new_app_token will make the client use the new app's token.
58
+ # that flag is set to false by default.
59
+ client.create_new_app(new_app_payload, set_new_app_token = true)
60
+ ```
61
+
62
+ ### Train the app programatically using '/utterances'
63
+
64
+ Trains and annotates an utterance or more.
65
+ See [POST /utterances](https://wit.ai/docs/http/20200513#post__utterances_link).
66
+
67
+ ```ruby
68
+ # you have to create the intent and entity before using any of them.
69
+ utterance_payload = {
70
+ text: "I want to fly to china",
71
+ intent: "flight_request",
72
+ entities: [
73
+ {
74
+ "entity": "wit$location:to",
75
+ "start": 17,
76
+ "end": 22,
77
+ "body": "china",
78
+ "entities": []
79
+ }
80
+ ],
81
+ traits: []
82
+ }
83
+
84
+ # utterance payload can be a list of utterances or a single one
85
+ client.post_utterances(utterance_payload)
86
+ ```
87
+
50
88
  ### .message()
51
89
 
52
90
  The Wit [message API](https://wit.ai/docs/http/20200513#get-intent-via-text-link).
data/lib/wit.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2
2
 
3
+ require 'cgi'
3
4
  require 'json'
4
5
  require 'logger'
5
6
  require 'net/http'
@@ -59,12 +60,30 @@ class Wit
59
60
  puts
60
61
  end
61
62
 
63
+ def create_new_app(payload, set_new_app_token = false)
64
+ response = req(logger, @access_token, Net::HTTP::Post, "/apps", {}, payload)
65
+ @access_token = response['access_token'] if set_new_app_token
66
+ return response
67
+ end
68
+
69
+ def post_utterances(payload)
70
+ payload = [payload] if payload.is_a? Hash
71
+
72
+ payload.each do |utterance|
73
+ unless utterance[:entities].empty?
74
+ utterance[:entities] = validate_entities utterance[:entities]
75
+ end
76
+ validate_payload utterance
77
+ end
78
+ req(logger, @access_token, Net::HTTP::Post, "/utterances", {}, payload)
79
+ end
80
+
62
81
  def get_intents
63
82
  req(logger, @access_token, Net::HTTP::Get, "/intents")
64
83
  end
65
84
 
66
85
  def get_intent(intent)
67
- req(logger, @access_token, Net::HTTP::Get, "/intents/#{URI.encode(intent)}")
86
+ req(logger, @access_token, Net::HTTP::Get, "/intents/#{CGI::escape(intent)}")
68
87
  end
69
88
 
70
89
  def post_intents(payload)
@@ -72,7 +91,7 @@ class Wit
72
91
  end
73
92
 
74
93
  def delete_intents(intent)
75
- req(logger, @access_token, Net::HTTP::Delete, "/intents/#{URI.encode(intent)}")
94
+ req(logger, @access_token, Net::HTTP::Delete, "/intents/#{CGI::escape(intent)}")
76
95
  end
77
96
 
78
97
  def get_entities
@@ -86,37 +105,37 @@ class Wit
86
105
  end
87
106
 
88
107
  def get_entity(entity)
89
- req(logger, @access_token, Net::HTTP::Get, "/entities/#{URI.encode(entity)}")
108
+ req(logger, @access_token, Net::HTTP::Get, "/entities/#{CGI::escape(entity)}")
90
109
  end
91
110
 
92
111
  def put_entities(entity, payload)
93
112
  payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:name, :roles, :lookups, :keywords].include?(k) }
94
113
  validate_payload payload
95
- req(logger, @access_token, Net::HTTP::Put, "/entities/#{URI.encode(entity)}", {}, payload)
114
+ req(logger, @access_token, Net::HTTP::Put, "/entities/#{CGI::escape(entity)}", {}, payload)
96
115
  end
97
116
 
98
117
  def delete_entities(entity)
99
- req(logger, @access_token, Net::HTTP::Delete, "/entities/#{URI.encode(entity)}")
118
+ req(logger, @access_token, Net::HTTP::Delete, "/entities/#{CGI::escape(entity)}")
100
119
  end
101
120
 
102
121
  def post_entities_keywords(entity, payload)
103
122
  payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:keyword, :synonyms].include?(k) }
104
123
  validate_payload payload
105
- req(logger, @access_token, Net::HTTP::Post, "/entities/#{URI.encode(entity)}/keywords", {}, payload)
124
+ req(logger, @access_token, Net::HTTP::Post, "/entities/#{CGI::escape(entity)}/keywords", {}, payload)
106
125
  end
107
126
 
108
127
  def delete_entities_keywords(entity, keyword)
109
- req(logger, @access_token, Net::HTTP::Delete, "/entities/#{URI.encode(entity)}/keywords/#{URI.encode(keyword)}")
128
+ req(logger, @access_token, Net::HTTP::Delete, "/entities/#{CGI::escape(entity)}/keywords/#{CGI::escape(keyword)}")
110
129
  end
111
130
 
112
131
  def post_entities_keywords_synonyms(entity, keyword, payload)
113
132
  payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:synonym].include?(k) }
114
133
  validate_payload payload
115
- req(logger,@access_token, Net::HTTP::Post, "/entities/#{URI.encode(entity)}/keywords/#{URI.encode(keyword)}/synonyms", {}, payload)
134
+ req(logger,@access_token, Net::HTTP::Post, "/entities/#{CGI::escape(entity)}/keywords/#{CGI::escape(keyword)}/synonyms", {}, payload)
116
135
  end
117
136
 
118
137
  def delete_entities_keywords_synonyms(entity, keyword, synonym)
119
- req(logger,@access_token, Net::HTTP::Delete, "/entities/#{URI.encode(entity)}/keywords/#{URI.encode(keyword)}/synonyms/#{URI.encode(synonym)}")
138
+ req(logger,@access_token, Net::HTTP::Delete, "/entities/#{CGI::escape(entity)}/keywords/#{CGI::escape(keyword)}/synonyms/#{CGI::escape(synonym)}")
120
139
  end
121
140
 
122
141
  def get_traits
@@ -124,7 +143,7 @@ class Wit
124
143
  end
125
144
 
126
145
  def get_trait(trait)
127
- req(logger, @access_token, Net::HTTP::Get, "/traits/#{URI.encode(trait)}")
146
+ req(logger, @access_token, Net::HTTP::Get, "/traits/#{CGI::escape(trait)}")
128
147
  end
129
148
 
130
149
  def post_traits(payload)
@@ -132,15 +151,15 @@ class Wit
132
151
  end
133
152
 
134
153
  def post_traits_values(trait, payload)
135
- req(logger, @access_token, Net::HTTP::Post, "/traits/#{URI.encode(trait)}/values", {}, payload)
154
+ req(logger, @access_token, Net::HTTP::Post, "/traits/#{CGI::escape(trait)}/values", {}, payload)
136
155
  end
137
156
 
138
157
  def delete_traits_values(trait, value)
139
- req(logger, @access_token, Net::HTTP::Delete, "/traits/#{URI.encode(trait)}/values/#{URI.encode(value)}")
158
+ req(logger, @access_token, Net::HTTP::Delete, "/traits/#{CGI::escape(trait)}/values/#{CGI::escape(value)}")
140
159
  end
141
160
 
142
161
  def delete_traits(trait)
143
- req(logger, @access_token, Net::HTTP::Delete, "/traits/#{URI.encode(trait)}")
162
+ req(logger, @access_token, Net::HTTP::Delete, "/traits/#{CGI::escape(trait)}")
144
163
  end
145
164
 
146
165
  private
@@ -152,12 +171,37 @@ class Wit
152
171
  roles: Array,
153
172
  lookups: Array,
154
173
  keywords: Array,
174
+ text: String,
175
+ intent: String,
176
+ entities: Array,
177
+ traits: Array
155
178
  }
156
179
  payload.each do |k, v|
157
180
  raise Error.new("#{k.to_s} in request body must be #{key_types[k].to_s} type") unless key_types[k] == v.class
158
181
  end
159
182
  end
160
183
 
184
+ def validate_entities(entities)
185
+ entity_keys = {
186
+ entity: String,
187
+ start: Integer,
188
+ end: Integer,
189
+ body: String,
190
+ entities: Array
191
+ }
192
+ entities.each do |entity|
193
+ entity = entity.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| !entity_keys.keys.include?(k) }
194
+ entity.each do |k, v|
195
+ if k == :entities && !v.empty?
196
+ validate_entities(v)
197
+ end
198
+ raise Error.new("#{k.to_s} in entities body must be #{entity_keys[k].to_s} type") unless entity_keys[k] == v.class
199
+ end
200
+ end
201
+
202
+ return entities
203
+ end
204
+
161
205
  def req(logger, access_token, meth_class, path, params={}, payload={})
162
206
  uri = URI(WIT_API_HOST + path)
163
207
  uri.query = URI.encode_www_form(params)
@@ -172,12 +216,10 @@ class Wit
172
216
 
173
217
  Net::HTTP.start(uri.host, uri.port, {:use_ssl => uri.scheme == 'https'}) do |http|
174
218
  rsp = http.request(request)
175
- if rsp.code.to_i != 200
176
- raise Error.new("HTTP error code=#{rsp.code}")
177
- end
178
219
  json = JSON.parse(rsp.body)
179
- if json.is_a?(Hash) and json.has_key?('error')
180
- raise Error.new("Wit responded with an error: #{json['error']}")
220
+ if rsp.code.to_i != 200
221
+ error_msg = (json.is_a?(Hash) and json.has_key?('error')) ? json['error'] : json
222
+ raise Error.new("Wit responded with an error: #{error_msg}")
181
223
  end
182
224
  logger.debug("#{meth_class} #{uri} #{json}")
183
225
  json
data/wit.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'wit'
5
- s.version = '7.0.0'
5
+ s.version = '7.0.1'
6
6
  s.summary = 'Ruby SDK for Wit.ai'
7
7
  s.description = 'Ruby SDK for Wit.ai'
8
8
  s.authors = ['The Wit Team']
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: 7.0.0
4
+ version: 7.0.1
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: 2020-05-27 00:00:00.000000000 Z
11
+ date: 2022-05-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby SDK for Wit.ai
14
14
  email: help@wit.ai
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  - !ruby/object:Gem::Version
51
51
  version: '0'
52
52
  requirements: []
53
- rubygems_version: 3.1.3
53
+ rubygems_version: 3.0.3.1
54
54
  signing_key:
55
55
  specification_version: 4
56
56
  summary: Ruby SDK for Wit.ai