<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ruby code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 00:19:12 GMT</pubDate>
    <description>DZone Snippets: ruby code</description>
    <item>
      <title>Make a remote URL work like a file upload (in Rails)</title>
      <link>http://snippets.dzone.com/posts/show/3994</link>
      <description>Want to load a remote URL into an acts_as_attachment/attachment_fu model?  Use this little utility class.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  class UrlUpload&lt;br /&gt;    EXTENSIONS = {&lt;br /&gt;      "image/jpeg" =&gt; ["jpg", "jpeg", "jpe"],&lt;br /&gt;      "image/gif" =&gt; ["gif"],&lt;br /&gt;      "image/png" =&gt; ["png"]&lt;br /&gt;    }&lt;br /&gt;    attr_reader :original_filename, :attachment_data&lt;br /&gt;    def initialize(url)&lt;br /&gt;      @attachment_data = open(url)&lt;br /&gt;      @original_filename = determine_filename&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # Pass things like size, content_type, path on to the downloaded file&lt;br /&gt;    def method_missing(symbol, *args)&lt;br /&gt;      if self.attachment_data.respond_to? symbol&lt;br /&gt;        self.attachment_data.send symbol, *args&lt;br /&gt;      else&lt;br /&gt;        super&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    private&lt;br /&gt;      def determine_filename&lt;br /&gt;        # Grab the path - even though it could be a script and not an actual file&lt;br /&gt;        path = self.attachment_data.base_uri.path&lt;br /&gt;        # Get the filename from the path, make it lowercase to handle those&lt;br /&gt;        # crazy Win32 servers with all-caps extensions&lt;br /&gt;        filename = File.basename(path).downcase&lt;br /&gt;        # If the file extension doesn't match the content type, add it to the end, changing any existing .'s to _&lt;br /&gt;        filename = [filename.gsub(/\./, "_"), EXTENSIONS[self.content_type].first].join(".") unless EXTENSIONS[self.content_type].any? {|ext| filename.ends_with?("." + ext) }&lt;br /&gt;        # Return the result&lt;br /&gt;        filename&lt;br /&gt;      end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now when you have the URL you want to load, do something like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;@model.uploaded_data = UrlUpload.new(url)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Or better yet, make a pseudo-accessor on your aaa/attachment_fu model so you can stay "model-heavy".&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def url=(value)&lt;br /&gt;  self.uploaded_data = UrlUpload.new(value)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 11 May 2007 18:04:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3994</guid>
      <author>seancribbs (Sean Cribbs)</author>
    </item>
    <item>
      <title>Compress your ActiveRecord sessions</title>
      <link>http://snippets.dzone.com/posts/show/3924</link>
      <description>Using ActiveRecordStore and your sessions are getting too big?  Try this!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# in environment.rb or some file you require&lt;br /&gt;require 'zlib'&lt;br /&gt;CGI::Session::ActiveRecordStore::Session.class_eval {&lt;br /&gt;  class &lt;&lt; self&lt;br /&gt;    def marshal_with_compression(data)&lt;br /&gt;      Zlib::Deflate.deflate(marshal_without_compression(data))&lt;br /&gt;    end&lt;br /&gt;    def unmarshal_with_compression(data)&lt;br /&gt;      unmarshal_without_compression(Zlib::Inflate.inflate(data))&lt;br /&gt;    end&lt;br /&gt;    alias_method_chain :marshal, :compression&lt;br /&gt;    alias_method_chain :unmarshal, :compression&lt;br /&gt;  end&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;# in migration&lt;br /&gt;def self.up&lt;br /&gt;  change_column :sessions, :data, :binary&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.down&lt;br /&gt;  change_column :sessions, :data, :text&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 27 Apr 2007 16:11:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3924</guid>
      <author>seancribbs (Sean Cribbs)</author>
    </item>
    <item>
      <title>Turn CSV with headers into Array of Hashes (in 5 lines or less)</title>
      <link>http://snippets.dzone.com/posts/show/3899</link>
      <description>This assumes you have a CSV file whose first line are headings/labels for the individual columns.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'csv'&lt;br /&gt;&lt;br /&gt;csv_data = CSV.read 'data.csv'&lt;br /&gt;headers = csv_data.shift.map {|i| i.to_s }&lt;br /&gt;string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }&lt;br /&gt;array_of_hashes = string_data.map {|row| Hash[*headers.zip(row).flatten] }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Apr 2007 15:10:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3899</guid>
      <author>seancribbs (Sean Cribbs)</author>
    </item>
    <item>
      <title>Home-brewed file upload in Ruby on Rails</title>
      <link>http://snippets.dzone.com/posts/show/1805</link>
      <description>Most of this comes from others' work, but I was able to tool it to my needs and fix some bugs.  All of these lines go in the model, which for me has a :file and :content_type attributes.  :file stores the complete path to the uploaded file.  Be sure to change the string in path_to_file to the place where you want files stored, and that proper permissions are set on that path.  Also, sanitize_filename doesn't HAVE to be a private method -- make it public if you want.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;### Model ###&lt;br /&gt; def file=(uploaded_file)  &lt;br /&gt;    @uploaded_file = uploaded_file&lt;br /&gt;    @filename = sanitize_filename(@uploaded_file.original_filename)&lt;br /&gt;    write_attribute("content_type", @uploaded_file.content_type)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def after_create&lt;br /&gt;    if !File.exists?(File.dirname(path_to_file))&lt;br /&gt;      Dir.mkdir(File.dirname(path_to_file))&lt;br /&gt;    end&lt;br /&gt;    if @uploaded_file.instance_of?(Tempfile)&lt;br /&gt;      FileUtils.copy(@uploaded_file.local_path, path_to_file)&lt;br /&gt;    else&lt;br /&gt;      File.open(self.path_to_file, "wb") { |f| f.write(@uploaded_file.read) }&lt;br /&gt;    end&lt;br /&gt;    write_attribute("file", path_to_file)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def after_destroy&lt;br /&gt;    if File.exists?(self.file)&lt;br /&gt;      File.delete(self.file)&lt;br /&gt;      Dir.rmdir(File.dirname(self.file))&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def path_to_file&lt;br /&gt;    File.expand_path("#{RAILS_ROOT}/upload/#{self.id}/#{@filename}")&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  private&lt;br /&gt;  def sanitize_filename(file_name)&lt;br /&gt;    # get only the filename, not the whole path (from IE)&lt;br /&gt;    just_filename = File.basename(file_name) &lt;br /&gt;    # replace all none alphanumeric, underscore or perioids with underscore&lt;br /&gt;    just_filename.gsub(/[^\w\.\_]/,'_') &lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;### View ###&lt;br /&gt;...&lt;br /&gt;&lt;input type="file" name="model[file]" /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 30 Mar 2006 02:33:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1805</guid>
      <author>seancribbs (Sean Cribbs)</author>
    </item>
  </channel>
</rss>
