Class: MlS4m::MercadoLivre
- Inherits:
-
Object
- Object
- MlS4m::MercadoLivre
- Defined in:
- lib/ml_s4m.rb
Instance Method Summary collapse
-
#initialize ⇒ MercadoLivre
constructor
A new instance of MercadoLivre.
- #setPNSearch(partNumber = nil, category = nil) ⇒ Object
- #top5Offers ⇒ Object
Constructor Details
#initialize ⇒ MercadoLivre
Returns a new instance of MercadoLivre.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/ml_s4m.rb', line 7 def initialize @url = { :ML_INFO => 'https://informatica.mercadolivre.com.br/PARTNUMBER_DisplayType_LF_ItemTypeID_N', :ML_CAMERA => 'https://cameras.mercadolivre.com.br/PARTNUMBER_DisplayType_LF_ItemTypeID_N', :ML_PHONE => 'https://celulares.mercadolivre.com.br/PARTNUMBER_DisplayType_LF_ItemTypeID_N', :ML_DRONE => 'https://eletronicos.mercadolivre.com.br/drones-e-acessorios-drone/PARTNUMBER_DisplayType_LF_ItemTypeID_N' } @url_default = nil @offers = [] end |
Instance Method Details
#setPNSearch(partNumber = nil, category = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ml_s4m.rb', line 18 def setPNSearch(partNumber = nil, category = nil) unless partNumber.nil? @offers = [] ppn = partNumber.downcase @url_default = @url[category.to_sym] unless category.nil? pn = ["#{ppn.gsub(' ','%20')}", "#{ppn.gsub(' ','%20')}ll", "#{ppn.gsub(' ','%20')}bz"] pn.each do |pnumber| url_page = @url_default.gsub('PARTNUMBER', pnumber.gsub(' ','%20')) begin page = Nokogiri::HTML(open(url_page)) @offers += crawler_list(page) unless page.nil? rescue OpenURI::HTTPError puts "[ERROR] - Nesta categoria não há anúncios que coincidam com a sua busca - #{pnumber}" end end return @offers end end |
#top5Offers ⇒ Object
37 38 39 |
# File 'lib/ml_s4m.rb', line 37 def top5Offers @offers[1..5] if @offers.count > 4 end |