Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@spenserhuang
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spenserhuang/d625e87df6053a28cd34 to your computer and use it in GitHub Desktop.
Save spenserhuang/d625e87df6053a28cd34 to your computer and use it in GitHub Desktop.
CSV and MVC Notes 05/07/2015
# There may be a few typos here and there and I also did not put up the Twilio Portion.
# ------------------------------------------------------------
# CSV
# ------------------------------------------------------------
require 'csv'
class Person
def initialize(args = {})
@first_name = args.fetch(:first_name)
@last_name = args.fetch(:last_name)
@email = args.fetch(:email)
end
end
CSV.foreach('sample.csv', :headers => true, :header_converters => :symbol) do |row|
p Person.new(row)
end
# CSV.open('sample.csv') do |line|
# p line
# end
# p CSV.read('sample.csv')
# CSV.foreach('sample.csv') do |line|
# p line
# end
# CSV.foreach('sample.csv', :headers => true, :header_converters => :symbol) do |row|
# p row[:first_name]
# p row[:last_name]
# p row[:email]
# end
# CSV.foreach('sample.csv', :headers => true) do |row|
# p row['first_name']
# p row['last_name']
# p row['email']
# end
# ------------------------------------------------------------
# MVC
# ------------------------------------------------------------
# p File.readlines('jokes.txt')
# require 'open-uri'
# class Joker
# def initialize
# @jokes = default_jokes
# end
# def joke
# @jokes.sample.capitalize
# end
# private
# def default_jokes
# # v1 where we use a literal array
# # [
# # 'knock knock...',
# # 'there once was a man from...',
# # 'guy walks into a bar',
# # 'you know when people...'
# # ]
# # v2 where we grab a resource from the interwebs
# # contents = open("http://www.textfiles.com/humor/JOKES/100.txt").each_line.to_a
# # contents.shift(5)
# # p contents.map(&:chomp).map{|line| line[7..-1]}.compact
# # v3 where we open a text file
# clean_contents = clean_file(File.readlines('jokes.txt'))
# end
# def clean_file(contents)
# contents.shift(5)
# contents.map(&:chomp).map{|line| line[7..-1]}.compact
# end
# end
# class Viewer
# def render(text) #, mode = :console
# # case mode
# # when :console
# puts header
# puts text
# puts footer
# # when :sms
# # insert twilio code
# # end
# end
# def prompt(text)
# render(text)
# gets.chomp
# end
# private
# def header
# "welcome, to my funny joker system thingamajig\n" + "-" * 30
# end
# def footer
# "-" * 30
# end
# end
# class JokeMachine
# def self.run
# view = Viewer.new
# joker = Joker.new
# view.render(joker.joke)
# end
# end
# JokeMachine.run
# ------------------------------------------------------------
# MVC
# ------------------------------------------------------------
# class Joker
# def initialize
# @jokes = default_jokes
# end
# def joke
# @jokes.sample.capitalize
# end
# private
# def default_jokes
# [
# 'knock knock...',
# 'there once was a man from...',
# 'guy walks into a bar',
# 'you know when people...'
# ]
# end
# end
# class Viewer
# def render(text), mode = :console
# case mode
# when :console
# puts header
# puts text
# puts footer
# when :sms
# # insert twilio code
# end
# end
# def prompt(text)
# render(text)
# gets.chomp
# end
# private
# def header
# "welcome, to my funny joker system thingamajig\n" + "-" * 30
# end
# def footer
# "-" * 30
# end
# end
# class JokeMachine
# def self.run
# view = Viewer.new
# joker = Joker.new
# view.render(joker.joke)
# end
# end
# JokeMachine.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment