stock 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +124 -0
- data/lib/stock.rb +50 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f15340a936a764737e0f2dacf4cc51cc8b4f7e68
|
4
|
+
data.tar.gz: cf03a271dbf1c57dd9d21efc333b00cdfa1983f3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4f08e176d30e2d9b0c37433c61328ba17b9486065056306f378e03993b97ae1a09f726171b465e2a14204634f5a8161f072e81ec1e2c5d608c7b7cb3fd957429
|
7
|
+
data.tar.gz: 62f8debab4d5618a0afcff9e4f809f56ab72d2ca27b87a03119e19d59782cd96f94ec66fe245ae5b8a7c12258cef3d7e6823a5cdfb5e6f76a07eff0bcfd69f72
|
data/README.md
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# Stock.rb
|
2
|
+
Grabs information about a stock from Yahoo! Finance.
|
3
|
+
|
4
|
+
### Installation
|
5
|
+
* Via RubyGems `gem install stock`
|
6
|
+
|
7
|
+
### Basic Usage
|
8
|
+
You can grab the entire information of a stock like so:
|
9
|
+
```ruby
|
10
|
+
require 'stock'
|
11
|
+
|
12
|
+
facebook = Stock.new('fb')
|
13
|
+
puts facebook.d
|
14
|
+
```
|
15
|
+
|
16
|
+
**OUTPUT**
|
17
|
+
|
18
|
+
```json
|
19
|
+
{
|
20
|
+
"sourceInterval":15,
|
21
|
+
"regularMarketOpen":{
|
22
|
+
"raw":181.78,
|
23
|
+
"fmt":"181.78"
|
24
|
+
},
|
25
|
+
"exchange":"NMS",
|
26
|
+
"regularMarketTime":{
|
27
|
+
"raw":1520370002,
|
28
|
+
"fmt":"4:00PM EST"
|
29
|
+
},
|
30
|
+
"sharesOutstanding":{
|
31
|
+
"raw":2395919872,
|
32
|
+
"fmt":"2.396B",
|
33
|
+
"longFmt":"2,395,919,872"
|
34
|
+
},
|
35
|
+
"regularMarketDayHigh":{
|
36
|
+
"raw":182.38,
|
37
|
+
"fmt":"182.38"
|
38
|
+
},
|
39
|
+
"shortName":"Facebook, Inc.",
|
40
|
+
"longName":"Facebook, Inc.",
|
41
|
+
"exchangeTimezoneName":"America/New_York",
|
42
|
+
"regularMarketChange":{
|
43
|
+
"raw":-0.6199951,
|
44
|
+
"fmt":"-0.62"
|
45
|
+
},
|
46
|
+
"regularMarketPreviousClose":{
|
47
|
+
"raw":180.4,
|
48
|
+
"fmt":"180.40"
|
49
|
+
},
|
50
|
+
"fiftyTwoWeekHighChange":{
|
51
|
+
"raw":-15.540009,
|
52
|
+
"fmt":"-15.54"
|
53
|
+
},
|
54
|
+
"exchangeTimezoneShortName":"EST",
|
55
|
+
"fiftyTwoWeekLowChange":{
|
56
|
+
"raw":42.789993,
|
57
|
+
"fmt":"42.79"
|
58
|
+
},
|
59
|
+
"exchangeDataDelayedBy":0,
|
60
|
+
"regularMarketDayLow":{
|
61
|
+
"raw":179.11,
|
62
|
+
"fmt":"179.11"
|
63
|
+
},
|
64
|
+
"priceHint":2,
|
65
|
+
"currency":"USD",
|
66
|
+
"regularMarketPrice":{
|
67
|
+
"raw":179.78,
|
68
|
+
"fmt":"179.78"
|
69
|
+
},
|
70
|
+
"regularMarketVolume":{
|
71
|
+
"raw":15086784,
|
72
|
+
"fmt":"15.087M",
|
73
|
+
"longFmt":"15,086,784"
|
74
|
+
},
|
75
|
+
"isLoading":false,
|
76
|
+
"gmtOffSetMilliseconds":-18000000,
|
77
|
+
"marketState":"POSTPOST",
|
78
|
+
"marketCap":{
|
79
|
+
"raw":522260873216,
|
80
|
+
"fmt":"522.261B",
|
81
|
+
"longFmt":"522,260,873,216"
|
82
|
+
},
|
83
|
+
"quoteType":"EQUITY",
|
84
|
+
"invalid":false,
|
85
|
+
"symbol":"FB",
|
86
|
+
"language":"en-US",
|
87
|
+
"fiftyTwoWeekLowChangePercent":{
|
88
|
+
"raw":0.3123585,
|
89
|
+
"fmt":"31.24%"
|
90
|
+
},
|
91
|
+
"messageBoardId":"finmb_20765463",
|
92
|
+
"fiftyTwoWeekHigh":{
|
93
|
+
"raw":195.32,
|
94
|
+
"fmt":"195.32"
|
95
|
+
},
|
96
|
+
"fiftyTwoWeekHighChangePercent":{
|
97
|
+
"raw":-0.079561785,
|
98
|
+
"fmt":"-7.96%"
|
99
|
+
},
|
100
|
+
"uuid":"30781f1f-c2f9-342c-ab1a-d2f6f35a51db",
|
101
|
+
"market":"us_market",
|
102
|
+
"fiftyTwoWeekLow":{
|
103
|
+
"raw":136.99,
|
104
|
+
"fmt":"136.99"
|
105
|
+
},
|
106
|
+
"regularMarketChangePercent":{
|
107
|
+
"raw":-0.343678,
|
108
|
+
"fmt":"-0.34%"
|
109
|
+
},
|
110
|
+
"fullExchangeName":"NasdaqGS",
|
111
|
+
"tradeable":true
|
112
|
+
}
|
113
|
+
```
|
114
|
+
|
115
|
+
You can also grab singular pieces of information about a stock like so:
|
116
|
+
```ruby
|
117
|
+
require 'stock'
|
118
|
+
|
119
|
+
facebook = Stock.new('fb')
|
120
|
+
p facebook.d['shortName'] #=> 'Facebook, Inc.'
|
121
|
+
```
|
122
|
+
|
123
|
+
# License
|
124
|
+
Copyright © 2018 Marcello Sabino (marchsabino@gmail.com) and released under the MIT License.
|
data/lib/stock.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
##
|
5
|
+
# Represents a singular stock on Yahoo! Finance.
|
6
|
+
#
|
7
|
+
# @author Marcello Sabino <marchsabino@gmail.com>
|
8
|
+
# @version 1.0.0
|
9
|
+
# @since 1.0.0
|
10
|
+
class Stock
|
11
|
+
# @return [String] The Stock's symbol
|
12
|
+
attr_reader :symbol
|
13
|
+
|
14
|
+
##
|
15
|
+
# Creates a new Stock object with the given symbol
|
16
|
+
#
|
17
|
+
# @param symbol [String] the stocks symbol
|
18
|
+
def initialize(symbol)
|
19
|
+
@symbol = symbol
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# Gets the stock's data from Yahoo!
|
24
|
+
#
|
25
|
+
# @since 1.0.0
|
26
|
+
# @raise RuntimeError if the Stock symbol can't be found on Yahoo! Finance.
|
27
|
+
# @return JSON of the Symbol's data from Yahoo! Finance.
|
28
|
+
def d
|
29
|
+
html = open("https://finance.yahoo.com/quote/#{@symbol}", &:read).freeze
|
30
|
+
json = JSON.parse(between(html, 'root.App.main = ', ";\n}(this));"))
|
31
|
+
json = json['context']['dispatcher']['stores']
|
32
|
+
symbol = json['PageStore']['pageData']['symbol']
|
33
|
+
|
34
|
+
return json['StreamDataStore']['quoteData'][symbol].to_json if symbol != nil
|
35
|
+
raise RuntimeError, "Stock #{@symbol} doesn't exist."
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
##
|
41
|
+
# A private helper method to grab text in-between
|
42
|
+
# 2 strings.
|
43
|
+
#
|
44
|
+
# @param str [String] string to search from
|
45
|
+
# @param start [String] where to start looking in-between
|
46
|
+
# @param ends [String] the end of the string to look in-between
|
47
|
+
def between(str, start, ends)
|
48
|
+
str[/#{Regexp.escape(start)}(.*?)#{Regexp.escape(ends)}/m, 1]
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcello A. Sabino
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Grabs stock information from Yahoo! Finance
|
14
|
+
email: marchsabino@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- README.md
|
20
|
+
- lib/stock.rb
|
21
|
+
homepage: http://rubygems.org/gems/stock
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.3.0
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.5.2.1
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Grabs stock information from Yahoo! Finance
|
45
|
+
test_files: []
|