YatranslateAPI 0.4.2 → 0.5.2
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 +4 -4
- data/lib/yatranslateAPI/client.rb +24 -36
- data/lib/yatranslateAPI/errors.rb +3 -7
- data/lib/yatranslateAPI/gateway.rb +14 -17
- data/lib/yatranslateAPI/version.rb +1 -1
- data/yatranslateAPI.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e7dd7145a66fc4ffc2815999d638cd481634de0
|
4
|
+
data.tar.gz: 4bc8b9f4f621dcea9448f61345b1f52daf20e467
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f1272e9f5c40ee85449a8b78b4a05f2239c7fc4704067e05e21078746ead31f9d3feea5355b3cd3de9063a3891e56ca2fad86342242a0c6a45d70d410858357
|
7
|
+
data.tar.gz: 97fbea20dd890214a4662410032ec896532b84425dabb133739c82232cd29bd299423c15506ed9f61aede83aed5b0f1f61a011a1494b5329fc53f92da0f00952
|
@@ -1,54 +1,42 @@
|
|
1
|
-
require '
|
1
|
+
require 'YatranslateAPI/gateway'
|
2
2
|
|
3
3
|
module YatranslateAPI
|
4
4
|
class Client
|
5
5
|
|
6
|
-
attr_accessor :
|
6
|
+
attr_accessor :original_phrase
|
7
7
|
|
8
|
-
#
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
@request = YatranslateAPI::Gateway.new(
|
8
|
+
#API_KEY = "trnsl.1.1.20170529T154416Z.047bb57fd7a8b3a1.97c5cf0ccade87d10fb983346cfb5703bf811b3e"
|
9
|
+
#VERSION 1.5
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@request = YatranslateAPI::Gateway.new("trnsl.1.1.20170529T154416Z.047bb57fd7a8b3a1.97c5cf0ccade87d10fb983346cfb5703bf811b3e")
|
13
|
+
@original_phrase = " "
|
13
14
|
end
|
14
15
|
|
15
16
|
def get_langs(lang)
|
16
|
-
|
17
|
-
&key=#{@api_key}"
|
18
|
-
|
19
|
-
@request.send_request('getLangs', params)
|
20
|
-
@request.get_request
|
21
|
-
end
|
17
|
+
@request.generate_request('getLangs?', ui: "#{lang}")
|
22
18
|
|
23
|
-
|
24
|
-
begin
|
25
|
-
puts @request.get_request
|
26
|
-
rescue => exception
|
27
|
-
puts 'Request not found!'
|
28
|
-
end
|
19
|
+
@request.send_request
|
29
20
|
end
|
30
21
|
|
31
22
|
def detect_lang
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
params = "hint=en,ru,de,fr
|
36
|
-
&text=#{phrase}
|
37
|
-
&key=#{@api_key}"
|
38
|
-
|
39
|
-
@request.send_request('detect', params)
|
40
|
-
@request.get_request
|
41
|
-
end
|
23
|
+
@request.generate_request('detect?', hint: "en,ru,de,fr",
|
24
|
+
text: "#{@original_phrase}")
|
25
|
+
@request.send_request
|
42
26
|
end
|
43
27
|
|
44
28
|
def translate(lang)
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
29
|
+
@request.generate_request('translate?', lang: "#{lang}",
|
30
|
+
text: "#{@original_phrase}")
|
31
|
+
@request.send_request
|
32
|
+
end
|
49
33
|
|
50
|
-
|
51
|
-
|
34
|
+
def get_result
|
35
|
+
begin
|
36
|
+
@request.get_request
|
37
|
+
rescue => exception
|
38
|
+
puts 'Request not found!'
|
39
|
+
end
|
52
40
|
end
|
53
41
|
end
|
54
|
-
end
|
42
|
+
end
|
@@ -5,15 +5,11 @@ module YatranslateAPI
|
|
5
5
|
class Errors
|
6
6
|
|
7
7
|
def status_check(status)
|
8
|
-
|
9
|
-
puts "Successful!"
|
10
|
-
else
|
11
|
-
puts "Failed!"
|
12
|
-
#print_error(status)
|
13
|
-
status.status
|
14
|
-
end
|
8
|
+
status.status
|
15
9
|
end
|
16
10
|
|
11
|
+
protected
|
12
|
+
|
17
13
|
def print_error(status)
|
18
14
|
puts JSON.parse(status.content)['message']
|
19
15
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'YatranslateAPI/errors'
|
2
2
|
require 'json'
|
3
3
|
require 'httpclient'
|
4
4
|
|
@@ -8,12 +8,13 @@ module YatranslateAPI
|
|
8
8
|
def initialize(api_key)
|
9
9
|
@client = HTTPClient.new
|
10
10
|
@error = YatranslateAPI::Errors.new
|
11
|
-
@api_key = api_key
|
11
|
+
@api_key = { key: "#{api_key}" }
|
12
|
+
@base_url = "https://translate.yandex.net/api/v1.5/tr.json/"
|
12
13
|
end
|
13
14
|
|
14
|
-
def generate_request(
|
15
|
-
@url =
|
16
|
-
|
15
|
+
def generate_request(method, params = {})
|
16
|
+
@url = @base_url + method + hash_to_param(params.merge! @api_key)
|
17
|
+
|
17
18
|
@body = {
|
18
19
|
"Host": "translate.yandex.net",
|
19
20
|
"Accept": "*/*",
|
@@ -21,25 +22,21 @@ module YatranslateAPI
|
|
21
22
|
"Content-Type": "application/x-www-form-urlencoded"
|
22
23
|
}
|
23
24
|
end
|
24
|
-
|
25
|
-
def send_request(type, params)
|
26
|
-
generate_request(type, params)
|
27
25
|
|
26
|
+
def send_request
|
28
27
|
uri = URI.parse(@url)
|
29
28
|
@res = @client.post(uri, @body.to_json)
|
30
29
|
@error.status_check(@res)
|
31
30
|
end
|
32
31
|
|
33
32
|
def get_request
|
34
|
-
@
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
@content_string['lang']
|
42
|
-
end
|
33
|
+
@res.content
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def hash_to_param(hash)
|
39
|
+
hash.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&")
|
43
40
|
end
|
44
41
|
end
|
45
42
|
end
|
data/yatranslateAPI.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["shepelevgeorg@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = "This is an example!"
|
13
|
-
spec.description = "
|
13
|
+
spec.description = "Gem for Yandex translate API"
|
14
14
|
spec.homepage = "https://github.com/GeorSh/yatranslateAPI"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: YatranslateAPI
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GeorSh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
|
-
description:
|
55
|
+
description: Gem for Yandex translate API
|
56
56
|
email:
|
57
57
|
- shepelevgeorg@gmail.com
|
58
58
|
executables: []
|