Class: Arbitrage::Profit

Inherits:
Object
  • Object
show all
Defined in:
lib/arbitrage/profit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pricesObject

Returns the value of attribute market_prices.



2
3
4
# File 'lib/arbitrage/profit.rb', line 2

def market_prices
  @market_prices
end

#nearby_clsObject

Returns the value of attribute nearby_cls.



2
3
4
# File 'lib/arbitrage/profit.rb', line 2

def nearby_cls
  @nearby_cls
end

#nearby_productsObject

Returns the value of attribute nearby_products.



2
3
4
# File 'lib/arbitrage/profit.rb', line 2

def nearby_products
  @nearby_products
end

#opportunityObject

Returns the value of attribute opportunity.



2
3
4
# File 'lib/arbitrage/profit.rb', line 2

def opportunity
  @opportunity
end

#productObject

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_pricesObject



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_priceObject



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_avgsObject



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_opportunityObject



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_opportunityObject



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_clsObject



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

Returns:

  • (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_opportunityObject



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