Skip to content
Browse files

Closer to fifth test passing.

  • Loading branch information...
1 parent 57a7036 commit 5abf4aaa17673c08be52183abeaf7c50fd865419 @aantix committed Nov 21, 2010
Showing with 62 additions and 21 deletions.
  1. +57 −20 lib/sex_it_up.rb
  2. +4 −0 spec/sex_it_up_spec.rb
  3. +1 −1 spec/spec_helper.rb
View
77 lib/sex_it_up.rb
@@ -4,6 +4,8 @@
require 'action_view'
require 'google-search'
require 'mechanize'
+require 'open-uri'
+
Paperclip.configure
Paperclip::Railtie.insert
@@ -13,34 +15,44 @@ module SexItUp
class SexItUpImage < ActiveRecord::Base
RESULT_LIMIT = 3
+ scope :random, lambda {
+ max_id = connection.select_value("select max(id) from #{self.table_name}")
+ where("id >= #{rand(max_id)}")
+ }
+
attr_reader :sizes
has_attached_file :image, :styles => Proc.new { |i| i.instance.attachment_sizes }
# Want to give each user a unique avatar? Assign their profile image to the image returned here.
def self.find_all(term)
+ puts "Querying....."
query = "site:commons.wikimedia.org \"is in the public domain\" #{term}"
- cache_search(query)
+ cache_search(query, term)
end
- def attachment_sizes
- sizes = {:thumb => ["100x100"]}
-# self.sizes.each do |size|
-# sizes[:"#{size.id}"] = ["#{size.width}x#{size.height}"]
-# end
+ def set_attachment_sizes(sizes = {:thumb => ["100x100"]})
+ puts "attachment_sizes = #{sizes.inspect}"
+ @sizes = sizes
+ end
- sizes
+ def attachment_sizes
+ @sizes||={:thumb => ["100x100"]}
end
private
- def self.cache_search(query)
+ def self.cache_search(query, term)
images = []
num_results = 0
+ puts "Caching results..."
Google::Search::Web.new(:query => query).each do |result|
+ puts "Result : [#{result.uri}]"
page = agent.get(result.uri)
image = find_image_link(page)
- images << cache(query, image.href) unless image.nil?
+ puts "image = #{image.inspect}"
+
+ images << cache(term, image.href) unless image.nil?
num_results+=1
break if num_results == RESULT_LIMIT
@@ -61,28 +73,53 @@ def self.agent
end
def self.cache(search_term, img_url)
+ puts "-- img_url = #{img_url}"
+
# No need to re-retrieve file if already done so
image = find_by_image_original_url(img_url)
return image unless image.nil?
- image = open img_url
- SexItUpImage.create(:image_original_url => img_url,
- :image_search_term => search_term,
- :image => image)
+ #image = open img_url
+ image = open(URI.parse(img_url))
+ def image.original_filename; base_uri.path.split('/').last; end
+ image.original_filename.blank? ? nil : image
+
+ SexItUpImage.create!(:image_original_url => img_url,
+ :image_search_term => search_term,
+ :image => image)
end
end
module SexItUpHelper
+ include ActionView::Helpers::AssetTagHelper
+
+ # Pass in a search term (e.g. 'dogs') and the resolution you
+ # want the image to be displayed at.
+ def sexy_image(term, opts = {:width => 100, :height => 100})
+ sexy_image = SexItUpImage.where(['image_search_term = ?', term]).random.first
+
+ if sexy_image.nil? || sexy_image.empty?
+ # No images for a given term; let's go find some.
+
+ SexItUpImage.find_all(term)
+ sexy_image = SexItUpImage.where(['image_search_term = ?', term]).random.first
+ end
+
+ unless sexy_image.nil?
+ style = "#{opts[:width]}x#{opts[:height]}"
+ style_sym = "s_#{style}".to_sym
+ style_hash = {style_sym => [style]}
+
+ sexy_image.set_attachment_sizes(style_hash)
+ sexy_image.image.reprocess! # unless File.exist?(sexy_image.image.path(style_sym))
+
+ return sexy_image.image.url(style_sym)
+ #return image_tag(sexy_image.image.url(style.to_sym))
+ end
- # Searches wikimedia for passed in term. Caches all initial results.
- # As you search through results, image files will generally follow the form e.g. "File:<image name>.jpg - Wikimedia Commons"
- # Retains the original image and will generate scaled versions based on coordinates passed in
- # Allow for an optional watermark that defaults to the text "Placeholder"
- def sexy_image(term, opts = {})
- #image_tag
- 'yes'
+ nil
end
end
View
4 spec/sex_it_up_spec.rb
@@ -3,6 +3,10 @@
require ROOT_DIR + '/spec/spec_helper'
describe SexItUp::SexItUpImage, '#find_all' do
+ before(:all) do
+ # Start with a clean slate
+ SexItUp::SexItUpImage.delete_all
+ end
it "shouldn't have any images cached" do
SexItUp::SexItUpImage.all.should have(0).things
View
2 spec/spec_helper.rb
@@ -13,7 +13,7 @@
gem 'sqlite3-ruby'
-DB = '/tmp/jobs.sqlite'
+DB = '/tmp/sex_it_up.sqlite'
# Delete the db if exists so we have a clean slate to run against.
File.delete(DB) rescue nil

0 comments on commit 5abf4aa

Please sign in to comment.
Something went wrong with that request. Please try again.