Class: MlS4m::MercadoLivre

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

Instance Method Summary collapse

Constructor Details

#initializeMercadoLivre

Returns a new instance of MercadoLivre.



8
9
10
11
12
13
# File 'lib/ml_s4m.rb', line 8

def initialize 
  @pn = nil
  @url_default = 'http://informatica.mercadolivre.com.br/PARTNUMBER_DisplayType_LF_OrderId_PRICE*DESC_ItemTypeID_N'
  http://informatica.mercadolivre.com.br/me087_DisplayType_LF_OrderId_PRICE*DESC
  @offers = []
end

Instance Method Details

#setPNSearch(partNumber = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ml_s4m.rb', line 15

def setPNSearch(partNumber = nil)
  unless partNumber.nil?
    @pn = partNumber.downcase
    url_page = @url_default.gsub('PARTNUMBER', @pn)
    page = Nokogiri::HTML(open(url_page))
    unless page.nil?
      list = page.css('div.section').css('ol#searchResults').css('li')
      list_price = []
      list.each do |item|
        if !item.children[6].nil? && !item.children[6].children[1].nil?
          price = item.children[6].children[1].children[1] unless item.children[6].children[1].children[1].nil? 
          list_price << "#{price.children[0]},#{price.children[1].text}"unless price.nil?
        end
      end
      @offers = list_price.map{ |x| current2int(x) }
    else 
      @offers = 0
    end
  end 
end

#top5OffersObject



36
37
38
# File 'lib/ml_s4m.rb', line 36

def top5Offers
   @offers[1..5] if @offers.count > 4 
end