Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
coverage
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
farmar
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['specs/*_spec.rb']
end

task default: :test
14 changes: 14 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# gems your project needs
require 'csv'
require 'time'

# our namespace module
module FarMar; end

# all of our data classes that live in the module
require './lib/market.rb'
require './lib/product.rb'
require './lib/sale.rb'
require './lib/vendor.rb'

# ...require all needed classes
81 changes: 81 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

class FarMar::Market
attr_reader :id, :name
def initialize(market_hash)
@id = market_hash[:id]
@name = market_hash[:name]
@address = market_hash[:address]
@city = market_hash[:city]
@county = market_hash[:county]
@state = market_hash[:state]
@zip = market_hash[:zip]
end

def self.all
all_markets = []
CSV.foreach("support/markets.csv", "r") do |line|
all_markets << self.new({:id => line[0], :name => line[1], :address => line[2], :city => line[3], :county => line[4], :state => line[5], :zip => line[6]})
end
return all_markets
end

def self.find(id)
self.all.each do |market_inst|
if market_inst.id.to_s == id.to_s
return market_inst
end
end
return false
end

def vendors
#Returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field.
FarMar::Vendor.all.find_all { |vendor| vendor.market_id == @id}
end

def products
# Returns a collection of FarMar::Product instances that are associated to the market through the FarMar::Vendor class.
# We can get with FarMar::Market - vendors an array of vendor instances
# We can then get with FarMar::Vendor - products and array of products for each vendor
vendors.collect { |vendor| vendor.products }
end

def self.search(search_term)
# Returns a collection of FarMar::Market instances where the market name or vendor name contain the search_term
results = []
in_market_names = self.all.find_all { |market| market.name.downcase.include? search_term.downcase }
results << in_market_names

in_vendor_names = FarMar::Vendor.all.find_all { |vendor| vendor.name.downcase.include? search_term.downcase }
results << in_vendor_names

### COMMENTED OUT BECAUSE THIS PREVIOUS METHOD TOOK 10+SECS ###
#vendor_names = []
#self.all.each do |market|
# results << market if market.name.downcase.include? search_term.downcase
# market.vendors.each do |vendor|
# if vendor.name.downcase.include? search_term.downcase
# vendor_names << market
# end
# end
#end
#results << vendor_names

return results.flatten!
end

def preferred_vendor
sorted = vendors.sort_by { |vendor| vendor.revenue }
preferred = sorted.last
puts "The preferred vendor is #{preferred.name} with a revenue of #{preferred.revenue}."
return preferred
end

def worst_vendor
sorted = vendors.sort_by { |vendor| vendor.revenue }
worst = sorted.first
puts "The worst vendor is #{worst.name} with a revenue of #{worst.revenue}."
return worst
end

end
49 changes: 49 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

class FarMar::Product
attr_reader :id, :name, :vendor_id

def initialize(vendor_hash)
@id = vendor_hash[:id]
@name = vendor_hash[:name]
@vendor_id = vendor_hash[:vendor_id]
end

def self.all
all_products = []
CSV.foreach("support/products.csv", "r") do |line|
all_products << self.new({:id => line[0], :name => line[1], :vendor_id => line[2]})
end
return all_products
end

def self.find(id)
self.all.each do |product_inst|
if product_inst.id.to_s == id.to_s
return product_inst
end
end
return false
end

def vendor
# Returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field
FarMar::Vendor.all.find { |vendor| vendor.id == @vendor_id}
end

def sales
# Returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field.
FarMar::Sale.all.find_all { |sale| sale.product_id == @id}
end

def number_of_sales
# Returns the number of times this product has been sold.
all_sales = FarMar::Sale.all.find_all { |sale| sale.product_id == @id}
return all_sales.count
end

def self.by_vendor(this_vendor_id)
# Returns an array with all of the products with the given vendor_id.
self.all.find_all { |product| product.vendor_id.to_s == this_vendor_id.to_s }
end

end
48 changes: 48 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

class FarMar::Sale
attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id

def initialize(sale_hash)
@id = sale_hash[:id]
@amount = sale_hash[:amount].to_f
@purchase_time = sale_hash[:purchase_time]
@vendor_id = sale_hash[:vendor_id]
@product_id = sale_hash[:product_id]
end

def self.all
all_sales = []
CSV.foreach("support/sales.csv", "r") do |line|
all_sales << self.new({:id => line[0], :amount => line[1], :purchase_time => Time.parse(line[2]), :vendor_id => line[3], :product_id => line[4]})
end
return all_sales
end

def self.find(id)
self.all.each do |sale_inst|
if sale_inst.id.to_s == id.to_s
return sale_inst
end
end
return false
end

def vendor
# Returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field
FarMar::Vendor.all.find { |vendor| vendor.id == @vendor_id}
end

def product
# Returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field
FarMar::Product.all.find { |product| product.id == @product_id }
end

def self.between(beginning_time, end_time)
# Returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments
begins = Time.parse(beginning_time)
ends = Time.parse(end_time)
return self.all.find_all { |sale| (begins..ends).cover?(sale.purchase_time) }
end


end
59 changes: 59 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

class FarMar::Vendor
attr_reader :id, :name, :num_employees, :market_id

def initialize(vendor_hash)
@id = vendor_hash[:id]
@name = vendor_hash[:name]
@num_employees = vendor_hash[:num_employees]
@market_id = vendor_hash[:market_id]
end

def self.all
all_vendors = []
CSV.foreach("support/vendors.csv", "r") do |line|
all_vendors << self.new({:id => line[0], :name => line[1], :num_employees => line[2], :market_id => line[3]})
end
return all_vendors
end

def self.find(id)
self.all.each do |vendor_inst|
if vendor_inst.id.to_s == id.to_s
return vendor_inst
end
end
return false
end

def market
# Returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field
FarMar::Market.all.find { |market| market.id == @market_id }
end

def products
# Returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field
FarMar::Product.all.find_all { |product| product.vendor_id == @id}
end

def sales
# Returns a collection of FarMar::Sale instances that are associated by the vendor_id field.
FarMar::Sale.all.find_all { |sale| sale.vendor_id == @id}
end

def revenue
# Returns the the sum of all of the vendor's sales (in cents)
this_vendor_sales = FarMar::Sale.all.find_all { |sale| sale.vendor_id == @id}
total = 0
this_vendor_sales.each do |a_sale|
total = total + a_sale.amount
end
return total
end

def self.by_market(this_market_id)
#Returns an array with all of the vendors with the given market_id
self.all.find_all { |vendor| vendor.market_id.to_s == this_market_id.to_s }
end

end
72 changes: 72 additions & 0 deletions specs/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require_relative './spec_helper'


describe FarMar::Market do
let(:market) { FarMar::Market.find(498) }

it "will not be nil" do
FarMar::Market.wont_be_nil
end

describe "FarMar::Market#self.all" do
it "will not be nil" do
FarMar::Market.all.wont_be_nil
end
end

describe "FarMar::Market#self.find(id)" do
it "will not be nil when it finds the market" do
market.wont_be_nil
end

it "returns false if it can't find the market" do
FarMar::Market.find(3_000_000).must_equal false
end

it "knows data about a particular market" do
market.name.must_equal "Morningside Park Farmers Market"
end
end

describe "FarMar::Market#vendors" do
it "returns an array when called" do
market.vendors.must_be_instance_of Array
end

it "returns the correct vendors" do
market.vendors[0].id.must_equal("2676")
end
end

describe "FarMar::Market#products" do
it "returns an array when called" do
market.products.must_be_instance_of Array
end

it "returns the correct products for the market" do
market.products[0][0].id.must_equal("8141")
end
end

describe "FarMar::Market#self.search(search_term)" do
it "returns an array when called" do
FarMar::Market.search("school").must_be_instance_of Array
end

it "returns the correct products for the market" do
FarMar::Market.search("school")[0].name.must_equal("Fox School Farmers Market")
end
end

describe "FarMar::Market.preferred_vendor" do
it "returns the vendor with the greatest revenue for a particular market" do
market.preferred_vendor.name.must_equal("Cruickshank Group")
end
end

describe "FarMar::Market.worst_vendor" do
it "returns the vendor with the lowest revenue for a particular market" do
market.worst_vendor.name.must_equal("Koepp-Haley")
end
end
end
Loading