Skip to content

Commit

Permalink
added photos products
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasha committed Nov 15, 2011
1 parent d50cce7 commit 145ef0f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
module ApplicationHelper
def short_name(name)
name.split(' ')[0..3].join(' ')
end
end
1 change: 1 addition & 0 deletions app/views/home/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- for product in @products
%p= product.name
%img{:src => product.photo, :alt => short_name(product.name)}
5 changes: 5 additions & 0 deletions db/migrate/20111115222044_add_photo_to_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPhotoToProduct < ActiveRecord::Migration
def change
add_column :products, :photo, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20111113160210) do
ActiveRecord::Schema.define(:version => 20111115222044) do

create_table "products", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "photo"
end

end
11 changes: 6 additions & 5 deletions lib/tasks/products.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ task :products => :environment do
require "nokogiri"
require "open-uri"

path_to_photos = "#{Rails.root}/public/amazon_photos/I"
photos_dir = "amazon_photos/I"
full_path_to_photos = "#{Rails.root}/public/#{photos_dir}"
url = 'http://www.amazon.com/Digital-SLRs-Cameras-Photo/b/ref=amb_link_6742562_1?ie=UTF8&node=3017941&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-5&pf_rd_r=0VGJA3MFRDFRCZX42DR6&pf_rd_t=101&pf_rd_p=1266111442&pf_rd_i=515382011'
doc = Nokogiri::HTML(open(url))

Product.delete_all
empty_dir(path_to_photos)
empty_dir(full_path_to_photos)

max, longest_name = 0, ''

Expand All @@ -21,10 +22,10 @@ task :products => :environment do
if max < name.size then max = name.size; longest_name = name end

src_url = item.at_css('img')[:src]
filename = File.basename(src_url)
system("curl --silent --remote-time --output #{path_to_photos}/#{filename} #{src_url}")
filename = "#{File.basename(src_url)}"
system("curl --silent --remote-time --output #{full_path_to_photos}/#{filename} #{src_url}")

Product.create!(name: name)
Product.create!(name: name, photo: "#{photos_dir}/#{filename}")
print '.'
#p "#{i.at_css('.srTitle').text} - #{i.at_css('.listprice, .otherprice').text}"
#p i.at_css('a')[:href]
Expand Down

0 comments on commit 145ef0f

Please sign in to comment.