dictionary_api 0.1.14 → 0.1.15
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 +8 -8
- data/lib/dictionary_api.rb +23 -21
- data/lib/dictionary_api/article.rb +9 -3
- data/lib/dictionary_api/client.rb +20 -20
- data/lib/dictionary_api/parser.rb +41 -38
- data/lib/dictionary_api/translation_article.rb +9 -3
- data/spec/dictionary_api_spec.rb +20 -20
- data/spec/lib/client_spec.rb +7 -7
- data/spec/lib/parser_spec.rb +9 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NjlkMjQ5YmVmNDRjZmM4Y2M0MWNkZWI0ODNiZTA5NjZjNDU2NzJhNQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZDUzNTUwYWFiZTE3ZDhlNjAwMzY4MjQ1MzBhZTFmYzhkZGI2ZjVkNQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDJkOGVlZTNjNzVkYTY0OWVkMmRjZDMxOTljNGM2NmMyZjE5YmFmZGZjMGVi
|
10
|
+
Zjg2YmQ3NGJmYTVmNGE0NTg1MTgyMzg2MGJhZmY5OGM0OTViZTkzNGVkNjg2
|
11
|
+
Zjc5NmRkZDgyODUzNzEwMjNjY2JhMTg4Njc2MjkyMWE1ZGM4OTQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NDBmMTVkYzk2NGFjNTMyZjExOThkOWRlN2I1ZTQxYzBmZWU4YTI5ZTA3OWQx
|
14
|
+
OThjNjNlMWYzNjBmNmQyMTg3Y2JmYTBkZDZkYzFiYzliNTExOTVkZjFmMzA4
|
15
|
+
MTRjZjQ2NTExMjJjNTQ2MGZlMTIwODczMGM1Y2FkNjc1ZjlhYTk=
|
data/lib/dictionary_api.rb
CHANGED
@@ -4,31 +4,33 @@ require 'dictionary_api/parser'
|
|
4
4
|
|
5
5
|
module DictionaryAPI
|
6
6
|
|
7
|
-
|
7
|
+
class DictionaryAPI
|
8
8
|
|
9
|
-
|
9
|
+
attr_accessor :api_key
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def initialize(key)
|
12
|
+
@api_key = key
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
def get_langs
|
16
|
+
JSON.parse client.execute('getLangs', {key: @api_key}).body
|
17
|
+
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def lookup(lang, text)
|
20
|
+
response = JSON.parse client.execute('lookup', {key: @api_key, lang: lang, text: text}).body
|
21
|
+
parser.parse(response)
|
22
|
+
end
|
23
23
|
|
24
|
-
|
24
|
+
protected
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
def client
|
27
|
+
@client ||= Client.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def parser
|
31
|
+
@parser ||= Parser.new
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
29
36
|
|
30
|
-
def parser
|
31
|
-
@parser ||= Parser.new
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,7 +1,13 @@
|
|
1
1
|
module DictionaryAPI
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
class Article
|
4
|
+
|
5
|
+
attr_accessor :text, :position, :transcription, :translation
|
6
|
+
|
7
|
+
def attributes
|
8
|
+
{text: text, position: position, transcription: transcription}
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
6
12
|
|
7
13
|
end
|
@@ -2,27 +2,27 @@ require 'httpclient'
|
|
2
2
|
|
3
3
|
module DictionaryAPI
|
4
4
|
|
5
|
-
|
5
|
+
class DictionaryAPI::Client
|
6
6
|
|
7
|
-
|
7
|
+
BASE_URL = "https://dictionary.yandex.net/api/v1/dicservice.json/"
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
def execute(path, options = {})
|
10
|
+
response = HTTPClient.post(BASE_URL + path, options)
|
11
|
+
self.check_errors(response)
|
12
|
+
end
|
13
13
|
|
14
|
-
|
14
|
+
protected
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
16
|
+
def check_errors(response)
|
17
|
+
case response.code
|
18
|
+
when 200 then response
|
19
|
+
when 401 then raise 'Invalid API key'
|
20
|
+
when 402 then raise 'API key blocked'
|
21
|
+
when 403 then raise 'Daily limit of requests is exceeded'
|
22
|
+
when 413 then raise 'Text too long'
|
23
|
+
when 501 then raise 'Translate direction is not supported'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -3,41 +3,44 @@ require 'dictionary_api/translation_article'
|
|
3
3
|
|
4
4
|
module DictionaryAPI
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
6
|
+
class DictionaryAPI::Parser
|
7
|
+
|
8
|
+
def parse(response)
|
9
|
+
response['def'].inject([]) { |result, response_el| result << self.build_article(response_el) }
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
def build_article(articles_hash)
|
15
|
+
article = Article.new
|
16
|
+
article.text = articles_hash['text']
|
17
|
+
article.position = articles_hash['pos']
|
18
|
+
article.transcription = articles_hash['ts']
|
19
|
+
article.translation = articles_hash['tr'].inject([]) { |result, el| result << self.build_translation_article(el) }
|
20
|
+
article
|
21
|
+
end
|
22
|
+
|
23
|
+
def build_translation_article(articles_hash)
|
24
|
+
translation_article = TranslationArticle.new
|
25
|
+
translation_article.text = articles_hash['text']
|
26
|
+
translation_article.position = articles_hash['pos']
|
27
|
+
translation_article.synonym = self.build_array(articles_hash['syn']) unless articles_hash['syn'].nil?
|
28
|
+
translation_article.meaning = self.build_array(articles_hash['mean']) unless articles_hash['mean'].nil?
|
29
|
+
translation_article.example = self.build_array(articles_hash['ex']) unless articles_hash['ex'].nil?
|
30
|
+
translation_article.aspect = articles_hash['asp']
|
31
|
+
translation_article
|
32
|
+
end
|
33
|
+
|
34
|
+
def build_array(articles_array)
|
35
|
+
articles_array.inject([]) do |result, element|
|
36
|
+
result << element.to_a.inject([]) do |result, el|
|
37
|
+
result << (el[1].class == Array ? self.build_array(el[1]) : el[1])
|
38
|
+
end
|
39
|
+
result.flatten!
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
|
@@ -1,7 +1,13 @@
|
|
1
1
|
module DictionaryAPI
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
class TranslationArticle
|
4
|
+
|
5
|
+
attr_accessor :text, :position, :gender, :animated, :synonym, :meaning, :example, :aspect
|
6
|
+
|
7
|
+
def attributes
|
8
|
+
{text: text, position: position, gender: gender, animated: animated, synonym: synonym, meaning: meaning, example: example, aspect: aspect}
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
6
12
|
|
7
13
|
end
|
data/spec/dictionary_api_spec.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe DictionaryAPI do
|
4
|
-
|
5
|
-
it "get_langs" do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
|
5
|
+
it "get_langs" do
|
6
|
+
dict = DictionaryAPI.new('dict.1.1.20150814T100205Z.a0d27651d642b1d1.93ce6ba9cd891aada1fb98d47b6cd89c15a32f2e')
|
7
|
+
dict.get_langs.size.should eq 86
|
8
|
+
dict.get_langs.should include('en-ru')
|
9
|
+
dict.get_langs.should include('ru-en')
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
it "lookup" do
|
13
|
+
dict = DictionaryAPI.new('dict.1.1.20150814T100205Z.a0d27651d642b1d1.93ce6ba9cd891aada1fb98d47b6cd89c15a32f2e')
|
14
|
+
response = dict.lookup('en-ru', 'time')
|
15
|
+
response.size.should eq 3
|
16
|
+
response[0].text.should_not be_nil
|
17
|
+
response[0].position.should_not be_nil
|
18
|
+
response[0].transcription.should_not be_nil
|
19
|
+
response[0].translation.should_not be_nil
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
it "check errors" do
|
23
|
+
dict = DictionaryAPI.new('q9fjq9fjioj')
|
24
|
+
expect{ dict.lookup('en-ru', 'time') }.to raise_error(Exception)
|
25
|
+
end
|
26
26
|
end
|
data/spec/lib/client_spec.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Client do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
|
5
|
+
it 'execute' do
|
6
|
+
key = 'dict.1.1.20150814T100205Z.a0d27651d642b1d1.93ce6ba9cd891aada1fb98d47b6cd89c15a32f2e'
|
7
|
+
client = Client.new(key)
|
8
|
+
JSON.parse(client.execute('getLangs').body).size.should eq 86
|
9
|
+
end
|
10
|
+
|
11
11
|
end
|
data/spec/lib/parser_spec.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Parser do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
|
5
|
+
it 'parse' do
|
6
|
+
key = 'dict.1.1.20150814T100205Z.a0d27651d642b1d1.93ce6ba9cd891aada1fb98d47b6cd89c15a32f2e'
|
7
|
+
client = Client.new(key)
|
8
|
+
response = JSON.parse(client.execute('lookup', ['en-ru', 'time']).body)
|
9
|
+
parser = Parser.new
|
10
|
+
parser.parse(response).size.should eq 3
|
11
|
+
end
|
12
|
+
|
13
13
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dictionary_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- spitsman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|