paperclip processors and after_post_process

Leave a comment

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

Net::Flickr::People#find_by_user_id

Leave a comment

…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

json_pure 1.1.3 patch

4 Comments

Following up on a previous blog post about a JSON transformation issue, there is now a patched version of the json_pure 1.1.3 gem on GitHub.

The issue had to do with transforming Hashes and Arrays to JSON when they contain objects (classes) that have their own to_json methods.

A typical error message is:

lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:251:in
`to_json': wrong # of arguments(2 for 0) (ArgumentError)

See the README file and the commit history for generator.rb for more info.

No warranties or guarantees that this will work for other users and uses of the json_pure gem! See the GPL license.

Feedback is welcome!

Older Entries