diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..38ba96f0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/test/tmp/ +/test/version_tmp/ +/tmp/ +/.DS_Store + +## Specific to RubyMotion: +.dat* +.repl_history +build/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalisation: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 00000000..73f861e5 --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +farmar diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..276cbf9e --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.3.0 diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..f45f7fcf --- /dev/null +++ b/Rakefile @@ -0,0 +1,10 @@ +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 \ No newline at end of file diff --git a/far_mar.rb b/far_mar.rb new file mode 100644 index 00000000..eee969a5 --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,8 @@ +require 'CSV' + +module FarMar + require_relative "./lib/markets.rb" + require_relative "./lib/products.rb" + require_relative "./lib/sales.rb" + require_relative "./lib/vendors.rb" +end \ No newline at end of file diff --git a/lib/markets.rb b/lib/markets.rb new file mode 100644 index 00000000..5eb3e78f --- /dev/null +++ b/lib/markets.rb @@ -0,0 +1,114 @@ +class FarMar::Market + attr_accessor :id, :name, :address, :city, :county, :state, :zip + MARKET_DATA = "./support/markets.csv" + + def initialize(market_hash) + @id = market_hash[:id].to_i + @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 + #returns a collection of instances, representing all of + #the objects described in the CSV + def self.all + all = [] + CSV.foreach(MARKET_DATA, "r") do |line| + market = FarMar::Market.new(id: line[0].to_i, name: line[1], + address: line[2], city: line[3], county: line[4], + zip: line[6]) + all << market + end + + return all + end + + #returns an instance of the object where the value of the + # id field in the CSV matches the passed parameter. + def self.find(id) + find = [] + CSV.foreach(MARKET_DATA, "r") do |line| + if line[0] == id.to_s + find =[line[0].to_i, line[1], line[2], line[3], + line[4], line[5], line[6]] + return find + end + end + return nil + end + + #returns a collection of FarMar::Vendor instances that + #are associated with the market by the market_id field. + def vendors + FarMar::Vendor.all.find_all do |vendor| + vendor.market_id == @id + end + end + + #returns a collection of FarMar::Product instances that + #are associated to the market through the FarMar::Vendor class + def products + products = [] + FarMar::Vendor.all.each do |vendor| + if vendor.market_id == @id + find_product = FarMar::Product.by_vendor(vendor.id) + find_product.each do |product| + products << product + end + end + end + return products + end + + #returns a collection of FarMar::Market instances where the + #market name or vendor name contain the search_term + def self.search(search_term) + market_collection = [] + markets = FarMar::Market.all + vendors = FarMar::Vendor.all + search_term = search_term.downcase + + vendors.each do |vendor| + if vendor.name.downcase.include? search_term + markets.each do |market| + if vendor.market_id == market.id + market_collection << market + end + end + end + end + + markets.each do |market| + if market.name.downcase.include? search_term + market_collection << market + end + end + return market_collection + end + + #returns the vendor with the highest revenue + def prefered_vendor + best_vendor = [] + sum = 0 + max_sum = 0 + FarMar::Vendor.all.each do |vendor| + if vendor.market_id == @id + sum = 0 + FarMar::Sale.all.each do |sale| + if sale.vendor_id == vendor.id + sum += sale.amount + end + end + + if sum > max_sum + max_sum = sum + best_vendor = vendor + end + end + end + return best_vendor + end +end \ No newline at end of file diff --git a/lib/products.rb b/lib/products.rb new file mode 100644 index 00000000..e280e5cd --- /dev/null +++ b/lib/products.rb @@ -0,0 +1,73 @@ +class FarMar::Product + attr_accessor :id, :name, :vendor_id + PRODUCT_DATA = "./support/products.csv" + + def initialize(product_hash) + @id = product_hash[:id].to_i + @name = product_hash[:name] + @vendor_id = product_hash[:vendor_id].to_i + + end + #returns a collection of instances, representing all of + #the objects described in the CSV + def self.all + all = [] + CSV.foreach(PRODUCT_DATA, "r") do |line| + product = FarMar::Product.new(id: line[0].to_i, name: line[1], + vendor_id: line[2].to_i) + all << product + end + return all + end + + #returns an instance of the object where the value of the + # id field in the CSV matches the passed parameter. + def self.find(id) + find = [] + CSV.foreach(PRODUCT_DATA, "r") do |line| + if line[0] == id.to_s + find =[line[0].to_i, line[1], line[2].to_i] + return find + end + end + return nil + end + + def vendor + FarMar::Vendor.all.find do |vendor| + vendor.id == @vendor_id + end + end + + #returns a collection of FarMar::Sale instances that are + #associated using the FarMar::Sale product_id field. + def sales + FarMar::Sale.all.find_all do |sale| + sale.product_id == @id + end + end + + #returns the number of times this product has been sold. + def number_of_sales + sum = 0 + FarMar::Sale.all.each do |sale| + if sale.product_id == @id + sum += 1 + end + end + return sum + end + + #returns all of the products with the given vendor_id + def self.by_vendor(vendor_id) + products = [] + CSV.foreach(PRODUCT_DATA, "r") do |line| + if line[2].to_i == vendor_id + products << FarMar::Product.new(id: line[0].to_i, name: line[1], + vendor_id: line[2].to_i) + end + end + return products + end + +end \ No newline at end of file diff --git a/lib/sales.rb b/lib/sales.rb new file mode 100644 index 00000000..3e3c0ecb --- /dev/null +++ b/lib/sales.rb @@ -0,0 +1,71 @@ +class FarMar::Sale + attr_accessor :id, :amount, :purchase_time, :vendor_id, :product_id + SALE_DATA = "./support/sales.csv" + + def initialize(sale_hash) + @id = sale_hash[:id].to_i + @amount = sale_hash[:amount].to_i + @purchase_time = sale_hash[:purchase_time] + @vendor_id = sale_hash[:vendor_id].to_i + @product_id = sale_hash[:product_id].to_i + + end + + #returns a collection of instances, representing all of + #the objects described in the CSV + def self.all + all = [] + CSV.foreach(SALE_DATA, "r") do |line| + sale = FarMar::Sale.new(id: line[0].to_i, + amount: line[1].to_i, purchase_time: DateTime.parse(line[2]), + vendor_id: line[3].to_i, product_id: line[4].to_i) + all << sale + end + return all + end + + #returns an instance of the object where the value of the + # id field in the CSV matches the passed parameter. + def self.find(id) + find = [] + CSV.foreach(SALE_DATA, "r") do |line| + if line[0] == id.to_s + find =[line[0].to_i, line[1].to_i, line[2].to_i, + line[3].to_i, line[4].to_i] + return find + end + end + return nil + end + + #returns the FarMar::Vendor instance that is associated + # with this sale using the FarMar::Sale vendor_id field + def vendor + FarMar::Vendor.all.find do |vendor| + vendor.id == @vendor_id + end + end + + #returns the FarMar::Product instance that is associated + #with this sale using the FarMar::Sale product_id field + def product + FarMar::Product.all.find do |product| + product.id == @product_id + end + end + + #returns a collection of FarMar::Sale objects where the + #purchase time is between the two times given as arguments + def self.between(beginning_time, end_time) + sales = [] + beginning_time = DateTime.parse(beginning_time) + end_time = DateTime.parse(end_time) + FarMar::Sale.all.find_all do |sale| + if (sale.purchase_time >= beginning_time) && + (sale.purchase_time <= end_time) + sales << sale + end + end + return sales + end +end \ No newline at end of file diff --git a/lib/vendors.rb b/lib/vendors.rb new file mode 100644 index 00000000..3eb6ccf3 --- /dev/null +++ b/lib/vendors.rb @@ -0,0 +1,86 @@ +class FarMar::Vendor + attr_accessor :id, :name, :no_of_employees, :market_id + + VENDOR_DATA = "./support/vendors.csv" + + def initialize(vendor_hash) + @id = vendor_hash[:id].to_i + @name = vendor_hash[:name] + @no_of_employees = vendor_hash[:no_of_employees].to_i + @market_id = vendor_hash[:market_id].to_i + + end + #returns a collection of instances, representing all of + #the objects described in the CSV + def self.all + all = [] + CSV.foreach(VENDOR_DATA, "r") do |line| + vendor = FarMar::Vendor.new(id: line[0].to_i, name: line[1], + no_of_employees: line[2].to_i, market_id: line[3].to_i) + all << vendor + end + return all + end + + #returns an instance of the object where the value of the + # id field in the CSV matches the passed parameter. + def self.find(id) + find = [] + CSV.foreach(VENDOR_DATA, "r") do |line| + if line[0] == id.to_s + find =[line[0].to_i, line[1], line[2].to_i, + line[3].to_i] + return find + end + end + return nil + end + + + #returns the FarMar::Market instance that is associated + #with this vendor using the FarMar::Vendor market_id field + def market + FarMar::Market.all.find do |market| + market.id == @market_id + end + end + + #returns a collection of FarMar::Product instances that + #are associated by the FarMar::Product vendor_id field. + def products + FarMar::Product.all.find_all do |product| + product.vendor_id == @id + end + end + + #returns a collection of FarMar::Sale instances that are + #associated by the vendor_id field. + def sales + FarMar::Sale.all.find_all do |sale| + sale.vendor_id == @id + end + end + + #returns the the sum of all of the vendor's sales(in cents) + def revenue + sum = 0 + FarMar::Sale.all.each do |sale| + if sale.vendor_id == @id + sum += sale.amount + end + end + return sum + end + + #returns all of the vendors with the given market_id + def self.by_market(market_id) + vendors = [] + CSV.foreach(VENDOR_DATA, "r") do |line| + if line[3].to_i == market_id + vendors << FarMar::Vendor.new(id: line[0].to_i, name: line[1], + no_of_employees: line[2].to_i, market_id: line[3].to_i) + end + end + return vendors + end +end \ No newline at end of file diff --git a/specs/markets_spec.rb b/specs/markets_spec.rb new file mode 100644 index 00000000..86b81e80 --- /dev/null +++ b/specs/markets_spec.rb @@ -0,0 +1,99 @@ +require_relative "./spec_helper" +require_relative "../far_mar" + + +describe FarMar::Market do + market1 = FarMar::Market.new(id: 1, name: "People's Co-op Farmers Market", address: "30th and Burnside", + city: "Portland", county: "Multnomah", state: "Oregon",zip: "97202") + market13 = FarMar::Market.new(id: 13, name: "Otsiningo Park Farmers' Market", + address: "1 Bevier St", city:"Binghamton",county: "Broome", + state: "New York", zip: "13905") + + let (:market15) {FarMar::Market.find(15)} + + it "it is an object we have acccess to" do + FarMar::Market.wont_be_nil + end + + describe "Market#find" do + + + it "checks that market15 is instance of an array" do + market15.must_be_instance_of(Array) + end + + it "returns nil for id numbers that don't exist" do + FarMar::Market.find(10_000).must_equal(nil) + end + + it "checks the values of each variable for market15" do + market15[0].must_equal(15) + market15[1].must_equal("Farmers Market in Denison") + market15[2].must_equal("1 block west of Main St.") + market15[3].must_equal("Denison") + market15[4].must_equal(nil) + market15[5].must_equal("Texas") + market15[6].must_equal("75090") + end + end + + describe "Market#all" do + let (:market) {FarMar::Market.all} + + it "market should be instance of hash" do + market.must_be_instance_of(Array) + end + + it "market should have 500 entries" do + market.count.must_equal(500) + end + + it "should return array full of instances" do + classes = market.map{|m| m.class} + classes.uniq.must_equal([FarMar::Market]) + end + end + + describe "Market#vendors" do + it "should return an array of vendors" do + market13.vendors.must_be_instance_of(Array) + end + + it "should return vendor count for a market" do + market13.vendors.count.must_equal(7) + end + end + + describe "Market#products" do + it "should return an array" do + market1.products.must_be_instance_of(Array) + end + + it "should return the correct size of the array" do + market1.products.count.must_equal(13) + end + end + + describe "Market#search(search_term)" do + it "should return an array" do + FarMar::Market.search("Silverdale Farmers Market").must_be_instance_of(Array) + end + + it "should return correct size of array" do + FarMar::Market.search("Von-").count.must_equal(2) + FarMar::Market.search("Top").count.must_equal(4) + FarMar::Market.search("Inc").count.must_equal(283) + end + end + + describe "Market#prefered_vendor" do + it "should return an instance of FarMar::Vendor" do + market1.prefered_vendor.must_be_instance_of(FarMar::Vendor) + end + + it "should return the correct vendor" do + market1.prefered_vendor.id.must_equal(5) + end + end + +end \ No newline at end of file diff --git a/specs/products_spec.rb b/specs/products_spec.rb new file mode 100644 index 00000000..17781810 --- /dev/null +++ b/specs/products_spec.rb @@ -0,0 +1,71 @@ +require_relative "./spec_helper" +require_relative "../far_mar" + + +describe FarMar::Product do + product171 = FarMar::Product.new(id: 171, + name: "Depressed Carrots", vendor_id: 54) + + it "it is an object we have acccess to" do + FarMar::Product.wont_be_nil + end + + describe "Product#find" do + let (:product219) {FarMar::Product.find(219)} + + it "checks that product219 is instance of an Array" do + product219.must_be_instance_of(Array) + end + + it "returns nil for id numbers that don't exist" do + FarMar::Product.find(10_000).must_equal(nil) + end + + it "checks the values of each variable for product219" do + product219[0].must_equal(219) + product219[1].must_equal("Strange Pretzel") + product219[2].must_equal(69) + end + end + + describe "Product#all" do + let (:product) {FarMar::Product.all} + + it "Product should be instance of array" do + product.must_be_instance_of(Array) + end + + it "Product should have 8193 entries" do + product.count.must_equal(8193) + end + + it "should return array full of instances" do + classes = product.map{|m| m.class} + classes.uniq.must_equal([FarMar::Product]) + end + end + + describe "Product#vendor,sales" do + it "should return an array of vendors" do + product171.vendor.must_be_instance_of(FarMar::Vendor) + product171.sales.must_be_instance_of(Array) + end + + it "should return vendor count for a market" do + product171.sales.count.must_equal(3) + product171.vendor.id.must_equal(54) + end + end + + describe "Product#number_of_sales" do + it "should return the # of sales for a product" do + product171.number_of_sales.must_equal(3) + end + end + + describe "Product#by_vendor(vendor_id)" do + it "should return count of products for a vendor" do + FarMar::Product.by_vendor(5).count.must_equal(3) + end + end +end \ No newline at end of file diff --git a/specs/sales_spec.rb b/specs/sales_spec.rb new file mode 100644 index 00000000..d26143f4 --- /dev/null +++ b/specs/sales_spec.rb @@ -0,0 +1,68 @@ +require_relative "./spec_helper" +require_relative "../far_mar" + + +describe FarMar::Sale do + it "it is an object we have acccess to" do + FarMar::Sale.wont_be_nil + end + + describe "Sale#find" do + let (:sale311) {FarMar::Sale.find(311)} + + it "checks that sale311 is instance of an Array" do + sale311.must_be_instance_of(Array) + end + + it "returns nil for id numbers that don't exist" do + FarMar::Sale.find(1000_000).must_equal(nil) + end + + it "checks the values of each variable for sale311" do + sale311[0].must_equal(311) + sale311[1].must_equal(3520) + #sale311[2].must_equal("2013-11-08 20:36:34 -0800") + sale311[3].must_equal(69) + sale311[4].must_equal(219) + end + end + + describe "sale#all" do + let (:sale) {FarMar::Sale.all} + + it "sale should be instance of array" do + sale.must_be_instance_of(Array) + end + + it "sale should have 12798 entries" do + sale.count.must_equal(12798) + end + + it "should return array full of instances" do + classes = sale.map{|m| m.class} + classes.uniq.must_equal([FarMar::Sale]) + end + end + + describe "Sale#vendor,product" do + sale253 = FarMar::Sale.new(id: 253, amount: 3924, + purchase_time: "2013-11-10 17:44:52 -0800", + vendor_id: 54, product_id: 171) + it "should return an array of vendors" do + sale253.vendor.must_be_instance_of(FarMar::Vendor) + sale253.product.must_be_instance_of(FarMar::Product) + end + + it "should return a value from method" do + sale253.vendor.id.must_equal(54) + sale253.product.id.must_equal(171) + end + end + + describe "Sale#between" do + test_sale = FarMar::Sale.between("2013-11-06 14:19:49 -0800","2013-11-07 04:34:56 -0800") + it "should return an array" do + test_sale.must_be_instance_of(Array) + end + end +end \ No newline at end of file diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..3ccb41dd --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,11 @@ +require "simplecov" +SimpleCov.start + +require "minitest" +require "minitest/spec" +require "minitest/autorun" +require "minitest/reporters" + +#give us some really pretty output +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + diff --git a/specs/vendors_spec.rb b/specs/vendors_spec.rb new file mode 100644 index 00000000..47f9d90c --- /dev/null +++ b/specs/vendors_spec.rb @@ -0,0 +1,74 @@ +require_relative "./spec_helper" +require_relative "../far_mar" + + +describe FarMar::Vendor do + vendor54 = FarMar::Vendor.new(id: 54, name: "Bayer Inc", + no_of_employees: 3, market_id:13) + + it "it is an object we have acccess to" do + FarMar::Vendor.wont_be_nil + end + + describe "Vendor#find" do + let (:vendor69) {FarMar::Vendor.find(69)} + + it "checks that Vendor69 is instance of an array" do + vendor69.must_be_instance_of(Array) + end + + it "returns nil for id numbers that don't exist" do + FarMar::Vendor.find(10_000).must_equal(nil) + end + + it "checks the values of each variable for Vendor69" do + vendor69[0].must_equal(69) + vendor69[1].must_equal("Emard-Streich") + vendor69[2].must_equal(5) + vendor69[3].must_equal(15) + end + end + + describe "Vendor#all" do + let (:vendor) {FarMar::Vendor.all} + + it "Vendor should be instance of array" do + vendor.must_be_instance_of(Array) + end + + it "Vendor should have 2690 entries" do + vendor.count.must_equal(2690) + end + + it "should return the class" do + classes = vendor.map{|m| m.class} + classes.uniq.must_equal([FarMar::Vendor]) + end + end + + describe "Vendor#market,product,sales" do + it "should return the instance type of the methods" do + vendor54.market.must_be_instance_of(FarMar::Market) + vendor54.products.must_be_instance_of(Array) + vendor54.sales.must_be_instance_of(Array) + end + + it "should return correct values from vendor methods" do + vendor54.products.count.must_equal(5) + vendor54.sales.count.must_equal(8) + vendor54.market.id.must_equal(13) + end + end + + describe "Vendor#revenue" do + it "should return the revenue from a vendor" do + vendor54.revenue.must_equal(39552) + end + end + + describe "Vendor#by_market(market_id)" do + it "should return count of vendors for a market" do + FarMar::Vendor.by_market(2).count.must_equal(3) + end + end +end \ No newline at end of file