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
14
15
16
17
# File 'lib/ml_s4m.rb', line 8

def initialize 
  @pn = nil
  @url = {
    :ML_INFO => 'http://informatica.mercadolivre.com.br/PARTNUMBER_DisplayType_LF_OrderId_PRICE*DESC_ItemTypeID_N',
    :ML_CAMERA => 'http://cameras.mercadolivre.com.br/PARTNUMBER_DisplayType_LF_OrderId_PRICE*DESC_ItemTypeID_N',
    :ML_PHONE => 'http://celulares.mercadolivre.com.br/PARTNUMBER_DisplayType_LF_OrderId_PRICE*DESC_ItemTypeID_N'
  }
  @url_default = nil
  @offers = []
end

Instance Method Details

#setPNSearch(partNumber = nil, category = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ml_s4m.rb', line 19

def setPNSearch(partNumber = nil, category = nil)
  unless partNumber.nil?
    @pn = partNumber.downcase
    @url_default = @url[category.to_sym] unless category.nil?
    url_page = @url_default.gsub('PARTNUMBER', @pn)
    begin
      page = Nokogiri::HTML(open(url_page))
      unless page.nil?
        list = page.css('div').css('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) }  
      end
    rescue OpenURI::HTTPError
    end
  end 
end

#top5OffersObject



42
43
44
# File 'lib/ml_s4m.rb', line 42

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