Sha256: a34389d1dc4a58e81fe991507dcb0f3eddf0e61161ef82024498b30375e4750b

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module WebTranslateIt

  class Connection

    @@api_key = nil
    @@http_connection = nil
    @@debug = false

    #
    # 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) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
      @@api_key = api_key
      proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : Struct.new(:host, :port, :user, :password).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 = 60
      http.set_debug_output($stderr) if @@debug
      begin
        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
        @@http_connection = http.start
        yield @@http_connection if block_given?
      rescue OpenSSL::SSL::SSLError
        puts 'Error: Unable to verify SSL certificate.'
        exit 1
      rescue StandardError
        puts $ERROR_INFO
      end
    end

    def self.turn_debug_on
      @@debug = true
    end

    def self.api_key
      @@api_key
    end

    def self.http_connection
      @@http_connection
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
web_translate_it-3.1.1 lib/web_translate_it/connection.rb
web_translate_it-3.1.0 lib/web_translate_it/connection.rb
web_translate_it-3.0.2 lib/web_translate_it/connection.rb
web_translate_it-3.0.1 lib/web_translate_it/connection.rb
web_translate_it-3.0.0 lib/web_translate_it/connection.rb
web_translate_it-3.0.0.beta1 lib/web_translate_it/connection.rb