api-ai-ruby 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 730482838f5570e11f15f9dc035bd1660dcc9ee6
4
- data.tar.gz: 50f4c68548a98f9055f01e8196f5bb1cea2931ef
3
+ metadata.gz: 00c4d8ce3cf45b523509982821303403e1dc26c4
4
+ data.tar.gz: 18107f16fe11ea0477c3b0b9797fe2c5164630b6
5
5
  SHA512:
6
- metadata.gz: ec864af3471a6f93aa1bb898e7fc29ed341b4dfb4828da406beb2570538bcd3f21b71755fdd3eb66822bda1a1e5baac3ae6e6ba866a24957eeee16903504f238
7
- data.tar.gz: 9d1548277b2bb7a98b5f11bb789f91d80d050bfa3d9b7dad7fd29da6a63c87d2a48d3833648c594358124b623185cee0e87d5a0e68d2546c7cc2bc1d2dc9a53c
6
+ metadata.gz: f3ce511381bc5c787c68dcd87da8ae0fa5eb65c6a0b64cf822a54218810c818a8fab23ba6bbf8a66bc2993085f2faee649f3ce7bb20a02b5d6dce1a5f9d3268b
7
+ data.tar.gz: b5ec12321765aaf92ebb7f764a4c737ae92fb916f20a6d52e15736aaf3b225fc028d1159b223b35dd01d295dd21e99e48df037ad131d905acef55f15545fec17
data/README.md CHANGED
@@ -79,15 +79,15 @@ client.text_request 'call Mozart', entities: [
79
79
  {
80
80
  name: 'contacts',
81
81
  entries: [
82
- {value: 'Mozart', synonyms: %w(Mozart Wolfgang))},
83
- {value: 'Salieri', synonyms: %w(Salieri Antonio))},
82
+ {value: 'Mozart', synonyms: %w(Mozart Wolfgang)},
83
+ {value: 'Salieri', synonyms: %w(Salieri Antonio)}
84
84
  ]
85
85
  }
86
86
  ]
87
87
 
88
88
  ```
89
89
 
90
- Or with separate **user_entities_request** object with full CRUD support:
90
+ Or with separate **create_user_entities_request** object with full CRUD support:
91
91
 
92
92
  ```ruby
93
93
 
@@ -105,7 +105,7 @@ entries_unknown = [
105
105
  entity_contacts = ApiAiRuby::Entity.new('contacts', [entries_composers])
106
106
 
107
107
  # let's go
108
- uer = client.user_entities_request
108
+ uer = client.create_user_entities_request
109
109
  uer.create(contacts) # or uer.create([entity1, entity2...])
110
110
 
111
111
  client.text_request 'call Mozart' # will work
@@ -119,6 +119,40 @@ uer.retrieve('contacts') # will return current state of user entity
119
119
  uer.delete('contacts') # will remove user entities for given session
120
120
 
121
121
  ```
122
+ ## Context
123
+ Also SDK has full support of [contexts](https://docs.api.ai/docs/contexts) API.AI endpoint with special object, called ```contexts_request```
124
+ Usage is simple:
125
+ ```ruby
126
+
127
+ # some preparations
128
+ lifespan = 5
129
+ parameters = {
130
+ :param_name => 'param_value'
131
+ }
132
+ name = 'test_context'
133
+
134
+ # you can create context using built-in model ApiAiRuby::Context
135
+ test_context = ApiAiRuby::Context.new(name, lifespan, parameters)
136
+ another_test_context = ApiAiRuby::Context.new('another_test_context')
137
+ one_more_test_context = ApiAiRuby::Context.new('one_more_test_context', 4)
138
+
139
+ # ok, we are ready
140
+
141
+ context_request = @client.create_contexts_request
142
+
143
+ # there are different options to be used with .create
144
+
145
+ context_request.create(test_context)
146
+ context_request.create([another_test_context, one_more_test_context])
147
+ context_request.create('one_more_super_final_mega_context')
148
+
149
+ context_request.retrieve('test_context') # will return you single context or nothing
150
+ context_request.list() # will return you list of all contexts used in current session
151
+ context_request.delete('test_context') # will remove single context
152
+ context_request.delete() # will remove all context in session
153
+
154
+ ```
155
+
122
156
  #Timeouts
123
157
  **ApiAiRuby::Client** uses the [http gem](https://github.com/httprb/http) under the hood. You can use ```timeout_options``` on the client to set these.
124
158
  ```ruby
@@ -141,6 +175,15 @@ Please see the [httprb wiki on timeouts](https://github.com/httprb/http/wiki/Tim
141
175
 
142
176
  #Changelog
143
177
 
178
+ ##1.3.0
179
+ ###Non-breaking
180
+ - contexts endpoint support (https://docs.api.ai/docs/contexts)
181
+ - better RDoc
182
+ ###Breaking
183
+ - ApiAiRuby::Client::user_entities_request renamed to ApiAiRuby::Client::create_user_entities_request
184
+ - ApiAiRuby::Entity::addEntry renamed to ApiAiRuby::Entity::add_entry
185
+
186
+ ##Previous
144
187
  * 1.2.3 - events support
145
188
  * 1.2.2 - added configurable timeouts for requests (thanks [bramski](https://github.com/bramski))
146
189
  * 1.2.1 - fixed UTF-8 in text-requests
@@ -6,6 +6,8 @@ require 'api-ai-ruby/request/request_query'
6
6
  require 'api-ai-ruby/request/text_request'
7
7
  require 'api-ai-ruby/request/event_request'
8
8
  require 'api-ai-ruby/request/voice_request'
9
+ require 'api-ai-ruby/models/context'
9
10
  require 'api-ai-ruby/models/entry'
10
11
  require 'api-ai-ruby/models/entity'
12
+ require 'api-ai-ruby/crud/contexts_request'
11
13
  require 'api-ai-ruby/crud/user_entity_request'
@@ -2,6 +2,7 @@ require 'securerandom'
2
2
 
3
3
  module ApiAiRuby
4
4
  class Client
5
+
5
6
  attr_accessor :client_access_token, :timeout_options
6
7
  attr_writer :user_agent, :api_version, :api_lang, :api_base_url, :api_session_id
7
8
 
@@ -25,18 +26,22 @@ module ApiAiRuby
25
26
  @user_agent ||= "ApiAiRubyGem/#{ApiAiRuby::Constants::VERSION}"
26
27
  end
27
28
 
29
+ # @return [String]
28
30
  def api_base_url
29
31
  @api_base_url ||= ApiAiRuby::Constants::DEFAULT_BASE_URL
30
32
  end
31
33
 
34
+ # @return [String]
32
35
  def api_lang
33
36
  @api_lang ||= ApiAiRuby::Constants::DEFAULT_CLIENT_LANG
34
37
  end
35
38
 
39
+ # @return [String]
36
40
  def api_version
37
41
  @api_version ||= ApiAiRuby::Constants::DEFAULT_API_VERSION
38
42
  end
39
43
 
44
+ # @return [String]
40
45
  def api_session_id
41
46
  @api_session_id
42
47
  end
@@ -59,27 +64,38 @@ module ApiAiRuby
59
64
  ApiAiRuby::TextRequest.new(self, options).perform
60
65
  end
61
66
 
62
- # @param eventName [String]
63
- # @param data [Object]
64
- # @param options [Object]
65
- def event_request (eventName = '', data = {}, options = {})
67
+ # @param event_name [String]
68
+ # @param data [Hash]
69
+ # @param options [Hash]
70
+ # @return [Hash]
71
+ def event_request (event_name = '', data = {}, options = {})
66
72
  raise ApiAiRuby::ClientError.new('Credentials missing') if !credentials?
67
73
  options[:event] = {
68
- name: eventName,
74
+ name: event_name,
69
75
  data: data
70
76
  }
71
77
  ApiAiRuby::EventRequest.new(self, options).perform
72
78
  end
73
79
 
80
+ # @deprecated
81
+ # @param file_stream [File]
82
+ # @param options [Object]
83
+ # @return [Array, Hash]
74
84
  def voice_request(file_stream, options = {})
75
85
  raise ApiAiRuby::ClientError.new('Credentials missing') if !credentials?
76
86
  options[:file] = file_stream
77
87
  ApiAiRuby::VoiceRequest.new(self, options).perform
78
88
  end
79
89
 
80
- def user_entities_request
90
+ # @return [ApiAiRuby::UserEntitiesRequest]
91
+ def create_user_entities_request
81
92
  ApiAiRuby::UserEntitiesRequest.new(self);
82
93
  end
83
94
 
95
+ # @return [ApiAiRuby::ContextsRequest]
96
+ def create_contexts_request
97
+ ApiAiRuby::ContextsRequest.new(self)
98
+ end
99
+
84
100
  end
85
101
  end
@@ -1,6 +1,6 @@
1
1
  module ApiAiRuby
2
2
  class Constants
3
- VERSION = '1.2.3'
3
+ VERSION = '1.3.0'
4
4
  DEFAULT_BASE_URL = 'https://api.api.ai/v1/'
5
5
  DEFAULT_API_VERSION = '20150910'
6
6
  DEFAULT_CLIENT_LANG = 'en'
@@ -0,0 +1,54 @@
1
+ module ApiAiRuby
2
+ class ContextsRequest < ApiAiRuby::RequestQuery
3
+
4
+ def initialize(client, options = {})
5
+ super client, options
6
+ @headers['Content-Type'] = 'application/json; charset=UTF-8'
7
+ @crud_base_uri = client.api_base_url + 'contexts'
8
+ @uri = @crud_base_uri
9
+ end
10
+
11
+ # @param name [String]
12
+ # @return [ApiAiRuby::Context]
13
+ def retrieve(name)
14
+ raise ApiAiRuby::ClientError.new('context name required') if !name
15
+ @request_method = :get
16
+ @options = nil
17
+ @uri = @crud_base_uri + '/' + name + '?sessionId=' + self.client.api_session_id
18
+ self.perform
19
+ end
20
+
21
+ # @return [Array<ApiAiRuby::Context>]
22
+ def list
23
+ @request_method = :get
24
+ @uri = @crud_base_uri + '?sessionId=' + self.client.api_session_id
25
+ @options = nil
26
+ self.perform
27
+ end
28
+
29
+ # @param context [Array<ApiAiRuby::Context>, ApiAiRuby::Context, String]
30
+ def create(contexts)
31
+
32
+ @request_method = :post
33
+ @uri = @crud_base_uri + '?sessionId=' + self.client.api_session_id
34
+
35
+ @options = nil
36
+ @options = contexts if contexts.is_a? Array
37
+ @options = [contexts] if contexts.is_a? ApiAiRuby::Context
38
+ @options = [{:name => contexts}] if contexts.is_a? String
39
+ raise ApiAiRuby::ClientError.new(
40
+ 'You should pass instance of ApiAiRuby::Context, Array<ApiAiRuby::Context> or String to ContextsRequest::create ') if @options == nil
41
+ self.perform
42
+ end
43
+
44
+ def delete(name = nil)
45
+ @request_method = :delete
46
+ @options = nil
47
+ @uri = @crud_base_uri + '?sessionId=' + self.client.api_session_id
48
+ @uri = @crud_base_uri + '/' + name + '?sessionId=' + self.client.api_session_id if name != nil
49
+ # @options[:sessionId] = self.client.api_session_id
50
+ self.perform
51
+ end
52
+
53
+ end
54
+ end
@@ -8,7 +8,8 @@ module ApiAiRuby
8
8
  @uri = @crud_base_uri
9
9
  end
10
10
 
11
-
11
+ # @param argument [Array<ApiAiRuby::Entity, Hash>, ApiAiRuby::Entity, Hash]
12
+ # @return [Hash]
12
13
  def create(argument)
13
14
  if !(argument && (argument.is_a?(Array) || argument.is_a?(Hash) || argument.is_a?(ApiAiRuby::Entity)))
14
15
  raise ApiAiRuby::ClientError.new('Argument should be array of Entities or single Entity object')
@@ -0,0 +1,23 @@
1
+ module ApiAiRuby
2
+ class Context
3
+ attr_accessor :name, :lifespan, :parameters
4
+
5
+ # @param name [String]
6
+ # @param lifespan [Numeric]
7
+ # @param parameters [Hash]
8
+ def initialize (name, lifespan = 5, parameters = {})
9
+ @name = name
10
+ @lifespan = lifespan
11
+ @parameters = parameters
12
+ end
13
+
14
+ def to_json(*args)
15
+ {
16
+ :name => name,
17
+ :lifespan => lifespan,
18
+ :parameters => parameters
19
+ }.to_json(*args)
20
+ end
21
+
22
+ end
23
+ end
@@ -3,7 +3,7 @@ module ApiAiRuby
3
3
  attr_accessor :name, :entries
4
4
 
5
5
  # @param name [String]
6
- # @param entries [Array]
6
+ # @param entries [Array<ApiAiRuby::Entry, Hash>]
7
7
  def initialize (name, entries)
8
8
  @name = name
9
9
  @entries = entries
@@ -16,7 +16,7 @@ module ApiAiRuby
16
16
  }.to_json(*args)
17
17
  end
18
18
 
19
- def addEntry(value, synonyms)
19
+ def add_entry(value, synonyms)
20
20
  @entries.push new ApiAiRuby::Entry(value, synonyms)
21
21
  end
22
22
 
@@ -21,9 +21,9 @@ module ApiAiRuby
21
21
  @timeout_options = client.timeout_options || options[:timeout_options]
22
22
  end
23
23
 
24
- # @return [Array, Hash]
24
+ # @return [Hash]
25
25
  def perform
26
- if @options.has_key?(:voiceData)
26
+ if @options && @options.is_a?(Hash) && @options.has_key?(:voiceData)
27
27
  options_key = :form
28
28
  else
29
29
  options_key = (@request_method === :get) ? :params : :json
@@ -31,17 +31,17 @@ module ApiAiRuby
31
31
 
32
32
  request = HTTP.with(@headers)
33
33
  request = request.timeout(*@timeout_options) if @timeout_options
34
- response = request.public_send(@request_method, @uri.to_s, options_key => @options)
34
+ response = request.public_send(@request_method, @uri.to_s, options_key => @options) if @options
35
+ response = request.public_send(@request_method, @uri.to_s) if @options == nil
35
36
  response_body = symbolize_keys!(response.parse)
36
37
  fail_or_return_response_body(response.code, response_body)
37
38
  end
38
39
 
39
40
  private
40
41
 
41
-
42
42
  def fail_or_return_response_body(code, body)
43
43
  error = false
44
- if code != 200 || (body[:status] && body[:status][:code] && body[:status][:code] != 200)
44
+ if code != 200 || (body.is_a?(Hash) && body[:status] && body[:status][:code] && body[:status][:code] != 200)
45
45
  error = ApiAiRuby::RequestError.new body[:status][:errorDetails], body[:status][:code]
46
46
  end
47
47
  fail(error) if error
@@ -1,10 +1,12 @@
1
1
  require 'helper'
2
2
 
3
3
  describe 'api' do
4
- before(:all) { @client = ApiAiRuby::Client.new(
4
+ before(:all) {
5
+ @client = ApiAiRuby::Client.new(
5
6
  :client_access_token => '3485a96fb27744db83e78b8c4bc9e7b7',
6
7
  :api_session_id => 'testsession'
7
- )}
8
+ )
9
+ }
8
10
 
9
11
  it 'should return response' do
10
12
  response = @client.text_request 'Hello'
@@ -40,9 +42,9 @@ describe 'api' do
40
42
  end
41
43
 
42
44
  it 'should send voiceData to API' do
43
- # expect(@client.voice_request(File.new(fixture_path + '/hello.wav'))[:result][:resolvedQuery]).to eq 'hello'
45
+ expect(@client.voice_request(File.new(fixture_path + '/hello.wav'))[:result][:resolvedQuery]).to eq 'hello'
44
46
  # asr was disabled for non-premium users
45
- expect {@client.voice_request(File.new(fixture_path + '/hello.wav'))}.to raise_error(ApiAiRuby::RequestError)
47
+ # expect {@client.voice_request(File.new(fixture_path + '/hello.wav'))}.to raise_error(ApiAiRuby::RequestError)
46
48
  end
47
49
 
48
50
  it 'should correctly set contexts with parameters' do
@@ -70,7 +72,7 @@ describe 'api' do
70
72
 
71
73
  describe 'userEntities endpoint' do
72
74
 
73
- before(:all) { @uer = @client.user_entities_request}
75
+ before(:all) { @uer = @client.create_user_entities_request}
74
76
 
75
77
  let(:entity1) {
76
78
  ApiAiRuby::Entity.new 'dwarfs', [ApiAiRuby::Entry.new('giur', %w(Giur Amaldur))]
@@ -115,4 +117,40 @@ describe 'api' do
115
117
  # end
116
118
 
117
119
  end
120
+
121
+ describe 'contexts endpoint' do
122
+
123
+ before(:all) { @context_request = @client.create_contexts_request}
124
+
125
+ it 'should retrieve contexts' do
126
+ @client.text_request 'Hello', :contexts => %w(firstContext secondContext), :resetContexts => true
127
+ response = @context_request.list
128
+ expect(response[0][:name]).to eq 'secondcontext'
129
+ expect(response[1][:name]).to eq 'firstcontext'
130
+ end
131
+
132
+ it 'should retrieve contexts by name' do
133
+ response = @context_request.retrieve 'secondContext'
134
+ expect(response[:name]).to eq 'secondcontext'
135
+ end
136
+
137
+ it 'should delete all curret contexts' do
138
+ @context_request.delete
139
+ expect(@context_request.list.length).to be 0
140
+ end
141
+
142
+ it 'should create context via crud' do
143
+ @context_request.create('test')
144
+ @context_request.create(ApiAiRuby::Context.new('test1'))
145
+ @context_request.create([ApiAiRuby::Context.new('test2'), ApiAiRuby::Context.new('test3',)])
146
+ expect(@context_request.list.length).to be 4
147
+ end
148
+
149
+ it 'should delete context by name' do
150
+ @context_request.delete('test3')
151
+ expect(@context_request.list.length).to be 3
152
+ end
153
+
154
+ end
155
+
118
156
  end
@@ -0,0 +1 @@
1
+ # @todo: write unit-tests
@@ -0,0 +1 @@
1
+ # @todo: write unit-tests
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-ai-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - api.ai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-05 00:00:00.000000000 Z
11
+ date: 2017-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.7'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: http
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: 0.9.4
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.9.4
55
55
  description: Plugin makes it easy to integrate your Ruby application with https://api.ai
@@ -60,7 +60,7 @@ executables: []
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
- - ".gitignore"
63
+ - .gitignore
64
64
  - Gemfile
65
65
  - LICENSE
66
66
  - README.md
@@ -70,7 +70,9 @@ files:
70
70
  - lib/api-ai-ruby/client.rb
71
71
  - lib/api-ai-ruby/client_error.rb
72
72
  - lib/api-ai-ruby/constants.rb
73
+ - lib/api-ai-ruby/crud/contexts_request.rb
73
74
  - lib/api-ai-ruby/crud/user_entity_request.rb
75
+ - lib/api-ai-ruby/models/context.rb
74
76
  - lib/api-ai-ruby/models/entity.rb
75
77
  - lib/api-ai-ruby/models/entry.rb
76
78
  - lib/api-ai-ruby/request/event_request.rb
@@ -80,6 +82,7 @@ files:
80
82
  - lib/api-ai-ruby/request_error.rb
81
83
  - spec/api-ai-ruby/api_spec.rb
82
84
  - spec/api-ai-ruby/client_spec.rb
85
+ - spec/api-ai-ruby/context_request_spec.rb
83
86
  - spec/api-ai-ruby/error_spec.rb
84
87
  - spec/api-ai-ruby/text_request_spec.rb
85
88
  - spec/api-ai-ruby/user_entities_request_spec.rb
@@ -96,26 +99,28 @@ require_paths:
96
99
  - lib
97
100
  required_ruby_version: !ruby/object:Gem::Requirement
98
101
  requirements:
99
- - - "~>"
102
+ - - ~>
100
103
  - !ruby/object:Gem::Version
101
104
  version: '2.0'
102
105
  required_rubygems_version: !ruby/object:Gem::Requirement
103
106
  requirements:
104
- - - ">="
107
+ - - '>='
105
108
  - !ruby/object:Gem::Version
106
109
  version: '0'
107
110
  requirements: []
108
111
  rubyforge_project:
109
- rubygems_version: 2.5.1
112
+ rubygems_version: 2.0.14.1
110
113
  signing_key:
111
114
  specification_version: 4
112
115
  summary: ruby SDK for https://api.ai
113
116
  test_files:
114
117
  - spec/api-ai-ruby/api_spec.rb
115
118
  - spec/api-ai-ruby/client_spec.rb
119
+ - spec/api-ai-ruby/context_request_spec.rb
116
120
  - spec/api-ai-ruby/error_spec.rb
117
121
  - spec/api-ai-ruby/text_request_spec.rb
118
122
  - spec/api-ai-ruby/user_entities_request_spec.rb
119
123
  - spec/api-ai-ruby/voice_request_spec.rb
120
124
  - spec/fixtures/hello.wav
121
125
  - spec/helper.rb
126
+ has_rdoc: