Example of how to use Ruby's NET::HTTP

Ruby posted about 1 year ago by christian

I always forget how to use NET:HTTP and examples online are usually of little help, so I use this code as a starting point when I work with NET:HTTP:

   1  require 'net/http'
   2  require 'net/https'
   3  require 'uri'
   4  
   5  class HTTP
   6  
   7    class << self
   8      def get_proxy
   9        http_proxy = ENV["http_proxy"]
  10        URI.parse(http_proxy) rescue nil
  11      end
  12  
  13      def parse_url(url)
  14        begin
  15          if !url.kind_of?(URI) 
  16  
  17            url = URI.parse(url)
  18          end
  19        rescue
  20          raise URI::InvalidURIError, "Invalid url '#{url}'"
  21        end
  22  
  23        if (url.class != URI::HTTP && url.class != URI::HTTPS)
  24          raise URI::InvalidURIError, "Invalid url '#{url}'"
  25        end
  26  
  27        url
  28      end
  29  
  30      def get_connection(url, debug = false, http_timeout = 60)
  31        url = parse_url(url)
  32        
  33        proxy = get_proxy
  34        
  35        if proxy
  36          http = Net::HTTP::Proxy(proxy.host, proxy.port).new(url.host, url.port)
  37        else
  38          http = Net::HTTP.new(url.host, url.port)
  39        end
  40        
  41        if url.scheme == 'https'
  42          http.use_ssl = true
  43          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  44        end
  45        
  46        http.open_timeout = http.read_timeout = http_timeout
  47        
  48        http.set_debug_output $stderr if debug
  49        
  50        http
  51      end
  52    end
  53  end

The example shows you how to work with proxies, HTTPs, debugging, timeouts, URL validation. A real-world example would look something like this:

   1  connection = HTTP.get_connection(url)
   2  
   3  request = Net::HTTP::Get.new(url.request_uri)
   4  			
   5  # Set request headers
   6  headers.each { |key, value| request[key] = value }
   7  		
   8  # Basic authentication example
   9  #request.basic_auth username, password
  10  
  11  response = connection.request(request)
  12  
  13  	
  14  case response 
  15  when Net::HTTPRedirection
  16  ..
  17  ..
  18  end

This should also work:

   1  response, body = http.get(url.request_uri, headers)

Tagged net::http, http, ruby, example, debug, https, timeout, proxy

How to add logging to CakePHP applications

PHP posted over 2 years ago by christian

Note that Pear’s Logging package is a lot more flexible, so I recommend you use that instead of CakePHP’s built-in logging.

Use this code to add a debug message to the CakePHP debug log:

   1  $this->log("Upload action accessed", LOG_DEBUG);

Note that using something other than LOG _DEBUG will log the message as an error.

Run the code once and you should be able to see the message in $CAKE_APP/tmp/logs/debug.log.

Tagged php, cakephp, logging, debug

Fixing "config.breakpoint_server has been deprecated and has no effect" when using Rails Edge

Ruby posted over 2 years ago by christian

You might get this error if you’re using Rails Edge: config.breakpoint_server has been deprecated and has no effect:

To fix the error we need to remove the following from config/environments/development.rb:

   1  config.breakpoint_server = true

Next install ruby-debug:

   1  gem install ruby-debug

Then add this to the end of config/environments/development.rb:

   1  require 'ruby-debug'

Next, start your server, and the error should be gone…

To debug your code just add a call to debugger:

   1  class MySillySpace ...
   2    def create
   3      debugger # add this line
   4    end

Now when you access the URL with your browser you’ll have access to the debugger from the console window.

To learn how to use ruby-debug, read this tutorial written by the ruby-debug author.

Tagged rails, edge, debug, deprecated

Enable remote debugging in weblogic

Shell Script (Bash) posted over 2 years ago by marko

Sometimes it’s necessary to debug because you can’t write a test for it (e.g a legacy system). Just add the following parameters to the server startup and connect your preferred debugger into port 1044 (or whatever you choose the port to be). Works in weblogic, but should work in JBoss and other java based application servers too.

   1  -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044

Tagged debug, weblogic