Sha256: c3488ac1a3a8b7a97b66cb3187be578cae2819daffd549673dd1afa5e200e66d

Contents?: true

Size: 1.79 KB

Versions: 10

Compression:

Stored size: 1.79 KB

Contents

# encoding: utf-8
module WebTranslateIt
  class Connection
    require 'net/http'
    require 'net/https'
    require 'uri'
    require 'ostruct'
    
    @@api_key = nil
    @@http_connection = nil
    
    #
    # Initialize and yield a HTTPS Keep-Alive connection to WebTranslateIt.com
    #
    # Usage:
    #
    # WebTranslateIt::Connection.new(api_key) do
    #   # do something with Connection.api_key and Connection.http_connection
    # end
    #
    # Or:
    #
    # WebTranslateIt::Connection.new(api_key) do |http_connection|
    #   http_connection.request(request)
    # end
    #
    def initialize(api_key)
      @@api_key = api_key
      proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : OpenStruct.new
      http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
      http.use_ssl      = true
      http.open_timeout = http.read_timeout = 30
      begin
        http.verify_mode  = OpenSSL::SSL::VERIFY_PEER
        if File.exists?('/etc/ssl/certs') # Ubuntu
          http.ca_path = '/etc/ssl/certs'
        else
          http.ca_file = File.expand_path('cacert.pem', __FILE__)
        end
        @@http_connection = http.start
        yield @@http_connection if block_given?
      rescue OpenSSL::SSL::SSLError
        puts "Unable to verify SSL certificate."
        http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
        http.use_ssl      = true
        http.open_timeout = http.read_timeout = 30
        http.verify_mode  = OpenSSL::SSL::VERIFY_NONE
        @@http_connection = http.start
        yield @@http_connection if block_given?
      end
    end
    
    def self.http_connection
      @@http_connection
    end
    
    def self.api_key
      @@api_key
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
web_translate_it-2.0.7 lib/web_translate_it/connection.rb
web_translate_it-2.0.6 lib/web_translate_it/connection.rb
web_translate_it-2.0.5 lib/web_translate_it/connection.rb
web_translate_it-2.0.4 lib/web_translate_it/connection.rb
web_translate_it-2.0.3 lib/web_translate_it/connection.rb
web_translate_it-2.0.2 lib/web_translate_it/connection.rb
web_translate_it-2.0.1 lib/web_translate_it/connection.rb
web_translate_it-2.0.0 lib/web_translate_it/connection.rb
web_translate_it-2.0.0.rc4 lib/web_translate_it/connection.rb
web_translate_it-2.0.0.rc3 lib/web_translate_it/connection.rb