dictionary_api 0.1.11

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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjYyMDMxN2JlZDQ0MzJmNGU0OWJmY2YxMmY4YmUwYmM0ZGQxZWE0Mg==
5
+ data.tar.gz: !binary |-
6
+ MTM3MGQ2ZTdhNDMyZmUzOGNmOWJhZTk4ZmRlZGVjMGYzZTczZWJhNA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZDY2MWMwOTc0NWVkYjBkMmUyMzFlNDYzNWM5YTY1YzgzYjVjZGMwYjAwNzU0
10
+ ZjgxMzE2ZTc2OTI2NmU1NzY5YmIyMTI1ZTUzMjZkNTlhZmNhMWM3YWJlMjk3
11
+ OTFhMzU5ZDQ1YTQ0ZDZhZjNlZWFhZWZlYWY2OTNlYmExODFmMTc=
12
+ data.tar.gz: !binary |-
13
+ MzAwYzBmMDhjZTNiNDkwOTQ1NDNmNjg0Yzg0NTk2OTFhNTJiOTdiM2EwODA0
14
+ NDRhZTc5ZTZkZmZlZjJiZmJjZjE4ODdhOTNkZjU4Y2JiMzVhNzgxZmNjZjJm
15
+ Y2FmZWJiNzA1MDNmMWU1NzMxYTI2MzA0OGEwNjUwMWJmNzhmMDM=
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+ require 'dictionary_api/client'
3
+ require 'dictionary_api/parser'
4
+
5
+ module DictionaryAPI
6
+
7
+ class DictionaryAPI
8
+
9
+ attr_accessor :api_key
10
+
11
+ def initialize(key)
12
+ @api_key = key
13
+ end
14
+
15
+ def get_langs
16
+ JSON.parse client.execute('getLangs').body
17
+ end
18
+
19
+ def lookup(lang, text)
20
+ text.gsub!(' ', '%20')
21
+ response= JSON.parse client.execute('lookup', {'lang'=>lang, 'text'=>text}).body
22
+ parser = Parser.new
23
+ parser.parse(response)
24
+ end
25
+
26
+ protected
27
+
28
+ def client
29
+ @client ||= Client.new(api_key)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ class Article
2
+ attr_accessor :text, :position, :transcription, :translation
3
+ end
@@ -0,0 +1,39 @@
1
+ require 'httparty'
2
+
3
+ module DictionaryAPI
4
+
5
+ class DictionaryAPI::Client
6
+
7
+ attr_accessor :api_key
8
+
9
+ BASE_URL = "https://dictionary.yandex.net/api/v1/dicservice.json/"
10
+
11
+ def initialize(key)
12
+ @api_key = key
13
+ end
14
+
15
+ def execute(path, options = {})
16
+ response = HTTParty.post(self.build_url(path, options))
17
+ self.check_errors(response)
18
+ end
19
+
20
+ protected
21
+
22
+ def check_errors(response)
23
+ case response.code
24
+ when 200 then response
25
+ when 401 then raise 'Invalid API key'
26
+ when 402 then raise 'API key blocked'
27
+ when 403 then raise 'Daily limit of requests is exceeded'
28
+ when 413 then raise 'Text too long'
29
+ when 501 then raise 'Translate direction is not supported'
30
+ end
31
+ end
32
+
33
+ def build_url(path, options = {})
34
+ request = BASE_URL + path + "?key=#{@api_key}"
35
+ options.each_pair { |key, value| request += '&' + key + '=' + value }
36
+ request
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,43 @@
1
+ require 'dictionary_api/article'
2
+ require 'dictionary_api/translation_article'
3
+
4
+ module DictionaryAPI
5
+
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
+ end
@@ -0,0 +1,3 @@
1
+ class TranslationArticle
2
+ attr_accessor :text, :position, :gender, :animated, :synonym, :meaning, :example, :aspect
3
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe DictionaryAPI do
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
+
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
+
22
+ it "check errors" do
23
+ dict = DictionaryAPI.new('q9fjq9fjioj')
24
+ expect{ dict.lookup('en-ru', 'time') }.to raise_error(Exception)
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Client do
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
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Parser do
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
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dictionary_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.11
5
+ platform: ruby
6
+ authors:
7
+ - spitsman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-10 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
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.13'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.8'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.8'
83
+ description:
84
+ email:
85
+ - sanspits@yandex.ru
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/dictionary_api.rb
91
+ - lib/dictionary_api/article.rb
92
+ - lib/dictionary_api/client.rb
93
+ - lib/dictionary_api/parser.rb
94
+ - lib/dictionary_api/translation_article.rb
95
+ - spec/dictionary_api_spec.rb
96
+ - spec/lib/client_spec.rb
97
+ - spec/lib/parser_spec.rb
98
+ homepage: https://github.com/Spitsman/dictionary_api
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.4.8
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: ! '''getLangs and lookup methods'''
122
+ test_files:
123
+ - spec/dictionary_api_spec.rb
124
+ - spec/lib/parser_spec.rb
125
+ - spec/lib/client_spec.rb