July 14, 2009 by Dave
Opal Released!
 

Opal Has been Released today!

Opal is a powerful Item Management Application built in Ruby on Rails. It allows you to list any kind of item, from houses to video games to hotdog vendors. When you install Opal, you tell it the name of the items you're going to be listing(example: houses), and that's it! After that, you can create any number of items and show them off using different Item Objects. Try out the demo, or download it for free here:

http://www.hulihanapplications.com/projects/opal

0 Comments


Here's a quick function that we use in some of our Ruby on Rails applications to print out a colored number that indicates a comparison with another number. Basically, you pass in a value(the number you're comparing) and a maximum(the number you're comparing value against), and it will return a color that indicates its amount in comparison to the maximum. We use this to compare quantities in inventory systems, file comparison percentages, and other fun stuff.

Here's the code, all you have to do is place it in app/helpers/application_helper.rb
 def colored_comparison(value = 0, maximum = 0, options = {:direction => "down", :display => "plain"}) # compare value against another value(maximum), print a pretty colored number comparison(red = bad, green = good) signifying comparison
   colors = {:good => "green", :middle => "orange", :bad => "red", :none => "black"} # color hash
  
   color_key = "none".to_sym # set default as "none"...see color key below
   if options[:direction] == "down" # compare from maximum down to 0...70/100 -> good, 10/100 -> bad
       if value.to_i >= (maximum.to_i * 0.8) # above half max 
        color_key = "good" # set as good 
       elsif (value.to_i <=  (maximum.to_i * 0.8)) && value.to_i >= (maximum.to_i * 0.5) # between 50% and 20%
        color_key = "middle" # set as middle
       elsif (value.to_i <=  (maximum.to_i * 0.5)) && value.to_i >= 0  # less than 20%
        color_key = "bad" # set as good 
       else 
        color_key = "none" # set as none
       end 
    elsif options[:direction] == "up" # compare from 0 up to maximum...70/100 -> bad, 10/100 -> good
       if value.to_i <= (maximum.to_i * 0.2) # below half 
        color_key = "good" # set as good 
       elsif (value.to_i >=  (maximum.to_i * 0.2)) && value.to_i <= (maximum.to_i * 0.5) # between 50% and 70%
        color_key = "middle" # set as middle
       elsif (value.to_i >= (maximum.to_i * 0.5)) && (value.to_i <=  maximum.to_i)  # between 70% and 100%
        color_key = "bad" # set as good 
       else 
        color_key = "none" # set as none
       end 
    end 

    if options[:display] == "plain" # return plain colored value, ie: 5
      message = "#{value}"
    elsif options[:display] == "fraction" # return colored fraction, ie: 5/100 
      message = "#{value}/#{maximum}"
    else 
      message = "There was a problem! Please check the options you passed in."
    end 

    return message
  end
Then you can call it anywhere in your views like this:
	<%= colored_comparison(10, 100) %> Items Remaining
This should return:
10 Items Remaining
By default, the function assumes that if the value you're comparing is really low(counting in a downward direction), this is a bad thing. Sometimes this isn't a bad thing(If you're counting down to something or trying to get rid of t-shirts, etc.), so you can reverse the direction, by passing in the :direction option.
	<%= colored_comparison(10, 100, options = {:direction => "up", :display => "plain"}) %> Items Remaining
This should return:
10 Items Remaining
You can also set the :display option to return a fraction-looking amount that prints out the number you're comparing against. Here's an example:
	<%= colored_comparison(10, 100, options = {:direction => "up", :display => "fraction"}) %> Items Remaining
This should return:
10/100 Items Remaining
You can change the amounts that are being compared easily(see the function above). It should be pretty straightforward, I just thought I'd share. Post a comment if you have any questions!
0 Comments


May 25, 2009 by Dave
Checking HTTP Response codes in Ruby is easy to do, and very useful. A normal, working webpage will usually return a 200 response code, which means everything's okay. We all know about 404s(file not found). Also, 500 errors are pretty prevalent, they usually tell you if something strange or buggy is going with the code of a dynamic website or script. Most rails applications throw a 500 Error when they're not working.

Using Ruby's Net::HTTP library, you can check these HTTP response codes to see if one of your websites are down, or if one of your ruby on rails applications are acting insane. I like to use these with lists of domains that I monitor for downtime or status. Here's an easy way to do it in ruby:
require "net/http" 
@uri = "http://www.hulihanapplications.com"
@request = Net::HTTP.get_response(URI.parse(@uri))  # returns an Net::HTTPResponse Object
puts "Code: #{@request.code} Message: #{@request.message}" # print the HTTP Response code and Message
This should return:
Code: 200 Message: OK

Integrate this into your Rails Application

You can also work this into a ruby on rails application, as a view helper, to make it look pretty and color coded, just add this to app/helpers/application_helper.rb:
module ApplicationHelper
  require "net/http"
  def print_response(url) # print out HTTP response code and message
	 @req = Net::HTTP.get_response(URI.parse(url)) 
	 color = "black"
	 color_hash = Hash.new # create a hash indexed by response code, contains color
	 # Assign color codes by response code
	 color_hash["200"] = "green"
	 color_hash["302"] = "yellow"
	 color_hash["404"] = "red"
	 color_hash["500"] = "red"
	 
	 #if @req.code == 200
	 #   color = "green"
	 #end
	 return "#{@req.code} #{@req.message}"
  end
end

Then you would call this from any view:
www.hulihanapplications.com - <%= print_response("http://www.hulihanapplications.com") %>
This would end up looking like this:
www.hulihanapplications.com - 200 OK

or, if the site is down
www.hulihanapplications.com - 404 Not Found
That's it! This is a good way to check if a website or domain is down from any script or application.

You can learn more about what attributes a Net:HTTPResponse object has here.

You can also read more about the Ruby Net::HTTP library here.
0 Comments