class HNjobsCLI

Public Instance Methods

call() click to toggle source

Initialize program with greeting and starts interface

# File lib/controller.rb, line 20
def call
  puts yellow(greeting)
  interface
end
details(input) click to toggle source

show details about a specific job posting

# File lib/controller.rb, line 83
def details(input)
  job = Job.find_by_id(input.to_i)
  if job
    puts "\n\n" + job.description
    puts menu
  else
    puts "Out of range. Please input a number between 1 and #{Job.count}"
  end
end
filter() click to toggle source

Filter jobs

# File lib/controller.rb, line 73
def filter
  puts 'Enter keyword:'
  keyword = gets.downcase.strip
  Job.filter(keyword).each do |job|
    puts "#{job.id} #{job.firstline}"
  end
  puts menu
end
green(text) click to toggle source
# File lib/controller.rb, line 116
def green(text) "\e[32m#{text}\e[0m" end
greeting() click to toggle source
# File lib/controller.rb, line 93
  def greeting
    <<~EOL
      Hello! Welcome to the Hacker News Job Scraper.
      I can distill an 'Ask HN: Who is hiring?' post down to its fine job postings.
    EOL
  end
interface() click to toggle source

Interface with user

# File lib/controller.rb, line 26
def interface
  scrape
  input = ''
  while input != 'exit'
    input = gets.strip
    case input
      when -> (input) { input.to_i != 0 } # this proc lets you make comparisons inside case statements
        details(input)
      when 'list'
        list
      when 'scrape'
        scrape
      when 'filter'
        filter
      when 'exit'
        puts "\n\nGoodbye!\n\n"
      else
        puts red('Unknown command')
        puts menu
    end
  end
end
list() click to toggle source

List jobs with numbers for reference

# File lib/controller.rb, line 50
def list
  puts "\n\n#{Job.count} job postings found\n\n"
  Job.list.map do |job|
    puts "#{job.id}. #{job.firstline}"
  end
  puts menu
end
menu() click to toggle source
red(text) click to toggle source

coloration

# File lib/controller.rb, line 115
def red(text) "\e[31m#{text}\e[0m" end
scrape() click to toggle source

Scrape page and create jobs

# File lib/controller.rb, line 59
def scrape
  Job.reset
  puts 'Enter a URL to scrape (or press enter for default):'
  url = gets.strip
  url = 'https://news.ycombinator.com/item?id=16052538' if url == ''
  puts "Scraping...\n\n"
  jobs_data = Scraper.scrape(url)
  jobs_data.each_with_index do |job_data, i|
    job = Job.new(job_data.merge({id: i+1}))
  end
  puts list
end
yellow(text) click to toggle source
# File lib/controller.rb, line 117
def yellow(text) "\e[33m#{text}\e[0m" end