languagedetection 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +4 -0
  4. data/CODE_OF_CONDUCT.md +49 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +41 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/example/example_batch.rb +34 -0
  12. data/example/example_detect.rb +30 -0
  13. data/example/example_languages.rb +27 -0
  14. data/language_detection.gemspec +32 -0
  15. data/lib/language_detection/batch/batch_exception.rb +15 -0
  16. data/lib/language_detection/batch/batch_options.rb +19 -0
  17. data/lib/language_detection/batch/batch_request.rb +16 -0
  18. data/lib/language_detection/batch/batch_response.rb +17 -0
  19. data/lib/language_detection/detect/detect_exception.rb +15 -0
  20. data/lib/language_detection/detect/detect_options.rb +20 -0
  21. data/lib/language_detection/detect/detect_request.rb +16 -0
  22. data/lib/language_detection/detect/detect_response.rb +17 -0
  23. data/lib/language_detection/languages/languages_exception.rb +15 -0
  24. data/lib/language_detection/languages/languages_options.rb +16 -0
  25. data/lib/language_detection/languages/languages_request.rb +16 -0
  26. data/lib/language_detection/languages/languages_response.rb +15 -0
  27. data/lib/language_detection/missing_argument_exception.rb +13 -0
  28. data/lib/language_detection/version.rb +3 -0
  29. data/lib/language_detection.rb +161 -0
  30. data/out/production/language_detection/language_detection/batch/batch_exception.rb +15 -0
  31. data/out/production/language_detection/language_detection/batch/batch_options.rb +19 -0
  32. data/out/production/language_detection/language_detection/batch/batch_request.rb +16 -0
  33. data/out/production/language_detection/language_detection/batch/batch_response.rb +17 -0
  34. data/out/production/language_detection/language_detection/detect/detect_exception.rb +15 -0
  35. data/out/production/language_detection/language_detection/detect/detect_options.rb +20 -0
  36. data/out/production/language_detection/language_detection/detect/detect_request.rb +16 -0
  37. data/out/production/language_detection/language_detection/detect/detect_response.rb +17 -0
  38. data/out/production/language_detection/language_detection/languages/languages_exception.rb +15 -0
  39. data/out/production/language_detection/language_detection/languages/languages_options.rb +16 -0
  40. data/out/production/language_detection/language_detection/languages/languages_request.rb +16 -0
  41. data/out/production/language_detection/language_detection/languages/languages_response.rb +15 -0
  42. data/out/production/language_detection/language_detection/missing_argument_exception.rb +13 -0
  43. data/out/production/language_detection/language_detection/version.rb +3 -0
  44. data/out/production/language_detection/language_detection.rb +161 -0
  45. data/out/test/language_detection/language_detection_spec.rb +99 -0
  46. data/out/test/language_detection/spec_helper.rb +2 -0
  47. 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,15 @@
1
+ require "language_detection/version"
2
+
3
+ module LanguageLayer
4
+
5
+ class BatchException < Exception
6
+
7
+ attr_accessor :error
8
+
9
+ def initialize(error)
10
+ self.error = error
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,19 @@
1
+ require "language_detection/version"
2
+
3
+ module LanguageLayer
4
+
5
+ class BatchOptions
6
+
7
+ include Hashable
8
+
9
+ attr_accessor :access_key
10
+
11
+ attr_accessor :query
12
+
13
+ def initialize()
14
+ @query = nil
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,16 @@
1
+ require "hashable"
2
+
3
+ module LanguageLayer
4
+
5
+ class BatchRequest
6
+
7
+ include Hashable
8
+
9
+ attr_accessor :query
10
+
11
+ def initialize(query = {})
12
+ self.query = query;
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ require "language_detection/version"
2
+
3
+ module LanguageLayer
4
+
5
+ class BatchResponse
6
+
7
+ SUCCESS_EXPR = 'success'
8
+ ERROR_EXPR = 'error'
9
+
10
+ def bar
11
+ SUCCESS_EXPR
12
+ ERROR_EXPR
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,15 @@
1
+ require "language_detection/version"
2
+
3
+ module LanguageLayer
4
+
5
+ class DetectException < Exception
6
+
7
+ attr_accessor :error
8
+
9
+ def initialize(error)
10
+ self.error = error
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,20 @@
1
+ require "language_detection/version"
2
+
3
+ module LanguageLayer
4
+
5
+ class DetectOptions
6
+
7
+ include Hashable
8
+
9
+ attr_accessor :access_key
10
+
11
+ attr_accessor :query
12
+
13
+
14
+ def initialize()
15
+ @query = nil
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,16 @@
1
+ require "hashable"
2
+
3
+ module LanguageLayer
4
+
5
+ class DetectRequest
6
+
7
+ include Hashable
8
+
9
+ attr_accessor :query
10
+
11
+ def initialize(query = {})
12
+ self.query = query;
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ require "language_detection/version"
2
+
3
+ module LanguageLayer
4
+
5
+ class DetectResponse
6
+
7
+ SUCCESS_EXPR = 'success'
8
+ ERROR_EXPR = 'error'
9
+
10
+ def bar
11
+ SUCCESS_EXPR
12
+ ERROR_EXPR
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,15 @@
1
+ require "language_detection/version"
2
+
3
+ module LanguageLayer
4
+
5
+ class LanguagesException < Exception
6
+
7
+ attr_accessor :error
8
+
9
+ def initialize(error)
10
+ self.error = error
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,16 @@
1
+ require "language_detection/version"
2
+
3
+ module LanguageLayer
4
+
5
+ class LanguagesOptions
6
+
7
+ include Hashable
8
+
9
+ attr_accessor :access_key
10
+
11
+ def initialize()
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+ require "hashable"
2
+
3
+ module LanguageLayer
4
+
5
+ class LanguagesRequest
6
+
7
+ include Hashable
8
+
9
+ attr_accessor :query
10
+
11
+ def initialize(query = {})
12
+ self.query = query;
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ require "language_detection/version"
2
+
3
+ module LanguageLayer
4
+
5
+ class LanguagesResponse
6
+
7
+ SUCCESS_EXPR = 'success'
8
+
9
+ def bar
10
+ SUCCESS_EXPR
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,13 @@
1
+ module LanguageLayer
2
+
3
+ class MissingArgumentException < Exception
4
+
5
+ attr_accessor :argument
6
+
7
+ def initialize(argument)
8
+ self.argument = argument
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,3 @@
1
+ module LanguageLayer
2
+ VERSION = "0.1.0"
3
+ 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
+
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'language_detection'