nse_finance 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/nse_finance.rb +103 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94222a4bc78c0e410edb8a6cd6c9a0acae15a508
4
+ data.tar.gz: 9227fba0c3d2af3a44896af5716bd02b2dbcd965
5
+ SHA512:
6
+ metadata.gz: 36ebec8a8236b4acfecf8e4df47cfd20a32491157c0681e61b6b4f5d5f0ae476c90a321c242b066f285540cc500556d896360a5891dd6561aae53b4974c2929a
7
+ data.tar.gz: ecbe4e73fa6a6b3385a33289855dd0292b0bb4b5644a5d0ae842512597f5842e685074872fafc342b804ebd2540eb40fb58b64d48feade467d8fcfe4212459ab
@@ -0,0 +1,103 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'Date'
4
+
5
+ class NseFinance
6
+
7
+ # this is the base nsefinance.com JSON service URI
8
+ @@base_uri = 'http://nsefinance.com/api/stocks'
9
+
10
+ # These are the available keys that the JSON service returns per stock symbol
11
+ # ['high', 'symbol', 'value', 'deals', 'date','low','units','close','open','change']
12
+
13
+ # get the full trading day records for all stocks as at closing time
14
+ def self.all_closing_today
15
+ tmp = Net::HTTP.get( URI(@@base_uri) )
16
+ JSON.parse(tmp)
17
+ end
18
+
19
+ # get closing prices for all the symbols for the trading day
20
+ def self.all_closing_prices_today
21
+ json_package = Net::HTTP.get( URI(@@base_uri) )
22
+ day_records = JSON.parse(json_package)
23
+
24
+ result = Array.new
25
+ day_records.map {|day_record|
26
+ result << day_record["close"].to_f
27
+ }
28
+ result
29
+ end
30
+
31
+ # get the full trading day record for the last 5 trading days for a symbol
32
+ def self.symbol(symbol)
33
+ result = '{"error" : "Invalid symbol"}'
34
+
35
+ if !symbol.nil?
36
+ url = "#{@@base_uri}/#{symbol.upcase}"
37
+ tmp = Net::HTTP.get( URI(url) )
38
+ result = JSON.parse(tmp)
39
+ end
40
+ result
41
+ end
42
+
43
+ # get the closing price for the last 5 trading days for a symbol
44
+ def self.symbol_closing_prices(symbol)
45
+ result = ''
46
+
47
+ records = NseFinance.symbol(symbol)
48
+ if records.include?("error")
49
+ result = records
50
+ else
51
+ result = Array.new
52
+ records.map { |day_record|
53
+ result << day_record["close"].to_f
54
+ }
55
+ end
56
+ result
57
+ end
58
+
59
+ # get the full trading day record for a particular symbol on a particular day
60
+ def self.symbol_on(symbol, date)
61
+
62
+ # check if the date is in the proper format: YYYY-MM-dd
63
+ parsed_date = nil
64
+ begin
65
+ parsed_date = Date.parse(date)
66
+ rescue
67
+ result = '{"error" : "Invalid date"}'
68
+ end
69
+
70
+ if !symbol.nil? && !parsed_date.nil?
71
+ url = "#{@@base_uri}/#{symbol.upcase}/#{date}"
72
+ tmp = Net::HTTP.get( URI(url) )
73
+
74
+ if tmp.include?("error")
75
+ result = '{"error" : "Invalid symbol"}'
76
+ else
77
+ tmp_result = JSON.parse(tmp)
78
+ result = tmp_result
79
+ if tmp_result.length > 1
80
+ result = '{"error" : "Invalid date"}'
81
+ end
82
+ end
83
+ end
84
+
85
+ result
86
+ end
87
+
88
+ # get the closing price for a particular symbol on a particular day
89
+ def self.symbol_closing_price_on(symbol, date)
90
+ result = ''
91
+
92
+ record = NseFinance.symbol_on(symbol, date)
93
+ if record.include?("error")
94
+ result = record
95
+ elsif record.empty?
96
+ result = '{"response": "No data"}'
97
+ else
98
+ result = record["#{symbol}"]["close"]
99
+ end
100
+ result
101
+ end
102
+
103
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nse_finance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nii Amon Dsane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A thin layer on top of the nsefinance.com JSON service
14
+ email: jazzyy@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/nse_finance.rb
20
+ homepage: http://rubygems.org/gems/nse_finance
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Get stock information from the Nigerian Stock Exchange
44
+ test_files: []