stocks 0.0.4 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/stocks.rb +0 -35
- data/lib/stocks/quote.rb +34 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fe77a236eb3ca2f989553caf4d57cbfa165de57
|
4
|
+
data.tar.gz: 955e7bc142a46587bc745b4ad37f8bc849931610
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0353ff677cc636d391d2e7c5e3c1d91a83ad94248069fd37fe46310c38bc8e30f78f67fb58f8045775e880708327b7806f6ca3e3f3c77b7a2e863a1eb621b649
|
7
|
+
data.tar.gz: 1462b1b44059f65a4b89b8754993abddfaeffab4d1b6b228497d97545e2385c09cb9ea182604815e673a2d3093e56733fe872363f9682b2c59e2b129d235aa0d
|
data/lib/stocks.rb
CHANGED
@@ -34,43 +34,8 @@ module Stocks
|
|
34
34
|
raise RetrievalError.new(symbol) if !Stocks.exists?(symbol)
|
35
35
|
end
|
36
36
|
|
37
|
-
# Fetches the last trade for the provided symbol.
|
38
|
-
#
|
39
|
-
# ===== *Args*
|
40
|
-
# - +symbol+ The symbol to evaluate
|
41
|
-
# ===== *Returns*
|
42
|
-
# The last trade for the provided symbol
|
43
|
-
# ===== *Raises*
|
44
|
-
# - +RetrievalError+ If the provided symbol does not exist
|
45
|
-
def self.last_trade(symbol)
|
46
|
-
last_trade = quote(symbol)[:lastTrade]
|
47
|
-
raise RetrievalError.new(symbol) if last_trade.nil?
|
48
|
-
last_trade
|
49
|
-
end
|
50
|
-
|
51
|
-
# Fetches a quote for the provided symbol.
|
52
|
-
#
|
53
|
-
# ===== *Args*
|
54
|
-
# - +symbol+ The symbol to evaluate
|
55
|
-
# - +type+ The type of quote to retreive
|
56
|
-
# ===== *Returns*
|
57
|
-
# The quote for the provided symbol
|
58
|
-
# ===== *Raises*
|
59
|
-
# - +RetrievalError+ If the provided symbol does not exist
|
60
|
-
# - +UnsupportedError+ If the provided type is not supported
|
61
|
-
def self.quote(symbol, type = :standard)
|
62
|
-
raise UnsupportedError.new(type, Quote::TYPES.keys) if !Quote::TYPES.has_key?(type)
|
63
|
-
|
64
|
-
symbol.upcase!
|
65
|
-
quote = YahooFinance.send(Quote::TYPES[type], symbol)[symbol]
|
66
|
-
raise RetrievalError.new(symbol) if quote.name == NOT_AVAILABLE
|
67
|
-
|
68
|
-
Quote.new(quote)
|
69
|
-
end
|
70
|
-
|
71
37
|
private
|
72
38
|
|
73
39
|
EXISTS_CACHE = LRUCache.new(max_size: 500, ttl: 1.month) # :nodoc:
|
74
|
-
NOT_AVAILABLE = 'N/A' # :nodoc:
|
75
40
|
end
|
76
41
|
|
data/lib/stocks/quote.rb
CHANGED
@@ -1,22 +1,54 @@
|
|
1
1
|
# Author:: Matt Fornaciari (mailto:mattforni@gmail.com)
|
2
2
|
# License:: MIT
|
3
3
|
|
4
|
-
# Provides an instance of a stock quote.
|
5
4
|
module Stocks
|
5
|
+
# Reprsents an instance of a stock quote.
|
6
6
|
class Quote < Hash
|
7
7
|
# Mapping from base_quote type to YahooFinance method of retrieval
|
8
|
+
# (options: :extended, :standard)
|
8
9
|
TYPES = {
|
9
10
|
'extended': :get_extended_quotes,
|
10
11
|
'standard': :get_standard_quotes
|
11
12
|
}
|
12
13
|
|
14
|
+
# Fetches a quote for the provided symbols.
|
15
|
+
#
|
16
|
+
# ===== *Args*
|
17
|
+
# - +symbols+ The symbols for which to retrieve a quote
|
18
|
+
# - +type+ The type of quote to retreive (default: :standard)
|
19
|
+
# ===== *Returns*
|
20
|
+
# The quote for the provided symbol
|
21
|
+
# ===== *Raises*
|
22
|
+
# - +UnsupportedError+ If the provided type is not supported
|
23
|
+
def self.get(symbols, type = :standard)
|
24
|
+
raise UnsupportedError.new(type, Quote::TYPES.keys) if !Quote::TYPES.has_key?(type)
|
25
|
+
|
26
|
+
symbols = [symbols] if !symbols.is_a?(Array)
|
27
|
+
symbols = symbols.collect { |s| s.upcase }
|
28
|
+
|
29
|
+
quotes = YahooFinance.send(Quote::TYPES[type], symbols)
|
30
|
+
quotes.each_pair { |symbol, quote| quotes[symbol] = Quote.new(quote) }
|
31
|
+
quotes
|
32
|
+
end
|
33
|
+
|
34
|
+
# :nodoc:
|
13
35
|
def initialize(base_quote)
|
14
36
|
@base_quote = base_quote
|
15
|
-
get_fields.each { |field| self[field] = base_quote.send(field) }
|
37
|
+
get_fields.each { |field| self[field] = base_quote.send(field) } if valid?
|
38
|
+
end
|
39
|
+
|
40
|
+
# Determines whether or not the quote is valid.
|
41
|
+
#
|
42
|
+
# ===== *Returns*
|
43
|
+
# Whether or not the quote is valid
|
44
|
+
def valid?
|
45
|
+
@base_quote.name != NOT_AVAILABLE
|
16
46
|
end
|
17
47
|
|
18
48
|
private
|
19
49
|
|
50
|
+
NOT_AVAILABLE = 'N/A' # :nodoc:
|
51
|
+
|
20
52
|
attr_accessor :base_quote
|
21
53
|
|
22
54
|
# :nodoc:
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stocks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Fornaciari
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|