lolibrarian/nypl-tweetwall

View on GitHub
app/models/remote_image.rb

Summary

Maintainability
A
0 mins
Test Coverage
class RemoteImage < ActiveRecord::Base
  MAX_TWEETWALL_WIDTH = 300

  attr_accessible :url

  validates :url,
            :width,
            :height,
            :presence => true
  validates :url, :length => {:maximum => 1020}

  before_validation :fetch_dimensions

  def fetch_dimensions
    return unless url

    # FastImage raises an exception if the URL is not URL-encoded. For
    # example, if a space is present.
    self.url = encoded_url

    self.width, self.height = FastImage.size(url)
  end

  # Returns a URL-encoded copy of the URL (doesn't re-encode if already
  # encoded).
  def encoded_url
    [:decode, :encode].reduce(url) { |memo, method| URI.send(method, memo) }
  end

  # Returns the preferred width of the remote image for the Tweetwall.
  def tweetwall_width
    (width > MAX_TWEETWALL_WIDTH) ? MAX_TWEETWALL_WIDTH : width
  end

  # Returns the preferred height of the remote image for the Tweetwall.
  def tweetwall_height
    (height * tweetwall_width) / width
  end
end