Class: WebTranslateIt::Util
- Inherits:
-
Object
- Object
- WebTranslateIt::Util
- Defined in:
- lib/web_translate_it/util.rb
Overview
A few useful functions
Class Method Summary collapse
- .add_fields(request) ⇒ Object
-
.ask(question, default = nil) ⇒ Object
Ask a question.
-
.ask_yes_no(question, default = nil) ⇒ Object
Ask a question.
- .calculate_percentage(processed, total) ⇒ Object
-
.can_display_colors? ⇒ Boolean
Returns whether a terminal can display ansi colors.
- .handle_response(response, return_response = false, raise_exception = false) ⇒ Object
-
.version ⇒ Object
Return a string representing the gem version For example “1.8.3”.
Class Method Details
.add_fields(request) ⇒ Object
43 44 45 46 47 |
# File 'lib/web_translate_it/util.rb', line 43 def self.add_fields(request) request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", version) request.add_field("Content-Type", "application/json") end |
.ask(question, default = nil) ⇒ Object
Ask a question. Returns an answer.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/web_translate_it/util.rb', line 85 def self.ask(question, default=nil) question = question + " (Default: #{default})" unless default.nil? print(question + " ") STDOUT.flush result = STDIN.gets result.chomp! if result result = default if result.nil? or result == '' result end |
.ask_yes_no(question, default = nil) ⇒ Object
Ask a question. Returns a true for yes, false for no, default for nil.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/web_translate_it/util.rb', line 52 def self.ask_yes_no(question, default=nil) qstr = case default when nil 'yn' when true 'Yn' else 'yN' end result = nil while result.nil? result = ask("#{question} [#{qstr}]") result = case result when /^[Yy].*/ true when /^[Nn].*/ false when /^$/ when nil default else nil end end return result end |
.calculate_percentage(processed, total) ⇒ Object
15 16 17 18 |
# File 'lib/web_translate_it/util.rb', line 15 def self.calculate_percentage(processed, total) return 0 if total == 0 ((processed*10)/total).to_f.ceil*10 end |
.can_display_colors? ⇒ Boolean
Returns whether a terminal can display ansi colors
99 100 101 |
# File 'lib/web_translate_it/util.rb', line 99 def self.can_display_colors? !RUBY_PLATFORM.downcase.include?("mingw32") end |
.handle_response(response, return_response = false, raise_exception = false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/web_translate_it/util.rb', line 20 def self.handle_response(response, return_response = false, raise_exception = false) if response.code.to_i >= 400 and response.code.to_i < 500 if raise_exception raise "Error: #{MultiJson.load(response.body)['error']}" else puts StringUtil.failure(MultiJson.load(response.body)['error']) end elsif response.code.to_i == 500 if raise_exception raise "Error: Server temporarily unavailable (Error 500)." else puts StringUtil.failure("Error: Server temporarily unavailable (Error 500).") end else return response.body if return_response return StringUtil.success("OK") if response.code.to_i == 200 return StringUtil.success("Created") if response.code.to_i == 201 return StringUtil.success("Accepted") if response.code.to_i == 202 return StringUtil.success("Not Modified") if response.code.to_i == 304 return StringUtil.failure("Locked\n (another import in progress)") if response.code.to_i == 503 end end |
.version ⇒ Object
Return a string representing the gem version For example “1.8.3”
11 12 13 |
# File 'lib/web_translate_it/util.rb', line 11 def self.version Gem.loaded_specs['web_translate_it'].version end |