yahoo_finanza 0.2.0 → 0.3.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: 8eec01d7178fe8b9594e143eab83e8a30632f0ef
4
- data.tar.gz: b230ec6053954c51cd45ef5b5bf1785a11bf7713
3
+ metadata.gz: af130ef3ca135486c91cca2ceb3ae2f431162480
4
+ data.tar.gz: 36af5ea5d997e60d828b365c0b47488fd05f43b0
5
5
  SHA512:
6
- metadata.gz: 2d894214d272c9194992386463ff4bf3558e85ff8a6271d628a9ca2d813f015676eb1ea8c8f2eb9c43b1299b482666fc86b2ac77b1da7f39ec24f0c72fc0ebfd
7
- data.tar.gz: 8e83bd13b441e671e189fd46a52f84e9c1fb68046dad4a9b52833d639c41cee9b6ffee74e4cc2ec72053a63259487224f1942d44a7afce99c062fb3b5083746a
6
+ metadata.gz: a651ce4227b9f057b29e83c1e47dd98a25db82888eab8ed5d3d8c6365b96cf98821e96fb49050493ab4e318cdf621ded8ade0b68f5d606dc0a0f837118b2b2cd
7
+ data.tar.gz: 84e2e7c1cb5ac41bc22cf0e5723acf85edb3a49170411dcb60069ec42927eb5637b4ce8f771fa777e13e18dc14ddef90b7c76331bcefa6528f40dec3a7e809dd
@@ -0,0 +1,54 @@
1
+ require 'yahoo_finanza/parser'
2
+ require 'yahoo_finanza/finance_utils'
3
+
4
+ require 'yql'
5
+ require 'json'
6
+ require 'ostruct'
7
+ require 'mechanize'
8
+
9
+ module YahooFinanza
10
+ class Client
11
+ include YahooFinanza::FinanceUtils
12
+ include YahooFinanza::Parser
13
+
14
+ attr_reader :yql, :mecha_yahoo
15
+
16
+ def initialize
17
+ @yql = Yql::Client.new
18
+ @yql.format = 'json'
19
+ end
20
+
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)
28
+ end
29
+
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)
37
+ end
38
+
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)
48
+ end
49
+
50
+ def popular_quotes
51
+ quotes(active_symbols)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,35 @@
1
+ require "yaml"
2
+
3
+ module YahooFinanza
4
+ module Constants
5
+ extend self
6
+
7
+ def nyse
8
+ YAML.load(File.read("config/nyse.yml"))["nyse_symbols"]
9
+ end
10
+
11
+ def amex
12
+ YAML.load(File.read("config/amex.yml"))["amex_symbols"]
13
+ end
14
+
15
+ def nasdaq
16
+ YAML.load(File.read("config/nasdaq.yml"))["nasdaq_symbols"]
17
+ end
18
+
19
+ def sp_500
20
+ YAML.load(File.read("config/sp_500.yml"))["sp_500_symbols"]
21
+ end
22
+
23
+ def active_nyse_url
24
+ YAML.load(File.read("config/config.yml"))["active_nyse_url"]
25
+ end
26
+
27
+ def active_nnm_url
28
+ YAML.load(File.read("config/config.yml"))["active_nnm_url"]
29
+ end
30
+
31
+ def search_url
32
+ YAML.load(File.read("config/config.yml"))["search_url"]
33
+ end
34
+ end
35
+ end
@@ -1,59 +1,28 @@
1
1
  require 'ostruct'
2
2
  require 'csv'
3
3
  require 'open-uri'
4
- require 'rest-client'
4
+ require 'uri'
5
+
6
+ require 'yahoo_finanza/parser'
5
7
 
6
8
  module YahooFinanza
7
- # Finance Utils For Yahoo Finanza
9
+ class MarketDoesntExistError < Exception; end
10
+
8
11
  module FinanceUtils
9
12
  def self.included(base)
10
13
  base.extend(self)
11
14
  end
12
15
 
13
- def method_missing(method_sym, *args, &block)
14
- if !!(method_sym =~ /'_url'/)
15
- "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=#{method_sym}&render=download"
16
- end
17
- end
18
-
19
- %w(nasdaq nyse amex).each do |market_symbol|
20
- define_method "#{market_symbol}_url".to_sym do
21
- "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=#{market_symbol}&render=download"
22
- end
23
- end
24
-
25
- %w(nnm nyse).each do |market_symbol|
26
- define_method "active_#{market_symbol}_url".to_sym do
27
- "http://online.wsj.com/mdc/public/page/2_3021-activ#{market_symbol}-actives.html"
28
- end
29
- end
30
-
31
- def search_endpoint(key)
32
- "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=#{key}&callback=YAHOO.Finance.SymbolSuggest.ssCallback"
33
- end
34
-
35
16
  def symbols_by_market(market)
36
- symbols = []
37
- return symbols if market.nil?
38
- if respond_to?("#{market}_url".to_sym)
39
- url = send("#{market}_url".to_sym)
40
- market_file = open(url)
41
-
42
- CSV.foreach(market_file) do |row|
43
- next if row.first == 'Symbol'
44
- symbols.push(row.first.gsub(' ', ''))
45
- end
17
+ if [:nyse, :nasdaq, :amex].include? market.to_sym
18
+ YahooFinanza::Constants.send(market.to_sym)
19
+ else
20
+ raise YahooFinanza::MarketDoesntExistError
46
21
  end
47
- symbols
48
22
  end
49
23
 
50
- def sp_symbols(limit)
51
- response = JSON.parse(RestClient.get("http://46.101.6.197/standard_poor/#{limit}"))
52
- if response['error']
53
- Array.new
54
- else
55
- response['result']
56
- end
24
+ def sp_symbols
25
+ YahooFinanza::Constants.sp_500
57
26
  end
58
27
 
59
28
  def all_symbols
@@ -61,30 +30,19 @@ module YahooFinanza
61
30
  end
62
31
 
63
32
  def search_symbols(key = '')
64
- response = RestClient.get search_endpoint(key)
65
- result = response.gsub(/YAHOO.Finance.SymbolSuggest.ssCallback\(|\)/, '')
66
- json = JSON.parse(result)
67
- json['ResultSet']['Result'].map{|quote| quote['symbol']}
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)
68
37
  end
69
38
 
70
39
  def active_symbols
71
- mecha_yahoo = Mechanize.new
72
- symbols = Array.new
73
- page1 = mecha_yahoo.get(active_nnm_url)
74
- page1.links.each do |link|
75
- if link.text.split(" (").count > 1
76
- symbols.push(link.text.split(" (")[1].gsub(')', '').strip)
77
- end
78
- break unless symbols.count < 8
79
- end
80
- page2 = mecha_yahoo.get(active_nyse_url)
81
- page2.links.each do |link|
82
- if link.text.split(" (").count > 1
83
- symbols.push(link.text.split(" (")[1].gsub(')', '').strip)
84
- end
85
- break unless symbols.count < 16
86
- end
87
- symbols
40
+ worker = YahooFinanza::Worker.new
41
+ worker.process_page_2(worker.process_page_1)
42
+ end
43
+
44
+ def hash_to_query(hash)
45
+ URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
88
46
  end
89
47
  end
90
48
  end
@@ -0,0 +1,36 @@
1
+ require 'json'
2
+ require 'ostruct'
3
+
4
+ module YahooFinanza
5
+ module Parser
6
+ extend self
7
+ def parse(response)
8
+ json = JSON.parse(response.gsub(/YAHOO.Finance.SymbolSuggest.ssCallback\(|\)/, ''))
9
+ json['ResultSet']['Result'].map { |quote| quote['symbol'] }
10
+ end
11
+
12
+ def yahoofy(payload)
13
+ jsons = JSON.parse(payload)['query']['results']['quote']
14
+ if jsons.class == Hash && jsons['Name']
15
+ [OpenStruct.new(jsons)]
16
+ elsif jsons.class == Array
17
+ jsons.map { |symbol| OpenStruct.new(symbol) }
18
+ else
19
+ []
20
+ end
21
+ end
22
+
23
+ def single_yahoofy(payload)
24
+ jsons = JSON.parse(payload)['query']['results']['quote']
25
+ result = OpenStruct.new(jsons)
26
+ return [] unless result.Name
27
+ result
28
+ end
29
+
30
+ def historical_yahoofy(payload)
31
+ jsons = JSON.parse(payload)['query']['results']
32
+ return [] unless jsons
33
+ jsons['quote'].map { |symbol| OpenStruct.new(symbol) }
34
+ end
35
+ end
36
+ end
@@ -1,4 +1,4 @@
1
1
  # Yahoo Finanza Version
2
2
  module YahooFinanza
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
@@ -0,0 +1,35 @@
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 process_page_1
13
+ symbols = []
14
+ page_1 = @browser.get(YahooFinanza::Constants.active_nnm_url)
15
+ page_1.links.each do |link|
16
+ if link.text.split(" (").count > 1
17
+ symbols.push(link.text.split(" (")[1].gsub(')', '').strip)
18
+ end
19
+ break unless symbols.count < 8
20
+ end
21
+ symbols
22
+ end
23
+
24
+ def process_page_2(symbols)
25
+ page_2 = @browser.get(YahooFinanza::Constants.active_nyse_url)
26
+ page_2.links.each do |link|
27
+ if link.text.split(" (").count > 1
28
+ symbols.push(link.text.split(" (")[1].gsub(')', '').strip)
29
+ end
30
+ break unless symbols.count < 16
31
+ end
32
+ symbols
33
+ end
34
+ end
35
+ end
data/lib/yahoo_finanza.rb CHANGED
@@ -1,59 +1,7 @@
1
1
  require 'yahoo_finanza/version'
2
- require 'yahoo_finanza/finance_utils'
3
- require 'yahoo_finanza/yahoo_helpers'
4
- require 'yql'
5
- require 'json'
6
- require 'ostruct'
7
- require 'mechanize'
2
+ require 'yahoo_finanza/client'
3
+ require 'yahoo_finanza/constants'
4
+ require 'yahoo_finanza/worker'
8
5
 
9
6
  module YahooFinanza
10
- # Yahoo Finanza Client
11
- class Client
12
- include YahooFinanza::FinanceUtils
13
- include YahooFinanza::YahooHelpers
14
-
15
- attr_reader :yql, :mecha_yahoo
16
-
17
- def initialize
18
- @yql = Yql::Client.new
19
- @yql.format = 'json'
20
- end
21
-
22
- def quotes(symbols)
23
- return nil if symbols.empty?
24
- query = Yql::QueryBuilder.new 'yahoo.finance.quotes'
25
- query.conditions = "symbol in (\"#{symbols.join('\", \"')}\")"
26
- @yql.query = query
27
- response = @yql.get
28
- yahoofy(response.show)
29
- end
30
-
31
- def quote(symbol)
32
- return nil if symbol == ''
33
- query = Yql::QueryBuilder.new 'yahoo.finance.quotes'
34
- query.conditions = "symbol in (\"#{symbol}\")"
35
- @yql.query = query
36
- response = @yql.get
37
- single_yahoofy(response.show)
38
- end
39
-
40
- def historical_quotes(symbol, options = {})
41
- return [] if symbol == ''
42
- query = Yql::QueryBuilder.new 'yahoo.finance.historicaldata'
43
- query.conditions = {
44
- symbol: symbol, startDate: options[:start_date],
45
- endDate: options[:end_date] }
46
- @yql.query = query
47
- response = @yql.get
48
- historical_yahoofy(response.show)
49
- end
50
-
51
- def sp_quotes_top(limit)
52
- quotes(sp_symbols(limit))
53
- end
54
-
55
- def popular_quotes
56
- quotes(active_symbols)
57
- end
58
- end
59
7
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yahoo_finanza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric D. Santos
8
+ - Robert Grayson
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-09-04 00:00:00.000000000 Z
12
+ date: 2015-09-28 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: yql
@@ -120,7 +121,10 @@ files:
120
121
  - lib/yahoo_finanza.rb
121
122
  - lib/yahoo_finanza/finance_utils.rb
122
123
  - lib/yahoo_finanza/version.rb
123
- - lib/yahoo_finanza/yahoo_helpers.rb
124
+ - lib/yahoo_finanza/parser.rb
125
+ - lib/yahoo_finanza/client.rb
126
+ - lib/yahoo_finanza/constants.rb
127
+ - lib/yahoo_finanza/worker.rb
124
128
  homepage: https://github.com/cometaworks/yahoo_finanza
125
129
  licenses:
126
130
  - MIT
@@ -1,45 +0,0 @@
1
- require 'json'
2
- require 'ostruct'
3
-
4
- module YahooFinanza
5
- # Yahoo Helpers for Yahoo Finanza
6
- module YahooHelpers
7
- def yahoofy(payload)
8
- yahoofied = OpenStruct.new
9
- jsons = JSON.parse(payload)['query']['results']['quote']
10
- if jsons.class == Hash
11
- tmpsym = OpenStruct.new(symbol)
12
- if tmpsym.Name
13
- yahoofied[tmpsym.symbol] = tmpsym
14
- end
15
- else
16
- jsons.each do |symbol|
17
- tmpsym = OpenStruct.new(symbol)
18
- if tmpsym.Name
19
- yahoofied[tmpsym.symbol] = tmpsym
20
- end
21
- end
22
- end
23
- return nil if yahoofied.to_h.keys.empty?
24
- yahoofied
25
- end
26
-
27
- def single_yahoofy(payload)
28
- jsons = JSON.parse(payload)['query']['results']['quote']
29
- result = OpenStruct.new(jsons)
30
- return nil unless result.Name
31
- result
32
- end
33
-
34
- def historical_yahoofy(payload)
35
- yahoofied = []
36
- jsons = JSON.parse(payload)['query']['results']
37
- return [] unless jsons
38
- jsons['quote'].each do |symbol|
39
- tmpsym = OpenStruct.new(symbol)
40
- yahoofied << tmpsym
41
- end
42
- yahoofied
43
- end
44
- end
45
- end