Sha256: 538163ed8618bb1d01e30205c6c846d00d118038a0bb70c7baaad214c3255ae3

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

module WebTranslateIt
  # A few useful functions
  class Util
    
    # Return a string representing the gem version
    # For example "1.4.4"
    def self.version
      hash = YAML.load_file File.join(File.dirname(__FILE__), '..', '..' '/version.yml')
      [hash[:major], hash[:minor], hash[:patch]].join('.')
    end
    
    # Yields a HTTP connection over SSL to Web Translate It.
    # This is used for the connections to the API throughout the library.
    # Use it like so:
    # 
    #   WebTranslateIt::Util.http_connection do |http|
    #     request = Net::HTTP::Get.new(api_url)
    #     response = http.request(request)
    #   end
    #
    def self.http_connection
      http = Net::HTTP.new('webtranslateit.com', 443)
      http.use_ssl      = true
      http.verify_mode  = OpenSSL::SSL::VERIFY_NONE
      http.read_timeout = 40
      yield http
    end
    
    def self.calculate_percentage(processed, total)
      return 0 if total == 0
      ((processed*10)/total).to_f.ceil*10
    end
    
    def self.welcome_message
      puts "Web Translate It v#{WebTranslateIt::Util.version}"
    end
    
    def self.handle_response(response, return_response = false)
      if response.code.to_i >= 400 and response.code.to_i < 500
        "We had a problem connecting to Web Translate It with this API key. Make sure it is correct."
      elsif response.code.to_i >= 500
        "Web Translate It is temporarily unavailable. Please try again shortly."
      else
        if return_response
          response.body
        else
          "#{response.code.to_i} OK"
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
web_translate_it-1.6.4 lib/web_translate_it/util.rb
web_translate_it-1.6.3 lib/web_translate_it/util.rb
web_translate_it-1.6.2 lib/web_translate_it/util.rb
web_translate_it-1.6.1 lib/web_translate_it/util.rb
web_translate_it-1.6.0 lib/web_translate_it/util.rb