stockery 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :test do
4
+ gem "rake"
5
+ end
6
+
7
+ group :development do
8
+ gem "shoulda", ">= 0"
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.6.4"
11
+ gem "rcov", ">= 0"
12
+ end
13
+
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Pieter Michels
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ stockery
2
+ ======
3
+ Get stock quotes.
4
+
5
+
6
+ CLI Usage
7
+ ========
8
+
9
+ Usage: stockery [options]
10
+
11
+ Command options:
12
+ -s, --source HOST Stock source (default: GOOGLE)
13
+ -q, --quotes 'comma separated' Required, no default
14
+
15
+ -h, --help
16
+
17
+ Example
18
+ -------
19
+ `stockery -q "GOOG, APL, MSFT"`
20
+
21
+ or
22
+
23
+ `stockery -s "GOOGLE" -q "GOOG, APL"`
24
+
25
+
26
+ Contributing to Stockery
27
+ ======================
28
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
29
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
30
+ * Fork the project
31
+ * Start a feature/bugfix branch
32
+ * Commit and push until you are happy with your contribution
33
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
34
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
35
+
36
+
37
+ Copyright
38
+ =========
39
+ Copyright (c) 2011 Pieter Michels. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require 'rake'
6
+ require 'jeweler'
7
+ require 'rdoc/task'
8
+ require 'rcov/rcovtask'
9
+ require 'rake/testtask'
10
+
11
+ begin
12
+ Bundler.setup(:default, :development)
13
+ rescue Bundler::BundlerError => e
14
+ $stderr.puts e.message
15
+ $stderr.puts "Run `bundle install` to install missing gems"
16
+ exit e.status_code
17
+ end
18
+
19
+ Jeweler::Tasks.new do |gem|
20
+ gem.name = "stockery"
21
+ gem.homepage = "http://github.com/pierot/stockery"
22
+ gem.license = "MIT"
23
+ gem.summary = %Q{Fetch stock quotes}
24
+ gem.description = %Q{Fetch stock quotes. Build as a gem and command line tool.}
25
+ gem.email = "pieter@noort.be"
26
+ gem.authors = ["Pieter Michels"]
27
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ test.rcov_opts << '--exclude "gems/*"'
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "Stockery #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/bin/stockery ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # Stockery command line interface script.
3
+ # Run <tt>stockery -h</tt> to get more usage.
4
+
5
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
6
+
7
+ require 'stockery'
8
+
9
+ Stockery::Runner.new(ARGV).run!
@@ -0,0 +1,61 @@
1
+ module Stockery
2
+ class Quote
3
+ attr_accessor :source
4
+
5
+ def initialize
6
+ self.source = Stockery::GOOGLE
7
+ end
8
+
9
+ def get_status(symbol)
10
+ result = case source
11
+ when Stockery::GOOGLE then
12
+ fetch_goog(symbol)
13
+ else
14
+ abort "No valid source specified. The following source are available: \"GOOGLE\""
15
+ end
16
+
17
+ result
18
+ end
19
+
20
+ def print(stockery_data, do_print = false)
21
+ res = stockery_data
22
+ printed = ""
23
+
24
+ unless res.empty?
25
+ printed += "#{res[:name]} on #{res[:market]} @ #{res[:timestamp]}\n"
26
+ printed += " *** MARKET CLOSED ***\n" if Time.now.strftime("%H:%M") > "16:00"
27
+
28
+ printed_pr = "#{res[:price]} #{res[:change_points]} (#{res[:change_procent]}%)"
29
+ printed += res[:change_points].to_f < 0 ? printed_pr.red : printed_pr.green
30
+ else
31
+ printed += "Stock data invalid or empty"
32
+ end
33
+
34
+ puts printed if do_print
35
+ printed
36
+ end
37
+
38
+ private
39
+
40
+ def fetch_goog(symbol)
41
+ json = open("http://www.google.com/finance/info?client=ig&q=#{symbol}").read
42
+ json = json.sub(/^\n\/\//, '')
43
+
44
+ data = JSON.parse(json)[0]
45
+ data_stock = {}
46
+
47
+ unless data.nil?
48
+ data_stock[:market] = data['e']
49
+ data_stock[:name] = data['t']
50
+ data_stock[:price] = data['l']
51
+ data_stock[:price_currency] = data['l_curr']
52
+ data_stock[:change_points] = data['c']
53
+ data_stock[:change_procent] = data['cp']
54
+
55
+ data_stock[:timestamp] = data['ltt']
56
+ end
57
+
58
+ data_stock
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,60 @@
1
+ require 'optparse'
2
+
3
+ module Stockery
4
+ class Runner
5
+ def initialize(argv)
6
+ @argv = argv
7
+
8
+ # Default options values
9
+ @options = {
10
+ :source => Stockery::GOOGLE,
11
+ :quotes => '',
12
+ }
13
+
14
+ parse!
15
+ end
16
+
17
+ def run!
18
+ if @options[:quotes].length == 0
19
+ abort "Quote symbols missing. Usage `stockery -q \"GOOG, MSFT\""
20
+ end
21
+
22
+ puts "Quotes at #{Time.now}\n"
23
+ puts ""
24
+
25
+ s_q = Stockery::Quote.new
26
+ s_q.source = @options[:source]
27
+
28
+ quotes = @options[:quotes].split(',').collect { |q| q.to_s.strip }
29
+
30
+ quotes.each do |quote|
31
+ res = s_q.get_status(quote)
32
+ puts s_q.print(res)
33
+
34
+ puts "\n"
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def parse!
41
+ parser.parse! @argv
42
+ end
43
+
44
+ def parser
45
+ @parser ||= OptionParser.new do |opts|
46
+ opts.banner = "Usage: stockery [options]"
47
+
48
+ opts.separator ""
49
+ opts.separator "Command options:"
50
+
51
+ opts.on("-s", "--source HOST", "Stock source (default: #{@options[:source]})") { |source| @options[:source] = source }
52
+ opts.on("-q", "--quotes 'comma separated'", "Required, no default") { |quotes| @options[:quotes] = quotes }
53
+
54
+ opts.separator ""
55
+
56
+ opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,33 @@
1
+ module Stockery
2
+ # Raised when a feature is not supported on the
3
+ # current platform.
4
+ class PlatformNotSupported < RuntimeError; end
5
+
6
+ module VERSION #:nodoc:
7
+ MAJOR = 0
8
+ MINOR = 0
9
+ TINY = 1
10
+
11
+ STRING = [MAJOR, MINOR, TINY].join('.')
12
+
13
+ CODENAME = "Get-Them-Quotes".freeze
14
+
15
+ RACK = [1, 0].freeze # Rack protocol version
16
+ end
17
+
18
+ NAME = 'stockery'.freeze
19
+ SERVER = "#{NAME} #{VERSION::STRING} codename #{VERSION::CODENAME}".freeze
20
+
21
+ def self.win?
22
+ RUBY_PLATFORM =~ /mswin|mingw/
23
+ end
24
+
25
+ def self.linux?
26
+ RUBY_PLATFORM =~ /linux/
27
+ end
28
+
29
+ def self.ruby_18?
30
+ RUBY_VERSION =~ /^1\.8/
31
+ end
32
+ end
33
+
data/lib/stockery.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'open-uri'
2
+ require 'json'
3
+
4
+ module Stockery
5
+ GOOGLE = "GOOGLE"
6
+ YAHOO = "YAHOO"
7
+
8
+ ROOT = File.expand_path(File.dirname(__FILE__))
9
+
10
+ autoload :Runner, "#{ROOT}/stockery/runner"
11
+ autoload :Quote, "#{ROOT}/stockery/quote"
12
+ end
13
+
14
+ class String
15
+ def red; colorize(self, "\e[31m"); end
16
+ def green; colorize(self, "\e[1m\e[32m"); end
17
+ def red_bg; colorize(self, "\e[41m"); end
18
+ def green_bg; colorize(self, "\e[31m\e[42m"); end
19
+
20
+ def colorize(text, color_code)
21
+ "#{color_code}#{text}\e[0m"
22
+ end
23
+ end
24
+
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'stockery'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestStocky < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stockery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pieter Michels
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-22 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: &2157840980 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2157840980
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &2157840260 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2157840260
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &2157839600 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.6.4
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2157839600
47
+ - !ruby/object:Gem::Dependency
48
+ name: rcov
49
+ requirement: &2157838720 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2157838720
58
+ - !ruby/object:Gem::Dependency
59
+ name: thoughtbot-shoulda
60
+ requirement: &2157837720 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2157837720
69
+ description: Fetch stock quotes. Build as a gem and command line tool.
70
+ email: pieter@noort.be
71
+ executables:
72
+ - stockery
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE.txt
76
+ - README.md
77
+ files:
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - VERSION
83
+ - bin/stockery
84
+ - lib/stockery.rb
85
+ - lib/stockery/quote.rb
86
+ - lib/stockery/runner.rb
87
+ - lib/stockery/version.rb
88
+ - test/helper.rb
89
+ - test/test_stockery.rb
90
+ homepage: http://github.com/pierot/stockery
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: -3539302173484574299
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.10
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Fetch stock quotes
118
+ test_files: []