voice_recognition_bing 0.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: 3eb45933780de2113b5498b191fdf95d3d5894ed
4
+ data.tar.gz: 7a4c30a787aac74116c9f910adf91b781fcf390d
5
+ SHA512:
6
+ metadata.gz: a434dc84fe5817c2d576123edea6220aac1af14b2476a18d5ecf656f38f33123d7390fa0b7d8fbb2c233a58fb0b1656fb43148f5512e6a0bf738723965092bed
7
+ data.tar.gz: 25fc2b64f5d4e9560097b749eeafadf0ffcd451f3a826d8b6f7738896b0d5fcc9313e50eecbfd21c001eeda22151e5cecf103ff2473d46fa176c46b55ec78aea
@@ -0,0 +1,22 @@
1
+ require "voice_recognition_bing/version"
2
+ require "voice_recognition_bing/configuration"
3
+ require "voice_recognition_bing/authorization"
4
+ require "voice_recognition_bing/service"
5
+
6
+ module VoiceRecognitionBing
7
+ class << self
8
+ attr_writer :configuration
9
+
10
+ def configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def reset
15
+ @configuration = Configuration.new
16
+ end
17
+
18
+ def configure
19
+ yield(configuration)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+
3
+ require 'voice_recognition_bing/ms_service'
4
+ require 'voice_recognition_bing/credentials'
5
+
6
+ module VoiceRecognitionBing
7
+ class Authorization
8
+ attr_writer :ms_service
9
+ def self.credentials
10
+ new.credentials
11
+ end
12
+
13
+ def credentials
14
+ return NullCredentials.new unless valid_params
15
+
16
+ response = remote_service.authenticate(subscription_key: VoiceRecognitionBing.configuration.subscription_key)
17
+
18
+ Credentials.new(extract_token(response))
19
+ end
20
+
21
+ private
22
+
23
+ def extract_token(string)
24
+ return String.new if string =~ /"error":/
25
+
26
+ string
27
+ end
28
+
29
+ def valid_params
30
+ !VoiceRecognitionBing.configuration.subscription_key.nil?
31
+ end
32
+
33
+ def remote_service
34
+ @ms_service ||= MSService
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module VoiceRecognitionBing
2
+ class Configuration
3
+ attr_accessor :subscription_key
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module VoiceRecognitionBing
2
+ class Credentials < Struct.new(:token)
3
+ def valid?
4
+ !token.nil? && !token.empty?
5
+ end
6
+ end
7
+
8
+ class NullCredentials
9
+ def valid?
10
+ false
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,33 @@
1
+ require 'httparty'
2
+ require 'securerandom'
3
+
4
+ module VoiceRecognitionBing
5
+ class MSService
6
+ class << self
7
+ # https://www.microsoft.com/cognitive-services/en-us/speech-api/documentation/API-Reference-REST/BingVoiceRecognition#authenticate-the-api-call
8
+ def authenticate(subscription_key:)
9
+ response = HTTParty.post(
10
+ "https://api.cognitive.microsoft.com/sts/v1.0/issueToken",
11
+ { headers: {"Ocp-Apim-Subscription-Key" => subscription_key } }
12
+ )
13
+
14
+ response.body
15
+ end
16
+
17
+ # https://www.microsoft.com/cognitive-services/en-us/speech-api/documentation/API-Reference-REST/BingVoiceRecognition#access-the-speech-service-endpoint
18
+ def recognize(content:, token:)
19
+ url = 'https://speech.platform.bing.com/recognize?version=3.0' +
20
+ "&requestid=#{SecureRandom.uuid}" +
21
+ "&appID=#{SecureRandom.uuid}" +
22
+ "&instanceid=#{SecureRandom.uuid}" +
23
+ '&format=json&locale=es-ES&device.os=linux&scenarios=ulm'
24
+
25
+ opts = { headers: {"Authorization" => "Bearer #{token}",
26
+ "Content-Type" => "audio/wav; codec=audio/pcm; samplerate=16000; sourcerate=8000; trustsourcerate=false" },
27
+ body: content.to_s }
28
+
29
+ HTTParty.post(url, opts).body
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,28 @@
1
+ module VoiceRecognitionBing
2
+ class InvalidCredentials < StandardError; end
3
+ class Service
4
+ attr_writer :ms_service
5
+
6
+ def self.recognize(content, credentials)
7
+ new.recognize(content, credentials)
8
+ end
9
+
10
+ def recognize(content, credentials)
11
+ raise InvalidCredentials unless credentials.valid?
12
+
13
+ response = remote_service.recognize(content: content, token: credentials.token)
14
+
15
+ parse_result(response)
16
+ end
17
+
18
+ private
19
+
20
+ def parse_result(json)
21
+ JSON.parse(json)['results'].first['lexical']
22
+ end
23
+
24
+ def remote_service
25
+ @ms_service ||= MSService
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module VoiceRecognitionBing
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: voice_recognition_bing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Miguel Angel Fernandez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-23 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.14'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.14'
69
+ description:
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/voice_recognition_bing.rb
76
+ - lib/voice_recognition_bing/authorization.rb
77
+ - lib/voice_recognition_bing/configuration.rb
78
+ - lib/voice_recognition_bing/credentials.rb
79
+ - lib/voice_recognition_bing/ms_service.rb
80
+ - lib/voice_recognition_bing/service.rb
81
+ - lib/voice_recognition_bing/version.rb
82
+ homepage: https://github.com/elmendalerenda/voice_recognition_bing
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.5.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Client for the Voice Recognition service of Bing
106
+ test_files: []