api-ai-ruby 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +40 -0
- data/Gemfile +18 -0
- data/LICENSE +202 -0
- data/README.md +83 -0
- data/Rakefile +1 -0
- data/api-ai-ruby.gemspec +26 -0
- data/lib/api-ai-ruby.rb +8 -0
- data/lib/api-ai-ruby/client.rb +60 -0
- data/lib/api-ai-ruby/client_error.rb +5 -0
- data/lib/api-ai-ruby/constants.rb +8 -0
- data/lib/api-ai-ruby/request_error.rb +17 -0
- data/lib/api-ai-ruby/request_query.rb +60 -0
- data/lib/api-ai-ruby/text_request.rb +8 -0
- data/lib/api-ai-ruby/voice_request.rb +23 -0
- data/spec/api-ai-ruby/api_spec.rb +38 -0
- data/spec/api-ai-ruby/client_spec.rb +53 -0
- data/spec/api-ai-ruby/error_spec.rb +21 -0
- data/spec/api-ai-ruby/text_request_spec.rb +0 -0
- data/spec/api-ai-ruby/voice_request_spec.rb +0 -0
- data/spec/fixtures/hello.wav +0 -0
- data/spec/helper.rb +32 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b0c26a1fbd164ecd6dd0e2dc0f3b4f6b3786ff7c
|
4
|
+
data.tar.gz: bfe06cb9f86d69f0157a3779b6d464acd8cc5641
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d7f1aa1be77d99141a6925257cedb478c791cf5fd894863cf57dd9357df015c3217c640054ac179a11f5d4cd16f71946cbc2791af4be6a56c96bec1e033bba90
|
7
|
+
data.tar.gz: a7e6803956b3ad09e637cf428f6c429e82cb148f38f18567e664088380a697c0568fe1229585d1f114d3eac55eb20f7bbbf1b671d2b2484ec812e0ab8fe45f2e
|
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,83 @@
|
|
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_ACCESS_TOKEN',
|
15
|
+
:subscription_key => 'YOUR_SUBSCRIPTION_KEY'
|
16
|
+
)
|
17
|
+
```
|
18
|
+
After that you can send text requests to the https://api.ai with command
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
response = client.text_request 'hello!'
|
22
|
+
```
|
23
|
+
|
24
|
+
And voice requests with file stream
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
file = File.new 'hello.wav'
|
28
|
+
response = client.voice_request(file)
|
29
|
+
```
|
30
|
+
|
31
|
+
Example answer:
|
32
|
+
```
|
33
|
+
{
|
34
|
+
:id => "6daf5ab7-276c-43ad-a32d-bf6831918492",
|
35
|
+
:timestamp => "2015-12-22T08:42:15.785Z",
|
36
|
+
:result => {
|
37
|
+
:source => "agent",
|
38
|
+
:resolvedQuery => "Hello",
|
39
|
+
:speech => "Hi! How are you?",
|
40
|
+
:action => "greeting",
|
41
|
+
:parameters => {},
|
42
|
+
:contexts => [],
|
43
|
+
:metadata => {
|
44
|
+
:intentId => "a5d685ab-1f19-46b0-9478-69f794553668",
|
45
|
+
:intentName => "hello"
|
46
|
+
}
|
47
|
+
},
|
48
|
+
:status => {
|
49
|
+
:code => 200,
|
50
|
+
:errorType => "success"
|
51
|
+
}
|
52
|
+
}
|
53
|
+
```
|
54
|
+
|
55
|
+
voice_request and text_request methods returns symbolized https://api.ai response. Structure of response can be found here (https://docs.api.ai/docs/query#response)
|
56
|
+
|
57
|
+
## Advanced usage
|
58
|
+
|
59
|
+
During client instantination 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)
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
ApiAiRuby::Client.new(
|
63
|
+
subscription_key: 'YOUR_SUBSCRIPTION_KEY',
|
64
|
+
client_access_token: 'YOUR_ACCESS_TOKEN',
|
65
|
+
api_lang: 'FR',
|
66
|
+
api_base_url: 'http://example.com/v1/',
|
67
|
+
api_version: 'YYYYMMDD'
|
68
|
+
)
|
69
|
+
```
|
70
|
+
|
71
|
+
And you also can send additional data to server during request, use second parameter of text_request and voice_request methods for that
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
response = client.text_request 'Hello', :contexts => ['firstContext'], :resetContexts => true
|
75
|
+
response = client.voice_request 'Hello', :timezone => "America/New_York"
|
76
|
+
```
|
77
|
+
More information about possible parameters cand be found at https://docs.api.ai/docs/query page
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/api-ai-ruby.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
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 = '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{Plugin makes it easy to integrate your Ruby application with https://api.ai natural language processing service.}
|
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
|
+
|
26
|
+
end
|
data/lib/api-ai-ruby.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module ApiAiRuby
|
2
|
+
class Client
|
3
|
+
attr_accessor :client_access_token, :subscription_key
|
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
|
+
subscription_key: subscription_key
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
# @return [Boolean]
|
43
|
+
def credentials?
|
44
|
+
credentials.values.all?
|
45
|
+
end
|
46
|
+
|
47
|
+
def text_request (query = '', options = {})
|
48
|
+
raise ApiAiRuby::ClientError.new('Credentials missing') if !credentials?
|
49
|
+
options[:query] = query
|
50
|
+
ApiAiRuby::TextRequest.new(self, options).perform
|
51
|
+
end
|
52
|
+
|
53
|
+
def voice_request(file_stream, options = {})
|
54
|
+
raise ApiAiRuby::ClientError.new('Credentials missing') if !credentials?
|
55
|
+
options[:file] = file_stream
|
56
|
+
ApiAiRuby::VoiceRequest.new(self, options).perform
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
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,60 @@
|
|
1
|
+
require 'http'
|
2
|
+
require 'http/form_data'
|
3
|
+
|
4
|
+
module ApiAiRuby
|
5
|
+
class RequestQuery
|
6
|
+
|
7
|
+
attr_accessor :client, :headers, :options, :request_method, :uri
|
8
|
+
|
9
|
+
# @param client [ApiAiRuby::Client]
|
10
|
+
# @param options [Hash]
|
11
|
+
# @return [ApiAiRuby::TextRequest]
|
12
|
+
|
13
|
+
def initialize(client, options = {})
|
14
|
+
@client = client
|
15
|
+
@uri = client.api_base_url + 'query?v=' + client.api_version
|
16
|
+
@request_method = :post
|
17
|
+
options[:lang] = client.api_lang
|
18
|
+
@options = options
|
19
|
+
@headers = {
|
20
|
+
'Authorization': 'Bearer ' + client.client_access_token,
|
21
|
+
'ocp-apim-subscription-key': 'Bearer ' + client.client_access_token
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [Array, Hash]
|
26
|
+
def perform
|
27
|
+
options_key = @request_method == :get ? :params : :form
|
28
|
+
response = HTTP.with(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
|
29
|
+
response_body = symbolize_keys!(response.parse)
|
30
|
+
response_headers = response.headers
|
31
|
+
fail_or_return_response_body(response.code, response_body)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
|
37
|
+
def fail_or_return_response_body(code, body)
|
38
|
+
error = false
|
39
|
+
if code != 200 || body[:status][:code] != 200
|
40
|
+
error = ApiAiRuby::RequestError.new body[:status][:errorDetails], body[:status][:code]
|
41
|
+
end
|
42
|
+
fail(error) if error
|
43
|
+
body
|
44
|
+
end
|
45
|
+
|
46
|
+
def symbolize_keys!(object)
|
47
|
+
if object.is_a?(Array)
|
48
|
+
object.each_with_index do |val, index|
|
49
|
+
object[index] = symbolize_keys!(val)
|
50
|
+
end
|
51
|
+
elsif object.is_a?(Hash)
|
52
|
+
object.keys.each do |key|
|
53
|
+
object[key.to_sym] = symbolize_keys!(object.delete(key))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
object
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module ApiAiRuby
|
4
|
+
class VoiceRequest < ApiAiRuby::RequestQuery
|
5
|
+
|
6
|
+
|
7
|
+
# @param client [ApiAiRuby::Client]
|
8
|
+
# @param options [Hash]
|
9
|
+
# @return [ApiAiRuby::VoiceRequest]
|
10
|
+
def initialize(client, options = {})
|
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
|
+
self
|
20
|
+
end
|
21
|
+
|
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', :subscription_key => 'cb9693af-85ce-4fbf-844a-5563722fc27f')}
|
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(subscription_key: 'SK', 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,53 @@
|
|
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 '#credentials?' do
|
18
|
+
it 'returns true if all credentials are present' do
|
19
|
+
client = ApiAiRuby::Client.new(subscription_key: 'SK', client_access_token: 'CS')
|
20
|
+
expect(client.credentials?).to be true
|
21
|
+
end
|
22
|
+
it 'returns false if any credentials are missing' do
|
23
|
+
client = ApiAiRuby::Client.new(subscription_key: 'SK')
|
24
|
+
expect(client.credentials?).to be false
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'raises error on request without credentials' do
|
28
|
+
expect {subject.text_request '123'}.to raise_error(ApiAiRuby::ClientError)
|
29
|
+
expect {subject.voice_request '123'}.to raise_error(ApiAiRuby::ClientError)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#properties' do
|
35
|
+
|
36
|
+
it 'has correct default properties' do
|
37
|
+
client = ApiAiRuby::Client.new(subscription_key: 'SK', client_access_token: 'CS')
|
38
|
+
expect(client.api_base_url).to eq ApiAiRuby::Constants::DEFAULT_BASE_URL
|
39
|
+
expect(client.api_version).to eq ApiAiRuby::Constants::DEFAULT_API_VERSION
|
40
|
+
expect(client.api_lang).to eq ApiAiRuby::Constants::DEFAULT_CLIENT_LANG
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'correctly creates client with given properties' do
|
44
|
+
client = ApiAiRuby::Client.new(subscription_key: 'SK', client_access_token: 'CS', api_lang: 'RU', api_base_url: 'http://localhost', api_version: '1234')
|
45
|
+
expect(client.api_base_url).to eq 'http://localhost'
|
46
|
+
expect(client.api_version).to eq '1234'
|
47
|
+
expect(client.api_lang).to eq 'RU'
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
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
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api-ai-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- api.ai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-22 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: Plugin makes it easy to integrate your Ruby application with https://api.ai
|
70
|
+
natural language processing service.
|
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
|
+
- api-ai-ruby.gemspec
|
83
|
+
- lib/api-ai-ruby.rb
|
84
|
+
- lib/api-ai-ruby/client.rb
|
85
|
+
- lib/api-ai-ruby/client_error.rb
|
86
|
+
- lib/api-ai-ruby/constants.rb
|
87
|
+
- lib/api-ai-ruby/request_error.rb
|
88
|
+
- lib/api-ai-ruby/request_query.rb
|
89
|
+
- lib/api-ai-ruby/text_request.rb
|
90
|
+
- lib/api-ai-ruby/voice_request.rb
|
91
|
+
- spec/api-ai-ruby/api_spec.rb
|
92
|
+
- spec/api-ai-ruby/client_spec.rb
|
93
|
+
- spec/api-ai-ruby/error_spec.rb
|
94
|
+
- spec/api-ai-ruby/text_request_spec.rb
|
95
|
+
- spec/api-ai-ruby/voice_request_spec.rb
|
96
|
+
- spec/fixtures/hello.wav
|
97
|
+
- spec/helper.rb
|
98
|
+
homepage: https://api.ai
|
99
|
+
licenses:
|
100
|
+
- Apache 2.0 License
|
101
|
+
metadata: {}
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 2.4.8
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: ruby SDK for https://api.ai
|
122
|
+
test_files:
|
123
|
+
- spec/api-ai-ruby/api_spec.rb
|
124
|
+
- spec/api-ai-ruby/client_spec.rb
|
125
|
+
- spec/api-ai-ruby/error_spec.rb
|
126
|
+
- spec/api-ai-ruby/text_request_spec.rb
|
127
|
+
- spec/api-ai-ruby/voice_request_spec.rb
|
128
|
+
- spec/fixtures/hello.wav
|
129
|
+
- spec/helper.rb
|
130
|
+
has_rdoc:
|