Skip to content

Commit

Permalink
adding new project_item concept for projects on profiles. updating sp…
Browse files Browse the repository at this point in the history
…ecs and fixing a few other bugs.
  • Loading branch information
smudge committed Feb 4, 2013
1 parent e06190c commit d840ce1
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 10 deletions.
32 changes: 30 additions & 2 deletions lib/kickstarter/profile.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf-8
require_relative 'common'
require_relative "project_item"

module Kickstarter
class Profile
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion lib/kickstarter/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/kickstarter/project_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions lib/kickstarter/project_item.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 11 additions & 3 deletions spec/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
chimero = "fchimero"
gaspar = "georgegaspar"
rivetw = "coolminiornot"
ralfh = "ralfh"


it "finds the name" do
Expand Down Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion spec/project_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d840ce1

Please sign in to comment.