languagedetection 0.3.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 +7 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/example/example_batch.rb +34 -0
- data/example/example_detect.rb +30 -0
- data/example/example_languages.rb +27 -0
- data/language_detection.gemspec +32 -0
- data/lib/language_detection/batch/batch_exception.rb +15 -0
- data/lib/language_detection/batch/batch_options.rb +19 -0
- data/lib/language_detection/batch/batch_request.rb +16 -0
- data/lib/language_detection/batch/batch_response.rb +17 -0
- data/lib/language_detection/detect/detect_exception.rb +15 -0
- data/lib/language_detection/detect/detect_options.rb +20 -0
- data/lib/language_detection/detect/detect_request.rb +16 -0
- data/lib/language_detection/detect/detect_response.rb +17 -0
- data/lib/language_detection/languages/languages_exception.rb +15 -0
- data/lib/language_detection/languages/languages_options.rb +16 -0
- data/lib/language_detection/languages/languages_request.rb +16 -0
- data/lib/language_detection/languages/languages_response.rb +15 -0
- data/lib/language_detection/missing_argument_exception.rb +13 -0
- data/lib/language_detection/version.rb +3 -0
- data/lib/language_detection.rb +161 -0
- data/out/production/language_detection/language_detection/batch/batch_exception.rb +15 -0
- data/out/production/language_detection/language_detection/batch/batch_options.rb +19 -0
- data/out/production/language_detection/language_detection/batch/batch_request.rb +16 -0
- data/out/production/language_detection/language_detection/batch/batch_response.rb +17 -0
- data/out/production/language_detection/language_detection/detect/detect_exception.rb +15 -0
- data/out/production/language_detection/language_detection/detect/detect_options.rb +20 -0
- data/out/production/language_detection/language_detection/detect/detect_request.rb +16 -0
- data/out/production/language_detection/language_detection/detect/detect_response.rb +17 -0
- data/out/production/language_detection/language_detection/languages/languages_exception.rb +15 -0
- data/out/production/language_detection/language_detection/languages/languages_options.rb +16 -0
- data/out/production/language_detection/language_detection/languages/languages_request.rb +16 -0
- data/out/production/language_detection/language_detection/languages/languages_response.rb +15 -0
- data/out/production/language_detection/language_detection/missing_argument_exception.rb +13 -0
- data/out/production/language_detection/language_detection/version.rb +3 -0
- data/out/production/language_detection/language_detection.rb +161 -0
- data/out/test/language_detection/language_detection_spec.rb +99 -0
- data/out/test/language_detection/spec_helper.rb +2 -0
- metadata +187 -0
@@ -0,0 +1,161 @@
|
|
1
|
+
require "httparty"
|
2
|
+
require "hashable"
|
3
|
+
require "language_detection/version"
|
4
|
+
require "language_detection/detect/detect_options"
|
5
|
+
require "language_detection/detect/detect_request"
|
6
|
+
require "language_detection/detect/detect_response"
|
7
|
+
require "language_detection/detect/detect_exception"
|
8
|
+
require "language_detection/batch/batch_options"
|
9
|
+
require "language_detection/batch/batch_request"
|
10
|
+
require "language_detection/batch/batch_response"
|
11
|
+
require "language_detection/batch/batch_exception"
|
12
|
+
require "language_detection/languages/languages_options"
|
13
|
+
require "language_detection/languages/languages_request"
|
14
|
+
require "language_detection/languages/languages_response"
|
15
|
+
require "language_detection/languages/languages_exception"
|
16
|
+
|
17
|
+
|
18
|
+
module LanguageLayer
|
19
|
+
|
20
|
+
class Client
|
21
|
+
|
22
|
+
include HTTParty
|
23
|
+
|
24
|
+
base_uri 'apilayer.net/api'
|
25
|
+
|
26
|
+
def initialize(access_key)
|
27
|
+
|
28
|
+
if access_key.nil?
|
29
|
+
raise LanguageLayer::MissingArgumentException.new 'access_key'
|
30
|
+
end
|
31
|
+
|
32
|
+
@access_key = access_key
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def detect(query, options = {})
|
38
|
+
|
39
|
+
if query.nil?
|
40
|
+
raise LanguageLayer::MissingArgumentException.new 'query'
|
41
|
+
return
|
42
|
+
end
|
43
|
+
|
44
|
+
# Create a shallow copy so we don't manipulate the original reference
|
45
|
+
q = options.dup
|
46
|
+
|
47
|
+
# Populate the Query
|
48
|
+
q.access_key = @access_key
|
49
|
+
q.query = query
|
50
|
+
|
51
|
+
# We then create the Request
|
52
|
+
req = LanguageLayer::DetectRequest.new(q)
|
53
|
+
|
54
|
+
# We create a Hash of the request so we can send it via HTTP
|
55
|
+
req_dto = req.to_dh
|
56
|
+
|
57
|
+
begin
|
58
|
+
|
59
|
+
# We make the actual request
|
60
|
+
res = self.class.get('/detect', req_dto)
|
61
|
+
|
62
|
+
# We ensure that we tap the response so we can use the results
|
63
|
+
res.inspect
|
64
|
+
|
65
|
+
if (!res[LanguageLayer::DetectResponse::SUCCESS_EXPR])
|
66
|
+
raise LanguageLayer::DetectException.new res[LanguageLayer::DetectResponse::ERROR_EXPR]
|
67
|
+
end
|
68
|
+
|
69
|
+
# We just return the parsed binary response
|
70
|
+
return res.parsed_response
|
71
|
+
|
72
|
+
rescue => e
|
73
|
+
puts e.inspect
|
74
|
+
return
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
def batch(query, options = {})
|
81
|
+
|
82
|
+
if query.nil?
|
83
|
+
raise LanguageLayer::MissingArgumentException.new 'query'
|
84
|
+
return
|
85
|
+
end
|
86
|
+
|
87
|
+
# Create a shallow copy so we don't manipulate the original reference
|
88
|
+
q = options.dup
|
89
|
+
|
90
|
+
# Populate the Query
|
91
|
+
q.access_key = @access_key
|
92
|
+
q.query = query
|
93
|
+
|
94
|
+
# We then create the Request
|
95
|
+
req = LanguageLayer::BatchRequest.new(q)
|
96
|
+
|
97
|
+
# We create a Hash of the request so we can send it via HTTP
|
98
|
+
req_dto = req.to_dh
|
99
|
+
|
100
|
+
begin
|
101
|
+
|
102
|
+
# We make the actual request
|
103
|
+
res = self.class.get('/batch', req_dto)
|
104
|
+
|
105
|
+
# We ensure that we tap the response so we can use the results
|
106
|
+
res.inspect
|
107
|
+
|
108
|
+
if (!res[LanguageLayer::BatchResponse::SUCCESS_EXPR])
|
109
|
+
raise LanguageLayer::BatchException.new res[LanguageLayer::BatchResponse::ERROR_EXPR]
|
110
|
+
end
|
111
|
+
|
112
|
+
# We just return the parsed binary response
|
113
|
+
return res.parsed_response
|
114
|
+
|
115
|
+
rescue => e
|
116
|
+
puts e.inspect
|
117
|
+
return
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
def languages(options = {})
|
124
|
+
|
125
|
+
# Create a shallow copy so we don't manipulate the original reference
|
126
|
+
q = options.dup
|
127
|
+
|
128
|
+
# Populate the Query
|
129
|
+
q.access_key = @access_key
|
130
|
+
|
131
|
+
# We then create the Request
|
132
|
+
req = LanguageLayer::LanguagesRequest.new(q)
|
133
|
+
|
134
|
+
# We create a Hash of the request so we can send it via HTTP
|
135
|
+
req_dto = req.to_dh
|
136
|
+
|
137
|
+
begin
|
138
|
+
|
139
|
+
# We make the actual request
|
140
|
+
res = self.class.get('/languages', req_dto)
|
141
|
+
|
142
|
+
# We ensure that we tap the response so we can use the results
|
143
|
+
res.inspect
|
144
|
+
|
145
|
+
if (!res[LanguageLayer::LanguagesResponse::SUCCESS_EXPR])
|
146
|
+
raise LanguageLayer::LanguagesException.new res[LanguageLayer::LanguagesResponse::ERROR_EXPR]
|
147
|
+
end
|
148
|
+
|
149
|
+
# We just return the parsed binary response
|
150
|
+
return res.parsed_response
|
151
|
+
|
152
|
+
rescue => e
|
153
|
+
puts e.inspect
|
154
|
+
return
|
155
|
+
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
require "httparty"
|
2
|
+
require "hashable"
|
3
|
+
require "language_detection/version"
|
4
|
+
require "language_detection/detect/detect_options"
|
5
|
+
require "language_detection/detect/detect_request"
|
6
|
+
require "language_detection/detect/detect_response"
|
7
|
+
require "language_detection/detect/detect_exception"
|
8
|
+
require "language_detection/batch/batch_options"
|
9
|
+
require "language_detection/batch/batch_request"
|
10
|
+
require "language_detection/batch/batch_response"
|
11
|
+
require "language_detection/batch/batch_exception"
|
12
|
+
require "language_detection/languages/languages_options"
|
13
|
+
require "language_detection/languages/languages_request"
|
14
|
+
require "language_detection/languages/languages_response"
|
15
|
+
require "language_detection/languages/languages_exception"
|
16
|
+
|
17
|
+
|
18
|
+
module LanguageLayer
|
19
|
+
|
20
|
+
class Client
|
21
|
+
|
22
|
+
include HTTParty
|
23
|
+
|
24
|
+
base_uri 'apilayer.net/api'
|
25
|
+
|
26
|
+
def initialize(access_key)
|
27
|
+
|
28
|
+
if access_key.nil?
|
29
|
+
raise LanguageLayer::MissingArgumentException.new 'access_key'
|
30
|
+
end
|
31
|
+
|
32
|
+
@access_key = access_key
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def detect(query, options = {})
|
38
|
+
|
39
|
+
if query.nil?
|
40
|
+
raise LanguageLayer::MissingArgumentException.new 'query'
|
41
|
+
return
|
42
|
+
end
|
43
|
+
|
44
|
+
# Create a shallow copy so we don't manipulate the original reference
|
45
|
+
q = options.dup
|
46
|
+
|
47
|
+
# Populate the Query
|
48
|
+
q.access_key = @access_key
|
49
|
+
q.query = query
|
50
|
+
|
51
|
+
# We then create the Request
|
52
|
+
req = LanguageLayer::DetectRequest.new(q)
|
53
|
+
|
54
|
+
# We create a Hash of the request so we can send it via HTTP
|
55
|
+
req_dto = req.to_dh
|
56
|
+
|
57
|
+
begin
|
58
|
+
|
59
|
+
# We make the actual request
|
60
|
+
res = self.class.get('/detect', req_dto)
|
61
|
+
|
62
|
+
# We ensure that we tap the response so we can use the results
|
63
|
+
res.inspect
|
64
|
+
|
65
|
+
if (!res[LanguageLayer::DetectResponse::SUCCESS_EXPR])
|
66
|
+
raise LanguageLayer::DetectException.new res[LanguageLayer::DetectResponse::ERROR_EXPR]
|
67
|
+
end
|
68
|
+
|
69
|
+
# We just return the parsed binary response
|
70
|
+
return res.parsed_response
|
71
|
+
|
72
|
+
rescue => e
|
73
|
+
puts e.inspect
|
74
|
+
return
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
def batch(query, options = {})
|
81
|
+
|
82
|
+
if query.nil?
|
83
|
+
raise LanguageLayer::MissingArgumentException.new 'query'
|
84
|
+
return
|
85
|
+
end
|
86
|
+
|
87
|
+
# Create a shallow copy so we don't manipulate the original reference
|
88
|
+
q = options.dup
|
89
|
+
|
90
|
+
# Populate the Query
|
91
|
+
q.access_key = @access_key
|
92
|
+
q.query = query
|
93
|
+
|
94
|
+
# We then create the Request
|
95
|
+
req = LanguageLayer::BatchRequest.new(q)
|
96
|
+
|
97
|
+
# We create a Hash of the request so we can send it via HTTP
|
98
|
+
req_dto = req.to_dh
|
99
|
+
|
100
|
+
begin
|
101
|
+
|
102
|
+
# We make the actual request
|
103
|
+
res = self.class.get('/batch', req_dto)
|
104
|
+
|
105
|
+
# We ensure that we tap the response so we can use the results
|
106
|
+
res.inspect
|
107
|
+
|
108
|
+
if (!res[LanguageLayer::BatchResponse::SUCCESS_EXPR])
|
109
|
+
raise LanguageLayer::BatchException.new res[LanguageLayer::BatchResponse::ERROR_EXPR]
|
110
|
+
end
|
111
|
+
|
112
|
+
# We just return the parsed binary response
|
113
|
+
return res.parsed_response
|
114
|
+
|
115
|
+
rescue => e
|
116
|
+
puts e.inspect
|
117
|
+
return
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
def languages(options = {})
|
124
|
+
|
125
|
+
# Create a shallow copy so we don't manipulate the original reference
|
126
|
+
q = options.dup
|
127
|
+
|
128
|
+
# Populate the Query
|
129
|
+
q.access_key = @access_key
|
130
|
+
|
131
|
+
# We then create the Request
|
132
|
+
req = LanguageLayer::LanguagesRequest.new(q)
|
133
|
+
|
134
|
+
# We create a Hash of the request so we can send it via HTTP
|
135
|
+
req_dto = req.to_dh
|
136
|
+
|
137
|
+
begin
|
138
|
+
|
139
|
+
# We make the actual request
|
140
|
+
res = self.class.get('/languages', req_dto)
|
141
|
+
|
142
|
+
# We ensure that we tap the response so we can use the results
|
143
|
+
res.inspect
|
144
|
+
|
145
|
+
if (!res[LanguageLayer::LanguagesResponse::SUCCESS_EXPR])
|
146
|
+
raise LanguageLayer::LanguagesException.new res[LanguageLayer::LanguagesResponse::ERROR_EXPR]
|
147
|
+
end
|
148
|
+
|
149
|
+
# We just return the parsed binary response
|
150
|
+
return res.parsed_response
|
151
|
+
|
152
|
+
rescue => e
|
153
|
+
puts e.inspect
|
154
|
+
return
|
155
|
+
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'dotenv'
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'language_detection'
|
4
|
+
|
5
|
+
|
6
|
+
# Load Environment Variables
|
7
|
+
Dotenv.load
|
8
|
+
|
9
|
+
|
10
|
+
describe LanguageLayer do
|
11
|
+
|
12
|
+
|
13
|
+
it 'has a version number' do
|
14
|
+
expect(LanguageLayer::VERSION).not_to be nil
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
it 'detect (simple)' do
|
19
|
+
|
20
|
+
begin
|
21
|
+
|
22
|
+
# Declare the Client instance passing in the authentication parameters
|
23
|
+
@client = LanguageLayer::Client.new(ENV['ACCESS_KEY'])
|
24
|
+
|
25
|
+
# Set the query to detect
|
26
|
+
query = 'I like apples & oranges.'
|
27
|
+
|
28
|
+
# We declare the options
|
29
|
+
options = LanguageLayer::DetectOptions.new()
|
30
|
+
|
31
|
+
# We make the call to detect
|
32
|
+
response = @client.detect(query, options)
|
33
|
+
|
34
|
+
# First we check the response
|
35
|
+
expect(response).not_to be nil
|
36
|
+
|
37
|
+
rescue => e
|
38
|
+
puts e.inspect
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'detect (batch)' do
|
45
|
+
|
46
|
+
begin
|
47
|
+
|
48
|
+
# Declare the Client instance passing in the authentication parameters
|
49
|
+
@client = LanguageLayer::Client.new(ENV['ACCESS_KEY'])
|
50
|
+
|
51
|
+
# Set the query to detect
|
52
|
+
query = [
|
53
|
+
'I like apples & oranges.'
|
54
|
+
]
|
55
|
+
|
56
|
+
# We declare the options
|
57
|
+
options = LanguageLayer::BatchOptions.new()
|
58
|
+
|
59
|
+
# We make the call to detect
|
60
|
+
response = @client.batch(query, options)
|
61
|
+
|
62
|
+
# First we check the response
|
63
|
+
expect(response).not_to be nil
|
64
|
+
|
65
|
+
rescue => e
|
66
|
+
puts e.inspect
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'languages' do
|
73
|
+
|
74
|
+
begin
|
75
|
+
|
76
|
+
# Declare the Client instance passing in the authentication parameters
|
77
|
+
@client = LanguageLayer::Client.new(ENV['ACCESS_KEY'])
|
78
|
+
|
79
|
+
# We declare the options
|
80
|
+
options = LanguageLayer::LanguagesOptions.new()
|
81
|
+
|
82
|
+
# We make the call to languages
|
83
|
+
response = @client.languages(options)
|
84
|
+
|
85
|
+
# First we check the response
|
86
|
+
expect(response).not_to be nil
|
87
|
+
|
88
|
+
rescue => e
|
89
|
+
puts e.inspect
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|