easy_translate 0.0.3 → 0.0.4

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.
@@ -18,33 +18,7 @@ module EasyTranslate
18
18
  json = api_call "#{API_DETECT_PATH}?#{base_params(text, options)}&q=#{URI.escape text}"
19
19
  json['responseData']['language']
20
20
  end
21
-
22
- # Translate a string using Google Translate
23
- # Optional parameters:
24
- # :key - API key for google language (defaults to nil)
25
- # :host_language - language (defaults to 'en') (symbol/string)
26
- # :user_ip - the ip of the end user - will help not be mistaken for abuse
27
- # :from - the language to translate from (symbol/string)
28
- # :html - boolean indicating whether the text you're translating is HTML or plaintext
29
- # Required Parameters:
30
- # the text to translate
31
- # :to - the language to translate to (symbol/string)
32
- # Returns:
33
- # the translated string
34
- def self.translate(text, options)
35
- # translate params if necessary
36
- to_lang = get_language(options[:to])
37
- from_lang = get_language(options[:from])
38
- # make the call
39
- path = "#{API_TRANSLATE_PATH}?#{base_params(text, options)}"
40
- path << '&format=' << (options[:html] ? 'html' : 'text')
41
- path << '&q=' << URI.escape(text)
42
- path << '&langpair=' << URI.escape("#{from_lang}|#{to_lang}")
43
- # get the proper response and return
44
- json = api_call path
45
- json['responseData']['translatedText']
46
- end
47
-
21
+
48
22
  # Translate batches of string using Google Translate
49
23
  # Optional parameters:
50
24
  # :key - API key for google language (defaults to nil)
@@ -56,8 +30,13 @@ module EasyTranslate
56
30
  # the text(s) to translate (string/array)
57
31
  # :to - the language(s) to translate to (symbol/string/array)
58
32
  # Returns:
59
- # an array of the translated strings
60
- def self.translate_batch(text, options)
33
+ # string / an array of the translated strings
34
+ def self.translate(text, options)
35
+ # TODO failing because we run options[:to] through get langauge and it strips it to nil
36
+
37
+ # what type of call is this?
38
+ multi_call = options[:to].class == Array || text.class == Array
39
+ all_multi_call = options[:to].class == Array && text.class == Array
61
40
  # translate params if necessary
62
41
  to_lang = (options[:to].class == Array) ? options[:to].map { |to| get_language(to) } : get_language(options[:to])
63
42
  from_lang = get_language(options[:from])
@@ -67,17 +46,23 @@ module EasyTranslate
67
46
  # for each to language, put all of the q's
68
47
  to_lang.each do |tol|
69
48
  escaped_lang_pair = URI.escape "#{from_lang}|#{tol}"
70
- text.each do |t|
49
+ text.each do |t|
71
50
  path << "&q=#{URI.escape t}"
72
51
  path << "&langpair=#{escaped_lang_pair}"
73
52
  end
74
53
  end
54
+ # TODO cleanup
75
55
  # get the proper response and return
76
56
  json = api_call path
77
- if json['responseData'].class == Array
78
- translations = json['responseData'].map { |j| j['responseData']['translatedText'] }
57
+ # single argument is returned
58
+ if !multi_call
59
+ single_single(json)
60
+ # array should be returned
61
+ elsif multi_call && !all_multi_call
62
+ single_multiple(json)
63
+ # the big badass case
79
64
  else
80
- [json['responseData']['translatedText']]
65
+ multiple_multiple(json, options[:to].count)
81
66
  end
82
67
  end
83
68
 
@@ -86,6 +71,30 @@ module EasyTranslate
86
71
  end
87
72
 
88
73
  private
74
+
75
+ def self.multiple_multiple(json, translation_count)
76
+ if json['responseData'].class == Hash
77
+ [[json['responseData']['translatedText']]]
78
+ else
79
+ translations = []
80
+ responseData = json['responseData'].map { |r| r['responseData']['translatedText'] }
81
+ per_bucket = responseData.count / translation_count # should always be integer
82
+ 0.upto(translation_count - 1) { |i| translations[i] = responseData.slice(i * per_bucket, per_bucket) }
83
+ end
84
+ translations
85
+ end
86
+
87
+ def self.single_multiple(json)
88
+ if json['responseData'].class == Hash
89
+ [json['responseData']['translatedText']]
90
+ else
91
+ json['responseData'].map { |j| j['responseData']['translatedText'] }
92
+ end
93
+ end
94
+
95
+ def self.single_single(json)
96
+ json['responseData']['translatedText']
97
+ end
89
98
 
90
99
  # take in the base parameters for the google translate api
91
100
  def self.base_params(text, options)
@@ -113,7 +122,9 @@ module EasyTranslate
113
122
  # can take -- :english, 'english', :en, 'en'
114
123
  def self.get_language(lang)
115
124
  lang = lang.to_s
116
- LANGUAGES.include?(lang) ? lang : LANGUAGES.index(lang)
125
+ lang = LANGUAGES.include?(lang) ? lang : LANGUAGES.index(lang)
126
+ raise ArgumentError.new('please supply a valid language') unless lang
127
+ lang
117
128
  end
118
129
 
119
- end
130
+ end
@@ -1,5 +1,5 @@
1
1
  module EasyTranslate
2
2
 
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
 
5
- end
5
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Crepezzi
@@ -29,6 +29,18 @@ dependencies:
29
29
  version: "0"
30
30
  type: :development
31
31
  version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: json
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
32
44
  description: easy_translate is a wrapper for the google translate API that makes sense programatically, and implements API keys
33
45
  email: john@crepezzi.com
34
46
  executables: []