Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FarMar! - Deirdre #45

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
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 @@
coverage
.DS_Store
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
35 changes: 35 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# gems your project needs
require 'csv'
require 'chronic'
require 'date'

# our namespace module
module FarMar
end

class RepeatMethods
# DATA = "./support/vendors.csv"

def self.all
CSV.read(self::DATA).map { |line| self.new(line) }
end

def self.find(id)
CSV.foreach(self::DATA) do |line|
return self.new(line) if line[0].to_i == id
end
end

end

PRODUCT_CSV = "./support/products.csv"
MARKET_CSV = "./support/markets.csv"
VENDORS_CSV = "./support/vendors.csv"
SALE_CSV = "./support/sales.csv"
# all of our data classes that live in the module
require_relative './lib/farmar_market'
# ...require all needed classes

require_relative "./lib/farmar_sale"
require_relative './lib/farmar_vendor'
require_relative './lib/farmar_product'
81 changes: 81 additions & 0 deletions lib/farmar_market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

class FarMar::Market < RepeatMethods
DATA = "./support/markets.csv"

attr_reader :market_id, :name, :address, :city, :county, :state, :zip

def initialize(market_info)
@market_id, @name, @address, @city, @county, @state, @zip = market_info
@market_id = @market_id.to_i
end

# vendors: returns a collection of FarMar::Vendor instances that are associated with
# the market by the market_id field.
def vendors
vendors = CSV.read(VENDORS_CSV).select { |line| line[3].to_i == self.market_id }
vendors.collect { |v| FarMar::Vendor.new(v)}
end

# products returns a collection of FarMar::Product instances that are associated to the
# market through the FarMar::Vendor class.
# use vendors method to find vendors, make instances of their products
def products
vendor_ids = vendors.collect { |vendor| vendor.vendor_id}
CSV.read(PRODUCT_CSV).collect do |line|
FarMar::Product.new(line) if vendor_ids.include? line[2].to_i
end
end

# self.search(search_term) returns a collection of FarMar::Market instances
# where the market name or vendor name contain the search_term.
def self.search(search_term)
#find vendors whose names match the search term and collect their ids
vendor_matches = CSV.read(VENDORS_CSV).select do |line|
line[1].downcase.include? search_term.downcase
end
vendor_ids = vendor_matches.collect { |vendor| vendor[3]}

#iterate through markets to see if any market names match search term
market_matches = CSV.read(MARKET_CSV).select do |line|
(line[1].downcase.include? search_term.downcase) || (vendor_ids.include? line[0])
end
market_matches.collect { |market| FarMar::Market.new(market)}

end

# prefered_vendor: returns the vendor with the highest revenue
def preferred_vendor
self.vendors.max_by { |vendor| vendor.revenue}
end

def worst_vendor
self.vendors.min_by { |vendor| vendor.revenue}
end

#prefered_vendor(date): returns the vendor with the highest revenue for the given date
def preferred_vendor_on(date)
date = Chronic.parse(date)
#when you give a day it defaults to noon, so i caught the whole day by adding and substracting 11hours 59min
time_range = ((date - 43199)..(date + 43199))
vendor_ids = self.vendors.collect { |vendor| vendor.vendor_id }
vendor_sales = CSV.read(SALE_CSV).select do |line|
vendor_ids.include? line[3].to_i
end
vendor_sales.collect! { |sale| FarMar::Sale.new(sale) }
vendor_sales.select { |sale| time_range.include? sale.purchase_time }
#create a hash that will assign a key to each vendor id and add amount to value
answer = {}
vendor_sales.each do |sale|
vendor = sale.vendor_id
amount = sale.amount

if answer[vendor].nil?
answer[vendor] = amount
else
answer[vendor] += amount
end
end
answer = answer.max_by { |vendor, amount| amount}
return FarMar::Vendor.find(answer[0])
end
end
41 changes: 41 additions & 0 deletions lib/farmar_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class FarMar::Product < RepeatMethods
DATA = "./support/products.csv"

attr_reader :product_id, :name, :vendor_id
def initialize(product_info)
@product_id, @name, @vendor_id = product_info
@product_id = @product_id.to_i
@vendor_id = @vendor_id.to_i
end

#vendor: returns the FarMar::Vendor instance that is associated with this vendor
#using the FarMar::Product vendor_id field
def vendor
CSV.foreach(VENDORS_CSV) do |line|
return FarMar::Vendor.new(line) if line[0].to_i == vendor_id
end
end

# sales: returns a collection of FarMar::Sale instances that are associated using
# the FarMar::Sale product_id field.
def sales
sales = CSV.read(SALE_CSV).select do |line|
line[4].to_i == self.product_id
end
sales.collect { |sale| FarMar::Sale.new(sale)}
end

#number_of_sales: returns the number of times this product has been sold.
def number_of_sales
sales.length
end

#self.by_vendor(vendor_id): returns all of the products with the given vendor_id
def self.by_vendor(vendor_id)
products = CSV.read(PRODUCT_CSV).select do |line|
line[2].to_i == vendor_id
end
products.collect { |product| FarMar::Product.new(product)}
end

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

class FarMar::Sale < RepeatMethods
DATA = "./support/sales.csv"

attr_reader :sale_id, :amount, :purchase_time, :vendor_id, :product_id
def initialize(sale_info)
@sale_id, @amount, @purchase_time, @vendor_id, @product_id = sale_info
@sale_id = @sale_id.to_i
@amount = @amount.to_i
@purchase_time = Chronic.parse(@purchase_time)
@vendor_id = @vendor_id.to_i
@product_id = @product_id.to_i
end

# vendor: returns the FarMar::Vendor instance that is associated with this sale using the
# FarMar::Sale vendor_id field
def vendor
CSV.foreach(VENDORS_CSV) do |line|
return FarMar::Vendor.new(line) if line[0].to_i == self.vendor_id
end
end

# product: returns the FarMar::Product instance that is associated with this sale using
# the FarMar::Sale product_id field
def product
CSV.foreach(PRODUCT_CSV) do |line|
return FarMar::Product.new(line) if line[0].to_i == self.product_id
end
end

def self.between(beginning_time, end_time)
begin_time = Chronic.parse(beginning_time)
end_time = Chronic.parse(end_time)
time_range = (begin_time..end_time)
sales = CSV.read(SALE_CSV).select do |line|
time_range.include? Chronic.parse(line[2])
end
sales.collect { |sale| FarMar::Sale.new(sale)}
end
end
51 changes: 51 additions & 0 deletions lib/farmar_vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

class FarMar::Vendor < RepeatMethods
DATA = "./support/vendors.csv"

attr_reader :vendor_id, :name, :number_of_employees, :market_id

def initialize(vendor_info)
@vendor_id, @name, @number_of_employees, @market_id = vendor_info
@vendor_id = @vendor_id.to_i
@number_of_employees = @number_of_employees.to_i
@market_id = @market_id.to_i
end

#market: returns the FarMar::Market instance that is associated with this vendor using
#the FarMar::Vendor market_id field
def market
CSV.foreach(MARKET_CSV) do |line|
return FarMar::Market.new(line) if line[0].to_i == self.market_id
end
end

#products: returns a collection of FarMar::Product instances that are associated by
#the FarMar::Product vendor_id field.
def products
products = CSV.read(PRODUCT_CSV).select do |line|
line[2].to_i == self.vendor_id
end
products.collect { |product| FarMar::Product.new(product)}
end

#sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field.
def sales
sales = CSV.read(SALE_CSV).select do |line|
line[3].to_i == vendor_id
end
sales.collect { |sale| FarMar::Sale.new(sale)}
end

#revenue: returns the the sum of all of the vendor's sales (in cents)
def revenue
all_money = sales.collect { |sale| sale.amount}
all_money.reduce(0, :+)
end

#self.by_market(market_id): returns all of the vendors with the given market_id
def self.by_market(market_id)
vendors = CSV.read(VENDORS_CSV).select { |line| line[3].to_i == market_id }
vendors.collect { |vendor| FarMar::Vendor.new(vendor)}
end

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


describe FarMar::Market do

it "does this exist" do
FarMar::Market.wont_be_nil
end

it "creates an array of market instance" do
FarMar::Market.all.class.must_equal(Array)
end

it "creates instances of market from csv file" do
classes = FarMar::Market.all.map { |market| market.class }
classes.uniq.must_equal [FarMar::Market]
end

it "creates an instance of FarMar::Market" do
FarMar::Market.new([4, "yay", "dirt", "ryan", "claire", "five", "six"]).must_be_instance_of(FarMar::Market)
end

describe "self.find" do

it "should find instance of market with matching id" do
FarMar::Market.find(1).name.must_equal("People's Co-op Farmers Market")
end

it "should return nil when it can't find a matching market id" do
FarMar::Market.find(2727272727).must_equal(nil)
end

end

describe ".vendors" do
let(:market_one) {FarMar::Market.find(1)}

it "should return an array of that market's vendors" do
market_one.vendors.must_be_instance_of(Array)
end

it "vendors should have same market_id as the market" do
market_one.market_id.must_equal(market_one.vendors[1].market_id)
end

end

describe "products" do
let(:market_one) {FarMar::Market.find(1)}

it "should return array of products sold by that market" do
market_one.products.must_be_instance_of Array
end

it 'should return instances of product' do
market_one.products[0].class.must_equal FarMar::Product
end

end

describe 'self.search' do
it "should return collection where search term is in market name" do
FarMar::Market.search('school').length.must_equal 3
end
end

describe 'preferred_vendor' do
it "should return vendor in that market with highest revenue" do
market = FarMar::Market.find(1)
market.preferred_vendor.vendor_id.must_equal(5)
end
end

describe 'worst_vendor' do
it "should return vendor in that market with least revenue" do
market = FarMar::Market.find(1)
market.worst_vendor.vendor_id.must_equal(6)
end
end

describe "preferred_vendor_on(date)" do
let (:market) { FarMar::Market.find(1)}

it "returns vendor with most revenue on specific date" do
market.preferred_vendor_on("11/12/13").vendor_id.must_equal 5
end
end

end
Loading