ytterb 0.1.1 → 0.1.147.77920

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ require 'csv'
2
+
3
+ module Ytterb
4
+
5
+ class StockSymbol
6
+ def initialize(options={})
7
+ @options = {}
8
+ options.reject {|k,v| !k }.each do |k,v|
9
+ sym_k = k.gsub(/\s+/,"_").downcase.to_sym
10
+ @options[sym_k] = v
11
+ end
12
+ end
13
+
14
+ def attributes
15
+ return @options.keys
16
+ end
17
+
18
+ def method_missing(meth, *args, &block)
19
+ if @options.has_key?(meth)
20
+ return @options[meth]
21
+ else
22
+ # don't know how to handle it
23
+ super
24
+ end
25
+ end
26
+
27
+ def to_s
28
+ @options.to_s
29
+ end
30
+ end
31
+
32
+
33
+ class StockSymbolLoader
34
+ def initialize(stock_symbol_file, exchange)
35
+ @stock_symbol_file = stock_symbol_file
36
+ @exchange = exchange
37
+ end
38
+
39
+ def parse
40
+ CSV.foreach(@stock_symbol_file, :headers => true) do |obj|
41
+ yield StockSymbol.new(obj.to_hash.merge("Exchange" => @exchange))
42
+ end
43
+ end
44
+ end
45
+
46
+ class StockMarketLoader
47
+ def initialize
48
+ @stock_symbols = []
49
+ path = File.join(File.expand_path(File.dirname(__FILE__)),"raw_symbol_data")
50
+ Dir.entries(path).select {|f| !File.directory?(f) and /companylist_[a-zA-Z]+\.csv/ =~ f }.each do |file_to_process|
51
+ market = /companylist_(?<market>[a-zA-Z]+)\.csv/.match(file_to_process)[:market]
52
+ StockSymbolLoader.new(File.join(path,file_to_process),market).parse do |stock_symbol|
53
+ @stock_symbols << stock_symbol
54
+ end
55
+ end
56
+ end
57
+
58
+ def industries
59
+ return @industries if @industries
60
+ @raw_industries = {}
61
+ @stock_symbols.each do |stock_symbol|
62
+ @raw_industries[stock_symbol.industry] = 1
63
+ end
64
+ @industries = @raw_industries.keys
65
+ end
66
+
67
+ def sectors
68
+ return @sectors if @sectors
69
+ @raw_sectors = {}
70
+ @stock_symbols.each do |stock_symbol|
71
+ @raw_sectors[stock_symbol.sector] = 1
72
+ end
73
+ @sectors = @raw_sectors.keys
74
+ end
75
+
76
+ def stock_symbols
77
+ @stock_symbols
78
+ end
79
+ end
80
+
81
+ end
@@ -1,4 +1,4 @@
1
1
  module Ytterb
2
2
  # ytterb version
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1"
4
4
  end
@@ -0,0 +1,38 @@
1
+ require 'rest-client'
2
+ require 'cgi'
3
+ require 'json'
4
+ require 'date'
5
+
6
+ module Ytterb
7
+
8
+ class YQLClient
9
+ def intialize
10
+ @endpoint = "http://query.yahooapis.com/v1/public/yql"
11
+ end
12
+
13
+ def get_yql_endpoint
14
+ @endpoint
15
+ end
16
+
17
+ def run_select_query(query)
18
+ RestClient.get("#{get_yql_endpoint}?q=#{CGI::escape(query)}&format=json&env=store://datatables.org/alltableswithkeys") do |response, request, result|
19
+ raise "Failed performing YQLQuery\n Request: #{request.inspect}\n Response #{response.inspect}" unless response.code == 200
20
+ return JSON.parse(response)
21
+ end
22
+ end
23
+
24
+ def get_symbol_current(my_symbol)
25
+ run_select_query("select * from yahoo.finance.quotes where symbol in (\"#{my_symbol}\")")["query"]["results"]
26
+ end
27
+
28
+ def get_symbol_historical(my_symbol, start_date = nil, end_date = nil)
29
+ requested_end = Date.parse(end_date) if end_date
30
+ requested_end = Date.today unless requested_end
31
+
32
+ requested_start = Date.parse(start_date) if start_date
33
+ requested_start = requested_end - 30 unless requested_start
34
+ run_select_query("select * from yahoo.finance.historicaldata where symbol in (\"#{my_symbol}\") and startDate='#{requested_start}' and endDate='#{requested_end}'")["query"]["results"]
35
+ end
36
+
37
+ end
38
+ end
data/ytterb.gemspec CHANGED
@@ -1,10 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
+ require 'date'
3
4
  require File.expand_path('../lib/ytterb/version', __FILE__)
4
5
 
6
+ t = Time.now
7
+ minor_rev = t.hour * 3600 + t.min * 60 + t.sec
8
+
5
9
  Gem::Specification.new do |gem|
6
10
  gem.name = "ytterb"
7
- gem.version = Ytterb::VERSION
11
+ gem.version = "#{Ytterb::VERSION}.#{Date.today.yday}.#{minor_rev}"
8
12
  gem.summary = %q{ytterb finance data gen}
9
13
  gem.description = %q{pulls financial data data via yahoo's YQL apis}
10
14
  gem.license = "MIT"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ytterb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.147.77920
5
5
  platform: ruby
6
6
  authors:
7
7
  - thext
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-25 00:00:00.000000000 Z
11
+ date: 2014-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,7 +94,12 @@ files:
94
94
  - README.md
95
95
  - Rakefile
96
96
  - lib/ytterb.rb
97
+ - lib/ytterb/raw_symbol_data/companylist_amex.csv
98
+ - lib/ytterb/raw_symbol_data/companylist_nasdaq.csv
99
+ - lib/ytterb/raw_symbol_data/companylist_nyse.csv
100
+ - lib/ytterb/stock_symbol_loader.rb
97
101
  - lib/ytterb/version.rb
102
+ - lib/ytterb/yql_client.rb
98
103
  - spec/spec_helper.rb
99
104
  - spec/ytterb_spec.rb
100
105
  - ytterb.gemspec