stocks 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8a32a3160735bcc269c2da3567553382d56c810
4
- data.tar.gz: b443130a3470403e57cd9bad82688c6b915c1f70
3
+ metadata.gz: aaf935fa30015a7d522d988c453476978fd429df
4
+ data.tar.gz: 858b612f40c17f3144d633a06c41cdf0f479b899
5
5
  SHA512:
6
- metadata.gz: 59f678bceefd7903c2444c1e452a67243668f7c0a8bbe3e2b6b2254b24b6e876a7c66fc2fc25ab5ba069d1806fd062e192d6ee841105a4db4b689126a2f8af37
7
- data.tar.gz: 066a218fe46f8a47647945a37fa1f15ec33b577f90505558c63d4443eb1c6fb9e65c3c4cada46a0bd54fd6d2defa560cde2079464aff2a1d33682e574e7c5c3c
6
+ metadata.gz: 42cade0497d0503ce34492ccb85604c03a827e62ff4db6de63a5b3d570979b274dd00322eacce248970cbcca9a4338bb10d4167f76504e36122ac5f9cf26d70b
7
+ data.tar.gz: efbda82899547a538d324be44cb9824cf9b06f2aecbc262b039ed933d5fe3edd864d9a766cc00880f1b98b9b3473e3dd159867955817b83d776d201f2c8e9699
data/lib/stocks.rb CHANGED
@@ -8,6 +8,7 @@ require 'yahoofinance'
8
8
  # Require all Stocks related files
9
9
  require 'stocks/errors'
10
10
  require 'stocks/historical'
11
+ require 'stocks/quote'
11
12
  require 'stocks/validators/exists'
12
13
 
13
14
  # Provides an interface to do real-time analysis of stocks.
@@ -29,7 +30,6 @@ module Stocks
29
30
  # - +symbol+ The symbol to evaluate for existence
30
31
  # ===== *Raises*
31
32
  # - +RetrievalError+ If the provided symbol does not exist
32
- # Whether or not the provided symbol exists
33
33
  def self.exists!(symbol)
34
34
  raise RetrievalError.new(symbol) if !Stocks.exists?(symbol)
35
35
  end
@@ -48,17 +48,29 @@ module Stocks
48
48
  last_trade
49
49
  end
50
50
 
51
- private
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)
52
63
 
53
- EXISTS_CACHE = LRUCache.new(max_size: 500, ttl: 1.month) # :nodoc:
64
+ symbol.upcase!
65
+ quote = YahooFinance.send(Quote::TYPES[type], symbol)[symbol]
66
+ raise RetrievalError.new(symbol) if quote.name == NOT_AVAILABLE
54
67
 
55
- def self.quote(symbol, fields = [:lastTrade])
56
- data = {}
57
- standard_quote = YahooFinance::get_standard_quotes(symbol)[symbol]
58
- fields.each do |field|
59
- data[field] = standard_quote.send(field) rescue nil
60
- end
61
- data
68
+ Quote.new(quote)
62
69
  end
70
+
71
+ private
72
+
73
+ EXISTS_CACHE = LRUCache.new(max_size: 500, ttl: 1.month) # :nodoc:
74
+ NOT_AVAILABLE = 'N/A' # :nodoc:
63
75
  end
64
76
 
data/lib/stocks/errors.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  module Stocks
5
5
  # An error raised when data retrieval fails
6
6
  class RetrievalError < ArgumentError
7
- def initialize(symbol) # :nodoc:
7
+ def initialize(symbol)
8
8
  super(self.class.message(symbol))
9
9
  end
10
10
 
@@ -25,7 +25,7 @@ module Stocks
25
25
 
26
26
  # An error raised when an unsupported value is provided
27
27
  class UnsupportedError < ArgumentError
28
- def initialize(provided, supported) # :nodoc:
28
+ def initialize(provided, supported)
29
29
  super(self.class.message(provided, supported))
30
30
  end
31
31
 
@@ -0,0 +1,30 @@
1
+ # Author:: Matt Fornaciari (mailto:mattforni@gmail.com)
2
+ # License:: MIT
3
+
4
+ # Provides an instance of a stock quote.
5
+ class Quote < Hash
6
+ # Mapping from base_quote type to YahooFinance method of retrieval
7
+ TYPES = {
8
+ 'extended': :get_extended_quotes,
9
+ 'standard': :get_standard_quotes
10
+ }
11
+
12
+ def initialize(base_quote)
13
+ @base_quote = base_quote
14
+ get_fields.each { |field| self[field] = base_quote.send(field) }
15
+ end
16
+
17
+ private
18
+
19
+ attr_accessor :base_quote
20
+
21
+ # :nodoc:
22
+ def get_fields
23
+ @base_quote.instance_variables.map { |f|
24
+ f.to_s.gsub('@','').to_sym
25
+ }.reject { |f_sym|
26
+ !@base_quote.public_methods.include?(f_sym)
27
+ }
28
+ end
29
+ end
30
+
@@ -1,6 +1,8 @@
1
1
  # Author:: Matt Fornaciari (mailto:mattforni@gmail.com)
2
2
  # License:: MIT
3
3
 
4
+ require 'validators/exists'
5
+
4
6
  module Stocks
5
7
  # Provides an interface for validating ActiveRecord models.
6
8
  module Validators
@@ -1,6 +1,8 @@
1
1
  # Author:: Matt Fornaciari (mailto:mattforni@gmail.com)
2
2
  # License:: MIT
3
3
 
4
+ require 'active_model'
5
+
4
6
  module Stocks
5
7
  module Validators
6
8
  # An ActiveRecord validator which validates that an attribute on a model is a
@@ -26,7 +28,7 @@ module Stocks
26
28
  ERROR_MESSAGE % symbol
27
29
  end
28
30
 
29
- # Validates that a record have a valid symbol.
31
+ # Validates that a record has a valid symbol.
30
32
  #
31
33
  # ===== *Args*
32
34
  # - +record+ The record to validate
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.2
4
+ version: 0.0.3
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-05-08 00:00:00.000000000 Z
11
+ date: 2015-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -103,6 +103,7 @@ files:
103
103
  - lib/stocks.rb
104
104
  - lib/stocks/errors.rb
105
105
  - lib/stocks/historical.rb
106
+ - lib/stocks/quote.rb
106
107
  - lib/stocks/validators.rb
107
108
  - lib/stocks/validators/exists.rb
108
109
  homepage: http://rubygems.org/gems/stocks