Skip to content

Commit

Permalink
Merge pull request #14 from phbernard/master
Browse files Browse the repository at this point in the history
Typos in entity sample code
  • Loading branch information
Gugic authored Jun 29, 2017
2 parents 1b0a7e9 + 920c0e0 commit f154ef9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ client.text_request 'call Mozart', entities: [
]
}
]

```

Or with separate **create_user_entities_request** object with full CRUD support:
Expand All @@ -102,11 +102,11 @@ entries_unknown = [
ApiAiRuby::Entry.new('Jane Doe', %w(Jane))
]

entity_contacts = ApiAiRuby::Entity.new('contacts', [entries_composers])
entity_contacts = ApiAiRuby::Entity.new('contacts', entries_composers)

# let's go
uer = client.create_user_entities_request
uer.create(contacts) # or uer.create([entity1, entity2...])
uer.create(entity_contacts) # or uer.create([entity1, entity2...])

client.text_request 'call Mozart' # will work

Expand All @@ -117,7 +117,7 @@ client.text_request 'call John' # will work

uer.retrieve('contacts') # will return current state of user entity
uer.delete('contacts') # will remove user entities for given session

```
## Context
Also SDK has full support of [contexts](https://docs.api.ai/docs/contexts) API.AI endpoint with special object, called ```contexts_request```
Expand All @@ -131,7 +131,7 @@ parameters = {
}
name = 'test_context'

# you can create context using built-in model ApiAiRuby::Context
# you can create context using built-in model ApiAiRuby::Context
test_context = ApiAiRuby::Context.new(name, lifespan, parameters)
another_test_context = ApiAiRuby::Context.new('another_test_context')
one_more_test_context = ApiAiRuby::Context.new('one_more_test_context', 4)
Expand Down Expand Up @@ -194,6 +194,6 @@ Please see the [httprb wiki on timeouts](https://github.com/httprb/http/wiki/Tim
* 1.2.2 - added configurable timeouts for requests (thanks [bramski](https://github.com/bramski))
* 1.2.1 - fixed UTF-8 in text-requests
* 1.2.0 - added configurable session_id and full userEntities support
* 1.1.4 - removed unused dependency and updated default API version
* 1.1.4 - removed unused dependency and updated default API version
* 1.1.3 - fixed non-correctly serialized parameters in new contexts during query send process
* 1.1.2 - fixed compatibility with ruby version less then 2.1.6
9 changes: 7 additions & 2 deletions lib/api-ai-ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ def voice_request(file_stream, options = {})

# @return [ApiAiRuby::UserEntitiesRequest]
def create_user_entities_request
ApiAiRuby::UserEntitiesRequest.new(self);
ApiAiRuby::UserEntitiesRequest.new(self)
end

# @return [ApiAiRuby::UserEntitiesRequest]
def create_entities_request
ApiAiRuby::UserEntitiesRequest.new(self, {uri_path: 'entities'})
end

# @return [ApiAiRuby::ContextsRequest]
Expand All @@ -98,4 +103,4 @@ def create_contexts_request
end

end
end
end
55 changes: 49 additions & 6 deletions lib/api-ai-ruby/crud/user_entity_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class UserEntitiesRequest < ApiAiRuby::RequestQuery
def initialize(client, options = {})
super client, options
@headers['Content-Type'] = 'application/json; charset=UTF-8'
@crud_base_uri = client.api_base_url + 'userEntities'
@uri = @crud_base_uri
@crud_base_uri = client.api_base_url +
(options[:uri_path] ? options[:uri_path] : 'userEntities')
end

# @param argument [Array<ApiAiRuby::Entity, Hash>, ApiAiRuby::Entity, Hash]
Expand All @@ -16,9 +16,23 @@ def create(argument)
end
@uri = @crud_base_uri
@request_method = :post
@options[:entities] = argument.is_a?(Array) ? argument : [argument]
response = self.perform
@options.delete(:entities)

old_options = nil

begin
if argument.is_a?(ApiAiRuby::Entity)
old_options = @options
@options = argument
else
@options[:entities] = argument.is_a?(Array) ? argument : [argument]
end

response = self.perform

@options.delete(:entities) if @options.respond_to? :delete
rescue
@options = old_options || @options
end
response
end

Expand Down Expand Up @@ -46,6 +60,35 @@ def update(name, entries, extend = false)
response
end

def update_entries(name, entries)

raise ApiAiRuby::ClientError.new('Entity name required') if !name

@options = entries

@request_method = :put
@uri = @crud_base_uri + '/' + name + '/entries'
response = self.perform
@options.delete(:extend)
@options.delete(:name)
@options.delete(:entries)
response
end

def add_entries(name, entries)
raise ApiAiRuby::ClientError.new('Entity name required') if !name

@options = entries

@request_method = :post
@uri = @crud_base_uri + '/' + name + '/entries'
response = self.perform
@options.delete(:extend)
@options.delete(:name)
@options.delete(:entries)
response
end

def delete(name)
raise ApiAiRuby::ClientError.new('Entity name required') if !name
@request_method = :delete
Expand All @@ -54,4 +97,4 @@ def delete(name)
end

end
end
end

0 comments on commit f154ef9

Please sign in to comment.