diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index f804f194..a497baf0 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -1,6 +1,66 @@ require './lib/far_mar' +require 'pry' module FarMar class Market - + attr_reader :id, :name, :address, :city, :county, :state, :zip + def initialize(id, name, address, city, county, state, zip) + @id = id.to_i + @name = name + @address = address + @city = city + @county = county + @state = state + @zip = zip + end + def self.all + @@markets_all ||= [] + if @@markets_all == [] + CSV.read("./support/markets.csv").map do |row| + @@markets_all.push(FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6])) + end + end + return @@markets_all + end + def self.find(id) + FarMar::Market.all.find do |market| + market.id == id + end + end + def vendors + FarMar::Vendor.all.find_all do |vendor| + self.id == vendor.market_id + end + end + def products + products = [] + vendors.map do |vendor| + vendor.products.map do |product| + products.push(product) + end + end + return products + end + def self.search(search_term) + search_term = search_term.downcase + matching_markets = [] + all.each do |market| + matching_markets.push(market) if (market.name.downcase.include? search_term) && !(matching_markets.include? market) + end + FarMar::Vendor.all.each do |vendor| + matching_markets.push(vendor.market) if (vendor.name.downcase.include? search_term) && !(matching_markets.include? vendor.market) + end + return matching_markets + end + def preferred_vendor(date = nil) + if date == nil + vendors.max_by do |vendor| + vendor.revenue + end + else + vendors.max_by do |vendor| + vendor.revenue(date) + end + end + end end end diff --git a/lib/far_mar/Product.rb b/lib/far_mar/Product.rb index 5377c1bd..aa8c98bd 100644 --- a/lib/far_mar/Product.rb +++ b/lib/far_mar/Product.rb @@ -1,6 +1,43 @@ require './lib/far_mar' module FarMar class Product - + attr_reader :id, :name, :vendor_id + def initialize(id, name, vendor_id) + @id = id.to_i + @name = name + @vendor_id = vendor_id.to_i + end + def self.all + @@products_all ||= [] + if @@products_all == [] + CSV.read("./support/products.csv").map do |row| + @@products_all.push(self.new(row[0], row[1], row[2])) + end + end + return @@products_all + end + def self.find(id) + all.find do |product| + product.id == id + end + end + def vendor + Vendor.all.find do |vendor| + vendor.id == self.vendor_id + end + end + def sales + Sale.all.find_all do |sale| + sale.product_id == self.id + end + end + def number_of_sales + sales.length + end + def self.by_vendor(vendor_id) + self.all.find_all do |product| + product.vendor_id == vendor_id + end + end end end diff --git a/lib/far_mar/Sale.rb b/lib/far_mar/Sale.rb index 9f967839..4e20ee60 100644 --- a/lib/far_mar/Sale.rb +++ b/lib/far_mar/Sale.rb @@ -1,6 +1,44 @@ require './lib/far_mar' module FarMar class Sale - + attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id + def initialize(id, amount, purchase_time, vendor_id, product_id) + @id = id.to_i + @amount = amount.to_i + @purchase_time = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") + @vendor_id = vendor_id.to_i + @product_id = product_id.to_i + end + def self.all + @@sales_all ||= [] + if @@sales_all == [] + CSV.read("./support/sales.csv").map do |row| + @@sales_all.push(FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4])) + end + end + return @@sales_all + end + def self.find(id) + all.find do |sale| + sale.id == id + end + end + def vendor + Vendor.all.find do |vendor| + vendor.id == self.vendor_id + end + end + def product + Product.all.find do |product| + product.id == self.product_id + end + end + def self.between(beginning_time, end_time) + beginning_time = DateTime.strptime(beginning_time, "%Y-%m-%d %H:%M:%S %z") + end_time = DateTime.strptime(end_time, "%Y-%m-%d %H:%M:%S %z") + self.all.find_all do |sale| + sale.purchase_time > beginning_time && sale.purchase_time < end_time + end + end end end diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index 2b2338f1..9d4d8fea 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -1,6 +1,62 @@ require './lib/far_mar' module FarMar class Vendor - + attr_reader :id, :name, :num_employees, :market_id + def initialize(id, name, num_employees, market_id) + @id = id.to_i + @name = name + @num_employees = num_employees.to_i + @market_id = market_id.to_i + end + def self.all + @@vendors_all ||= [] + if @@vendors_all == [] + CSV.read("./support/vendors.csv").map do |row| + @@vendors_all.push(FarMar::Vendor.new(row[0], row[1], row[2], row[3])) + end + end + return @@vendors_all + end + def self.find(id) + all.find do |vendor| + vendor.id == id + end + end + def market + Market.all.find do |market| + market.id == self.market_id + end + end + def products + Product.all.find_all do |product| + product.vendor_id == self.id + end + end + def sales + Sale.all.find_all do |sale| + sale.vendor_id == self.id + end + end + def revenue(date = nil) + if date == nil + amounts = self.sales.map do |sale| + sale.amount + end + else + date = DateTime.parse(date) + amounts = [] + sales.each do |sale| + if sale.purchase_time.to_date == date.to_date + amounts.push(sale.amount) + end + end + end + amounts.inject(0) { |result, sale| result + sale } + end + def self.by_market(market_id) + self.all.find_all do |vendor| + market_id == vendor.market_id + end + end end end diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index 53003bca..4124d751 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -1,11 +1,76 @@ require 'spec_helper' +require 'pry' describe FarMar::Market do before :each do - @market = FarMar::Market.new + @market = FarMar::Market.new("500", "Montefiore Medical Center Farmers Market_Thursday", "111 E. 210th Street", "Bronx", "Bronx", "New York", "10467") end describe "#new" do it "creates a new Market instance" do expect(@market).to be_an_instance_of FarMar::Market end end + describe "#all" do + it "creates new Market instances" do + expect(FarMar::Market.all[0].id).to eq 1 + end + it "returns a collection representing all the markets in the CSV" do + expect(FarMar::Market.all.length).to eq 500 + end + end + describe "#find(id)" do + it "returns the market instance with an id matching the parameter" do + expect(FarMar::Market.find(9).id).to eq 9 + end + end + describe ".vendors" do + it "returns collection of all vendor instances with market_id matching market instance's id" do + expect(@market.vendors.length).to eq 10 + end + it "returns a collection of vendor instances" do + expect(@market.vendors[0].class).to eq FarMar::Vendor + end + end + describe ".products" do + it "returns a collection of product instances associated through vendors" do + #vendor ids: 2681, 2682, 2683, 2684, 2685, 2686, 2687, 2688, 2689, 2690 + expect(@market.products.length).to eq 34 + end + end + describe "#search(search_term)" do + it "returns market instances where the market name contains search_term" do + expect(FarMar::Market.search('braintree')[0].id).to eq 36 + end + it "returns market instances where the vendor name contains search_term" do + expect(FarMar::Market.search("Eenhol")[1].id).to eq 21 + end + it "returns all market and vendor instances where search_term is within the market or vendor name only" do + search = FarMar::Market.search("johns") + # 3 markets with "johns" in name + # 25 vendors with "johns" in name + expect(search.length).to eq 28 + end + it "returns only market instances" do + search = FarMar::Market.search("johns") + incorrect_class = search.find_all do |instance| + instance.class != FarMar::Market + end + expect(incorrect_class).to eq [] + end + it "returns empty array if no search results" do + expect(FarMar::Market.search("a;oeaifhgsoifgj ~~~")).to eq [] + end + it "does not duplicate (if same market and vendor)" do + expect(FarMar::Market.search("").length).to eq 500 + end + end + describe ".preferred_vendor" do + it "returns the vendor with the highest revenue" do + expect(@market.preferred_vendor.id).to eq 2684 + end + end + describe ".preferred_vendor(date)" do + it "returns the vendor with the highest revenue for date passed in" do + expect(@market.preferred_vendor("2013-11-09").id).to eq 2687 + end + end end diff --git a/spec/far_mar/Product_spec.rb b/spec/far_mar/Product_spec.rb index 32cf098d..da0a87ed 100644 --- a/spec/far_mar/Product_spec.rb +++ b/spec/far_mar/Product_spec.rb @@ -1,11 +1,45 @@ require 'spec_helper' describe FarMar::Product do before :each do - @product = FarMar::Product.new + @product = FarMar::Product.new("8188", "Brief Carrots", "2689") end describe "#new" do it "creates a new Product instance" do expect(@product).to be_an_instance_of FarMar::Product end end + describe "#all" do + it "creates new Product instances" do + expect(FarMar::Product.all[0].name).to eq "Dry Beets" + end + it "returns a collection representing all the products in the CSV" do + expect(FarMar::Product.all.length).to eq 8193 + end + end + describe "#find(id)" do + it "returns the product instance with an id matching the parameter" do + expect(FarMar::Product.find(14).id).to eq 14 + end + end + describe ".vendor" do + it "returns the vendor instance whose id matches the product's vendor_id" do + expect(@product.vendor.id).to eq 2689 + end + end + describe ".sales" do + it "returns a collection of sales instances whose product_id matches the product's id" do + expect(@product.sales.length).to eq 2 + end + end + describe ".number_of_sales" do + it "returns number of sales instances whose product_id matches the product's id" do + expect(@product.number_of_sales).to eq 2 + end + end + describe "#by_vendor(vendor_id)" do + it "returns all product instances whose vendor_id matches the parameter passed in" do + vendor = FarMar::Product.by_vendor(2672) + expect(vendor.length).to eq 4 + end + end end diff --git a/spec/far_mar/Sale_spec.rb b/spec/far_mar/Sale_spec.rb index 70ba0c60..39c5a695 100644 --- a/spec/far_mar/Sale_spec.rb +++ b/spec/far_mar/Sale_spec.rb @@ -1,11 +1,61 @@ require 'spec_helper' describe FarMar::Sale do before :each do - @sale = FarMar::Sale.new + @sale = FarMar::Sale.new("3339", "7316", "2013-11-11 13:56:55 -0800", "742", "2213") end describe "#new" do it "creates a new Sale instance" do expect(@sale).to be_an_instance_of FarMar::Sale end end + describe "#all" do + it "creates new Sale instances" do + expect(FarMar::Sale.all[1].amount).to eq 2262 + end + it "returns a collection representing all sales in the CSV" do + expect(FarMar::Sale.all.length).to eq 12798 + end + end + describe "#find(id)" do + it "returns the sales instance with an id matching the parameter" do + expect(FarMar::Sale.find(20).id).to eq 20 + end + end + describe ".vendor" do + it "returns the vendor instance whose id matches the sale's vendor_id" do + expect(@sale.vendor.id).to eq 742 + end + end + describe ".product" do + it "returns the product instance whose id matches the sales's product_id" do + expect(@sale.product.id).to eq 2213 + end + end + describe "#between(beginning_time, end_time)" do + context "when beginning_time is before end_time" do + begin_time = "2013-11-06 08:47:12 -0800" + end_time = "2013-11-06 08:52:59 -0800" + it "returns a collection of sales instances" do + expect(FarMar::Sale.between(begin_time, end_time)[1].class).to eq FarMar::Sale + end + it "returns all sales that occurred between beginning_time and end_time (exclusive)" do + expect(FarMar::Sale.between(begin_time, end_time).length).to eq 4 + expect(FarMar::Sale.between(begin_time, end_time)[1].id).to eq 4119 + end + end + context "when end_time is before beginning_time" do + it "returns an empty array" do + begin_time = "2013-11-06 08:52:59 -0800" + end_time = "2013-11-06 08:47:12 -0800" + expect(FarMar::Sale.between(begin_time, end_time)).to eq [] + end + end + context "when beginning_time and end_time are the same" do + it "returns an empty array" do + begin_time = "2013-11-06 08:47:12 -0800" + end_time = "2013-11-06 08:47:12 -0800" + expect(FarMar::Sale.between(begin_time, end_time)).to eq [] + end + end + end end diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index ddf4476d..475c6221 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -1,11 +1,72 @@ require 'spec_helper' describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new + @vendor = FarMar::Vendor.new("2689", "Durgan-Moen", "1", "500") end describe "#new" do it "creates a new Vendor instance" do expect(@vendor).to be_an_instance_of FarMar::Vendor end end + describe "#all" do + it "creates a new Vendor instance" do + expect(FarMar::Vendor.all[3].market_id).to eq 1 + end + it "returns a collection representing all vendors in CSV" do + expect(FarMar::Vendor.all.length).to eq 2690 + end + end + describe "#find(id)" do + it "returns the vendor instance with an id matching the parameter" do + expect(FarMar::Vendor.find(556).id).to eq 556 + end + end + describe ".market" do + it "returns the market instance whose id matches the vendor's market_id" do + expect(@vendor.market.id).to eq 500 + end + end + describe ".products" do + it "returns all product instances whose vendor_id matches the vendor's id" do + expect(@vendor.products.length).to eq 5 + end + end + describe ".sales" do + it "returns all sales instances whose vendor_id matches the vendor's id" do + expect(@vendor.sales.length).to eq 2 + end + end + describe "#by_market(market_id)" do + it "returns all vendor instances whose market_id matches the parameter passed in" do + vendors = FarMar::Vendor.by_market(498) + expect(vendors.length).to eq 2 + end + end + describe ".revenue(date)" do + context "when no date parameter is passed in" do + it "returns total revenue from all dates" do + expect(@vendor.revenue).to eq 7586 + end + it "returns 0 when vendor had no sales" do + @vendor_0_sales = FarMar::Vendor.new("51", "Bernier Inc", "1", "12") + expect(@vendor_0_sales.revenue).to eq 0 + end + end + context "when no sales happened on date" do + it "returns 0" do + expect(@vendor.revenue("2013-11-09")).to eq 0 + end + end + context "when 1 sale happened on date" do + it "returns the sale amount" do + expect(@vendor.revenue("2013-11-10")).to eq 3793 + end + end + context "when multiple sales happened on date" do + it "returns the sum of sales amounts for the date" do + @vendor2 = FarMar::Vendor.new("2687", "Pacocha Group", "5", "500") + expect(@vendor2.revenue("2013-11-12")).to eq 20781 + end + end + end end diff --git a/support/markets.csv b/support/markets.csv index 6a265cd0..9eff6fb4 100644 --- a/support/markets.csv +++ b/support/markets.csv @@ -497,4 +497,4 @@ 497,Nevada County Farmers Market,11078 West First Street North,Prescott,Nevada,Arkansas,71857 498,Morningside Park Farmers Market,110th Street & Manhattan Avenue,Mornignside Heights,New York,New York,10027 499,Fletcher Allen's Farmers' Market,111 Colchester Ave 204 EN3,Burlington,Chittenden,Vermont,5401 -500,Montefiore Medical Center Farmers Market_Thursday,111 E. 210th Street,Bronx,Bronx,New York,10467 \ No newline at end of file +500,Montefiore Medical Center Farmers Market_Thursday,111 E. 210th Street,Bronx,Bronx,New York,10467