yandex_dictionary_api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/lib/yandex_dictionary_api.rb +26 -0
- data/lib/yandex_dictionary_api/api_client.rb +42 -0
- data/lib/yandex_dictionary_api/api_error.rb +25 -0
- data/lib/yandex_dictionary_api/version.rb +3 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/spec_yandex_dictionary_api.rb +20 -0
- data/yandex_dictionary_api.gemspec +27 -0
- metadata +140 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 IronSerj
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# YandexDictionaryApi
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'yandex_dictionary_api'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install yandex_dictionary_api
|
18
|
+
|
19
|
+
## Exapmle
|
20
|
+
|
21
|
+
i = YandexDictionaryApi::ApiInterface.new("<your_api_key>")
|
22
|
+
i.get_langs
|
23
|
+
params = { "lang" => "en-ru", "text" => "time" }
|
24
|
+
i.lookup( params )
|
25
|
+
|
26
|
+
## Return
|
27
|
+
|
28
|
+
ApiInterface returns classes, created by JSON parser.
|
29
|
+
In case of the bad response from server raises ApiError with suitable message.
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it ( https://github.com/[my-github-username]/yandex_dictionary_api/fork )
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "yandex_dictionary_api/version"
|
2
|
+
require "yandex_dictionary_api/api_client"
|
3
|
+
require "yandex_dictionary_api/api_error"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module YandexDictionaryApi
|
7
|
+
|
8
|
+
class ApiInterface
|
9
|
+
|
10
|
+
def initialize( api_key )
|
11
|
+
@client = YandexDictionaryApi::ApiClient.new( api_key )
|
12
|
+
end
|
13
|
+
|
14
|
+
#Executes getLangs (list of supported languages) request to YandexDictionaryApi api
|
15
|
+
def get_langs
|
16
|
+
response = @client.execute_get_langs
|
17
|
+
JSON.parse( response.body )
|
18
|
+
end
|
19
|
+
|
20
|
+
#Executes lookup (Interpretation and translate) request to yandex dictionary api
|
21
|
+
def lookup( params_hash )
|
22
|
+
response = @client.execute_lookup( params_hash )
|
23
|
+
JSON.parse( response.body )
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
gem "httpclient"
|
2
|
+
require "httpclient"
|
3
|
+
|
4
|
+
module YandexDictionaryApi
|
5
|
+
|
6
|
+
class ApiClient
|
7
|
+
|
8
|
+
GET_LANGS_HTTP_REQUEST_TMPL = "https://dictionary.yandex.net/api/v1/dicservice.json/getLangs?".freeze
|
9
|
+
LOOKUP_HTTP_REQUEST_TMPL = "https://dictionary.yandex.net/api/v1/dicservice.json/lookup?".freeze
|
10
|
+
|
11
|
+
def initialize( api_key )
|
12
|
+
@http_client = HTTPClient.new
|
13
|
+
@api_key = api_key
|
14
|
+
end
|
15
|
+
|
16
|
+
def execute_get_langs
|
17
|
+
str_request = self.add_parameters_to_request(GET_LANGS_HTTP_REQUEST_TMPL, Hash.new)
|
18
|
+
self.exec_request_http( str_request )
|
19
|
+
end
|
20
|
+
|
21
|
+
def execute_lookup( params_hash )
|
22
|
+
str_request = self.add_parameters_to_request(LOOKUP_HTTP_REQUEST_TMPL, params_hash)
|
23
|
+
self.exec_request_http( str_request )
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def add_parameters_to_request( str_request, params_hash )
|
29
|
+
str_res = str_request + "key=#{@api_key}"
|
30
|
+
params_hash.each do |(key, value)|
|
31
|
+
str_res += "&#{key}=#{value}"
|
32
|
+
end
|
33
|
+
str_res
|
34
|
+
end
|
35
|
+
|
36
|
+
def exec_request_http( str_request )
|
37
|
+
response = @http_client.get( str_request )
|
38
|
+
ApiError.check( response )
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module YandexDictionaryApi
|
2
|
+
|
3
|
+
class ApiError < StandardError
|
4
|
+
|
5
|
+
def self.check( response )
|
6
|
+
case response.code
|
7
|
+
when 200
|
8
|
+
response
|
9
|
+
when 401
|
10
|
+
raise ApiError.new("Invalid api key.")
|
11
|
+
when 402
|
12
|
+
raise ApiError.new("Spicified api key is blocked.")
|
13
|
+
when 403
|
14
|
+
raise ApiError.new("The daily limit on the number of requests exceeded.")
|
15
|
+
when 413
|
16
|
+
raise ApiError.new("Limit on the size of text exceeded.")
|
17
|
+
when 501
|
18
|
+
raise ApiError.new("Spicified direction of translation is not supported.")
|
19
|
+
else
|
20
|
+
raise ApiError.new("Try again later.")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe YandexDictionaryApi do
|
4
|
+
|
5
|
+
before do
|
6
|
+
$interface = YandexDictionaryApi::ApiInterface.new(ENV['YANDEX_API_KEY'])
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return a list of supported languages" do
|
10
|
+
res = $interface.get_langs
|
11
|
+
res.include?("en-en")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return a class contains translation and interpretation of the text" do
|
15
|
+
params = { "lang" => "en-ru", "text" => "time" }
|
16
|
+
res = $interface.lookup( params )
|
17
|
+
res["def"][0]["text"] == "time"
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yandex_dictionary_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "yandex_dictionary_api"
|
8
|
+
spec.version = YandexDictionaryApi::VERSION
|
9
|
+
spec.authors = ["IronSerj"]
|
10
|
+
spec.email = ["iron.serj@gmail.com"]
|
11
|
+
spec.summary = %q{Yandex dictionary API realization}
|
12
|
+
spec.description = %q{Yandex dictionary API realization}
|
13
|
+
spec.homepage = "https://github.com/IronSerj/yandex_dictionary_api"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "httpclient"
|
24
|
+
spec.add_development_dependency "json"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
27
|
+
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yandex_dictionary_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- IronSerj
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-09-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
none: false
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.7'
|
21
|
+
name: bundler
|
22
|
+
type: :development
|
23
|
+
requirement: !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.7'
|
29
|
+
prerelease: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
version_requirements: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ~>
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '10.0'
|
37
|
+
name: rake
|
38
|
+
type: :development
|
39
|
+
requirement: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '10.0'
|
45
|
+
prerelease: false
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
name: httpclient
|
54
|
+
type: :development
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
prerelease: false
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
name: json
|
70
|
+
type: :development
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
prerelease: false
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
name: rspec
|
86
|
+
type: :development
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
prerelease: false
|
94
|
+
description: Yandex dictionary API realization
|
95
|
+
email:
|
96
|
+
- iron.serj@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- Gemfile
|
103
|
+
- LICENSE.txt
|
104
|
+
- README.md
|
105
|
+
- Rakefile
|
106
|
+
- lib/yandex_dictionary_api.rb
|
107
|
+
- lib/yandex_dictionary_api/api_client.rb
|
108
|
+
- lib/yandex_dictionary_api/api_error.rb
|
109
|
+
- lib/yandex_dictionary_api/version.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- spec/spec_yandex_dictionary_api.rb
|
112
|
+
- yandex_dictionary_api.gemspec
|
113
|
+
homepage: https://github.com/IronSerj/yandex_dictionary_api
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 1.8.23.2
|
135
|
+
signing_key:
|
136
|
+
specification_version: 3
|
137
|
+
summary: Yandex dictionary API realization
|
138
|
+
test_files:
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
- spec/spec_yandex_dictionary_api.rb
|