yahoo_stocks 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4413bb3bad3157c0dd3193855dc13f3260921c76
4
+ data.tar.gz: 74a849cebc64821499c43470c9f83fd1ec27d75a
5
+ SHA512:
6
+ metadata.gz: 831691bab71cce1ad7dfa18b7e76e72a6e2630c9975de74659914b49e1adf54fb891a0b819d29c516e4705f09632853013662d91792848bf8183fae1215f8f84
7
+ data.tar.gz: 767df3cbcb380fd62aeb4e82c856d5bc805a749e66aaa59800b27a65ca4d422e9eb192e982ca8d20b02f1e831db8a57379d553e2aaee55f3d2d0d1a3fa65ff74
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Denis Redozubov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH << File.expand_path('./lib')
2
+ require "yahoo_stocks"
3
+
4
+ task :build do
5
+ system "gem build yahoo_stocks.gemspec"
6
+ end
7
+
8
+ task :release => :build do
9
+ system "gem push yahoo_stocks-#{YahooStocks::VERSION}"
10
+ end
11
+
12
+ task :test do
13
+ system "rspec spec"
14
+ end
@@ -0,0 +1,7 @@
1
+ require 'yahoo_stocks/endpoint'
2
+ require 'yahoo_stocks/quotes'
3
+ require 'yahoo_stocks/version'
4
+
5
+ module YahooStocks
6
+ include YahooStocks::Endpoint
7
+ end
@@ -0,0 +1,3 @@
1
+ require 'yahoo_stocks/backend/nmatrix.rb'
2
+ require 'yahoo_stocks/backend/openstruct.rb'
3
+ require 'yahoo_stocks/backend/array.rb'
@@ -0,0 +1,19 @@
1
+ require 'yahoo_stocks/backend/basic'
2
+
3
+ module YahooStocks
4
+ module Backend
5
+ class Array < YahooStocks::Backend::Basic
6
+
7
+ def produce(stream)
8
+ result = []
9
+
10
+ CSV.parse(stream.body, quote_char: '"') do |*values|
11
+ result << values.map { |v| v.to_f rescue v }
12
+ end
13
+
14
+ result.flatten
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ require 'csv'
2
+
3
+ module YahooStocks
4
+ module Backend
5
+ class Basic
6
+
7
+ def produce(*args)
8
+ raise NotImplementedError, 'backend not implemented'
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+
File without changes
File without changes
File without changes
@@ -0,0 +1,32 @@
1
+ require 'yahoo_stocks/endpoint/tags'
2
+
3
+ module YahooStocks
4
+ module Common
5
+
6
+ include YahooStocks::Endpoint::Tags
7
+
8
+ private
9
+
10
+ def get_values(values)
11
+ case values
12
+ when String then [values]
13
+ when Symbol then [values.to_s]
14
+ when Array then values.map(&:to_s)
15
+ end
16
+ end
17
+
18
+ def get_format(tags)
19
+ tags.map! do |tag|
20
+ tag = tag.to_sym
21
+ case tag
22
+ when *TAGS.keys then TAGS[tag]
23
+ when *TAGS.values then tag
24
+ else raise "invalid format: #{tag}"
25
+ end
26
+ end
27
+
28
+ tags.compact.join
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ require 'uri'
2
+ require 'yahoo_stocks/common'
3
+
4
+ module YahooStocks
5
+ module Endpoint
6
+
7
+ extend URI::Escape
8
+ extend YahooStocks::Common
9
+
10
+ ENDPOINT = 'http://download.finance.yahoo.com/d/quotes.csv?'
11
+ FORMAT = [:symbol, :last_trade_price_only, :change, :previous_close] #
12
+
13
+ def self.compose_quotes(symbols, format = nil)
14
+ format = get_format(format ? format : FORMAT)
15
+ symbols = get_values(symbols)
16
+ uri = get_uri(format, s: symbols)
17
+ URI.parse(uri)
18
+ end
19
+
20
+ private
21
+
22
+ def self.get_uri(format, options={})
23
+ ENDPOINT + joined(options) + "&f=#{format}"
24
+ end
25
+
26
+ def self.joined(options)
27
+ out = options.inject('') do |s, el|
28
+ option, values = el
29
+ values = get_values(values)
30
+ s << "#{option}=#{values.map{ |v| escape(v) }.join(',')}&"
31
+ end
32
+ out.chomp!('&')
33
+ out
34
+ end
35
+
36
+ end
37
+ end
38
+
@@ -0,0 +1,98 @@
1
+ module YahooStocks
2
+ module Endpoint
3
+ module Tags
4
+ # format note:
5
+ # x0 same as x there
6
+ TAGS = {
7
+ after_hours_change_realtime: :c8,
8
+ annualized_gain: :g3,
9
+ ask: :a0,
10
+ ask_realtime: :b2,
11
+ ask_size: :a5,
12
+ average_daily_volume: :a2,
13
+ bid: :b0,
14
+ bid_realtime: :b3,
15
+ bid_size: :b6,
16
+ book_value_per_share: :b4,
17
+ change: :c1,
18
+ change__change_in_percent: :c0,
19
+ change_from_fiftyday_moving_average: :m7,
20
+ change_from_two_hundredday_moving_average: :m5,
21
+ change_from_year_high: :k4,
22
+ change_from_year_low: :j5,
23
+ change_in_percent: :p2,
24
+ change_in_percent_realtime: :k2,
25
+ change_realtime: :c6,
26
+ commission: :c3,
27
+ currency: :c4,
28
+ days_high: :h0,
29
+ days_low: :g0,
30
+ days_range: :m0,
31
+ days_range_realtime: :m2,
32
+ days_value_change: :w1,
33
+ days_value_change_realtime: :w4,
34
+ dividend_pay_date: :r1,
35
+ trailing_annual_dividend_yield: :d0,
36
+ trailing_annual_dividend_yield_in_percent: :y0,
37
+ diluted_eP_s: :e0,
38
+ e_bI_tD_a: :j4,
39
+ e_pS_estimate_current_year: :e7,
40
+ e_pS_estimate_next_quarter: :e9,
41
+ e_pS_estimate_next_year: :e8,
42
+ ex_dividend_date: :q0,
43
+ fiftyday_moving_average: :m3,
44
+ shares_float: :f6,
45
+ high_limit: :l2,
46
+ holdings_gain: :g4,
47
+ holdings_gain_percent: :g1,
48
+ holdings_gain_percent_realtime: :g5,
49
+ holdings_gain_realtime: :g6,
50
+ holdings_value: :v1,
51
+ holdings_value_realtime: :v7,
52
+ last_trade_date: :d1,
53
+ last_trade_price_only: :l1,
54
+ last_trade_realtime_with_time: :k1,
55
+ last_trade_size: :k3,
56
+ last_trade_time: :t1,
57
+ last_trade_with_time: :l0,
58
+ low_limit: :l3,
59
+ market_capitalization: :j1,
60
+ market_cap_realtime: :j3,
61
+ more_info: :i0,
62
+ name: :n0,
63
+ notes: :n4,
64
+ oneyr_target_price: :t8,
65
+ open: :o0,
66
+ order_book_realtime: :i5,
67
+ p_eG_ratio: :r5,
68
+ p_eRatio: :r0,
69
+ p_eRatio_realtime: :r2,
70
+ percent_change_from_fiftyday_moving_average: :m8,
71
+ percent_change_from_two_hundredday_moving_average: :m6,
72
+ change_in_percent_from_year_high: :k5,
73
+ percent_change_from_year_low: :j6,
74
+ previous_close: :p0,
75
+ price_book: :p6,
76
+ price_eP_sEstimate_current_year: :r6,
77
+ price_eP_sEstimate_next_year: :r7,
78
+ price_paid: :p1,
79
+ price_sales: :p5,
80
+ revenue: :s6,
81
+ shares_owned: :s1,
82
+ shares_outstanding: :j2,
83
+ short_ratio: :s7,
84
+ stock_exchange: :x0,
85
+ symbol: :s0,
86
+ ticker_trend: :t7,
87
+ trade_date: :d2,
88
+ trade_links: :t6,
89
+ trade_links_additional: :f0,
90
+ two_hundredday_moving_average: :m4,
91
+ volume: :v0,
92
+ year_high: :k0,
93
+ year_low: :j0,
94
+ year_range: :w0,
95
+ }
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,11 @@
1
+ require 'net/http'
2
+
3
+ module YahooStocks
4
+ module Http
5
+
6
+ def http_get(uri)
7
+ Net::HTTP.get_response(uri)
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'yahoo_stocks/common'
2
+ require 'yahoo_stocks/backend'
3
+ require 'yahoo_stocks/endpoint'
4
+ require 'yahoo_stocks/http'
5
+
6
+ module YahooStocks
7
+ module Quotes
8
+
9
+ BACKEND = YahooStocks::Backend::Array
10
+
11
+ include YahooStocks::Endpoint
12
+ extend YahooStocks::Http
13
+
14
+ def self.get(symbols, format = nil)
15
+ uri = YahooStocks::Endpoint.compose_quotes(symbols, format)
16
+ response = http_get(uri)
17
+ BACKEND.new.produce(response)
18
+ end
19
+
20
+ def self.method_missing(symbol, format = nil)
21
+ # http://yehudakatz.com/2010/01/02/the-craziest-fing-bug-ive-ever-seen/
22
+ # http://stackoverflow.com/questions/8960685/ruby-why-does-puts-call-to-ary
23
+ super if symbol == :to_ary
24
+ get([symbol], format)
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module YahooStocks
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'yahoo_stocks/common'
3
+
4
+ describe YahooStocks::Common do
5
+
6
+ include YahooStocks::Common
7
+
8
+ describe '.get_format' do
9
+
10
+ it "works with strings" do
11
+ expect { get_format("foo") }.to raise_error
12
+ end
13
+
14
+ it "works with array of symbols" do
15
+ get_format([:v1, :s1, :l1]).should == "v1s1l1"
16
+ end
17
+
18
+ it "works with array of strings" do
19
+ get_format(%W{v1 s1 l1}).should == "v1s1l1"
20
+ end
21
+
22
+ end
23
+
24
+ describe '.get_values' do
25
+
26
+ it "works with string" do
27
+ get_values("AAPL").should == %W{AAPL}
28
+ end
29
+
30
+ it "works with array of symbols" do
31
+ get_values([:AAPL, :GOOG]).should == %W{AAPL GOOG}
32
+ end
33
+
34
+ it "works with array of strings" do
35
+ get_values(%W{AAPL GOOG}).should == %W{AAPL GOOG}
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'uri'
3
+ require 'yahoo_stocks/endpoint'
4
+
5
+ describe YahooStocks::Endpoint do
6
+
7
+ let(:format) { YahooStocks::Endpoint::FORMAT }
8
+ let(:test_params) { "?s=AAPL&f=#{format}" }
9
+
10
+ describe "compose quotes uri" do
11
+ YahooStocks::Endpoint.compose_quotes(:AAPL).should == \
12
+ URI.parse('http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&f=s0l1c1p0')
13
+ end
14
+
15
+ describe "get_uri with one symbol" do
16
+
17
+ it "as array of strings" do
18
+ subject.get_uri(format, s: %W{AAPL}).should end_with test_params
19
+ end
20
+
21
+ it "as array of strings" do
22
+ subject.get_uri(format, s: [:AAPL]).should end_with test_params
23
+ end
24
+
25
+ it "as symbol" do
26
+ subject.get_uri(format, s: :AAPL).should end_with test_params
27
+ end
28
+
29
+ it "as string" do
30
+ subject.get_uri(format, s: 'AAPL').should end_with test_params
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+ require 'yahoo_stocks/quotes'
3
+
4
+ describe YahooStocks::Quotes do
5
+
6
+ let(:symbol) { :AAPL }
7
+ let(:format) { [:l1, :v1] }
8
+
9
+ describe "dynamic calls" do
10
+
11
+ context "with format" do
12
+
13
+ specify 'upcase' do
14
+ subject.should_receive(symbol).with(kind_of(Array))
15
+ expect { subject.send(symbol, format) }.to_not raise_error
16
+ end
17
+
18
+ specify 'downcase' do
19
+ s = symbol.downcase.to_sym
20
+ subject.should_receive(s).with(kind_of(Array))
21
+ expect { subject.send(s, format) }.to_not raise_error
22
+ end
23
+
24
+ end
25
+
26
+ specify "without format" do
27
+ subject.should_receive(symbol)
28
+ expect { subject.send(symbol) }.to_not raise_error
29
+ end
30
+
31
+ end
32
+
33
+ describe '.get' do
34
+
35
+ context 'symbol as ' do
36
+
37
+ specify 'array of symbols' do
38
+ expect { subject.get([:AAPL, :GOOG], format) }.to_not raise_error
39
+ end
40
+
41
+ specify 'array of strings' do
42
+ expect { subject.get(%W{AAPL GOOG}, format) }.to_not raise_error
43
+ end
44
+
45
+ specify 'symbol' do
46
+ expect { subject.get(symbol, format) }.to_not raise_error
47
+ end
48
+
49
+ specify 'string' do
50
+ expect { subject.get(symbol.to_s, format) }.to_not raise_error
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ $LOAD_PATH << File.expand_path('../lib')
9
+ #require 'yahoo_stocks'
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ require 'yahoo_stocks'
3
+
4
+ describe YahooStocks do
5
+ end
6
+
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yahoo_stocks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Denis Redozubov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email: denis.redozubov@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE
48
+ - Rakefile
49
+ - lib/yahoo_stocks.rb
50
+ - lib/yahoo_stocks/backend.rb
51
+ - lib/yahoo_stocks/backend/array.rb
52
+ - lib/yahoo_stocks/backend/basic.rb
53
+ - lib/yahoo_stocks/backend/hash.rb
54
+ - lib/yahoo_stocks/backend/nmatrix.rb
55
+ - lib/yahoo_stocks/backend/openstruct.rb
56
+ - lib/yahoo_stocks/common.rb
57
+ - lib/yahoo_stocks/endpoint.rb
58
+ - lib/yahoo_stocks/endpoint/tags.rb
59
+ - lib/yahoo_stocks/http.rb
60
+ - lib/yahoo_stocks/quotes.rb
61
+ - lib/yahoo_stocks/version.rb
62
+ - spec/common_spec.rb
63
+ - spec/endpoint_spec.rb
64
+ - spec/quotes_spec.rb
65
+ - spec/spec_helper.rb
66
+ - spec/yahoo_stocks_spec.rb
67
+ homepage: http://www.denisredozubov.com
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.2.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Sane Yahoo! Finance rubiesque client
91
+ test_files: []