yahoo_finanza 0.3.2 → 0.4.0

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: 76d722ed1eb7c00680153fcbddf312a115a1d62f
4
- data.tar.gz: e62c6d0cdfb36c7ea8fbf602ed61124ca16fbeb4
3
+ metadata.gz: 2bf5897a7c1dab870202deb24deff1f11ccf4f95
4
+ data.tar.gz: e9d1bec7671281e049b3eec56e9e06cbf060d67a
5
5
  SHA512:
6
- metadata.gz: fea0930a1d2e68265dd8e666261e95af5aeed6ee3dd01a1eb5c3fbb12e015758696de4217bed3a9cc4ccff902e919f09162191bc12952836c33f966cb9947527
7
- data.tar.gz: 51ad5f30d582b14e5a1cd2d06e51a2b66a3d01e3430c54e65b8940038ec4af999fd5444ebfe3f761740017f4590b8981fd6103b7829e43324402345da9110332
6
+ metadata.gz: e72882fc69bec34b9d0bc566e28296afc789bcb23f15918eb398f2f220fb6490c1b5238bc5e548d97a9eb87e785c86ce44e8c09dce3691084968028fe06dfd70
7
+ data.tar.gz: 5c4bbe495075825ea6cd03f1d57a5978ec86da5679311b0e153a730515b7eb5d46027ca0dbe032035b57861209533faf522e661b943579497c8dd711e7debe52
data/lib/yahoo_finanza.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'yahoo_finanza/version'
2
- require 'yahoo_finanza/client'
3
2
  require 'yahoo_finanza/constants'
4
- require 'yahoo_finanza/worker'
3
+ require 'yahoo_finanza/workers'
4
+ require 'yahoo_finanza/client'
5
5
 
6
6
  module YahooFinanza
7
7
  end
@@ -1,54 +1,31 @@
1
- require 'yahoo_finanza/parser'
2
- require 'yahoo_finanza/finance_utils'
3
-
4
- require 'yql'
5
- require 'json'
6
- require 'ostruct'
7
- require 'mechanize'
1
+ require 'yahoo_finanza/workers'
8
2
 
9
3
  module YahooFinanza
10
4
  class Client
11
- include YahooFinanza::FinanceUtils
12
- include YahooFinanza::Parser
13
-
14
- attr_reader :yql, :mecha_yahoo
5
+ def quote symbol
6
+ YahooFinanza::SingleQuoteWorker.new(symbol).run
7
+ end
15
8
 
16
- def initialize
17
- @yql = Yql::Client.new
18
- @yql.format = 'json'
9
+ def quotes symbols
10
+ clean_quotes(YahooFinanza::MultiQuoteWorker.new(symbols).run)
19
11
  end
20
12
 
21
- def quotes(symbols)
22
- return [] if symbols.empty?
23
- query = Yql::QueryBuilder.new 'yahoo.finance.quotes'
24
- query.conditions = "symbol in (\"#{symbols.join('\", \"')}\")"
25
- @yql.query = query
26
- response = @yql.get
27
- yahoofy(response.show)
13
+ def quotes_history symbol, options
14
+ YahooFinanza::HistoricQuoteWorker.new(symbol, options).run
28
15
  end
29
16
 
30
- def quote(symbol)
31
- return [] if symbol == ''
32
- query = Yql::QueryBuilder.new 'yahoo.finance.quotes'
33
- query.conditions = "symbol in (\"#{symbol}\")"
34
- @yql.query = query
35
- response = @yql.get
36
- single_yahoofy(response.show)
17
+ def search_symbols key
18
+ YahooFinanza::SymbolSearchWorker.new(key).run
37
19
  end
38
20
 
39
- def historical_quotes(symbol, options = {})
40
- return [] if symbol == ''
41
- query = Yql::QueryBuilder.new 'yahoo.finance.historicaldata'
42
- query.conditions = {
43
- symbol: symbol, startDate: options[:start_date],
44
- endDate: options[:end_date] }
45
- @yql.query = query
46
- response = @yql.get
47
- historical_yahoofy(response.show)
21
+ def popular_stock_quotes
22
+ popular_symbols = YahooFinanza::ActiveStockWorker.new.get_active_stocks
23
+ YahooFinanza::MultiQuoteWorker.new(popular_symbols).run
48
24
  end
49
25
 
50
- def popular_quotes
51
- quotes(active_symbols)
26
+ private
27
+ def clean_quotes quotes
28
+ quotes.reject { |quote| quote.ask }
52
29
  end
53
30
  end
54
31
  end
@@ -1,5 +1,7 @@
1
1
  require 'json'
2
2
  require 'ostruct'
3
+ require 'pry'
4
+ require 'uri'
3
5
 
4
6
  module YahooFinanza
5
7
  module Parser
@@ -9,12 +11,16 @@ module YahooFinanza
9
11
  json['ResultSet']['Result'].map { |quote| quote['symbol'] }
10
12
  end
11
13
 
14
+ def hash_to_query(hash)
15
+ URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
16
+ end
17
+
12
18
  def yahoofy(payload)
13
19
  jsons = JSON.parse(payload)['query']['results']['quote']
14
20
  if jsons.class == Hash && jsons['Name']
15
- [OpenStruct.new(jsons)]
21
+ OpenStruct.new(viperize_hash(jsons))
16
22
  elsif jsons.class == Array
17
- jsons.map { |symbol| OpenStruct.new(symbol) }
23
+ jsons.map { |symbol| OpenStruct.new(viperize_hash(symbol)) }
18
24
  else
19
25
  []
20
26
  end
@@ -22,15 +28,29 @@ module YahooFinanza
22
28
 
23
29
  def single_yahoofy(payload)
24
30
  jsons = JSON.parse(payload)['query']['results']['quote']
25
- result = OpenStruct.new(jsons)
26
- return [] unless result.Name
31
+ result = OpenStruct.new(viperize_hash(jsons))
32
+ return '' unless result.name
27
33
  result
28
34
  end
29
35
 
30
36
  def historical_yahoofy(payload)
31
37
  jsons = JSON.parse(payload)['query']['results']
32
38
  return [] unless jsons
33
- jsons['quote'].map { |symbol| OpenStruct.new(symbol) }
39
+ jsons['quote'].map { |symbol| OpenStruct.new(viperize_hash(symbol)) }
40
+ end
41
+
42
+ def viperize(symbol)
43
+ word = symbol.to_s.dup
44
+ word.gsub!(/::/, '/')
45
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
46
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
47
+ word.tr!("-", "_")
48
+ word.downcase!
49
+ word.to_sym
50
+ end
51
+
52
+ def viperize_hash hash
53
+ hash.map { |key, value| { viperize(key.to_s) => value } }.reduce(:merge)
34
54
  end
35
55
  end
36
56
  end
@@ -1,4 +1,4 @@
1
1
  # Yahoo Finanza Version
2
2
  module YahooFinanza
3
- VERSION = '0.3.2'
3
+ VERSION = '0.4.0'
4
4
  end
@@ -0,0 +1,97 @@
1
+ require 'yahoo_finanza/parser'
2
+
3
+ require 'rest-client'
4
+ require 'mechanize'
5
+ require 'yql'
6
+
7
+ module YahooFinanza
8
+
9
+ class BaseWorker
10
+ attr_accessor :yql
11
+ include YahooFinanza::Parser
12
+ include YahooFinanza::Constants
13
+
14
+ def invalid_symbol? symbol
15
+ symbol == '' || !((nyse + nasdaq + amex + sp_500).include? symbol)
16
+ end
17
+ end
18
+
19
+ class ActiveStockWorker
20
+ attr_accessor :browser
21
+
22
+ def initialize
23
+ @browser = Mechanize.new
24
+ end
25
+
26
+ def get_active_stocks
27
+ nasdaq = @browser.get(YahooFinanza::Constants.active_nnm_url).search(".linkb")[0..24]
28
+ nyse = @browser.get(YahooFinanza::Constants.active_nyse_url).search(".linkb")[0..24]
29
+ (nasdaq + nyse).map do |stock|
30
+ stock.text.match(/\(.*?\)/)[0].gsub!("(", "").gsub!(")", "")
31
+ end
32
+ end
33
+ end
34
+
35
+ class SymbolSearchWorker < BaseWorker
36
+ def initialize(key)
37
+ @key = key
38
+ end
39
+
40
+ def run
41
+ params = { callback: "YAHOO.Finance.SymbolSuggest.ssCallback", query: @key }
42
+ response = RestClient.get("#{YahooFinanza::Constants.search_url}" + "#{hash_to_query(params)}")
43
+ parse(response)
44
+ end
45
+ end
46
+
47
+ class MultiQuoteWorker < BaseWorker
48
+ def initialize(symbols)
49
+ @yql = Yql::Client.new(format: 'json')
50
+ @symbols = symbols
51
+ end
52
+
53
+ def run
54
+ return [] if @symbols.empty?
55
+ query = Yql::QueryBuilder.new 'yahoo.finance.quotes'
56
+ query.conditions = "symbol in (\"#{@symbols.join('\", \"')}\")"
57
+ @yql.query = query
58
+ response = @yql.get
59
+ yahoofy(response.show)
60
+ end
61
+ end
62
+
63
+ class SingleQuoteWorker < BaseWorker
64
+ def initialize(symbol)
65
+ @yql = Yql::Client.new(format: 'json')
66
+ @symbol = symbol
67
+ end
68
+
69
+ def run
70
+ return '' if invalid_symbol? @symbol
71
+ query = Yql::QueryBuilder.new 'yahoo.finance.quotes'
72
+ query.conditions = "symbol in (\"#{@symbol}\")"
73
+ @yql.query = query
74
+ response = @yql.get
75
+ single_yahoofy(response.show)
76
+ end
77
+ end
78
+
79
+ class HistoricQuoteWorker < BaseWorker
80
+ def initialize(symbol, opts)
81
+ @yql = Yql::Client.new(format: 'json')
82
+ @symbol = symbol
83
+ @opts = opts
84
+ end
85
+
86
+ def run
87
+ return [] if (invalid_symbol? @symbol) || @opts.empty?
88
+ @query = Yql::QueryBuilder.new('yahoo.finance.historicaldata')
89
+ @query.conditions = {
90
+ symbol: @symbol, startDate: @opts[:start_date],
91
+ endDate: @opts[:end_date] }
92
+ @yql.query = @query
93
+ response = @yql.get
94
+ historical_yahoofy(response.show)
95
+ end
96
+ end
97
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yahoo_finanza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric D. Santos
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-09-28 00:00:00.000000000 Z
12
+ date: 2015-09-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yql
@@ -119,12 +119,11 @@ extensions: []
119
119
  extra_rdoc_files: []
120
120
  files:
121
121
  - lib/yahoo_finanza.rb
122
- - lib/yahoo_finanza/finance_utils.rb
123
122
  - lib/yahoo_finanza/version.rb
124
123
  - lib/yahoo_finanza/parser.rb
125
124
  - lib/yahoo_finanza/client.rb
126
125
  - lib/yahoo_finanza/constants.rb
127
- - lib/yahoo_finanza/worker.rb
126
+ - lib/yahoo_finanza/workers.rb
128
127
  homepage: https://github.com/cometaworks/yahoo_finanza
129
128
  licenses:
130
129
  - MIT
@@ -1,47 +0,0 @@
1
- require 'ostruct'
2
- require 'csv'
3
- require 'open-uri'
4
- require 'uri'
5
-
6
- require 'yahoo_finanza/parser'
7
-
8
- module YahooFinanza
9
- class MarketDoesntExistError < Exception; end
10
-
11
- module FinanceUtils
12
- def self.included(base)
13
- base.extend(self)
14
- end
15
-
16
- def symbols_by_market(market)
17
- if [:nyse, :nasdaq, :amex].include? market.to_sym
18
- YahooFinanza::Constants.send(market.to_sym)
19
- else
20
- raise YahooFinanza::MarketDoesntExistError
21
- end
22
- end
23
-
24
- def sp_symbols
25
- YahooFinanza::Constants.sp_500
26
- end
27
-
28
- def all_symbols
29
- symbols_by_market('nyse').concat(symbols_by_market('nasdaq'))
30
- end
31
-
32
- def search_symbols(key = '')
33
- params = { callback: "YAHOO.Finance.SymbolSuggest.ssCallback", query: key }
34
- response = RestClient.get("#{YahooFinanza::Constants.search_url}" +
35
- "#{hash_to_query(params)}")
36
- YahooFinanza::Parser.parse(response)
37
- end
38
-
39
- def active_symbols
40
- YahooFinanza::Worker.new.get_active_stocks
41
- end
42
-
43
- def hash_to_query(hash)
44
- URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
45
- end
46
- end
47
- end
@@ -1,20 +0,0 @@
1
- require 'mechanize'
2
- require 'rest-client'
3
-
4
- module YahooFinanza
5
- class Worker
6
- attr_accessor :browser
7
-
8
- def initialize
9
- @browser = Mechanize.new
10
- end
11
-
12
- def get_active_stocks
13
- nasdaq = @browser.get(YahooFinanza::Constants.active_nnm_url).search(".linkb")[0..24]
14
- nyse = @browser.get(YahooFinanza::Constants.active_nyse_url).search(".linkb")[0..24]
15
- (nasdaq + nyse).map do |stock|
16
- stock.text.match(/\(.*?\)/)[0].gsub!("(", "").gsub!(")", "")
17
- end
18
- end
19
- end
20
- end