class NewestKickstarter::Project

Attributes

author[RW]
name[RW]
summary[RW]
url[RW]

Public Class Methods

all() click to toggle source
# File lib/newest_kickstarter/project.rb, line 13
def self.all
  # fetch all projects if available or scrape KS
  @@all ||= scrape_newest_projects
end
find(id) click to toggle source
# File lib/newest_kickstarter/project.rb, line 18
def self.find(id)
  # find method that searches all projects by index
  # [id-1] for offset, id starts @ 1, index starts @ 0
  self.all[id-1]
end
new(name = nil, url = nil) click to toggle source

Initialize new projects with a name and url Set to nil if not provided

# File lib/newest_kickstarter/project.rb, line 7
def initialize(name = nil, url = nil)
  # Set instance variables for project properties
  @name = name
  @url = url
end

Private Class Methods

scrape_newest_projects() click to toggle source

scrape method for kickstarter projects

# File lib/newest_kickstarter/project.rb, line 36
def self.scrape_newest_projects
  # Set doc variable for all html scraped from KS Page
  doc = Nokogiri::HTML(open('https://www.kickstarter.com/discover/advanced?recommended=false&sort=newest'))
  # Set names variable for all project names scraped from doc
  names = doc.search('h6.project-title a[data-score="null"]')
  # iterate through names variable, collect name text, and url
  # instantiate new project with name text and url for each pair
  names.collect{|e| new(e.text.strip, "https://www.kickstarter.com#{e.attr("href").split("?").first.strip}")}
  # Split at "?", take first part and remove whitespace
end

Private Instance Methods

doc() click to toggle source

Set doc variable for all html scraped from an objects url

# File lib/newest_kickstarter/project.rb, line 48
def doc
  @doc ||= Nokogiri::HTML(open(self.url))
end