Skip to content

Commit

Permalink
Update API version to 20200513
Browse files Browse the repository at this point in the history
  • Loading branch information
patapizza committed May 13, 2020
1 parent e3fdb23 commit 12e6350
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 56 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ See the `examples` folder for more examples.
### Overview

`wit-ruby` provides a Wit class with the following methods:
* `message` - the Wit [message API](https://wit.ai/docs/http/20160330#get-intent-via-text-link)
* `message` - the Wit [message API](https://wit.ai/docs/http/20200513#get-intent-via-text-link)
* `interactive` - starts an interactive conversation with your bot

### Wit class
Expand All @@ -49,7 +49,7 @@ client.message('set an alarm tomorrow at 7am')

### .message()

The Wit [message API](https://wit.ai/docs/http/20160330#get-intent-via-text-link).
The Wit [message API](https://wit.ai/docs/http/20200513#get-intent-via-text-link).

Takes the following parameters:
* `msg` - the text you want Wit.ai to extract the information from
Expand All @@ -74,39 +74,39 @@ payload in the parameters is a hash containing API arguments

#### .get_entities()
Returns a list of available entities for the app.
See [GET /entities](https://wit.ai/docs/http/20160526#get--entities-link)
See [GET /entities](https://wit.ai/docs/http/20200513#get--entities-link)

#### .post_entities(payload)
Creates a new entity with the given attributes.
See [POST /entities](https://wit.ai/docs/http/20160526#post--entities-link)
See [POST /entities](https://wit.ai/docs/http/20200513#post--entities-link)

#### .get_entity(entity_id)
Returns all the expressions validated for an entity.
See [GET /entities/:entity-id](https://wit.ai/docs/http/20160526#get--entities-:entity-id-link)
Returns all the information available for an entity.
See [GET /entities/:entity](https://wit.ai/docs/http/20200513#get--entities-:entity-link)

#### .put_entities(entity_id, payload)
Updates an entity with the given attributes.
See [PUT /entities/:entity-id](https://wit.ai/docs/http/20160526#put--entities-:entity-id-link)
See [PUT /entities/:entity](https://wit.ai/docs/http/20200513#put--entities-:entity-link)

#### .delete_entities(entity_id)
Permanently remove the entity.
See [DELETE /entities/:entity-id](https://wit.ai/docs/http/20160526#delete--entities-:entity-id-link)
Permanently removes the entity.
See [DELETE /entities/:entity](https://wit.ai/docs/http/20200513#delete--entities-:entity-link)

#### .post_values(entity_id, payload)
Add a possible value into the list of values for the entity.
See [POST /entities/:entity-id/values](https://wit.ai/docs/http/20160526#post--entities-:entity-id-values-link)
#### .post_keywords(entity_id, payload)
Adds a possible value into the list of keywords for the keywords entity.
See [POST /entities/:entity/keywords](https://wit.ai/docs/http/20160526#post--entities-:entity-id-values-link)

#### .delete_values(entity_id, value)
Delete a canonical value from the entity.
See [DELETE /entities/:entity-id/values/:value](https://wit.ai/docs/http/20160526#delete--entities-:entity-id-values-link)
#### .delete_keywords(entity_id, keyword)
Deletes a keyword from the entity.
See [DELETE /entities/:entity/keywords/:keyword](https://wit.ai/docs/http/20200513#delete--entities-:entity-keywords-link)

#### post_expressions(entity_id, value, payload)
Create a new expression of the canonical value of the entity.
See [POST /entities/:entity-id/values/:value/expressions](https://wit.ai/docs/http/20160526#post--entities-:entity-id-values-:value-id-expressions-link)
#### .post_synonyms(entity_id, keyword, payload)
Creates a new synonym for the keyword of the entity.
See [POST /entities/:entity/keywords/:keyword/synonyms](https://wit.ai/docs/http/20200513#post--entities-:entity-keywords-:keyword-synonyms-link)

#### delete_expressions(entity_id, value, expression)
Delete an expression of the canonical value of the entity.
See [DELETE /entities/:entity-id/values/:value/expressions/:expression](https://wit.ai/docs/http/20160526#delete--entities-:entity-id-values-:value-id-expressions-link)
#### delete_synonyms(entity_id, keyword, synonym)
Deletes a synonym of the keyword of the entity.
See [DELETE /entities/:entity/keywords/:keyword/synonyms/:synonym](https://wit.ai/docs/http/20200513#delete--entities-:entity-keywords-:keyword-synonyms-link)

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

Expand Down
18 changes: 12 additions & 6 deletions examples/celebrities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@
ARGV.shift

# Celebrities example
# See https://wit.ai/aforaleka/wit-example-celebrities/
# See https://wit.ai/aleka/wit-example-celebrities/

def first_entity_value(entities, entity)
def first_entity_resolved_value(entities, entity)
return nil unless entities.has_key? entity
val = entities[entity][0]['value']
val = entities[entity][0]['resolved']['values'][0]
return nil if val.nil?
return val
end

def first_trait_value(traits, trait)
return nil unless traits.has_key? trait
val = traits[trait][0]['value']
return nil if val.nil?
return val
end

def handle_message(response)
entities = response['entities']
greetings = first_entity_value(entities, 'greetings')
celebrity = first_entity_value(entities, 'notable_person')
greetings = first_trait_value(response['traits'], 'wit$greetings')
celebrity = first_entity_resolved_value(responsee['entities'], 'wit$notable_person')

case
when celebrity
Expand Down
19 changes: 10 additions & 9 deletions examples/joke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
ARGV.shift

# Joke example
# See https://wit.ai/aforaleka/wit-example-joke-bot/
# See https://wit.ai/aleka/wit-example-joke-bot/

def first_entity_value(entities, entity)
return nil unless entities.has_key? entity
val = entities[entity][0]['value']
def first_value(obj, key)
return nil unless obj.has_key? key
val = obj[key][0]['value']
return nil if val.nil?
return val.is_a?(Hash) ? val['value'] : val
return val
end

$all_jokes = {
Expand All @@ -36,10 +36,11 @@ def first_entity_value(entities, entity)

def handle_message(response)
entities = response['entities']
get_joke = first_entity_value(entities, 'getJoke')
greetings = first_entity_value(entities, 'greetings')
category = first_entity_value(entities, 'category')
sentiment = first_entity_value(entities, 'sentiment')
traits = response['traits']
get_joke = first_value(traits, 'getJoke')
greetings = first_value(traits, 'wit$greetings')
category = first_value(entities, 'category:category')
sentiment = first_value(traits, 'wit$sentiment')

case
when get_joke
Expand Down
Binary file modified examples/wit-example-celebrities.zip
Binary file not shown.
Binary file modified examples/wit-example-joke-bot.zip
Binary file not shown.
36 changes: 16 additions & 20 deletions lib/wit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Wit
class Error < StandardError; end

WIT_API_HOST = ENV['WIT_URL'] || 'https://api.wit.ai'
WIT_API_VERSION = ENV['WIT_API_VERSION'] || '20160516'
DEFAULT_MAX_STEPS = 5
WIT_API_VERSION = ENV['WIT_API_VERSION'] || '20200513'
LEARN_MORE = 'Learn more at https://wit.ai/docs/quickstart'

def initialize(opts = {})
Expand Down Expand Up @@ -65,7 +64,7 @@ def get_entities
end

def post_entities(payload)
payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:id, :doc, :values, :lookups].include?(k) }
payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:name, :roles, :lookups, :keywords].include?(k) }
validate_payload payload
req(logger, @access_token, Net::HTTP::Post, "/entities", {}, payload)
end
Expand All @@ -75,7 +74,7 @@ def get_entity(entity_id)
end

def put_entities(entity_id, payload)
payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:id, :doc, :values].include?(k) }
payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:name, :roles, :lookups, :keywords].include?(k) }
validate_payload payload
req(logger, @access_token, Net::HTTP::Put, "/entities/#{URI.encode(entity_id)}", {}, payload)
end
Expand All @@ -84,38 +83,35 @@ def delete_entities(entity_id)
req(logger, @access_token, Net::HTTP::Delete, "/entities/#{URI.encode(entity_id)}")
end

def post_values(entity_id, payload)
payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:value, :expressions, :metadata].include?(k) }
def post_keywords(entity_id, payload)
payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:keyword, :synonyms].include?(k) }
validate_payload payload
req(logger, @access_token, Net::HTTP::Post, "/entities/#{URI.encode(entity_id)}/values", {}, payload)
req(logger, @access_token, Net::HTTP::Post, "/entities/#{URI.encode(entity_id)}/keywords", {}, payload)
end

def delete_values(entity_id, value)
req(logger, @access_token, Net::HTTP::Delete, "/entities/#{URI.encode(entity_id)}/values/#{URI.encode(value)}")
def delete_keywords(entity_id, keyword)
req(logger, @access_token, Net::HTTP::Delete, "/entities/#{URI.encode(entity_id)}/keywords/#{URI.encode(keyword)}")
end

def post_expressions(entity_id, value, payload)
payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:expression].include?(k) }
def post_synonyms(entity_id, keyword, payload)
payload = payload.map {|k, v| [(k.to_sym rescue k), v]}.to_h.reject{ |k| ![:synonym].include?(k) }
validate_payload payload
req(logger,@access_token, Net::HTTP::Post, "/entities/#{URI.encode(entity_id)}/values/#{URI.encode(value)}/expressions", {}, payload)
req(logger,@access_token, Net::HTTP::Post, "/entities/#{URI.encode(entity_id)}/keywords/#{URI.encode(keyword)}/synonyms", {}, payload)
end

def delete_expressions(entity_id, value, expression)
req(logger,@access_token, Net::HTTP::Delete, "/entities/#{URI.encode(entity_id)}/values/#{URI.encode(value)}/expressions/#{URI.encode(expression)}")
def delete_synonyms(entity_id, keyword, synonym)
req(logger,@access_token, Net::HTTP::Delete, "/entities/#{URI.encode(entity_id)}/keywords/#{URI.encode(keyword)}/synonyms/#{URI.encode(synonym)}")
end

private

def validate_payload(payload)
key_types = {
id: String,
doc: String,
value: String,
values: Array,
name: String,
roles: Array,
lookups: Array,
expression: String,
expressions: Array,
metadata: String,
keywords: Array,
}
payload.each do |k, v|
raise Error.new("#{k.to_s} in request body must be #{key_types[k].to_s} type") unless key_types[k] == v.class
Expand Down

0 comments on commit 12e6350

Please sign in to comment.