Permalink
Browse files

adding new project_item concept for projects on profiles. updating sp…

…ecs and fixing a few other bugs.
  • Loading branch information...
1 parent e06190c commit d840ce15d4a374503d3f6699e5f96c9b8796c9e7 @smudge committed Feb 4, 2013
View
@@ -1,5 +1,6 @@
# encoding: utf-8
require_relative 'common'
+require_relative "project_item"
module Kickstarter
class Profile
@@ -38,8 +39,8 @@ def backed_count
@backed_count ||= Integer(page_content.css('#profile_bio .backed').inner_html.gsub(/Backed/,'').gsub(/projects/,'').gsub(/\n/,''))
end
- def backed_projects
- []
+ def backed_projects(opts = {})
+ @backed_projects ||= Profile.fetch_projects(url, opts)
end
def page_content
@@ -73,5 +74,32 @@ def self.fetch_page(url)
end
end
+ def self.fetch_projects(url, options = {})
+ pages = options.fetch(:pages, :all)
+ pages -= 1 unless pages == 0 || pages == :all
+
+ start_page = options.fetch(:page, 1)
+ end_page = pages == :all ? 10000 : start_page + pages
+
+ results = []
+
+ (start_page..end_page).each do |page|
+ retries = 0
+ begin
+ doc = Nokogiri::HTML(open("#{url}?page=#{page}"))
+ nodes = doc.css('#profile_projects_list .project_item')
+ break if nodes.empty?
+
+ nodes.each do |node|
+ results << Kickstarter::ProjectItem.new(node)
+ end
+ rescue Timeout::Error
+ retries += 1
+ retry if retries < 3
+ end
+ end
+ results
+ end
+
end
end
@@ -37,8 +37,9 @@ def owner
@owner ||= details_page.css('#creator-name h3 a').inner_html.to_s
end
- def image_url
+ def image_url(style=:main)
@image_url ||= details_page.css('#video-section img').attr('src').value.split('?').first
+ @image_url.gsub(/photo-(main|full)\.jpg/,"photo-#{style}.jpg")
end
def currency
@@ -34,8 +34,9 @@ def owner
@owner ||= node.css('h2 span').first.inner_html.gsub(/by/, "").strip
end
- def image_url
- @image_url ||= thumbnail_url.gsub(/photo-little\.jpg/,'photo-full.jpg').split('?').first
+ def image_url(style = :main)
+ @image_url ||= thumbnail_url.split('?').first
+ @image_url.gsub(/photo-little\.jpg/,"photo-#{style}.jpg")
end
def currency
@@ -0,0 +1,64 @@
+# encoding: utf-8
+require_relative 'common'
+
+module Kickstarter
+ class ProjectItem
+
+ attr_reader :node
+
+ def initialize(node)
+ @node = node
+ end
+
+ def id
+ @id ||= /\/projects\/([0-9]+)\/photo-carousel\.jpg/.match(thumbnail_url)[1].to_i
+ end
+
+ def name
+ @name ||= node.css('.project_name').first.inner_html
+ end
+
+ def description
+ @description ||= node.css('.project_description').first.inner_html
+ end
+
+ def url
+ @url ||= node.attribute('href').to_s.split('?').first
+ end
+
+ def handle
+ @handle ||= url.split('/projects/').last.gsub(/\/$/,"")
+ end
+
+ def image_url(style = :main)
+ @image_url ||= thumbnail_url.split('?').first
+ @image_url.gsub(/photo-carousel\.jpg/,"photo-#{style}.jpg")
+ end
+
+
+ def to_hash
+ {
+ :id => id,
+ :name => name,
+ :handle => handle,
+ :url => url,
+ :description => description,
+ :image_url => image_url
+ }
+ end
+
+ def inspect
+ to_hash.inspect
+ end
+
+ #######################################################
+ private
+ #######################################################
+
+ #Use image_url instead.
+ def thumbnail_url
+ node.css('.project_thumbnail img').first.attribute('src').to_s
+ end
+
+ end
+end
View
@@ -16,6 +16,7 @@
chimero = "fchimero"
gaspar = "georgegaspar"
rivetw = "coolminiornot"
+ ralfh = "ralfh"
it "finds the name" do
@@ -67,10 +68,17 @@
end
end
- it "finds the backed_projects" do
- VCR.use_cassette "profile/#{sensible}" do
+ it "finds the backed_projects (1 page)" do
+ VCR.use_cassette "profile/#{sensible}", :record => :new_episodes do
@profile = Kickstarter::Profile.new(sensible)
- @profile.backed_projects.count.should eq(20)
+ @profile.backed_projects.count.should eq(5)
+ end
+ end
+
+ it "finds the backed_projects (many pages)" do
+ VCR.use_cassette "profile/#{ralfh}", :record => :new_episodes do
+ @profile = Kickstarter::Profile.new(ralfh)
+ @profile.backed_projects.count.should eq(246)
end
end
@@ -49,7 +49,12 @@ def get_node(project)
it "finds the project image_url" do
@project = Kickstarter::ProjectCard.new(get_node(spacesuit))
- @project.image_url.should eq("https://s3.amazonaws.com/ksr/projects/217667/photo-full.jpg")
+ @project.image_url.should eq("https://s3.amazonaws.com/ksr/projects/217667/photo-main.jpg")
+ end
+
+ it "finds the project image_url (full)" do
+ @project = Kickstarter::ProjectCard.new(get_node(spacesuit))
+ @project.image_url(:full).should eq("https://s3.amazonaws.com/ksr/projects/217667/photo-full.jpg")
end
it "finds a USD project's currency" do
View
@@ -69,7 +69,14 @@
it "finds the project's image_url" do
VCR.use_cassette minecraft do
@project = Kickstarter::Project.new(base_url + minecraft)
- @project.image_url.should eq('https://s3.amazonaws.com/ksr/projects/21767/photo-full.jpg')
+ @project.image_url.should eq('https://s3.amazonaws.com/ksr/projects/21767/photo-main.jpg')
+ end
+ end
+
+ it "finds the project's image_url (full)" do
+ VCR.use_cassette minecraft do
+ @project = Kickstarter::Project.new(base_url + minecraft)
+ @project.image_url(:full).should eq('https://s3.amazonaws.com/ksr/projects/21767/photo-full.jpg')
end
end

0 comments on commit d840ce1

Please sign in to comment.