stocktastic 0.1.0

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: 0644c3d920263dfb868b56c6ba8871ade3f6be41
4
+ data.tar.gz: 45c0a5bb26eff0ea4b1814cb5d1cf7472b46b06e
5
+ SHA512:
6
+ metadata.gz: 228c47bb333d6b5d86dcb7e1637f2f33945f38303483aa0de5e6774ebf1f8f12b8cec4856a91f04a0650b46a12a11bf6c693d5341e593240f1dd4fc85e0a0eee
7
+ data.tar.gz: f2f32650cf61d047cebbca58282f2d8a374cc248aa256a0cd01f6af784b4c6f2f0d95bb04bb028e37c9a71109da959f934873bc9e4e0f12f0ae091897ecec7c3
@@ -0,0 +1,25 @@
1
+ # -*- ruby -*-
2
+
3
+ require "autotest/restart"
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.testlib = "minitest/unit"
7
+ #
8
+ # at.extra_files << "../some/external/dependency.rb"
9
+ #
10
+ # at.libs << ":../some/external"
11
+ #
12
+ # at.add_exception "vendor"
13
+ #
14
+ # at.add_mapping(/dependency.rb/) do |f, _|
15
+ # at.files_matching(/test_.*rb$/)
16
+ # end
17
+ #
18
+ # %w(TestA TestB).each do |klass|
19
+ # at.extra_class_map[klass] = "test/test_misc.rb"
20
+ # end
21
+ # end
22
+
23
+ # Autotest.add_hook :run_command do |at|
24
+ # system "rake build"
25
+ # end
File without changes
@@ -0,0 +1,18 @@
1
+ === 0.1.0 / 2014-08-16
2
+
3
+ * Added Stocktastic::Base for main Stocktastic usage.
4
+ It uses the Yahoo adapter by default and can fetch quote
5
+ details for a single symbol or a list of symbols.
6
+
7
+ stocktastic = Stocktastic::Base.new
8
+ quote = stocktastic.quote 'AAPL'
9
+ quotes = stocktastic.quotes ['AAPL', 'MSFT']
10
+
11
+ * Added Yahoo adapter which can fetch quotes from Yahoo Finance
12
+
13
+ === 0.0.1 / 2014-08-15
14
+
15
+ * 1 minor enhancement
16
+
17
+ * Birthday!
18
+
@@ -0,0 +1,12 @@
1
+ .autotest
2
+ History.rdoc
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ bin/stocktastic
7
+ lib/stocktastic.rb
8
+ lib/stocktastic/base.rb
9
+ lib/stocktastic/adapter.rb
10
+ lib/stocktastic/adapter/base.rb
11
+ lib/stocktastic/adapter/yahoo.rb
12
+ test/test_stocktastic.rb
@@ -0,0 +1,66 @@
1
+ = stocktastic
2
+
3
+ * https://github.com/pubis/stocktastic
4
+
5
+ == Description
6
+
7
+ Stocktastic fetches stock quotes
8
+
9
+ It uses Yahoo Finance as a source by default.
10
+
11
+ == Usage
12
+
13
+ === Basics
14
+
15
+ require 'stocktastic'
16
+
17
+ stocktastic = Stocktastic::Base.new
18
+ # => #<Stocktastic::Base @adapter=#<Stocktastic::Adapter::Yahoo>>
19
+
20
+ quote = stocktastic.quote 'AAPL'
21
+ # => {:symbol=>"AAPL", :name=>"Apple Inc.", :last_trade_price_only=>"97.98", ...}
22
+
23
+ qoutes = stocktastic.quotes ['AAPL', 'GOOG', 'MSFT']
24
+ # => [{:symbol=>"AAPL", :name=>"Apple Inc.", ...}, {:symbol=>"GOOG", ...}, ...]
25
+
26
+ # Searching is not implemented yet
27
+ #stocks = stocktastic.search 'Google'
28
+ # => [{:symbol => 'GOOG', :name => 'Google'}, ...]
29
+
30
+ # History is not implemented yet
31
+ #last_month = Date.today.prev_month..Date.today
32
+ #history = stocktastic.history 'AAPL', last__month
33
+ # => [{:date=>"2014-08-16", :close=>"97.98"}, ...]
34
+
35
+ == Requirements
36
+
37
+ * Ruby 1.9 or greater
38
+
39
+ == Installation
40
+
41
+ gem install stocktastic
42
+
43
+ == License
44
+
45
+ (The MIT License)
46
+
47
+ Copyright (c) 2014 Jonas Lundqvist
48
+
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of this software and associated documentation files (the
51
+ 'Software'), to deal in the Software without restriction, including
52
+ without limitation the rights to use, copy, modify, merge, publish,
53
+ distribute, sublicense, and/or sell copies of the Software, and to
54
+ permit persons to whom the Software is furnished to do so, subject to
55
+ the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be
58
+ included in all copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
61
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ # -*- ruby -*-
2
+
3
+ require "rubygems"
4
+ require "hoe"
5
+
6
+ Hoe.plugin :minitest
7
+ Hoe.plugin :rcov
8
+ Hoe.plugin :rdoc
9
+
10
+ Hoe.spec "stocktastic" do
11
+ developer("Jonas Lundqvist", "lundqvist.j@gmail.com")
12
+
13
+ license "MIT"
14
+ end
15
+
16
+ # vim: syntax=ruby
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ abort "you need to write me"
@@ -0,0 +1,13 @@
1
+ require 'stocktastic/adapter'
2
+ require 'stocktastic/base'
3
+
4
+ module Stocktastic
5
+ # The version of Stocktastic
6
+ VERSION = "0.1.0"
7
+
8
+ ##
9
+ # Default adapter through which quotes are fetched.
10
+ def self.default_adapter
11
+ Stocktastic::Adapter::Yahoo
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require 'stocktastic/adapter/base'
2
+ require 'stocktastic/adapter/yahoo'
3
+
4
+ module Stocktastic
5
+ module Adapter #:nodoc:
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Stocktastic
2
+ module Adapter
3
+ # Abstract base class for adapters
4
+ class Base
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,68 @@
1
+ require 'net/http'
2
+ require 'csv'
3
+
4
+ module Stocktastic
5
+ module Adapter
6
+ ##
7
+ # Adapter for interfacing with Yahoo finance
8
+ class Yahoo < Base
9
+ # Uri used for fetching quotes
10
+ QUOTES_URI = "http://download.finance.yahoo.com/d/quotes.csv"
11
+
12
+ ##
13
+ # List of available paramters
14
+ # This list is used as a single source of reference for
15
+ # a) mapping between requested parameters and url parameters
16
+ # and b) mapping between the result array and hash keys.
17
+ PARAMETER_MAP = {
18
+ symbol: 's',
19
+ name: 'n',
20
+ last_trade_price_only: 'l1',
21
+ last_trade_date: 'd1',
22
+ last_trade_time: 't1'
23
+ }
24
+
25
+ ##
26
+ # Fetches quote details for a single symbol.
27
+ # Last argument can specify options:
28
+ # - :parameters Array of parameters to get (default is everything)
29
+ def get_single_quote symbol, options = {}
30
+ get_multiple_quotes([symbol], options)[0]
31
+ end
32
+
33
+ # Fetches quote details for multiple symbols.
34
+ # Last argument can specify options:
35
+ # - :parameters Array of parameters to get (default is everything)
36
+ def get_multiple_quotes symbols, options = {}
37
+ options[:parameters] ||= PARAMETER_MAP.keys
38
+ get_quotes symbols, options[:parameters]
39
+ end
40
+
41
+ private
42
+
43
+ def get_quotes symbols, parameters
44
+ response = get_response(uri(symbols, parameters))
45
+ map_result parse(response.body), parameters
46
+ end
47
+
48
+ def get_response uri
49
+ Net::HTTP.get_response(uri)
50
+ end
51
+
52
+ def map_result result, parameters
53
+ result.map {|q| parameters.zip(q).to_h }
54
+ end
55
+
56
+ def parse text
57
+ CSV.parse text
58
+ end
59
+
60
+ def uri symbols, parameters
61
+ symbols = symbols.join '+'
62
+ parameters = PARAMETER_MAP.values_at(*parameters).join
63
+
64
+ URI.parse "#{QUOTES_URI}?s=#{symbols}&f=#{parameters}"
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,41 @@
1
+ module Stocktastic #:nodoc:
2
+
3
+ # Main class for integrating with Stocktastic.
4
+ #
5
+ # stocktastic = Stocktastic::Base.new
6
+ # quote = stocktastic.quote('AAPL')
7
+ class Base
8
+ ##
9
+ # Returns the adapter currently in use
10
+ attr_reader :adapter
11
+
12
+ ##
13
+ # :call-seq:
14
+ # Stocktastic::Base.new
15
+ # Stocktastic::Base.new(CustomAdapter.new)
16
+ #
17
+ # Construct a new Stocktastic::Base object using the specified +adapter+.
18
+ # If no +adapter+ is passed the +default_adapter+ will be used.
19
+ def initialize adapter = Stocktastic::default_adapter.new
20
+ @adapter = adapter
21
+ end
22
+
23
+ ##
24
+ # Fetch quote information for a single symbol.
25
+ #
26
+ # quote = stocktastic.quote('AAPL')
27
+ # # => {:symbol => 'AAPL', :name => 'Apple Inc.', ...}
28
+ def quote symbol
29
+ adapter.get_single_quote symbol
30
+ end
31
+
32
+ ##
33
+ # Fetch quote information for many symbols.
34
+ #
35
+ # quotes = stocktastic.quotes(['AAPL', 'GOOG'])
36
+ # # => [{:symbol => 'AAPL', :name => 'Apple Inc.', ...}, {:symbol => 'GOOG', ...}]
37
+ def quotes symbols
38
+ adapter.get_multiple_quotes symbols
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,22 @@
1
+ require 'minitest/autorun'
2
+ require 'stocktastic'
3
+
4
+ describe "Fetch quotes" do
5
+ describe "using default (Yahoo) adapter" do
6
+ before do
7
+ @stocktastic = Stocktastic::Base.new
8
+ end
9
+
10
+ it "fetches a single quote" do
11
+ quote = @stocktastic.quote('AAPL')
12
+ quote[:symbol].must_equal 'AAPL'
13
+ end
14
+
15
+ it "fetches multiple quotes" do
16
+ symbols = ['AAPL', 'MSFT', 'GOOG']
17
+ quotes = @stocktastic.quotes(symbols)
18
+ quotes.size.must_equal 3
19
+ quotes.map {|q| q[:symbol] }.must_equal symbols
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ require 'minitest/autorun'
2
+ require 'stocktastic'
3
+
4
+ describe Stocktastic::Adapter::Base, "abstract functions" do
5
+ end
@@ -0,0 +1,80 @@
1
+ require 'minitest/autorun'
2
+ require 'stocktastic'
3
+
4
+ describe Stocktastic::Adapter::Yahoo do
5
+ before do
6
+ @adapter = Stocktastic::Adapter::Yahoo.new
7
+ end
8
+
9
+ it "inherits from base adapter" do
10
+ @adapter.must_be_kind_of Stocktastic::Adapter::Base
11
+ end
12
+
13
+ describe "Parameter Map" do
14
+ it "starts with :symbol" do
15
+ map = Stocktastic::Adapter::Yahoo::PARAMETER_MAP
16
+ map.keys[0].must_equal :symbol
17
+ map[:symbol].must_equal 's'
18
+ end
19
+ end
20
+
21
+ describe "get_single_quote" do
22
+ describe "without options" do
23
+ it "returns quote for a single symbol" do
24
+ quote = @adapter.get_single_quote 'AAPL'
25
+ quote[:symbol].must_equal 'AAPL'
26
+ end
27
+
28
+ it "gets everything by default" do
29
+ quote = @adapter.get_single_quote 'AAPL'
30
+ map = Stocktastic::Adapter::Yahoo::PARAMETER_MAP
31
+ map.keys.each do |k|
32
+ quote[k].wont_be_empty
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "with custom parameters" do
38
+ it "accepts custom parameters through options" do
39
+ params = [:symbol, :last_trade_price_only, :name]
40
+ quote = @adapter.get_single_quote 'AAPL', parameters: params
41
+ quote[:symbol].must_equal 'AAPL'
42
+ params.each do |p|
43
+ quote[p].wont_be_empty
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "get_multiple_quotes" do
50
+ describe "without options" do
51
+ it "returns quotes for an array of symbols" do
52
+ quotes = @adapter.get_multiple_quotes ['AAPL', 'GOOG', 'MSFT']
53
+ quotes.size.must_equal 3
54
+ quotes.map {|q| q[:symbol]}.must_equal ['AAPL', 'GOOG', 'MSFT']
55
+ end
56
+
57
+ it "gets everything by default" do
58
+ quotes = @adapter.get_multiple_quotes ['AAPL', 'GOOG']
59
+ map = Stocktastic::Adapter::Yahoo::PARAMETER_MAP
60
+ 2.times do |n|
61
+ map.keys.each do |k|
62
+ quotes[n][k].wont_be_empty
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "with custom parameters" do
69
+ it "accepts custom parameters through options" do
70
+ params = [:symbol, :last_trade_price_only, :name]
71
+ quotes = @adapter.get_multiple_quotes ['AAPL', 'GOOG'], parameters: params
72
+ 2.times do |n|
73
+ params.each do |p|
74
+ quotes[n][p].wont_be_empty
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,38 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/mock'
3
+ require 'stocktastic'
4
+
5
+ describe Stocktastic::Base do
6
+ describe "#adapter" do
7
+ it "can be set when created" do
8
+ adapter = Object.new
9
+ Stocktastic::Base.new(adapter).adapter.must_equal adapter
10
+ end
11
+
12
+ it "uses the default adapter" do
13
+ Stocktastic::Base.new.adapter.must_be_instance_of Stocktastic::default_adapter
14
+ end
15
+ end
16
+
17
+ describe "#quote" do
18
+ it "delegates to adapter#get_single_quote" do
19
+ adapter = MiniTest::Mock.new
20
+ adapter.expect(:get_single_quote, nil, ['SYM'])
21
+ stocktastic = Stocktastic::Base.new(adapter)
22
+
23
+ stocktastic.quote('SYM')
24
+ adapter.verify
25
+ end
26
+ end
27
+
28
+ describe "#quotes" do
29
+ it "delegates to adapter#get_multiple_quotes" do
30
+ adapter = MiniTest::Mock.new
31
+ adapter.expect(:get_multiple_quotes, nil, [['SYM1', 'SYM2']])
32
+ stocktastic = Stocktastic::Base.new(adapter)
33
+
34
+ stocktastic.quotes(['SYM1', 'SYM2'])
35
+ adapter.verify
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ require "minitest/autorun"
2
+ require "stocktastic"
3
+
4
+ describe Stocktastic do
5
+ it "has a version" do
6
+ Stocktastic::VERSION.must_be_instance_of String
7
+ end
8
+
9
+ describe "default_adapter" do
10
+ it "returns Yahoo adapter" do
11
+ Stocktastic::default_adapter.must_equal Stocktastic::Adapter::Yahoo
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stocktastic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Lundqvist
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.4'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rcov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hoe
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ description: |-
70
+ Stocktastic fetches stock quotes
71
+
72
+ It uses Yahoo Finance as a source by default.
73
+ email:
74
+ - lundqvist.j@gmail.com
75
+ executables:
76
+ - stocktastic
77
+ extensions: []
78
+ extra_rdoc_files:
79
+ - History.rdoc
80
+ - Manifest.txt
81
+ - README.rdoc
82
+ files:
83
+ - ".autotest"
84
+ - ".gemtest"
85
+ - History.rdoc
86
+ - Manifest.txt
87
+ - README.rdoc
88
+ - Rakefile
89
+ - bin/stocktastic
90
+ - lib/stocktastic.rb
91
+ - lib/stocktastic/adapter.rb
92
+ - lib/stocktastic/adapter/base.rb
93
+ - lib/stocktastic/adapter/yahoo.rb
94
+ - lib/stocktastic/base.rb
95
+ - test/integration/test_quotes.rb
96
+ - test/stocktastic/adapter/test_base.rb
97
+ - test/stocktastic/adapter/test_yahoo.rb
98
+ - test/stocktastic/test_base.rb
99
+ - test/test_stocktastic.rb
100
+ homepage: https://github.com/pubis/stocktastic
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options:
106
+ - "--main"
107
+ - README.rdoc
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Stocktastic fetches stock quotes It uses Yahoo Finance as a source by default.
126
+ test_files:
127
+ - test/stocktastic/test_base.rb
128
+ - test/stocktastic/adapter/test_base.rb
129
+ - test/stocktastic/adapter/test_yahoo.rb
130
+ - test/test_stocktastic.rb
131
+ - test/integration/test_quotes.rb