open("http://www.example.com", :http_basic_authentication=>["user", "password"])Thursday, May 19, 2011
Using Authentication with Open-uri
Monday, May 16, 2011
Calculating an Images Orientation using Paperclip
If you ever need to know if an image is portrait or landscape in orientation, you can do the following (assuming you have an
orientation attribute on your model:class Image < ActiveRecord::Base
# Paperclip
has_attached_file :file
# Callbacks
before_create :set_orientation
protected
def set_orientation
original_file = self.file.to_file(:original)
dimensions = Paperclip::Geometry.from_file(original_file)
self.orientation = (dimensions.width > dimensions.height) ? 'landscape' : 'portrait'
end
endThinkingSphinx and Nil Results
Have you ever gotten a result like the following when using ThinkingSphinx (by the awesome Pat Allen):
NoMethodError (undefined method `constantize' for nil:NilClass):
This error occurs when your index isn’t quite up to date and ThinkingSphinx returns results that have since been deleted from your database. To fix the problem add
:retry_stale => true to your searches, like this:Article.search('chunky bacon', :retry_stale => true)
There is more information about
:retry_stale in the source code:# If you pass :retry_stale => true to a single-model search, missing records will
# cause Thinking Sphinx to retry the query but excluding those records. Since search
# is paginated, the new search could potentially include missing records as well, so by
# default Thinking Sphinx will retry three times. Pass :retry_stale => 5 to retry five
# times, and so on. If there are still missing ids on the last retry, they are
# shown as nils.
Escaping Params Containing Periods Rails
Just a quick one. Say you have a route like the following:
map.resources :users
And you’re using email addresses to look up users like:
/users/jim@somewhere.com
You’re going to run into an error along the lines of:
Missing template users/show.com.erb in view path blah/app/views
Which obviously isn’t what you want. To fix this, change your route to the following:
map.resources :users, :requirements => { :id => /.*/ }Getting the Dimensions of a Flash (SWF) File rails
This is a just a quick post that will hopefully save some people a hell of a lot of time.
If you’re trying to get the width and height of a flash file, a banner for example, then you can use the ImageSpeclibrary by Brandon Anderson.
You can checkout the code with the following:
svn checkout http://ruby-imagespec.googlecode.com/svn/trunk/ vendor/plugins/ruby-imagespec
Then call it in your code to get the dimensions of a flash movie. Say you’re using Paperclip, then you could do the following:
dimensions = ImageSpec::Dimensions.new(@banner.file.path)
dimension.height # Get the height
dimesions.width # Get the width
Subscribe to:
Posts (Atom)