require 'yaml' require 'getapp/capterra/product' require 'getapp/import/products' module Getapp module Import # == Getapp::Import::Capterra Class # # Main entry point for Capterra Products Import. # Parse load Yaml supplied file and convert Capterra Products # into Getapp Products format. # class Capterra attr_reader :path, :verbose, :products # == Getapp::Import::Capterra initialize Method # # @params[String], Boolean # # path is a valid file path with format YAML # # @return Getapp::Import::Capterra instance # def initialize(path, verbose = false) @path = path @verbose = verbose end # == Getapp::Import::Capterra import Public Method # # Call all the privates method # # @return Boolean # def import check_file_exist? parse_products import_products true end private # == Getapp::Import::Capterra check_file_exist? Private Method # # Check that file exist on the give path. # Raise an Getapp::Capterra::FileNotFound exception if # file is missing. # def check_file_exist? puts "Capterra: Checking file exist" if verbose unless File.exist?(path) puts "Capterra: File does not exist." if verbose raise Getapp::Capterra::FileNotFound, "Capterra: File does not exist." end end # == Getapp::Import::Capterra parse_products Private Method # # Load yaml file into JSON format. # def parse_products puts "Capterra: Parsing products" if verbose @products = YAML.load_file(path) end # == Getapp::Import::Capterra format_products Private Method # # Transform Capterra Product format into Getapp Product format # # @return Array[OpenStruct] # def format_products puts "Capterra: Formating products" if verbose products.map { |product| Getapp::Capterra::Product.new(product, verbose).get } end # == Getapp::Import::Capterra import_products Private Method # # Import formatted products into persistent storage. # # @return NilClass # def import_products Getapp::Import::Products.new(format_products, verbose).import end end end end