Class: Arbitrage::Profit
- Inherits:
-
Object
- Object
- Arbitrage::Profit
- Defined in:
- lib/arbitrage/profit.rb
Instance Attribute Summary collapse
-
#market_prices ⇒ Object
Returns the value of attribute market_prices.
-
#nearby_cls ⇒ Object
Returns the value of attribute nearby_cls.
-
#nearby_products ⇒ Object
Returns the value of attribute nearby_products.
-
#opportunity ⇒ Object
Returns the value of attribute opportunity.
-
#product ⇒ Object
Returns the value of attribute product.
Instance Method Summary collapse
- #avg_profit(price) ⇒ Object
- #collect_market_prices ⇒ Object
- #compute_avg_market_price ⇒ Object
- #create_query(index) ⇒ Object
- #display_market_avgs ⇒ Object
- #display_market_opportunity ⇒ Object
-
#initialize(product) ⇒ Profit
constructor
A new instance of Profit.
- #list_opportunity ⇒ Object
- #products_from_nearby_cls ⇒ Object
- #profit_margin?(price) ⇒ Boolean
- #save_opportunity ⇒ Object
Constructor Details
#initialize(product) ⇒ Profit
Returns a new instance of Profit.
4 5 6 7 8 9 10 |
# File 'lib/arbitrage/profit.rb', line 4 def initialize(product) self.product = product self.nearby_cls = Arbitrage::NearbyCraigslists.new(self.product,self.product.scraper) self.opportunity = {} self.nearby_products = {} self.market_prices = {} end |
Instance Attribute Details
#market_prices ⇒ Object
Returns the value of attribute market_prices.
2 3 4 |
# File 'lib/arbitrage/profit.rb', line 2 def market_prices @market_prices end |
#nearby_cls ⇒ Object
Returns the value of attribute nearby_cls.
2 3 4 |
# File 'lib/arbitrage/profit.rb', line 2 def nearby_cls @nearby_cls end |
#nearby_products ⇒ Object
Returns the value of attribute nearby_products.
2 3 4 |
# File 'lib/arbitrage/profit.rb', line 2 def nearby_products @nearby_products end |
#opportunity ⇒ Object
Returns the value of attribute opportunity.
2 3 4 |
# File 'lib/arbitrage/profit.rb', line 2 def opportunity @opportunity end |
#product ⇒ Object
Returns the value of attribute product.
2 3 4 |
# File 'lib/arbitrage/profit.rb', line 2 def product @product end |
Instance Method Details
#avg_profit(price) ⇒ Object
39 40 41 |
# File 'lib/arbitrage/profit.rb', line 39 def avg_profit(price) (price) - (self.product.price.scan(/\d/).join.to_i) end |
#collect_market_prices ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/arbitrage/profit.rb', line 52 def collect_market_prices self.opportunity.each do |key, value| self.nearby_cls.all_indexes.detect do |index| if value[:url] == index if !(self.market_prices[index]) self.market_prices[index] = [] else self.market_prices[index] << value[:price].scan(/\d/).join.to_i end end end end end |
#compute_avg_market_price ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/arbitrage/profit.rb', line 44 def compute_avg_market_price collect_market_prices self.market_prices.each do |index, value| avg= value.inject{ |sum, el| sum + el} / value.size self.market_prices[index] = avg end end |
#create_query(index) ⇒ Object
103 104 105 |
# File 'lib/arbitrage/profit.rb', line 103 def create_query(index) self.nearby_cls.scraper.craigslist_search_url(index,self.product.query) end |
#display_market_avgs ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/arbitrage/profit.rb', line 30 def display_market_avgs self.market_prices.each_with_index do |(index, value), i| puts "#{i+1}.Craiglist Home Page:#{index}" puts "Potential Profit:$#{avg_profit(value)}" puts "Market Page:#{create_query(index)}" puts "------------------------------------------------------" end end |
#display_market_opportunity ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/arbitrage/profit.rb', line 21 def display_market_opportunity save_opportunity compute_avg_market_price puts "Below Are markets you should sell your product in to make a profit." puts " $$$$$<--Markets-->$$$$$" puts puts "------------------------------------------------------" display_market_avgs end |
#list_opportunity ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/arbitrage/profit.rb', line 12 def list_opportunity self.nearby_cls.save_original_url puts "Looking for places to sell for a profit at nearby Craiglists" self.nearby_cls.save_all_indexes puts "Profitable Selling Markets:" puts"______________________________________" display_market_opportunity end |
#products_from_nearby_cls ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/arbitrage/profit.rb', line 82 def products_from_nearby_cls q2 = 0 self.nearby_cls.all_indexes.each_with_index do |index, q| counter = 0 if index != self.nearby_cls.scraper.url doc = Nokogiri::HTML(open(create_query(index))) doc.css('.row').each_with_index do |row, i| if counter < 6 self.nearby_products["#{q2+1}".to_s] = { name: (row.css('.hdrlnk').text), price: (row.css('.l2 .price').text), url: index } q2 += 1 counter +=1 end end end end end |
#profit_margin?(price) ⇒ Boolean
75 76 77 78 79 80 |
# File 'lib/arbitrage/profit.rb', line 75 def profit_margin?(price) margin_threshold = 10 margin = 0 margin = (price) - (self.product.price.to_i) margin > margin_threshold && price > 5 end |
#save_opportunity ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/arbitrage/profit.rb', line 66 def save_opportunity products_from_nearby_cls self.nearby_products.each do |key, value| if profit_margin?(value[:price].scan(/\d/).join.to_i) self.opportunity[key] = value end end end |