module GxG::Networking

Constants

REGISTERED

Public Class Methods

get_handler_class(the_url=nil) click to toggle source
# File lib/gxg/net_tools.rb, line 11
def self.get_handler_class(the_url=nil)
  result = nil
  begin
    if the_url.is_a?(::URI::Generic)
      @@protocol_thread_safety.synchronize {
        ::GxG::Networking::REGISTERED[:protocols].keys.each do |the_scheme|
          if the_url.scheme.to_s == the_scheme.to_s
            result = ::GxG::Networking::REGISTERED[:protocols][(the_scheme)]
            break
          end
        end
      }
    else
      raise ArgumentError, "You MUST provide a valid URI"
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:url => the_url}})
  end
  result
end
register_handler_class(the_scheme=nil, the_class=nil) click to toggle source
# File lib/gxg/net_tools.rb, line 32
def self.register_handler_class(the_scheme=nil, the_class=nil)
  result = false
  begin
    unless the_class.is_a?(::Class)
      raise ArgumentError, "You MUST provide a valid class."
    end
    unless the_scheme.is_any?(::String, ::Symbol)
      raise ArgumentError, "You MUST provide a valid URI scheme (as a String or Symbol)."
    end
    @@protocol_thread_safety.synchronize {
      ::GxG::Networking::REGISTERED[:protocols][(the_scheme.to_sym)] = the_class
    }
    result = true
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:scheme => the_scheme, :class => the_class}})
  end
  result
end
registered_protocols() click to toggle source
# File lib/gxg/net_tools.rb, line 67
def self.registered_protocols()
  @@protocol_thread_safety.synchronize {
    ::GxG::Networking::REGISTERED[:protocols].clone
  }
end
unregister_handler_class(the_scheme=nil) click to toggle source
# File lib/gxg/net_tools.rb, line 51
def self.unregister_handler_class(the_scheme=nil)
  result = false
  begin
    unless the_scheme.is_any?(::String, ::Symbol)
      raise ArgumentError, "You MUST provide a valid URI scheme (as a String or Symbol)."
    end
      @@protocol_thread_safety.synchronize {
        ::GxG::Networking::REGISTERED[:protocols].delete(the_scheme.to_sym)
      }
    result = true
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:scheme => the_scheme}})
  end
  result
end
wget(the_url=nil, options={}) click to toggle source
# File lib/gxg/net_tools.rb, line 73
def self.wget(the_url=nil, options={})
  result = false
  begin
    unless options.is_a?(::Hash)
      options = {}
    end
    if the_url.is_a?(::String)
      the_url = ::URI::parse(the_url)
    end
    if the_url.is_a?(::URI::Generic)
      if options[:pem_file]
        client = ::GxG::Networking::HttpsClientWithPem.new(options[:pem_file])
      else
        if the_url.scheme.to_s.downcase == "https" || options[:use_ssl]
          client = ::GxG::Networking::HttpsClient.new
        else
          client = ::GxG::Networking::HttpClient.new
        end
      end
      response = client.get(the_url, options.merge({:raw_response => true}))
      if response
        if response.is_a?(::File)
          result = response
        else
          if options[:raw_response]
            result = response
          else
            result = response.body
          end
        end
      else
        raise Exception, "Failed to get a response."
      end
    else
      raise ArgumentError, "You MUST specify a valid URI"
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:url => the_url, :options => options}})
  end
  result
end