Skip to content

Commit

Permalink
Current tests are passing cleanly. Now, get some sleep.
Browse files Browse the repository at this point in the history
  • Loading branch information
aantix committed Nov 17, 2010
1 parent 9bb628b commit 5dbadc8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 51 deletions.
72 changes: 36 additions & 36 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions lib/sex_it_up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
require 'paperclip'
require 'active_record'
require 'action_view'
require 'google-search'
require 'mechanize'

Paperclip.configure
Paperclip::Railtie.insert

module SexItUp

class SexItUpImage < ActiveRecord::Base
RESULT_LIMIT = 3

attr_reader :sizes
puts "before has_attached"
has_attached_file :image, :styles => Proc.new { |i| i.instance.attachment_sizes }
puts "after has_attached"

# Want to give each user a unique avatar? Assign their profile image to the image returned here.
def self.find_all(term)
Expand All @@ -32,17 +33,23 @@ def attachment_sizes

private
def self.cache_search(query)
images = []
images = []
num_results = 0

Google::Search::Web.new(:query => query).each do |result|
page = agent.get(result.uri)
image = find_image_link(page)

images << cache(query, image)
images << cache(query, image.href) unless image.nil?

num_results+=1
break if num_results == RESULT_LIMIT
end

images
end

def find_image_link(page)
def self.find_image_link(page)
page.links.detect {|link| link.href =~ /http:\/\/upload.wikimedia.org\/wikipedia\/commons\/\w\// }
end

Expand All @@ -55,7 +62,7 @@ def self.agent

def self.cache(search_term, img_url)
# No need to re-retrieve file if already done so
image = find_by_original_image_url(img_url)
image = find_by_image_original_url(img_url)
return image unless image.nil?

image = open img_url
Expand Down
17 changes: 8 additions & 9 deletions spec/sex_it_up_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@

describe SexItUp::SexItUpImage, '#find_all' do

before(:each) do
@image = Factory.create(:sex_it_up_image)
end

it "shouldn't have anything cached to begin with" do
SexItUp::SexItUpImage.should have(0).things
it "shouldn't have any images cached" do
SexItUp::SexItUpImage.all.should have(0).things
end

it "should cache at least one image for the search term 'school'" do
SexItUp::SexItUpImage.find_all('school')
SexItUp::SexItUpImage.should have_at_least(1).things
SexItUp::SexItUpImage.all.should have_at_least(1).things
end

it "should find the 'Plato and Aristotle in The School of Athens' image'" do
SexItUp::SexItUpImage.find_all('school').should include @image
@image = Factory.build(:sex_it_up_image)

url = 'http://upload.wikimedia.org/wikipedia/commons/f/ff/Plato_and_Aristotle_in_The_School_of_Athens%2C_by_italian_Rafael.jpg'
SexItUp::SexItUpImage.find_by_image_original_url(url).image_original_url.should == @image.image_original_url
end

it "should return an empty series of SexItUpImage objects" do
SexItUp::SexItUpImage.find_all('blah123xyz').should be []
SexItUp::SexItUpImage.find_all('blah123xyz').should == []
end

end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
DB = '/tmp/jobs.sqlite'

File.delete(DB) rescue nil
FileUtils.rm_rf('public')

ActiveRecord::Base.logger = Logger.new('/tmp/sex_it_up.log')
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => DB)

Expand Down

0 comments on commit 5dbadc8

Please sign in to comment.