Skip to content

Commit fc30f55

Browse files
committed
First pass at Craigslist data model
1 parent f1004ec commit fc30f55

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

session-notes/2015-02-14/craigslist/Gemfile

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 1. Define our objects and object relationships (no DB)
2+
# 2. Add some hard-coded dummy data
3+
# 3. Replace dummy data with database and DataMapper
4+
5+
module Craigslist
6+
ALL_POSTINGS = []
7+
8+
def self.all
9+
ALL_POSTINGS
10+
end
11+
12+
# bikes = Craigslist.bikes(make: 'Santa Cruz', max_price: 400.00)
13+
def self.bikes(search_opts)
14+
make = search_opts[:make]
15+
max_price = search_opts[:max_price]
16+
17+
results = self.all.select do |posting|
18+
posting.category == 'bikes' &&
19+
posting.make == make &&
20+
posting.price < max_price
21+
end
22+
23+
return results
24+
end
25+
26+
Craigslist.bikes({})
27+
28+
class Posting
29+
attr_reader :category, :title, :price, :make
30+
31+
def initialize(fields)
32+
@category = fields[:category]
33+
@title = fields[:title]
34+
@price = fields[:price]
35+
@make = fields[:make]
36+
end
37+
38+
def save
39+
ALL_POSTINGS << self
40+
end
41+
end
42+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require_relative './craigslist.rb'
2+
3+
# TESTS ---------------------------------------
4+
5+
my_bike = Craigslist::Posting.new(
6+
category: 'bikes',
7+
title: 'My Mountain Bike for Sale',
8+
price: 350.00,
9+
make: 'Santa Cruz'
10+
)
11+
12+
# Test Craigslist::Posting attributes
13+
14+
p my_bike.category == 'bikes'
15+
p my_bike.title == 'My Mountain Bike for Sale'
16+
p my_bike.price == 350.00
17+
p my_bike.make == 'Santa Cruz'
18+
19+
# Test Craigslist::Posting persistence
20+
21+
p Craigslist.all.count == 0
22+
my_bike.save
23+
p Craigslist.all.count == 1
24+
25+
# Test Craigslist.all returns all postings
26+
27+
bike_two = Craigslist::Posting.new(category: 'bikes', title: 'Carbon Fibre Racing Bike', price: 500.00, make: 'Raleigh')
28+
bike_two.save
29+
30+
p Craigslist.all.count == 2
31+
p Craigslist.all.all? { |item| item.class == Craigslist::Posting }
32+
33+
# Search for a bike by make and price
34+
car = Craigslist::Posting.new(category: 'cars', title: 'Volkswagen Rabbit', price: 1000.00, make: 'Volkswagen')
35+
car.save
36+
37+
bikes = Craigslist.bikes(make: 'Santa Cruz', max_price: 400.00)
38+
# => return an array of Craigslist::Posting objects
39+
40+
bikes.first.title == 'My Mountain Bike for Sale'

0 commit comments

Comments
 (0)