wit 1.0.6 → 2.0.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 +5 -13
- data/.gitignore +3 -0
- data/CHANGES.md +13 -0
- data/LICENSE +22 -0
- data/README.md +41 -0
- data/lib/wit.rb +37 -0
- data/wit.gemspec +15 -0
- metadata +16 -15
- data/ext/wit/extconf.rb +0 -68
- data/ext/wit/extconf.rb_ +0 -39
- data/ext/wit/libwit/include/wit.h +0 -81
- data/ext/wit/wit.c +0 -152
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
OTRjZTg4MGVhYWJkYzUzNTZkMTFhNDc0NzE3ZDA4ZGM1MjJlYmMyNw==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fb6fd4fd673c2e0a4788d1f843d316fa925a7afd
|
4
|
+
data.tar.gz: f86c64544a7dd61b3b1f232c3a9a9e48e590d38b
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
YTc5YzVjODc3NTk4MDNlMjkyM2IwN2QyNjk3ZDg3Y2M1ZWI0MjFhZDg3ZTUx
|
11
|
-
OWEyYTNiZmM0YzA0ODFiNTAzMDZlZDhmYmE4NjczYmE2MWJmYmM=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MWZkZGI0Y2Q4NjQ0OTZjOTZlZTNkMjg0YzYyYjgyYWU1Yzc2ODg1ZmIxMDVm
|
14
|
-
NGFlNDQxNzU1ZDI5OGY3MzkwMGYzNzVjYmQ0ZjExODA2ZWFlYmI0OTU3MTky
|
15
|
-
MzU5N2RlYTIwMDNhNGI2YjZiNGFjNDcyNjQzZjc3NzNiNGFiODY=
|
6
|
+
metadata.gz: f4de22f57f076e5946c600e45a2cd09e8fbe222bbe5ff6215bcdd7bb978e51a6224ca5f74bfe5a7013c37085d093c1b393aa8d34305c7635b55753ba2f6fd41c
|
7
|
+
data.tar.gz: aa5003e437f4716b8961d0d58f42449a3ce490bc09b7c891a144e1bd605bf55e206162f111422221b786533a97651a1d756100ea7a5325b0b6ad1d413f43ec06
|
data/.gitignore
ADDED
data/CHANGES.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
## v2.0
|
2
|
+
|
3
|
+
Rewrite in pure Ruby
|
4
|
+
|
5
|
+
### breaking
|
6
|
+
|
7
|
+
- audio recording and streaming have been removed because:
|
8
|
+
- many people only needed access to the HTTP API, and audio recording did not make sense for server-side use cases
|
9
|
+
- dependent on platform, choice best left to developers
|
10
|
+
- forced us to maintain native bindings as opposed to a pure Pythonic library
|
11
|
+
- we renamed the functions to match the HTTP API more closely
|
12
|
+
- `.text_query(string, access_token)` becomes `.message(access_token, string)`
|
13
|
+
- all functions now return a Ruby dict instead of a JSON string
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2014, Wit.ai, Inc. All rights reserved.
|
3
|
+
*
|
4
|
+
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
|
5
|
+
* use, copy, modify, and distribute this software in source code or binary
|
6
|
+
* form for use in connection with the web services and APIs provided by
|
7
|
+
* Wit.ai.
|
8
|
+
*
|
9
|
+
* As with any software that integrates with the Wit.ai platform, your use
|
10
|
+
* of this software is subject to the Wit.ai Terms of Service
|
11
|
+
* [https://wit.ai/terms]. This copyright notice shall be included in all
|
12
|
+
* copies or substantial portions of the software.
|
13
|
+
*
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
17
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
19
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
20
|
+
* DEALINGS IN THE SOFTWARE.
|
21
|
+
*
|
22
|
+
*/
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# wit-ruby
|
2
|
+
|
3
|
+
`wit-ruby` is a Ruby client to easily use the [Wit.ai](http://wit.ai) HTTP API.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
```bash
|
8
|
+
gem install wit
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'wit'
|
15
|
+
p Wit.message('MY_ACCESS_TOKEN', 'turn on the lights in the kitchen')
|
16
|
+
```
|
17
|
+
|
18
|
+
See below for more examples.
|
19
|
+
|
20
|
+
## Install from source
|
21
|
+
|
22
|
+
```bash
|
23
|
+
git clone https://github.com/wit-ai/wit-ruby
|
24
|
+
gem build wit.gemspec
|
25
|
+
gem install wit-*.gem
|
26
|
+
```
|
27
|
+
|
28
|
+
## API
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'wit'
|
32
|
+
|
33
|
+
access_token = 'MY_ACCESS_TOKEN'
|
34
|
+
|
35
|
+
# GET /message to extract intent and entities from user request
|
36
|
+
p Wit.message(access_token, 'turn on the lights in the kitchen')
|
37
|
+
```
|
38
|
+
|
39
|
+
## Thanks
|
40
|
+
|
41
|
+
Thanks to [Justin Workman](http://github.com/xtagon) for releasing a first version in October 2013. We really appreciate!
|
data/lib/wit.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Wit
|
2
|
+
require 'json'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
WIT_API_HOST = ENV['WIT_URL'] || 'https://api.wit.ai'
|
6
|
+
|
7
|
+
class WitException < Exception
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.req(access_token, meth_class, path, params)
|
11
|
+
uri = URI(WIT_API_HOST + path)
|
12
|
+
uri.query = URI.encode_www_form(params)
|
13
|
+
|
14
|
+
req = meth_class.new(uri)
|
15
|
+
req['authorization'] = 'Bearer ' + access_token
|
16
|
+
req['accept'] = 'application/vnd.wit.20160330+json'
|
17
|
+
|
18
|
+
Net::HTTP.start(
|
19
|
+
uri.host, uri.port, :use_ssl => uri.scheme == 'https'
|
20
|
+
) do |http|
|
21
|
+
rsp = http.request(req)
|
22
|
+
if rsp.code.to_i > 200
|
23
|
+
raise WitException.new('HTTP error code=' + rsp.code)
|
24
|
+
end
|
25
|
+
|
26
|
+
JSON.parse(rsp.body)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.message(access_token, msg)
|
31
|
+
params = {}
|
32
|
+
if msg
|
33
|
+
params[:q] = msg
|
34
|
+
end
|
35
|
+
req(access_token, Net::HTTP::Get, '/message', params)
|
36
|
+
end
|
37
|
+
end
|
data/wit.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'wit'
|
3
|
+
s.version = '2.0.0'
|
4
|
+
s.date = '2014-12-05'
|
5
|
+
s.summary = 'Ruby SDK for Wit.ai'
|
6
|
+
s.description = 'Ruby SDK for Wit.ai'
|
7
|
+
s.authors = ['The Wit Team']
|
8
|
+
s.email = 'help@wit.ai'
|
9
|
+
s.homepage = 'https://wit.ai'
|
10
|
+
s.license = 'GPL-2.0'
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.required_ruby_version = '>= 1.9.3'
|
13
|
+
s.require_paths = ['lib']
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
end
|
metadata
CHANGED
@@ -1,27 +1,28 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- The Wit Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2014-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Ruby SDK for Wit.
|
14
|
-
email:
|
13
|
+
description: Ruby SDK for Wit.ai
|
14
|
+
email: help@wit.ai
|
15
15
|
executables: []
|
16
|
-
extensions:
|
17
|
-
- ext/wit/extconf.rb
|
16
|
+
extensions: []
|
18
17
|
extra_rdoc_files: []
|
19
18
|
files:
|
20
|
-
-
|
21
|
-
-
|
22
|
-
-
|
23
|
-
-
|
24
|
-
|
19
|
+
- .gitignore
|
20
|
+
- CHANGES.md
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- lib/wit.rb
|
24
|
+
- wit.gemspec
|
25
|
+
homepage: https://wit.ai
|
25
26
|
licenses:
|
26
27
|
- GPL-2.0
|
27
28
|
metadata: {}
|
@@ -31,18 +32,18 @@ require_paths:
|
|
31
32
|
- lib
|
32
33
|
required_ruby_version: !ruby/object:Gem::Requirement
|
33
34
|
requirements:
|
34
|
-
- -
|
35
|
+
- - '>='
|
35
36
|
- !ruby/object:Gem::Version
|
36
37
|
version: 1.9.3
|
37
38
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
39
|
requirements:
|
39
|
-
- -
|
40
|
+
- - '>='
|
40
41
|
- !ruby/object:Gem::Version
|
41
42
|
version: '0'
|
42
43
|
requirements: []
|
43
44
|
rubyforge_project:
|
44
|
-
rubygems_version: 2.
|
45
|
+
rubygems_version: 2.0.14.1
|
45
46
|
signing_key:
|
46
47
|
specification_version: 4
|
47
|
-
summary: Ruby SDK for Wit
|
48
|
+
summary: Ruby SDK for Wit.ai
|
48
49
|
test_files: []
|
data/ext/wit/extconf.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
require "mkmf"
|
2
|
-
require 'open-uri'
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
|
-
PATH = File.expand_path(File.dirname(__FILE__))
|
6
|
-
|
7
|
-
LIBDIR = RbConfig::CONFIG['libdir']
|
8
|
-
INCLUDEDIR = RbConfig::CONFIG['includedir']
|
9
|
-
HEADER_DIRS = [
|
10
|
-
"#{PATH}/libwit/include",
|
11
|
-
INCLUDEDIR
|
12
|
-
]
|
13
|
-
LIB_DIRS = [
|
14
|
-
"#{PATH}/libwit/lib",
|
15
|
-
LIBDIR
|
16
|
-
]
|
17
|
-
|
18
|
-
if RUBY_PLATFORM.include? 'arm'
|
19
|
-
LIBWIT_FILE = 'libwit-armv6.a'
|
20
|
-
elsif RUBY_PLATFORM.include? '64'
|
21
|
-
if RUBY_PLATFORM.include? 'darwin'
|
22
|
-
LIBWIT_FILE = 'libwit-64-darwin.a'
|
23
|
-
else
|
24
|
-
LIBWIT_FILE = 'libwit-64-linux.a'
|
25
|
-
end
|
26
|
-
else
|
27
|
-
LIBWIT_FILE = 'libwit-32-linux.a'
|
28
|
-
end
|
29
|
-
LIBWIT_PATH = "#{PATH}/libwit/lib/libwit.a"
|
30
|
-
|
31
|
-
if !File.file?(LIBWIT_PATH)
|
32
|
-
puts "Fetching libwit..."
|
33
|
-
dir = File.dirname(LIBWIT_PATH)
|
34
|
-
unless File.directory?(dir)
|
35
|
-
FileUtils.mkdir_p(dir)
|
36
|
-
end
|
37
|
-
open(LIBWIT_PATH, 'wb') do |file|
|
38
|
-
total = nil
|
39
|
-
file << open("https://github.com/wit-ai/libwit/releases/download/1.1.2/#{LIBWIT_FILE}",
|
40
|
-
:content_length_proc => lambda {|t|
|
41
|
-
total = t
|
42
|
-
print "0/#{total} bytes downloaded"
|
43
|
-
STDOUT.flush
|
44
|
-
},
|
45
|
-
:progress_proc => lambda {|s|
|
46
|
-
print "\r#{s}/#{total} bytes downloaded"
|
47
|
-
STDOUT.flush
|
48
|
-
}).read
|
49
|
-
end
|
50
|
-
STDOUT.print(" done\n")
|
51
|
-
end
|
52
|
-
|
53
|
-
if RUBY_PLATFORM.include? 'darwin'
|
54
|
-
libs = ['wit', 'ssl', 'crypto', 'z', 'sox', 'System', 'pthread', 'c', 'm']
|
55
|
-
else
|
56
|
-
libs = ['wit', 'rt', 'sox', 'ssl', 'crypto', 'dl', 'pthread', 'rt', 'gcc_s', 'pthread', 'c', 'm']
|
57
|
-
end
|
58
|
-
|
59
|
-
$LOCAL_LIBS = '-l' + libs.join(' -l')
|
60
|
-
|
61
|
-
dir_config 'wit', HEADER_DIRS, LIB_DIRS
|
62
|
-
|
63
|
-
for lib in libs
|
64
|
-
abort "Error: missing #{lib}" unless have_library lib
|
65
|
-
end
|
66
|
-
abort 'Error: missing wit.h' unless have_header 'wit.h'
|
67
|
-
|
68
|
-
create_makefile 'wit'
|
data/ext/wit/extconf.rb_
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
require "mkmf"
|
2
|
-
|
3
|
-
PATH = File.expand_path(File.dirname(__FILE__))
|
4
|
-
|
5
|
-
LIBDIR = RbConfig::CONFIG['libdir']
|
6
|
-
INCLUDEDIR = RbConfig::CONFIG['includedir']
|
7
|
-
HEADER_DIRS = [
|
8
|
-
PATH + '/libwit/include',
|
9
|
-
INCLUDEDIR
|
10
|
-
]
|
11
|
-
LIB_DIRS = [
|
12
|
-
PATH + '/libwit/lib',
|
13
|
-
LIBDIR
|
14
|
-
]
|
15
|
-
|
16
|
-
if RUBY_PLATFORM.include? "arm"
|
17
|
-
LIBWIT_FILE = "libwit-armv6.a"
|
18
|
-
elsif RUBY_PLATFORM.include? "64"
|
19
|
-
if RUBY_PLATFORM.include? "darwin"
|
20
|
-
LIBWIT_FILE = "libwit-64-darwin.a"
|
21
|
-
else
|
22
|
-
LIBWIT_FILE = "libwit-64-linux.a"
|
23
|
-
end
|
24
|
-
else
|
25
|
-
LIBWIT_FILE = "libwit-32-linux.a"
|
26
|
-
end
|
27
|
-
|
28
|
-
abort "unable to retrieve libwit" unless system('curl -Ss -L -o libwit.a https://github.com/wit-ai/libwit/releases/download/1.1.1/' + LIBWIT_FILE, :chdir=> PATH + '/libwit/lib')
|
29
|
-
|
30
|
-
$LOCAL_LIBS = '-lwit -lsox -lcurl'
|
31
|
-
|
32
|
-
dir_config 'wit', HEADER_DIRS, LIB_DIRS
|
33
|
-
|
34
|
-
abort "missing sox" unless have_library "sox"
|
35
|
-
abort "missing curl" unless have_library "curl"
|
36
|
-
abort "missing wit.h" unless have_header "wit.h"
|
37
|
-
abort "missing libwit" unless have_library "wit"
|
38
|
-
|
39
|
-
create_makefile "wit"
|
@@ -1,81 +0,0 @@
|
|
1
|
-
#ifndef WIT_H
|
2
|
-
#define WIT_H
|
3
|
-
|
4
|
-
#include <stdlib.h>
|
5
|
-
|
6
|
-
struct wit_context;
|
7
|
-
typedef void (*wit_resp_callback)(char *);
|
8
|
-
|
9
|
-
/**
|
10
|
-
* Initialize the resources for audio recording and Wit API requests.
|
11
|
-
* This function returns a context object used by all the other functions
|
12
|
-
* in the library.
|
13
|
-
* The resources can be released using wit_close.
|
14
|
-
*/
|
15
|
-
struct wit_context *wit_init(const char *device_opt, unsigned int verbosity);
|
16
|
-
|
17
|
-
/**
|
18
|
-
* Release the resources allocated by wit_init.
|
19
|
-
* The context object should not be used for any other purpose after this function
|
20
|
-
* has been called.
|
21
|
-
*/
|
22
|
-
void wit_close(struct wit_context *context);
|
23
|
-
|
24
|
-
/**
|
25
|
-
* Send a text query to the Wit instance identified by the access_token.
|
26
|
-
* This function is blocking, and returns the response from the Wit instance.
|
27
|
-
*/
|
28
|
-
char *wit_text_query(struct wit_context *context, const char *text, const char *access_token);
|
29
|
-
|
30
|
-
/**
|
31
|
-
* Send a text query to the Wit instance identified by the access_token.
|
32
|
-
* This function is non-blocking. When a response is received from the Wit instance, the
|
33
|
-
* given callback is called with the response given as an argument.
|
34
|
-
*/
|
35
|
-
void wit_text_query_async(struct wit_context *context, const char *text, const char *access_token, wit_resp_callback cb);
|
36
|
-
|
37
|
-
/**
|
38
|
-
* Send a voice query to the Wit instance identified by the access_token.
|
39
|
-
* This function is blocking, and returns the response from the Wit instance.
|
40
|
-
*
|
41
|
-
* The function attempts to automatically detect when the user stops speaking. If this
|
42
|
-
* fails, the wit_voice_query_stop or wit_voice_query_stop_async functions below can
|
43
|
-
* be used to trigger the end of the request and receive the response.
|
44
|
-
*/
|
45
|
-
char *wit_voice_query_auto(struct wit_context *context, const char *access_token);
|
46
|
-
|
47
|
-
/**
|
48
|
-
* Send a voice query to the Wit instance identified by the access_token.
|
49
|
-
* This function is non-blocking. When a response is received from the Wit instance, the
|
50
|
-
* given callback is called with the response given as an argument.
|
51
|
-
*
|
52
|
-
* The function attempts to automatically detect when the user stops speaking. If this
|
53
|
-
* fails, the wit_voice_query_stop or wit_voice_query_stop_async functions below can
|
54
|
-
* be used to trigger the end of the request and receive the response.
|
55
|
-
*/
|
56
|
-
void wit_voice_query_auto_async(struct wit_context *context, const char *access_token, wit_resp_callback cb);
|
57
|
-
|
58
|
-
/**
|
59
|
-
* Send a voice query to the Wit instance identified by the access_token.
|
60
|
-
* This function returns immediately: the recording session stops only when either
|
61
|
-
* wit_voice_query_stop or wit_voice_query_stop_async is called. No end-of-speech
|
62
|
-
* detection is performed.
|
63
|
-
*/
|
64
|
-
void wit_voice_query_start(struct wit_context *context, const char *access_token);
|
65
|
-
|
66
|
-
/**
|
67
|
-
* Stop the ongoing recording session and receive the response.
|
68
|
-
* This function is blocking, and returns the response from the Wit instance.
|
69
|
-
* This function has no effect if there is no ongoing recording session.
|
70
|
-
*/
|
71
|
-
char *wit_voice_query_stop(struct wit_context *context);
|
72
|
-
|
73
|
-
/**
|
74
|
-
* Stop the ongoing recording session and receive the response.
|
75
|
-
* This function is non-blocking. When a response is received from the Wit instance, the
|
76
|
-
* given callback is called with the response given as an argument.
|
77
|
-
* This function has no effect if there is no ongoing recording session.
|
78
|
-
*/
|
79
|
-
void wit_voice_query_stop_async(struct wit_context *context, wit_resp_callback cb);
|
80
|
-
|
81
|
-
#endif
|
data/ext/wit/wit.c
DELETED
@@ -1,152 +0,0 @@
|
|
1
|
-
#include <ruby.h>
|
2
|
-
#include "wit.h"
|
3
|
-
|
4
|
-
struct wit_context *context;
|
5
|
-
static VALUE e_WitError;
|
6
|
-
static VALUE rb_cb;
|
7
|
-
|
8
|
-
static VALUE libwit_init(int argc, VALUE *argv, VALUE obj) {
|
9
|
-
const char *device_opt = NULL;
|
10
|
-
VALUE device = Qnil;
|
11
|
-
rb_scan_args(argc, argv, "01", &device);
|
12
|
-
if (device != Qnil) {
|
13
|
-
Check_Type(device, T_STRING);
|
14
|
-
device_opt = StringValuePtr(device);
|
15
|
-
}
|
16
|
-
context = wit_init(device_opt, 4);
|
17
|
-
return Qnil;
|
18
|
-
}
|
19
|
-
|
20
|
-
static VALUE libwit_close(VALUE obj) {
|
21
|
-
if (context != NULL) {
|
22
|
-
wit_close(context);
|
23
|
-
}
|
24
|
-
return Qnil;
|
25
|
-
}
|
26
|
-
|
27
|
-
static VALUE libwit_text_query(VALUE obj, VALUE text, VALUE access_token) {
|
28
|
-
const char *resp;
|
29
|
-
if (context == NULL)
|
30
|
-
rb_raise(e_WitError, "Wit context uninitialized (did you call Wit.init?)");
|
31
|
-
Check_Type(text, T_STRING);
|
32
|
-
Check_Type(access_token, T_STRING);
|
33
|
-
VALUE str = Qnil;
|
34
|
-
resp = wit_text_query(context, StringValuePtr(text), StringValuePtr(access_token));
|
35
|
-
if (resp != NULL)
|
36
|
-
str = rb_str_new2(resp);
|
37
|
-
xfree((char *)resp);
|
38
|
-
return str;
|
39
|
-
}
|
40
|
-
|
41
|
-
static VALUE libwit_voice_query_start(VALUE obj, VALUE access_token) {
|
42
|
-
if (context == NULL)
|
43
|
-
rb_raise(e_WitError, "Wit context uninitialized (did you call Wit.init?)");
|
44
|
-
Check_Type(access_token, T_STRING);
|
45
|
-
wit_voice_query_start(context, StringValuePtr(access_token));
|
46
|
-
return Qnil;
|
47
|
-
}
|
48
|
-
|
49
|
-
static VALUE libwit_voice_query_stop(VALUE obj) {
|
50
|
-
const char *resp;
|
51
|
-
if (context == NULL)
|
52
|
-
rb_raise(e_WitError, "Wit context uninitialized (did you call Wit.init?)");
|
53
|
-
VALUE str = Qnil;
|
54
|
-
resp = wit_voice_query_stop(context);
|
55
|
-
if (resp != NULL)
|
56
|
-
str = rb_str_new2(resp);
|
57
|
-
xfree((char *)resp);
|
58
|
-
return str;
|
59
|
-
}
|
60
|
-
|
61
|
-
static VALUE libwit_voice_query_auto(VALUE obj, VALUE access_token)
|
62
|
-
{
|
63
|
-
const char *resp;
|
64
|
-
if (context == NULL)
|
65
|
-
rb_raise(e_WitError, "Wit context uninitialized (did you call Wit.init?)");
|
66
|
-
Check_Type(access_token, T_STRING);
|
67
|
-
VALUE str = Qnil;
|
68
|
-
resp = wit_voice_query_auto(context, StringValuePtr(access_token));
|
69
|
-
if (resp != NULL)
|
70
|
-
str = rb_str_new2(resp);
|
71
|
-
xfree((char *)resp);
|
72
|
-
return str;
|
73
|
-
}
|
74
|
-
|
75
|
-
static VALUE thread_wrapper_proc(void *args) {
|
76
|
-
VALUE str = Qnil;
|
77
|
-
if ((char *) args != NULL)
|
78
|
-
str = rb_str_new2((char *) args);
|
79
|
-
xfree(args);
|
80
|
-
rb_funcall(rb_cb, rb_intern("call"), 1, str);
|
81
|
-
return Qnil;
|
82
|
-
}
|
83
|
-
|
84
|
-
static VALUE thread_wrapper_meth(void *args) {
|
85
|
-
VALUE str = Qnil;
|
86
|
-
if ((char *) args != NULL)
|
87
|
-
str = rb_str_new2((char *) args);
|
88
|
-
xfree(args);
|
89
|
-
rb_funcall(rb_class_of(rb_cb), rb_to_id(rb_cb), 1, str);
|
90
|
-
return Qnil;
|
91
|
-
}
|
92
|
-
|
93
|
-
void my_wit_resp_callback(char *res) {
|
94
|
-
if (rb_cb == Qnil)
|
95
|
-
rb_raise(rb_eRuntimeError, "callback is nil");
|
96
|
-
if (rb_class_of(rb_cb) == rb_cProc)
|
97
|
-
rb_thread_create(thread_wrapper_proc, (char *)res);
|
98
|
-
else if (rb_class_of(rb_cb) == rb_cSymbol)
|
99
|
-
rb_thread_create(thread_wrapper_meth, (char *)res);
|
100
|
-
else
|
101
|
-
rb_raise(rb_eTypeError, "expected Proc or Symbol callback");
|
102
|
-
}
|
103
|
-
|
104
|
-
static VALUE libwit_text_query_async(VALUE obj, VALUE text, VALUE access_token, VALUE callback)
|
105
|
-
{
|
106
|
-
if (context == NULL)
|
107
|
-
rb_raise(e_WitError, "Wit context uninitialized (did you call Wit.init?)");
|
108
|
-
Check_Type(text, T_STRING);
|
109
|
-
Check_Type(access_token, T_STRING);
|
110
|
-
if (rb_class_of(callback) != rb_cSymbol && rb_class_of(callback) != rb_cProc)
|
111
|
-
rb_raise(rb_eTypeError, "expected Proc or Symbol callback");
|
112
|
-
rb_cb = callback;
|
113
|
-
wit_text_query_async(context, StringValuePtr(text), StringValuePtr(access_token), my_wit_resp_callback);
|
114
|
-
return Qnil;
|
115
|
-
}
|
116
|
-
|
117
|
-
static VALUE libwit_voice_query_stop_async(VALUE obj, VALUE callback)
|
118
|
-
{
|
119
|
-
if (context == NULL)
|
120
|
-
rb_raise(e_WitError, "Wit context uninitialized (did you call Wit.init?)");
|
121
|
-
if (rb_class_of(callback) != rb_cSymbol && rb_class_of(callback) != rb_cProc)
|
122
|
-
rb_raise(rb_eTypeError, "expected Proc or Symbol callback");
|
123
|
-
rb_cb = callback;
|
124
|
-
wit_voice_query_stop_async(context, my_wit_resp_callback);
|
125
|
-
return Qnil;
|
126
|
-
}
|
127
|
-
|
128
|
-
static VALUE libwit_voice_query_auto_async(VALUE obj, VALUE access_token, VALUE callback)
|
129
|
-
{
|
130
|
-
if (context == NULL)
|
131
|
-
rb_raise(e_WitError, "Wit context uninitialized (did you call Wit.init?)");
|
132
|
-
Check_Type(access_token, T_STRING);
|
133
|
-
if (rb_class_of(callback) != rb_cSymbol && rb_class_of(callback) != rb_cProc)
|
134
|
-
rb_raise(rb_eTypeError, "expected Proc or Symbol callback");
|
135
|
-
rb_cb = callback;
|
136
|
-
wit_voice_query_auto_async(context, StringValuePtr(access_token), my_wit_resp_callback);
|
137
|
-
return Qnil;
|
138
|
-
}
|
139
|
-
|
140
|
-
void Init_wit(void) {
|
141
|
-
VALUE wit_module = rb_define_module("Wit");
|
142
|
-
e_WitError = rb_define_class_under(wit_module, "Wit error", rb_eStandardError);
|
143
|
-
rb_define_module_function(wit_module, "init", libwit_init, -1);
|
144
|
-
rb_define_module_function(wit_module, "close", libwit_close, 0);
|
145
|
-
rb_define_module_function(wit_module, "text_query", libwit_text_query, 2);
|
146
|
-
rb_define_module_function(wit_module, "voice_query_start", libwit_voice_query_start, 1);
|
147
|
-
rb_define_module_function(wit_module, "voice_query_stop", libwit_voice_query_stop, 0);
|
148
|
-
rb_define_module_function(wit_module, "voice_query_auto", libwit_voice_query_auto, 1);
|
149
|
-
rb_define_module_function(wit_module, "text_query_async", libwit_text_query_async, 3);
|
150
|
-
rb_define_module_function(wit_module, "voice_query_stop_async", libwit_voice_query_stop_async, 1);
|
151
|
-
rb_define_module_function(wit_module, "voice_query_auto_async", libwit_voice_query_auto_async, 2);
|
152
|
-
}
|