voxable-api-ai-ruby 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7665b47e804237840f42020059abc0d330d79d4d
4
+ data.tar.gz: 70d3f09ecfa9f4b1fd9bcd072a67ce8c70277f5b
5
+ SHA512:
6
+ metadata.gz: e8ba7c983d06db394a4d5064e888b300f81efc7a038702e37e79a7ac06184132cc0af7e47ee3c8d92b1b7b56fbec59059cca81418605ab0a6d3eac50e598663d
7
+ data.tar.gz: a96b63eb857a12f0654191cbfb8d23cd793a45c972852e1f95287da3e8b01422ceb69d00c1b9d12aa0295e4d0d99d1ec5ca114c2bcebff80037d5ca903cb2a0f
data/.gitignore ADDED
@@ -0,0 +1,40 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Specific to IDEA
18
+ api-ai-ruby.iml
19
+ .idea
20
+
21
+
22
+ ## Documentation cache and generated files:
23
+ /.yardoc/
24
+ /_yardoc/
25
+ /doc/
26
+ /rdoc/
27
+
28
+ ## Environment normalisation:
29
+ /.bundle/
30
+ /lib/bundler/man/
31
+ Gemfile.lock
32
+
33
+ # for a library or gem, you might want to ignore these files since the code is
34
+ # intended to run in multiple environments; otherwise, check them in:
35
+ # Gemfile.lock
36
+ # .ruby-version
37
+ # .ruby-gemset
38
+
39
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
40
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'https://rubygems.org'
2
+
3
+ #gem 'jruby-openssl', platforms: :jruby
4
+ gem 'rake'
5
+ gem 'yard'
6
+
7
+ group :development do
8
+
9
+ end
10
+
11
+ group :test do
12
+ gem 'coveralls'
13
+ gem 'rspec', '>= 2.14'
14
+ gem 'simplecov', '>= 0.9'
15
+ gem 'webmock'
16
+ end
17
+
18
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # The API.AI ruby gem
2
+
3
+ A Ruby SDK to the https://api.ai natural language processing service.
4
+
5
+ ## Installation
6
+ gem install api-ai-ruby
7
+
8
+ ## Basic Usage
9
+
10
+ Just pass correct credentials to **ApiAiRuby::Client** constructor
11
+
12
+ ```ruby
13
+ client = ApiAiRuby::Client.new(
14
+ :client_access_token => 'YOUR_CLIENT_ACCESS_TOKEN',
15
+ )
16
+ ```
17
+ After that you can send text requests to the https://api.ai with command
18
+
19
+ ```ruby
20
+ response = client.text_request 'hello!'
21
+ ```
22
+
23
+ And voice requests with file stream
24
+
25
+ ```ruby
26
+ file = File.new 'hello.wav'
27
+ response = client.voice_request(file)
28
+ ```
29
+
30
+ Example answer:
31
+ ```
32
+ {
33
+ :id => "6daf5ab7-276c-43ad-a32d-bf6831918492",
34
+ :timestamp => "2015-12-22T08:42:15.785Z",
35
+ :result => {
36
+ :source => "agent",
37
+ :resolvedQuery => "Hello",
38
+ :speech => "Hi! How are you?",
39
+ :action => "greeting",
40
+ :parameters => {},
41
+ :contexts => [],
42
+ :metadata => {
43
+ :intentId => "a5d685ab-1f19-46b0-9478-69f794553668",
44
+ :intentName => "hello"
45
+ }
46
+ },
47
+ :status => {
48
+ :code => 200,
49
+ :errorType => "success"
50
+ }
51
+ }
52
+ ```
53
+
54
+ **voice_request** and **text_request** methods returns symbolized https://api.ai response. Structure of response can be found at https://docs.api.ai/docs/query#response.
55
+
56
+ ## Advanced usage
57
+
58
+ During client instantiating you can additionally set parameters like **api url**, request **language** and **version** (more info at https://docs.api.ai/docs/versioning, https://docs.api.ai/docs/languages)
59
+
60
+ ```ruby
61
+ ApiAiRuby::Client.new(
62
+ client_access_token: 'YOUR_ACCESS_TOKEN',
63
+ api_lang: 'FR',
64
+ api_base_url: 'http://example.com/v1/',
65
+ api_version: 'YYYYMMDD'
66
+ )
67
+ ```
68
+
69
+ And you also can send additional data to server during request, use second parameter of **text_request** and **voice_request** methods to do that
70
+
71
+ ```ruby
72
+ response = client.text_request 'Hello', :contexts => ['firstContext'], :resetContexts => true
73
+ response = client.voice_request file, :timezone => "America/New_York"
74
+ ```
75
+ More information about possible parameters can be found at https://docs.api.ai/docs/query page
76
+
77
+ #Error handling
78
+ **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**)
79
+
80
+
81
+
82
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,12 @@
1
+ require 'api-ai-ruby/constants'
2
+ require 'api-ai-ruby/client'
3
+ require 'api-ai-ruby/request_error'
4
+ require 'api-ai-ruby/client_error'
5
+ require 'api-ai-ruby/request'
6
+ require 'api-ai-ruby/text_request'
7
+ require 'api-ai-ruby/voice_request'
8
+ require 'api-ai-ruby/update_entities_request'
9
+ require 'api-ai-ruby/update_intent_request'
10
+ require 'api-ai-ruby/get_intent_request'
11
+ require 'api-ai-ruby/get_intents_request'
12
+
@@ -0,0 +1,89 @@
1
+ module ApiAiRuby
2
+ class Client
3
+ attr_accessor :client_access_token, :developer_access_token
4
+ attr_writer :user_agent, :api_version, :api_lang, :api_base_url
5
+
6
+ # Initializes a new Client object
7
+ #
8
+ # @param options [Hash]
9
+ # @return [Twitter::Client]
10
+ def initialize(options = {})
11
+ options.each do |key, value|
12
+ instance_variable_set("@#{key}", value)
13
+ end
14
+ yield(self) if block_given?
15
+ end
16
+
17
+ # @return [String]
18
+ def user_agent
19
+ @user_agent ||= "ApiAiRubyGem/#{ApiAiRuby::Constants::VERSION}"
20
+ end
21
+
22
+ def api_base_url
23
+ @api_base_url ||= ApiAiRuby::Constants::DEFAULT_BASE_URL
24
+ end
25
+
26
+ def api_lang
27
+ @api_lang ||= ApiAiRuby::Constants::DEFAULT_CLIENT_LANG
28
+ end
29
+
30
+ def api_version
31
+ @api_version ||= ApiAiRuby::Constants::DEFAULT_API_VERSION
32
+ end
33
+
34
+ # @return [Hash]
35
+ def credentials
36
+ {
37
+ client_access_token: client_access_token,
38
+ developer_access_token: developer_access_token
39
+ }
40
+ end
41
+
42
+ # @return [Boolean] true if the client credentials are present
43
+ def client_credentials?
44
+ credentials[:client_access_token]
45
+ end
46
+
47
+ # @return [Boolean] true if the developer credentials are present
48
+ def developer_credentials?
49
+ credentials[:developer_access_token]
50
+ end
51
+
52
+ def text_request (query = '', options = {})
53
+ raise ApiAiRuby::ClientError.new('Credentials missing') if !client_credentials?
54
+ options[:params] ||= {}
55
+ options[:params] = options[:params].merge({ query: query })
56
+ ApiAiRuby::TextRequest.new(self, options).perform
57
+ end
58
+
59
+ def voice_request(file_stream, options = {})
60
+ raise ApiAiRuby::ClientError.new('Credentials missing') if !client_credentials?
61
+ options[:form] ||= {}
62
+ options[:form] = options[:form].merge({ file: file_stream })
63
+ ApiAiRuby::VoiceRequest.new(self, options).perform
64
+ end
65
+
66
+ def update_entities_request(entities, options = {})
67
+ raise ApiAiRuby::ClientError.new('Credentials missing') if !developer_credentials?
68
+ options[:json] = entities
69
+ ApiAiRuby::UpdateEntitiesRequest.new(self, options).perform
70
+ end
71
+
72
+ def update_intent_request(intent, options = {})
73
+ raise ApiAiRuby::ClientError.new('Credentials missing') if !developer_credentials?
74
+ options[:json] = intent
75
+ ApiAiRuby::UpdateIntentRequest.new(self, options).perform
76
+ end
77
+
78
+ def get_intent_request(iid, options = {})
79
+ raise ApiAiRuby::ClientError.new('Credentials missing') if !developer_credentials?
80
+ options[:iid] = iid
81
+ ApiAiRuby::GetIntentRequest.new(self, options).perform
82
+ end
83
+
84
+ def get_intents_request(options = {})
85
+ raise ApiAiRuby::ClientError.new('Credentials missing') if !developer_credentials?
86
+ ApiAiRuby::GetIntentRequest.new(self, options).perform
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,5 @@
1
+ module ApiAiRuby
2
+ class ClientError < StandardError
3
+
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ module ApiAiRuby
2
+ class Constants
3
+ VERSION = '1.1.0'
4
+ DEFAULT_BASE_URL = 'https://api.api.ai/v1/'
5
+ DEFAULT_API_VERSION = '20150204'
6
+ DEFAULT_CLIENT_LANG = 'en'
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ require 'json'
2
+
3
+ module ApiAiRuby
4
+ class GetIntentRequest < ApiAiRuby::Request
5
+ def initialize (client, options={})
6
+ @path = "intents/#{ options.delete(:iid) }"
7
+
8
+ super client, options
9
+ @request_method = :get
10
+
11
+ # TODO: There should be a method for generating the proper headers.
12
+ @headers = {
13
+ 'Authorization': 'Bearer ' + client.developer_access_token
14
+ }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'json'
2
+
3
+ module ApiAiRuby
4
+ class GetIntentsRequest < ApiAiRuby::Request
5
+ def initialize (client, options={})
6
+ @path = "intents"
7
+
8
+ super client, options
9
+ @request_method = :get
10
+
11
+ # TODO: There should be a method for generating the proper headers.
12
+ @headers = {
13
+ 'Authorization': 'Bearer ' + client.developer_access_token
14
+ }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,61 @@
1
+ require 'http'
2
+ require 'http/form_data'
3
+ require 'uri'
4
+
5
+ module ApiAiRuby
6
+ class Request
7
+
8
+ attr_accessor :client, :headers, :options, :request_method, :uri
9
+
10
+ # @param client [ApiAiRuby::Client]
11
+ # @param path [String] the path to the proper API endpoint
12
+ # @param options [Hash]
13
+ # @return [ApiAiRuby::TextRequest]
14
+
15
+ def initialize(client, options = {})
16
+ @client = client
17
+ @uri = client.api_base_url + @path + '?v=' + client.api_version
18
+ @request_method = :post
19
+ options[:params] ||= {}
20
+ options[:params] = options[:params].merge({ lang: client.api_lang })
21
+ @options = options
22
+ @headers = {
23
+ 'Authorization': 'Bearer ' + client.client_access_token
24
+ }
25
+ end
26
+
27
+ # @return [Array, Hash]
28
+ def perform
29
+ response = HTTP.with(@headers).public_send(@request_method, @uri.to_s, @options)
30
+
31
+ response_body = symbolize_keys!(response.parse)
32
+ response_headers = response.headers
33
+ fail_or_return_response_body(response.code, response_body)
34
+ end
35
+
36
+ private
37
+
38
+
39
+ def fail_or_return_response_body(code, body)
40
+ error = false
41
+ if code != 200 || ( !body.kind_of?(Array) && body[:status] && body[:status][:code] != 200 )
42
+ error = ApiAiRuby::RequestError.new body[:status][:errorDetails], body[:status][:code]
43
+ end
44
+ fail(error) if error
45
+ body
46
+ end
47
+
48
+ def symbolize_keys!(object)
49
+ if object.is_a?(Array)
50
+ object.each_with_index do |val, index|
51
+ object[index] = symbolize_keys!(val)
52
+ end
53
+ elsif object.is_a?(Hash)
54
+ object.keys.each do |key|
55
+ object[key.to_sym] = symbolize_keys!(object.delete(key))
56
+ end
57
+ end
58
+ object
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,17 @@
1
+ module ApiAiRuby
2
+ class RequestError < StandardError
3
+ #return [Integer]
4
+ attr_reader :code
5
+
6
+ # Initializes a new Error object
7
+ #
8
+ # @param message [Exception, String]
9
+ # @param code [Integer]
10
+ # @return [ApiAiRuby::RequestError]
11
+ def initialize(message = '', code = nil)
12
+ super(message)
13
+ @code = code
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ module ApiAiRuby
2
+ class TextRequest < ApiAiRuby::Request
3
+ def initialize (client, options={})
4
+ @path = 'query'
5
+
6
+ super client, options
7
+ @request_method = :get
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ require 'json'
2
+
3
+ module ApiAiRuby
4
+ class UpdateEntitiesRequest < ApiAiRuby::Request
5
+ def initialize (client, options={})
6
+ @path = 'entities'
7
+
8
+ super client, options
9
+ @request_method = :put
10
+ # TODO: There should be a method for generating the proper headers.
11
+ @headers = {
12
+ 'Authorization': 'Bearer ' + client.developer_access_token
13
+ }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'json'
2
+
3
+ module ApiAiRuby
4
+ class UpdateIntentRequest < ApiAiRuby::Request
5
+ def initialize (client, options={})
6
+ @path = "intents/#{ options[:json][:id] }"
7
+
8
+ super client, options
9
+ @request_method = :put
10
+ # TODO: There should be a method for generating the proper headers.
11
+ @headers = {
12
+ 'Authorization': 'Bearer ' + client.developer_access_token
13
+ }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ require 'json'
2
+
3
+ module ApiAiRuby
4
+ class VoiceRequest < ApiAiRuby::Request
5
+ # @param client [ApiAiRuby::Client]
6
+ # @param options [Hash]
7
+ # @return [ApiAiRuby::VoiceRequest]
8
+ def initialize(client, options = {})
9
+ @path = 'query'
10
+
11
+ super client, options
12
+
13
+ file = options.delete(:file)
14
+ options = {
15
+ :request => options.to_json,
16
+ :voiceData => HTTP::FormData::File.new(file, filename: File.basename(file))
17
+ }
18
+ @options = options
19
+
20
+ self
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+
3
+ describe 'api' do
4
+ let (:client) { ApiAiRuby::Client.new(:client_access_token => '3485a96fb27744db83e78b8c4bc9e7b7')}
5
+
6
+ it 'should return response' do
7
+ response = client.text_request 'Hello'
8
+ expect(response[:result][:resolvedQuery]).to eq 'Hello'
9
+ expect(response[:result][:action]).to eq 'greeting'
10
+ end
11
+
12
+ it 'should use input contexts' do
13
+ response = client.text_request 'Hello', :resetContexts => true
14
+ expect(response[:result][:action]).to eq 'greeting'
15
+
16
+ response = client.text_request 'Hello', :contexts => ['firstContext'], :resetContexts => true
17
+ expect(response[:result][:action]).to eq 'firstGreeting'
18
+
19
+ response = client.text_request 'Hello', :contexts => ['secondContext'], :resetContexts => true
20
+ expect(response[:result][:action]).to eq 'secondGreeting'
21
+ end
22
+
23
+ it 'should return output contexts' do
24
+ response = client.text_request 'weather', :resetContexts => true
25
+ expect(response[:result][:action]).to eq 'showWeather'
26
+ expect(response[:result][:contexts]).not_to be_nil
27
+ expect(response[:result][:contexts].any? {|context| context[:name] == 'weather'}).to be true
28
+ end
29
+
30
+ it 'should response with error on wrong credentials' do
31
+ client = ApiAiRuby::Client.new(client_access_token: 'CS')
32
+ expect {client.text_request}.to raise_error(ApiAiRuby::RequestError)
33
+ end
34
+
35
+ it 'should send voiceData to API' do
36
+ expect(client.voice_request(File.new(fixture_path + '/hello.wav'))[:result][:resolvedQuery]).to eq 'hello'
37
+ end
38
+ end
@@ -0,0 +1,70 @@
1
+ require 'helper'
2
+
3
+ describe ApiAiRuby::Client do
4
+ describe '#user_agent' do
5
+ it 'defaults ApiAiRubyRubyGem/version' do
6
+ expect(subject.user_agent).to eq("ApiAiRubyGem/#{ApiAiRuby::Constants::VERSION}")
7
+ end
8
+ end
9
+
10
+ describe '#user_agent=' do
11
+ it 'overwrites the User-Agent string' do
12
+ subject.user_agent = 'MyApiAiRubyClient/1.0.0'
13
+ expect(subject.user_agent).to eq('MyApiAiRubyClient/1.0.0')
14
+ end
15
+ end
16
+
17
+ describe '#client_credentials?' do
18
+ it 'returns true if all client credentials are present' do
19
+ client = ApiAiRuby::Client.new(client_access_token: 'CS')
20
+ expect(client.client_credentials?).to be_truthy
21
+ end
22
+
23
+ it 'returns false if any client credentials are missing' do
24
+ client = ApiAiRuby::Client.new()
25
+ expect(client.client_credentials?).to be_falsey
26
+ end
27
+
28
+ it 'raises error on request without client credentials' do
29
+ expect {subject.text_request '123'}.to raise_error(ApiAiRuby::ClientError)
30
+ expect {subject.voice_request '123'}.to raise_error(ApiAiRuby::ClientError)
31
+ end
32
+ end
33
+
34
+ describe '#developer_credentials?' do
35
+ it 'returns true if all developer credentials are present' do
36
+ client = ApiAiRuby::Client.new(developer_access_token: 'CS')
37
+ expect(client.developer_credentials?).to be_truthy
38
+ end
39
+
40
+ it 'returns false if any developer credentials are missing' do
41
+ client = ApiAiRuby::Client.new()
42
+ expect(client.developer_credentials?).to be_falsey
43
+ end
44
+
45
+ it 'raises error on request without developer credentials' do
46
+ expect {subject.text_request '123'}.to raise_error(ApiAiRuby::ClientError)
47
+ expect {subject.voice_request '123'}.to raise_error(ApiAiRuby::ClientError)
48
+ end
49
+ end
50
+
51
+ describe '#properties' do
52
+
53
+ it 'has correct default properties' do
54
+ client = ApiAiRuby::Client.new(client_access_token: 'CS')
55
+ expect(client.api_base_url).to eq ApiAiRuby::Constants::DEFAULT_BASE_URL
56
+ expect(client.api_version).to eq ApiAiRuby::Constants::DEFAULT_API_VERSION
57
+ expect(client.api_lang).to eq ApiAiRuby::Constants::DEFAULT_CLIENT_LANG
58
+ end
59
+
60
+ it 'correctly creates client with given properties' do
61
+ client = ApiAiRuby::Client.new(client_access_token: 'CS', api_lang: 'RU', api_base_url: 'http://localhost', api_version: '1234')
62
+ expect(client.api_base_url).to eq 'http://localhost'
63
+ expect(client.api_version).to eq '1234'
64
+ expect(client.api_lang).to eq 'RU'
65
+ end
66
+
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ describe ApiAiRuby::RequestError do
4
+ describe '#code' do
5
+ it 'returns the error code' do
6
+ error = ApiAiRuby::RequestError.new('execution expired', 123)
7
+ expect(error.code).to eq(123)
8
+ end
9
+ end
10
+
11
+ describe '#message' do
12
+ it 'returns the error message' do
13
+ error = ApiAiRuby::RequestError.new('execution expired')
14
+ expect(error.message).to eq('execution expired')
15
+ end
16
+ end
17
+ end
18
+
19
+ describe ApiAiRuby::ClientError do
20
+
21
+ end
File without changes
File without changes
Binary file
data/spec/helper.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter]
5
+
6
+ SimpleCov.start do
7
+ add_filter '/spec/'
8
+ add_filter '/vendor/'
9
+ minimum_coverage(99.57)
10
+ end
11
+
12
+
13
+ require 'api-ai-ruby'
14
+ require 'rspec'
15
+ #require 'webmock/rspec'
16
+
17
+ #WebMock.disable_net_connect!(allow: 'coveralls.io')
18
+
19
+ RSpec.configure do |config|
20
+ config.expect_with :rspec do |c|
21
+ c.syntax = :expect
22
+ end
23
+ end
24
+
25
+
26
+ def fixture_path
27
+ File.expand_path('../fixtures', __FILE__)
28
+ end
29
+
30
+ def fixture(file)
31
+ File.new(fixture_path + '/' + file)
32
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api-ai-ruby/constants'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'voxable-api-ai-ruby'
8
+ spec.version = ApiAiRuby::Constants::VERSION
9
+ spec.authors = ['api.ai']
10
+ spec.email = ['shingarev@api.ai']
11
+ spec.summary = %q{ruby SDK for https://api.ai }
12
+ spec.description = %q{Voxable's fork of the official API.AI Ruby gem. This probably is not the gem you're looking for. :)}
13
+ spec.homepage = 'https://api.ai'
14
+ spec.license = 'Apache 2.0 License'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_dependency 'http', '~> 0.9.4'
24
+ spec.add_dependency 'http-form_data', '~> 1.0'
25
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: voxable-api-ai-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - api.ai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: http
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: http-form_data
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: Voxable's fork of the official API.AI Ruby gem. This probably is not
70
+ the gem you're looking for. :)
71
+ email:
72
+ - shingarev@api.ai
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - lib/api-ai-ruby.rb
83
+ - lib/api-ai-ruby/client.rb
84
+ - lib/api-ai-ruby/client_error.rb
85
+ - lib/api-ai-ruby/constants.rb
86
+ - lib/api-ai-ruby/get_intent_request.rb
87
+ - lib/api-ai-ruby/get_intents_request.rb
88
+ - lib/api-ai-ruby/request.rb
89
+ - lib/api-ai-ruby/request_error.rb
90
+ - lib/api-ai-ruby/text_request.rb
91
+ - lib/api-ai-ruby/update_entities_request.rb
92
+ - lib/api-ai-ruby/update_intent_request.rb
93
+ - lib/api-ai-ruby/voice_request.rb
94
+ - spec/api-ai-ruby/api_spec.rb
95
+ - spec/api-ai-ruby/client_spec.rb
96
+ - spec/api-ai-ruby/error_spec.rb
97
+ - spec/api-ai-ruby/text_request_spec.rb
98
+ - spec/api-ai-ruby/voice_request_spec.rb
99
+ - spec/fixtures/hello.wav
100
+ - spec/helper.rb
101
+ - voxable-api-ai-ruby.gemspec
102
+ homepage: https://api.ai
103
+ licenses:
104
+ - Apache 2.0 License
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.5.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: ruby SDK for https://api.ai
126
+ test_files:
127
+ - spec/api-ai-ruby/api_spec.rb
128
+ - spec/api-ai-ruby/client_spec.rb
129
+ - spec/api-ai-ruby/error_spec.rb
130
+ - spec/api-ai-ruby/text_request_spec.rb
131
+ - spec/api-ai-ruby/voice_request_spec.rb
132
+ - spec/fixtures/hello.wav
133
+ - spec/helper.rb