app/spiders/application_spider.rb
# Process item data. If running in production, pass the data for processing by background workers.
# In development, we'll simply output the data in json
Process item data. If running in production, pass the data for processing by background workers.
In development, we'll simply output the data in json
Process item data . If running in production pass the data for processing by background workers . In development we ' ll simply output the data in json
def schedule_processing data
    @product = Product.new
    @product.asin = data[:product][:asin]
    @product.description = data[:product][:description]
    @product.rating = data[:product][:rating]
    @product.star_rating = data[:product][:star_rating]
    @product.title = data[:product][:title]
    @product.save
    data[:seller].each do |seller|
      @seller = Seller.new
      check_seller = Seller.find_by merchant_id: seller[:merchant_id]
      if check_seller
        Seller.where(merchant_id: seller[:merchant_id]).first.update(
          updated_at: Time.now 
        )
      else
        @seller.merchant_id = seller[:merchant_id]
        @seller.address = seller[:address]
        @seller.city = seller[:city]
        @seller.state = seller[:state]
        @seller.country = seller[:country]
        @seller.star_rating = seller[:star_rating]
        @seller.total_rating = seller[:total_rating]
        @seller.save
      end
    end
    data[:product_data].each do |product_data|
      @product_data = ProductData.new
      @product_data.product_id = @product.id
      @product_data.seller_id = product_data[:seller_id]
      @product_data.price = product_data[:price]
      @product_data.ships_from = product_data[:ships_from]
      @product_data.quantity = product_data[:quantity]
      @product_data.save
    end
    save_to("app/spiders/outputs/#{self.class.to_s}_#{data[:product][:asin]}.json", data, format: :pretty_json)
  end
def schedule_processing data @product = Product . new @product . asin = data [ :product ] [ :asin ] @product . description = data [ :product ] [ :description ] @product . rating = data [ :product ] [ :rating ] @product . star_rating = data [ :product ] [ :star_rating ] @product . title = data [ :product ] [ :title ] @product . save data [ :seller ] . each do | seller | @seller = Seller . new check_seller = Seller . find_by merchant_id : seller [ :merchant_id ] if check_seller Seller . where ( merchant_id : seller [ :merchant_id ] ) . first . update ( updated_at : Time . now ) else @seller . merchant_id = seller [ :merchant_id ] @seller . address = seller [ :address ] @seller . city = seller [ :city ] @seller . state = seller [ :state ] @seller . country = seller [ :country ] @seller . star_rating = seller [ :star_rating ] @seller . total_rating = seller [ :total_rating ] @seller . save end end data [ :product_data ] . each do | product_data | @product_data = ProductData . new @product_data . product_id = @product . id @product_data . seller_id = product_data [ :seller_id ] @product_data . price = product_data [ :price ] @product_data . ships_from = product_data [ :ships_from ] @product_data . quantity = product_data [ :quantity ] @product_data . save end save_to ( "app/spiders/outputs/#{self.class.to_s}_#{data[:product][:asin]}.json" , data , format : :pretty_json ) end
Process item data.
Process item data .
data