2009 November 23
robertor
work
ruby, ruby on rails, rubygems
A few things to note when trying to process an uploaded file with paperclip:
- The only way a post-processor will run is if a style is defined
class MyModel < ActiveRecord::Base
has_attached_file :mp3,
:url => '/:class/:id/:style.:extension',
:path => ':rails_root/assets/:class/:id_partition/:style.:extension',
:whiny => true,
:styles => { :text => { :do_something => true } },
:processors => [:my_processor]
# ...
end
- To access the file after processing but before paperclip is finished, use the :after_process callback — model instance attributes can be modified and will be saved automatically as part of the paperclip process
class MyModel < ActiveRecord::Base
# ...
after_post_process :do_one_more_thing
# ...
private
def do_one_more_thing
logger.info "MyModel [paperclip] after_post_process"
logger.info "MyModel [paperclip] file #{mp3.queued_for_write[:original].path}"
# Do more with the file
# Make model attribute changes as needed
# self.my_attribute = "x"
end
end
2008 October 30
robertor
play, work
ruby, rubygems
…a short, random post…
It’s the end of the month, and I need to meet my self-imposed quota so that the archives have a link to October 2008.
After looking around at the different Ruby Flickr libraries, Net::Flickr seems to be the most active today. It’s undergoing some major refactoring for its next release. The current release is tagged 0.0.1 and Pre-Alpha. I think it was tagged this way to give people a warm fuzzy feeling when they use it on production apps.
Anyway, 0.0.1 Pre-Alpha needs a lookup to find a person by user_id. There’s code in Net::Flickr::Person that does a lookup by user_id, but you need to know the person’s email address or username.
0.0.1 Pre-Alpha Net::Flickr::People#find_by_user_id
module Net; class Flickr
class People
# Looks up a Flickr user based on their user_id.
#
# See http://flickr.com/services/api/flickr.people.getInfo.html for
# details.
def find_by_user_id(user_id, args = {})
args['user_id'] = user_id
response = @flickr.request('flickr.people.getInfo', args)
Person.new(@flickr, response.at('person'))
end
end
end; end
Based on the current code in SVN, the next release might need something like this:
module Net; class Flickr
class People
# Looks up a Flickr user based on their user_id.
#
# See http://flickr.com/services/api/flickr.people.getInfo.html for
# details.
def find_by_user_id(user_id, args = {})
args['user_id'] = user_id
response = Net::Flickr.instance().request('flickr.people.getInfo', args)
Person.new(response.at('person'))
end
end
end; end