yandex-translator-api 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46d9608bcfdc944f75b0b5f4df553b3db20cef4a
4
- data.tar.gz: ead7ffaf10b823fa0fae61c843374c37fedcef42
3
+ metadata.gz: f9c69bfa8ce26b71f8dbce328bb54b5ec6a31e32
4
+ data.tar.gz: ce99603a1859fafc89485608a750b7ae7383a3e6
5
5
  SHA512:
6
- metadata.gz: 67c3ed3b8634f8f1108e6d0a495f97555fe6a16ee84c9308a2d23cb7778385e8ac6c9bc5cff63c8f2dba55a888320f0cb23f1240b807c229601008a30f3681a2
7
- data.tar.gz: 43d444a507b52c87a0f588c7421be5835994360fb51b0a40f8f5cc325a1b7558d4e4b938a5ae3c0d4fddcc781a5ffe62b4182a63e5c87a2bf3a2f823f698e027
6
+ metadata.gz: 9c20185398da0e5e784c8dbf33ff91ed47f62853afdec1939f920f94967579b615c1abdc7b38a44aa6745518c5acb8626dc06005f7747e19903f17cc89196790
7
+ data.tar.gz: e80d06c8d20eb76498693fc30e5e6a22932d35a354170ccd5a4e24c7caeead36ad5bc5b79dc863bf4e31c794db5b3632a94cd0888d70eb5e886f4d55f64cacb0
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
+ --require spec_helper
1
2
  --format documentation
2
3
  --color
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ AllCops:
2
+ Exclude:
3
+ - bin/*
4
+
5
+ Metrics/AbcSize:
6
+ Enabled: false
7
+
8
+ Metrics/BlockLength:
9
+ Enabled: false
10
+
11
+ Metrics/ClassLength:
12
+ Enabled: false
13
+
14
+ Metrics/LineLength:
15
+ Enabled: false
16
+
17
+ Metrics/MethodLength:
18
+ Enabled: false
19
+
20
+ Metrics/ModuleLength:
21
+ Enabled: false
22
+
23
+ Metrics/CyclomaticComplexity:
24
+ Max: 20
25
+
26
+ Layout/IndentationWidth:
27
+ Width: 4
28
+
29
+ Layout/CaseIndentation:
30
+ EnforcedStyle: case
31
+ SupportedStyles:
32
+ - case
33
+ - end
34
+ IndentOneStep: true
35
+
36
+ Layout/AlignHash:
37
+ Enabled: false
38
+
39
+ Style/GuardClause:
40
+ Enabled: false
41
+
42
+ Style/FileName:
43
+ Enabled: false
44
+
45
+ Style/FormatStringToken:
46
+ EnforcedStyle: template
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
3
5
  group :test do
4
6
  gem 'rspec'
5
7
  gem 'webmock'
data/README.md CHANGED
@@ -23,7 +23,8 @@ Or install it yourself as:
23
23
  Create Translator object.
24
24
 
25
25
  ```ruby
26
- translator = Yandex::Translator.new('api_key')
26
+ require 'yandex'
27
+ translator = Yandex::Translator.new(api_key: 'api_key')
27
28
  ```
28
29
 
29
30
  ### Langs
@@ -36,6 +37,18 @@ Request for getting possible translation directions is #langs.
36
37
 
37
38
  It returns array of possible translations or error.
38
39
 
40
+ #### Responces
41
+
42
+ ```ruby
43
+ {
44
+ "dirs"=>[
45
+ "az-ru",
46
+ "be-bg",
47
+ "en-ru"
48
+ ]
49
+ }
50
+ ```
51
+
39
52
  ### Detect
40
53
 
41
54
  Request for detecting language of the text is #detect.
@@ -48,6 +61,20 @@ Request for detecting language of the text is #detect.
48
61
  text - text for detecting, required param
49
62
  hint - list of possible languages
50
63
 
64
+ #### Responces
65
+
66
+ ```ruby
67
+ {
68
+ "code"=>200,
69
+ "lang"=>"en"
70
+ }
71
+ ```
72
+ ```ruby
73
+ {
74
+ :error=>"Bad Request"
75
+ }
76
+ ```
77
+
51
78
  ### Translate
52
79
 
53
80
  Request for translating of the text is #translate.
@@ -61,6 +88,21 @@ Request for translating of the text is #translate.
61
88
  from - language of the text
62
89
  to - translating direction, required param
63
90
 
91
+ #### Responces
92
+
93
+ ```ruby
94
+ {
95
+ "code"=>200,
96
+ "lang"=>"en-ru",
97
+ "text"=>["привет"]
98
+ }
99
+ ```
100
+ ```ruby
101
+ {
102
+ :error=>"Bad Request"
103
+ }
104
+ ```
105
+
64
106
  ## Contributing
65
107
 
66
108
  Bug reports and pull requests are welcome on GitHub at https://github.com/kortirso/yandex-translator-api.
data/Rakefile CHANGED
@@ -3,4 +3,4 @@ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "yandex"
3
+ require 'bundler/setup'
4
+ require 'yandex'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "yandex"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
data/lib/yandex.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative 'yandex/version'
2
2
  require_relative 'yandex/translator'
3
3
 
4
+ # Top-level docs
4
5
  module Yandex
5
6
  end
@@ -1,46 +1,41 @@
1
+ require 'httparty'
1
2
  require_relative 'translator/langs'
2
3
  require_relative 'translator/detect'
3
4
  require_relative 'translator/translate'
4
- require 'net/http'
5
- require 'json'
6
5
 
7
6
  module Yandex
7
+ # Translator class
8
8
  class Translator
9
+ class YandexFailure < StandardError; end
10
+
11
+ include HTTParty
9
12
  include Yandex::Translator::Langs
10
13
  include Yandex::Translator::Detect
11
14
  include Yandex::Translator::Translate
12
15
 
13
- class YandexFailure < StandardError; end
16
+ base_uri 'https://translate.yandex.net/api/v1.5/tr.json'
17
+ format :json
14
18
 
15
- attr_reader :api_key, :base_uri
19
+ attr_reader :api_key
16
20
 
17
- def initialize(api_key = '')
18
- @api_key = api_key
19
- @base_uri = 'https://translate.yandex.net/api/v1.5/tr.json'
21
+ def initialize(args = {})
22
+ @api_key = args[:api_key]
20
23
  end
21
24
 
22
- private
23
-
24
- def call(address, args = {})
25
- response = http_request(address, args)
25
+ private def request(uri, args = {})
26
+ response = self.class.post(uri, query: args.merge(key: api_key))
26
27
  check_response(response)
27
28
  response
28
29
  end
29
30
 
30
- def http_request(address, args)
31
- uri = URI("#{base_uri}#{address}")
32
- req = Net::HTTP::Post.new(uri)
33
- req.set_form_data(args.merge(key: api_key))
34
- Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
35
- end
36
-
37
- def check_response(response)
38
- if response.code != '200'
39
- raise YandexFailure, JSON.parse(response.body)['message']
31
+ private def check_response(response)
32
+ if response.code != 200
33
+ error_message = response.message.empty? ? response.code : response.message
34
+ raise YandexFailure, error_message
40
35
  end
41
36
  end
42
37
 
43
- def check_args(args)
38
+ private def check_args(args)
44
39
  args.each do |arg_name, value|
45
40
  if value.nil? || !value.is_a?(String)
46
41
  raise YandexFailure, "Invalid argument '#{arg_name}', must be string"
@@ -48,4 +43,4 @@ module Yandex
48
43
  end
49
44
  end
50
45
  end
51
- end
46
+ end
@@ -1,15 +1,15 @@
1
1
  module Yandex
2
2
  class Translator
3
+ # Detection module
3
4
  module Detect
4
-
5
+ # Detect language
5
6
  def detect(args = {})
6
7
  check_args(text: args[:text])
7
- response = call('/detect', { hint: args[:hint], text: args[:text] })
8
- JSON.parse(response.body)['lang']
8
+ response = request('/detect', hint: args[:hint], text: args[:text])
9
+ response.parsed_response
9
10
  rescue YandexFailure => ex
10
11
  return { error: ex.message }
11
12
  end
12
-
13
13
  end
14
14
  end
15
- end
15
+ end
@@ -1,14 +1,14 @@
1
1
  module Yandex
2
2
  class Translator
3
+ # Available languages module
3
4
  module Langs
4
-
5
+ # Get list of available languages for translation
5
6
  def langs
6
- response = call('/getLangs')
7
- JSON.parse(response.body)['dirs']
7
+ response = request('/getLangs')
8
+ response.parsed_response
8
9
  rescue YandexFailure => ex
9
10
  return { error: ex.message }
10
11
  end
11
-
12
12
  end
13
13
  end
14
- end
14
+ end
@@ -1,16 +1,16 @@
1
1
  module Yandex
2
2
  class Translator
3
+ # Translate module
3
4
  module Translate
4
-
5
+ # Translate text
5
6
  def translate(args = {})
6
7
  check_args(text: args[:text], to: args[:to])
7
8
  translate_direction = args[:from].nil? ? args[:to] : "#{args[:from]}-#{args[:to]}"
8
- response = call('/translate', { lang: translate_direction, text: args[:text] })
9
- JSON.parse(response.body)['text']
9
+ response = request('/translate', lang: translate_direction, text: args[:text])
10
+ response.parsed_response
10
11
  rescue YandexFailure => ex
11
12
  return { error: ex.message }
12
13
  end
13
-
14
14
  end
15
15
  end
16
- end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Yandex
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'.freeze
3
3
  end
data/yandex.gemspec CHANGED
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'yandex/version'
@@ -23,5 +24,6 @@ Gem::Specification.new do |spec|
23
24
 
24
25
  spec.add_development_dependency 'bundler', '~> 1.14'
25
26
  spec.add_development_dependency 'rake', '~> 10.0'
26
- spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'rubocop', '~> 0.49.1'
28
+ spec.add_dependency 'httparty', '~> 0.15'
27
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex-translator-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Bogdanov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-17 00:00:00.000000000 Z
11
+ date: 2017-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,19 +39,33 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: rubocop
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: 0.49.1
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: 0.49.1
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.15'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.15'
55
69
  description: Gem for sending words for translation to Yandex Translator API
56
70
  email:
57
71
  - kortirso@gmail.com
@@ -61,6 +75,7 @@ extra_rdoc_files: []
61
75
  files:
62
76
  - ".gitignore"
63
77
  - ".rspec"
78
+ - ".rubocop.yml"
64
79
  - ".travis.yml"
65
80
  - Gemfile
66
81
  - LICENSE.txt
@@ -95,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
110
  version: '0'
96
111
  requirements: []
97
112
  rubyforge_project:
98
- rubygems_version: 2.6.11
113
+ rubygems_version: 2.6.14
99
114
  signing_key:
100
115
  specification_version: 4
101
116
  summary: Text translating by Yandex Translator API