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