yandex_dictionary_api 0.0.1 → 0.0.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.
@@ -7,20 +7,43 @@ module YandexDictionaryApi
7
7
 
8
8
  class ApiInterface
9
9
 
10
- def initialize( api_key )
10
+ def initialize(api_key)
11
11
  @client = YandexDictionaryApi::ApiClient.new( api_key )
12
12
  end
13
13
 
14
14
  #Executes getLangs (list of supported languages) request to YandexDictionaryApi api
15
15
  def get_langs
16
- response = @client.execute_get_langs
17
- JSON.parse( response.body )
16
+ response = @client.execute("getLangs", Hash.new)
17
+ self.check_response(response)
18
+ JSON.parse(response.body)
18
19
  end
19
20
 
20
21
  #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 )
22
+ def lookup(params_hash)
23
+ response = @client.execute("lookup", params_hash)
24
+ self.check_response(response)
25
+ JSON.parse(response.body)
26
+ end
27
+
28
+ protected
29
+
30
+ def check_response(response)
31
+ case response.code
32
+ when 200
33
+ response
34
+ when 401
35
+ raise ApiError.new("Invalid api key.")
36
+ when 402
37
+ raise ApiError.new("Spicified api key is blocked.")
38
+ when 403
39
+ raise ApiError.new("The daily limit on the number of requests exceeded.")
40
+ when 413
41
+ raise ApiError.new("Limit on the size of text exceeded.")
42
+ when 501
43
+ raise ApiError.new("Spicified direction of translation is not supported.")
44
+ else
45
+ raise ApiError.new("Try again later.")
46
+ end
24
47
  end
25
48
  end
26
49
  end
@@ -1,31 +1,26 @@
1
- gem "httpclient"
2
1
  require "httpclient"
3
2
 
4
3
  module YandexDictionaryApi
5
4
 
6
5
  class ApiClient
7
6
 
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
7
+ BASE_URI = "https://dictionary.yandex.net/api/v1/dicservice.json"
10
8
 
11
- def initialize( api_key )
9
+ def initialize(api_key)
12
10
  @http_client = HTTPClient.new
13
11
  @api_key = api_key
14
12
  end
15
13
 
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 )
14
+ def execute(str_req_type, params_hash)
15
+ str_request = BASE_URI + "/#{str_req_type}?"
16
+ params_hash["key"] = @api_key
17
+ #str_request = self.add_parameters_to_request(str_request, params_hash)
18
+ self.exec_request_http(str_request, params_hash)
24
19
  end
25
20
 
26
21
  protected
27
22
 
28
- def add_parameters_to_request( str_request, params_hash )
23
+ def add_parameters_to_request(str_request, params_hash)
29
24
  str_res = str_request + "key=#{@api_key}"
30
25
  params_hash.each do |(key, value)|
31
26
  str_res += "&#{key}=#{value}"
@@ -33,9 +28,8 @@ module YandexDictionaryApi
33
28
  str_res
34
29
  end
35
30
 
36
- def exec_request_http( str_request )
37
- response = @http_client.get( str_request )
38
- ApiError.check( response )
31
+ def exec_request_http(str_uri, params_hash)
32
+ @http_client.post(str_uri, params_hash)
39
33
  end
40
34
  end
41
35
 
@@ -2,24 +2,6 @@ module YandexDictionaryApi
2
2
 
3
3
  class ApiError < StandardError
4
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
5
  end
24
6
 
25
7
  end
@@ -1,3 +1,3 @@
1
1
  module YandexDictionaryApi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe YandexDictionaryApi do
4
4
 
5
5
  before do
6
- $interface = YandexDictionaryApi::ApiInterface.new(ENV['YANDEX_API_KEY'])
6
+ $interface = YandexDictionaryApi::ApiInterface.new("dict.1.1.20140904T085142Z.bc794363dea5e8da.bcafbef2c018fc447d8de5c67ff8e2dd7f57481f")
7
7
  end
8
8
 
9
9
  it "should return a list of supported languages" do
@@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "httpclient"
24
- spec.add_development_dependency "json"
23
+ spec.add_dependency "httpclient"
24
+ spec.add_dependency "json"
25
25
  spec.add_development_dependency "rspec"
26
26
  end
27
27
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: yandex_dictionary_api
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - IronSerj
@@ -51,7 +51,7 @@ dependencies:
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  name: httpclient
54
- type: :development
54
+ type: :runtime
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  none: false
57
57
  requirements:
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  name: json
70
- type: :development
70
+ type: :runtime
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements: