app/controllers/shares_controller.rb
#create method is called when a new share is being extracted from moneycontrol website. 
#create method takes care of sanitizing the url entered by user for share data extraction.
#it calls a method "readShareBasic" declared in the application_controller.rb file to extract the share details.
#If the Share is already present in the database(by comparing the name), the same record is picked up or else a new share record will be created.
#This routine so acts both as update(for editing) and create routine for the share controller.
#If the share data is extracted successfully, then the values are assigned to @share variable and saved.
#Any error either in data extraction code or in model will be communicated to user by setting flash[:error]
#
create method is called when a new share is being extracted from moneycontrol website.
create method takes care of sanitizing the url entered by user for share data extraction.
it calls a method "readShareBasic" declared in the application_controller.rb file to extract the share details.
If the Share is already present in the database(by comparing the name), the same record is picked up or else a new share record will be created.
This routine so acts both as update(for editing) and create routine for the share controller.
If the share data is extracted successfully, then the values are assigned to @share variable and saved.
Any error either in data extraction code or in model will be communicated to user by setting flash[:error]
create method is called when a new share is being extracted from moneycontrol website . create method takes care of sanitizing the url entered by user for share data extraction . it calls a method " readShareBasic " declared in the application_controller . rb file to extract the share details . If the Share is already present in the database ( by comparing the name ) the same record is picked up or else a new share record will be created . This routine so acts both as update ( for editing ) and create routine for the share controller . If the share data is extracted successfully then the values are assigned to @share variable and saved . Any error either in data extraction code or in model will be communicated to user by setting flash [ : error ]
def create
    @url = sanitize_website(params[:share][:url])
    if(@url.nil?)
      flash[:error] ="Error:Invalid URL. Please enter a valid Moneycontrol.com url"
      @share=Share.new
      render :new
      return 
    end
    @share_details = readShareBasic(@url)
    if(@share_details[:error].present?)
      flash[:error] = "Error:Invalid URL. \n " + @share_details[:error]
      @share=Share.new
      render :new
      return 
    end
    if @share_details[:current_price].blank? 
      flash[:alert] ="Error: Not a traded share"
      @share=Share.new
      render :new
      return
    end
      @share = Share.where(:name => @share_details[:name]).first_or_initialize(share_params)
      @share.name = (@share_details[:name])
      @share.current_price = @share_details[:current_price]
      @share.sector=(@share_details[:sector]).to_s
      @share.description=@share_details[:description]
      @share.year_low=@share_details[:year_low]
      @share.year_high=@share_details[:year_high]
      @share.market_cap=@share_details[:market_cap]
      @share.current_PE_ratio=@share_details[:current_PE_ratio]
      @share.book_value =@share_details[:book_value]
      @share.price_to_book_value=@share_details[:price_to_book_value]
    if @share.save
        redirect_to shares_path(@movie), notice: 'Share data created/updated successfully!'
    else
      flash[:error] = "Error: #{@share.errors.full_messages.to_sentence}"
      render :edit
    end
   end
def create @url = sanitize_website ( params [ :share ] [ :url ] ) if ( @url . nil? ) flash [ :error ] = "Error:Invalid URL. Please enter a valid Moneycontrol.com url" @share = Share . new render :new return end @share_details = readShareBasic ( @url ) if ( @share_details [ :error ] . present? ) flash [ :error ] = "Error:Invalid URL. \n " + @share_details [ :error ] @share = Share . new render :new return end if @share_details [ :current_price ] . blank? flash [ :alert ] = "Error: Not a traded share" @share = Share . new render :new return end @share = Share . where ( :name => @share_details [ :name ] ) . first_or_initialize ( share_params ) @share . name = ( @share_details [ :name ] ) @share . current_price = @share_details [ :current_price ] @share . sector = ( @share_details [ :sector ] ) . to_s @share . description = @share_details [ :description ] @share . year_low = @share_details [ :year_low ] @share . year_high = @share_details [ :year_high ] @share . market_cap = @share_details [ :market_cap ] @share . current_PE_ratio = @share_details [ :current_PE_ratio ] @share . book_value = @share_details [ :book_value ] @share . price_to_book_value = @share_details [ :price_to_book_value ] if @share . save redirect_to shares_path ( @movie ) , notice : 'Share data created/updated successfully!' else flash [ :error ] = "Error: #{@share.errors.full_messages.to_sentence}" render :edit end end
create method is called when a new share is being extracted from moneycontrol website.
create method is called when a new share is being extracted from moneycontrol website .
#sanitize_website is called to sanitize the URL entered by the user. #readShareBasic routine will set :error key to communicate any error while extracting the share information  #If the share price is blank, it concludes that it is not a traded share and such shares will not be put into the database. # "Not a traded share"