class NsefinanceRails

Constants

BASE_URI

change from using a class variable to constants. That would properly notify the user that the value isnt meant to be changed. @@base_uri = ‘nsefinance.com/api/stocks

Public Class Methods

all_closing_prices_today() click to toggle source

get closing prices for all the symbols for the trading day

# File lib/nsefinance_rails.rb, line 28
def self.all_closing_prices_today
      json_package = Net::HTTP.get( URI(BASE_URI) )
      day_records = JSON.parse(json_package)

      result = Array.new
      day_records.map {|day_record|
              result << day_record["close"].to_f
      }
      result
end
all_closing_today() click to toggle source

get the full trading day records for all stocks as at closing time

# File lib/nsefinance_rails.rb, line 22
def self.all_closing_today
      tmp = Net::HTTP.get( URI(BASE_URI) )
      JSON.parse(tmp)
end
all_stocks() click to toggle source
# File lib/nsefinance_rails.rb, line 13
def self.all_stocks
  tmp = Net::HTTP.get( URI(BASE_URI) )
  result = JSON.parse(tmp)
end
symbol(symbol) click to toggle source

get the full trading day record for the last 5 trading days for a symbol

# File lib/nsefinance_rails.rb, line 40
def self.symbol(symbol)
      result = '{"error" : "Invalid symbol"}'

      if !symbol.nil?
              url = "#{BASE_URI}/#{symbol.upcase}"
              tmp = Net::HTTP.get( URI(url) )
              result = JSON.parse(tmp)
      end
      result
end
symbol_closing_price_on(symbol, date) click to toggle source

get the closing price for a particular symbol on a particular day

# File lib/nsefinance_rails.rb, line 97
def self.symbol_closing_price_on(symbol, date)
              result = ''

      record = NseFinance.symbol_on(symbol, date)
      if record.include?("error")
              result = record
      elsif record.empty?
              result = '{"response": "No data"}'
      else
              result = record["#{symbol}"]["close"]
      end
      result
end
symbol_closing_prices(symbol) click to toggle source

get the closing price for the last 5 trading days for a symbol

# File lib/nsefinance_rails.rb, line 52
def self.symbol_closing_prices(symbol)
      result = ''

      records = NseFinance.symbol(symbol)
      if records.include?("error")
              result = records
      else
              result = Array.new
              records.map { |day_record|
                      result << day_record["close"].to_f
              }
      end
      result
end
symbol_on(symbol, date) click to toggle source

get the full trading day record for a particular symbol on a particular day

# File lib/nsefinance_rails.rb, line 68
def self.symbol_on(symbol, date)

      # check if the date is in the proper format: YYYY-MM-dd
      parsed_date = nil
      begin  
                     parsed_date = Date.parse(date)
        rescue  
          result = '{"error" : "Invalid date"}'
        end

      if !symbol.nil? && !parsed_date.nil?
              url = "#{BASE_URI}/#{symbol.upcase}/#{date}"
              tmp = Net::HTTP.get( URI(url) )

              if tmp.include?("error")
                      result = '{"error" : "Invalid symbol"}'
              else
                      tmp_result = JSON.parse(tmp)
                      result = tmp_result
                      if tmp_result.length > 1
                              result = '{"error" : "Invalid date"}'
                      end
              end
      end

      result
end