house9

random code and what not

Ruby Http Get with Net::HTTP


Resources



require ‘net/http’
require ‘uri’

def get_html_content(requested_url)
url = URI.parse(requested_url)
full_path = (url.query.blank?) ? url.path : “#{url.path}?#{url.query}”
the_request = Net::HTTP::Get.new(full_path)

the_response = Net::HTTP.start(url.host, url.port) { |http|
http.request(the_request)
}

raise “Response was not 200, response was #{the_response.code}” if the_response.code != “200”
return the_response.body
end

# this will fail with ArgumentError: HTTP request path is empty
s = get_html_content(“http://www.google.com”)
# these should be fine
s = get_html_content(“http://www.google.com/”)
s = get_html_content(“http://github.com/search?q=http”)
# above code does not handle redirects but raises exception for non-200
s = get_html_content(“http://www.yahoo.com/”) # http 302

Heroku - sending email with Gmail

I have been working on deploying a Rails application to Heroku. A couple of gotchas I ran into when following this post ”Sending email with Gmail

  • Don’t use a dollar sign ($) in your password 
  • this can work locally if you put single quotes around the value, but on the heroku server it saved the entire string but at runtime would truncate it at the dollar sign
  • for example using the password ’left-side$right-side
  • heroku config:add GMAIL_SMTP_PASSWORD=left-side$right-side
  • heroku config returns > GMAIL_SMTP_PASSWORD=left-side$right-side
  • heroku console 
  • type ENV[‘GMAIL_SMTP_PASSWORD’] returns > left-side

Heroku is running Ruby 1.8.6, locally I am running Ruby 1.8.7, I made a small hack to the plugin to avoid errors in development mode

./vendor/plugins/gmail_smtp/init.rb
changed the first line to
require ‘smtp_tls’ if RUBY_VERSION < "1.8.7" 

tls is already handled in 1.8.7 and this code must have been creating some type of conflict

Heroku - HTTP 502 Error

This might be helpful to someone?

I was receiving ‘App Failed To Respond (HTTP 502) Bad Gateway’ errors when deploying my application on Heroku. Reading the error codes page did not help me solve the problem, it took awhile but I was finally able to track it down.

The error was being generated in the browser but checking the rails logs, the requests were returning HTTP 200 success codes, so the problem was being generated at the web server level on the Heroku stack.

Turns out the problem was caused by setting response.header for no browser caching. I was using code similar to this post. Everything worked fine after I removed this line

  • response.headers[“Expires”] = 0

imeem on 64-bit ubuntu

Getting flash to work correctly on 64-bit Ubuntu can be challenging. By following various blog and forum posts I was able to get it to work, but never the flash player for imeem. Every now and again I would try to re-install flash and see if it would work for imeem but until last night I was never successful. 

It turns out that imeem will only work with the 32-bit flash player. After so much work to get the 64-bit player working in Firefox 3.0 and 3.5 I did not want to remove it so I downloaded Opera and installed the 32-bit player under its plugin directory and imeem works great!

  • Download and install Opera
  • verify that you cannot play music in imeem
  • Close Opera
  • download the 32-bit flash player in ‘.tar.gz’ format
  • extract the libflashplayer.so file from the archive
  • then you will need to put this in the directory /usr/lib/opera/plugins/
  • you won’t have permissions to write to that directory unless you are in sudo mode
  • open a terminal and type ’sudo nautilus
  • copy the file into the directory (you could do this from the terminal window also probably ’sudo mv libflashplayer.so /usr/lib/opera/plugins/libflashplayer.so
  • Open Opera
  • check the plugins - type opera:plugins in the address bar
  • you should see Shockwave Flash and it should be pointing to the libflashplayer.so file under opera plugins, by default my opera installation was pointing to /usr/lib/mozilla/plugins/ for the flash plugin, my guess is opera looks for plugins in its own directory and if not found then looks in mozilla directory
  • you should now be able to play music with imeem!

I am running Ubuntu 9.04 Jaunty Jackalope by the way. Here are some links that might work to get the 64-bit player working for Firefox


convert mkv files to dvd with ubuntu

Turns out there is a great program for this - my guess is it works well with other formats such as avi, mpeg, etc…

DeVeDe, if you are running 9.04 (jaunty) or higher it is in the repositories

System -> Administration -> Synaptic Package Manager

enter ‘devede’ in the search and then install

Procedure sp_droplogin, Line 93 Login is aliased or mapped to a user in one or more database(s)

Trying to remove a MSSQL login from a database server, recieve the following eror

Msg 15175, Level 16, State 1, Procedure sp_droplogin, Line 93
Login ‘qa’ is aliased or mapped to a user in one or more database(s). Drop the user or alias before dropping the login.

Solution: run this command to determine what alias or groups the login is associated with and then remove those; might be nice if they just added this to the error message?

sp_helpuser ‘qa’

scrolling screen shot on ubuntu

I’ve been using ubuntu for the last 6 months, really like it! But also really missing SnagIt :(

so far I have come across these screen shot tools

  • built in ‘save screen’ program - read this post for info about that and others
  • Shutter

but none that I came across would do scrolling screen shots; currently I only needed scrolling screen shots of web pages and I just came across a great tool for that - a firefox add-on called Abduction, it is really easy to use.

For full page screen shots (including all scrollable regions) or for cropped sections of a page. Just right click on the page and select ‘Save Page as Image’, if you are over an ‘element’ it will auto select that as the cropped area, you can then adjust the crop and save, if you want the entire page the easiest seems to be right clicking where there are no elements below the mouse and then adjust the fully selected page as needed

The saved png can then be opened in Shutter for applying arrows, boxes, etc…

Comments

Desperado
@vadi HOW?

Thanks to House 9 for the idea of using a browser extension. A simple idea I hadn't thought of.
Vadi
shutter can capture a whole webpage for you too though.

Ajax error handling with ruby on rails and jquery

This sample is just a slight modification of this post ’Handling AJAX errors and displaying friendly error messages to users’. Basically I wanted to maintain the existing rails error handling for non-ajax pages and then have good handling for errors in both development and production mode; i.e. get errors in the browser when in development mode and show a nice message when in production mode.

Modify /app/controllers/application.rb; add the rescue_from macro and the handler_exception method


Then add a global javascript method for presenting the errors; most likely in application.js

Then a sample usage, jquery making an ajax post to a rails controller action

in development mode the error message is returned in an easier to read view in the firebug console and an alert message is presented that has a truncated version of the error

dev mode

in production mode just a friendly error message.

production mode

Resources