class PricebrApple::PriceBR

Public Class Methods

new() click to toggle source
# File lib/pricebr_apple.rb, line 30
def initialize
  @model = nil
  @price = 0.0
  @list_partNumber = []
  @country = 'br'
end

Public Instance Methods

cleaning(str = nil) click to toggle source
# File lib/pricebr_apple.rb, line 122
def cleaning(str = nil)
  str.to_s.gsub('"','').gsub(':','').gsub(',','').gsub('_','.') unless str.nil?
end
get_last_price() click to toggle source
# File lib/pricebr_apple.rb, line 61
def get_last_price
        @price
end
get_list_partNumber(params) click to toggle source

params {url_page : 'http://'}

# File lib/pricebr_apple.rb, line 96
def get_list_partNumber(params) 
  unless params[:url_page].nil?
    @page = Nokogiri::HTML(open(params[:url_page]))
    @list_partNumber = @page.xpath("//meta[@itemprop='sku']/@content").map {|x| x.value} unless @page.nil?
  end
  @list_partNumber
end
get_price(params) click to toggle source

params {url_page: 'device page', partNumber: 'model'}

# File lib/pricebr_apple.rb, line 45
def get_price(params)
    @model = params[:partNumber]
  url_page = params[:url_page]
    if !url_page.nil? && !@model.nil?
            @page = Nokogiri::HTML(open(url_page))
            list_price = @page.css('.current_price')
    list_price = @page.css('.as-price-currentprice') if list_price.empty?
            unless list_price.nil?
                    list_price.map{|item| @price = item.children[1].children[3].children[0].text.gsub(' ', '').gsub("\nR$",'').gsub("\n",'').gsub('.','').gsub(',','.').to_f if !item.nil? && item.children[1].children[1].values[1].to_s == @model}
    else
      @price = 0.0
            end
    end 
    @price
end
script_crawler(params) click to toggle source

params {script: 'text', partNumber: 'model'}

# File lib/pricebr_apple.rb, line 66
def script_crawler(params)
  list = []
  download = open(params[:url_page]) unless params[:url_page].nil?
  unless download.nil?
    download.each do |line|
      if !line.nil? && (line.include? 'partNumber') && (line.include? 'currentPrice')
        frame = line.split('"products"')
        frame = frame.last.split('"dimensionCapacity"')
        frame.each do |item|
          if !item.nil? && (item.include? 'partNumber') && (item.include? 'price')
            part = item.split('partNumber')
            if part.size == 2
              partNumber, part = part[1].split('seoUrlToken')
              unless part.nil?
                part = part.split('carrierPolicyPart').first
                price = part.split('price').last
                price = self.cleaning(price)
              end
              partNumber = self.cleaning(partNumber)
              list << [ partNumber, price.to_f ] if !partNumber.nil? && !price.nil?
            end
          end
        end 
      end
    end
  end
  return list.empty? ? nil : list 
end
set_country(params) click to toggle source
# File lib/pricebr_apple.rb, line 37
def set_country(params)
  unless params.nil?
    @country |= params
    @country = '' if @country.equal?('eua')
  end
end
update_price() click to toggle source
# File lib/pricebr_apple.rb, line 104
def update_price
  list = []
  PRICE_URL.each do |x,y|
    new_format = ["IPHONE 7 PLUS", "IPHONE 7", "IPHONE 6S", "IPHONE 6S PLUS", "IPHONE SE", "WATCH SERIES 1", "WATCH SERIE 2", "WATCH SERIE 2 EDITION"]
    if (new_format.include? x)
      sub_list = script_crawler({url_page: y})
      list = list + sub_list unless sub_list.nil?
    else 
      get_list_partNumber({url_page: y})
      @list_partNumber.each do |part|
        self.get_price({url_page: y, partNumber: part})
        list << [ part, self.get_last_price ]
      end
    end
  end
  return list
end