ystock 0.3.8 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,93 +1,96 @@
1
1
  # Ystock
2
-
3
- Get stock information from Yahoo Finance and Google (in beta)
2
+ This gem provides you with the latest market data avalable, pulling from both Google and Yahoo Finance.
4
3
 
5
4
  ## Install
6
5
  Add the following to your Gemfile
7
6
  ```ruby
8
- gem "ystock", "~> 0.3.4"
7
+ gem "ystock", "~> 0.4.0"
9
8
  ```
10
9
 
11
10
  ### Dependancies
12
11
  Please note 'httparty' is required to run this gem.
13
12
 
14
-
15
- Then run bundle install
13
+ ## Bundle
14
+ ```
15
+ bundle install
16
+ ```
16
17
 
17
18
  ----
18
19
 
19
- # Basic Usage
20
+ # Google Usage
20
21
 
21
- ## Single stock
22
- After you have installed the gem and included it into your Gemfile you can now start to use ystock.
22
+ This uses the Google Finance API to request the latest market information (up to 15 min delay). The Google Fincance API tends to return better and more usful information on the stock requested.
23
23
 
24
- 1. In a controller you can do the following:
24
+ ### Single Stock
25
25
  ```ruby
26
- class SomeController < ApplicationController
27
- def index
28
- @stock = Ystock.get_stock('appl')
29
- end
30
- end
26
+ Ystock::Google.get_quote("appl")
31
27
  ```
32
28
 
33
- 2. In your view you may display the results:
34
-
35
- ```html+ruby
36
- <p>The price of Apple stock is: $<%=@stock[:AAPL][:price]%></p>
29
+ ### Multiple Stocks
30
+ ```ruby
31
+ Ystock::Google.find(["aapl", "f", "ggog"])
37
32
  ```
38
33
 
39
34
  #### Available data
40
35
  ```
41
- <%=@stock[:AAPL][:price]%>
42
- <%=@stock[:AAPL][:change]%>
43
- <%=@stock[:AAPL][:volume]%>
44
- ```
45
-
46
- ## Usage many stocks
47
- After you have installed the gem and included it into your Gemfile you can now start to use ystock.
48
-
49
- 1. In a controller you can do the following:
50
- ```ruby
51
- class SomeController < ApplicationController
52
- def index
53
- @stocks = Ystock.find(["aapl", "goog"])
54
- end
55
- end
56
- ```
57
-
58
- 2. In your view you may display the results:
59
-
60
- ```ruby
61
- @stocks.each do |sym, values|
62
- # sym -> AAPL
63
- # values[:price], values[:change], values[:volume]
64
- puts values[:price]
65
- end
36
+ symbol
37
+ pretty_symbol
38
+ symbol_lookup_url
39
+ company
40
+ exchange
41
+ exchange_timezone
42
+ exchange_utc_offset
43
+ exchange_closing
44
+ divisor
45
+ currency
46
+ last
47
+ high
48
+ low
49
+ volume
50
+ avg_volume
51
+ market_cap
52
+ open
53
+ y_close
54
+ change
55
+ perc_change
56
+ delay
57
+ trade_timestamp
58
+ trade_date_utc
59
+ trade_time_utc
60
+ current_date_utc
61
+ current_time_utc
62
+ symbol_url
63
+ chart_url
64
+ disclaimer_url
65
+ ecn_url
66
+ isld_last
67
+ isld_trade_date_utc
68
+ isld_trade_time_utc
69
+ brut_last
70
+ brut_trade_date_utc
71
+ brut_trade_time_utc
72
+ daylight_savings
66
73
  ```
67
74
 
68
75
  ----
69
76
 
70
- # Google Stock (Beta)
71
- I have added Google finance to this gem due to the number of stocks not supported by Yahoo.
72
-
73
- ## Usage for multiple stocks
77
+ # Yahoo Usage
74
78
 
75
- ```
76
- @stocks = Ystock::Google.find(["aapl","goog"])
77
-
78
- <%=@stocks[:AAPL][:price]%>
79
- <%=@stocks[:AAPL][:change]%>
80
- <%=@stocks[:AAPL][:volume]%>
81
- <%=@stocks[:AAPL][:market]%>
79
+ ### Single Stock
80
+ ```ruby
81
+ Ystock::Yahoo.get_quote("appl")
82
82
  ```
83
83
 
84
+ ### Multiple Stocks
84
85
  ```ruby
85
- @stocks.each do |sym, value|
86
- puts value[:price]
87
- end
86
+ Ystock::Yahoo.find(["aapl", "f", "ggog"])
88
87
  ```
89
88
 
90
- #### Sample response
89
+ #### Available data
91
90
  ```
92
- {:AAPL=>{:symbol=>"AAPL", :market=>"Nasdaq", :price=>"598.90", :change=>"-5.53", :volume=>"15285309"}, :GOOG=>{:symbol=>"GOOG", :market=>"Nasdaq", :price=>"570.48", :change=>"-0.71", :volume=>"2310094"}}
91
+ price
92
+ change
93
+ volume
94
+ symbol
93
95
  ```
96
+
data/lib/ystock/google.rb CHANGED
@@ -1,67 +1,44 @@
1
- # => Ystock::Google
2
1
  module Ystock
3
2
  class Google
4
- #@@google_service = "http://finance.google.com/finance/info"
5
3
  @@google_service = "http://www.google.com/ig/api"
6
- # => Ystock::Google.find(["aapl", "goog"])
7
4
  def self.find(args)
8
- first_stock = args.first
9
5
  stock_string = ""
10
6
  args.each do |stock|
11
- if first_stock == stock
7
+ if args.first == stock
12
8
  stock_string += "?stock=" + stock
13
9
  else
14
10
  stock_string += "&stock=" + stock
15
11
  end
16
12
  end
17
- # => Send Request to get quote then format
18
13
  format(send_request(stock_string), true)
19
14
  end
20
15
 
21
16
  def self.get_quote(arg)
22
- arg = "?stock=" + arg
23
- # => One stock
17
+ arg = "?stock=" + arg
24
18
  format(send_request(arg), false)
25
19
  end
26
20
 
27
21
  def self.format(results, is_many)
28
- output = Hash.new
22
+ output = Array.new
29
23
 
30
24
  results.each do |item|
25
+
31
26
  if is_many
27
+
32
28
  item[1]["finance"].each do |stock|
33
- output[stock["symbol"]["data"].to_sym] = {
34
- :symbol => stock["symbol"]["data"],
35
- :market => stock["exchange"]["data"],
36
- :price => stock["last"]["data"],
37
- :change => stock["change"]["data"],
38
- :volume => stock["volume"]["data"],
39
- :company => stock["company"]["data"],
40
- :high => stock["high"]["data"],
41
- :low => stock["low"]["data"],
42
- :avg_volume => stock["avg_volume"]["data"],
43
- :market_cap => stock["market_cap"]["data"],
44
- :perc_change => stock["perc_change"]["data"],
45
- :y_close => stock["y_close"]["data"],
46
- :open => stock["open"]["data"]
47
- }
29
+ stock_data = Hash.new
30
+ stock.each do |key, val|
31
+ stock_data[:"#{key}"] = (!val["data"].nil?) ? val["data"] : val
32
+ end
33
+ output << [stock_data]
48
34
  end
35
+
49
36
  else
50
- output = {
51
- :symbol => item[1]["finance"]["symbol"]["data"],
52
- :market => item[1]["finance"]["exchange"]["data"],
53
- :price => item[1]["finance"]["last"]["data"],
54
- :change => item[1]["finance"]["change"]["data"],
55
- :volume => item[1]["finance"]["volume"]["data"],
56
- :company => item[1]["finance"]["company"]["data"],
57
- :high => item[1]["finance"]["high"]["data"],
58
- :low => item[1]["finance"]["low"]["data"],
59
- :avg_volume => item[1]["finance"]["avg_volume"]["data"],
60
- :market_cap => item[1]["finance"]["market_cap"]["data"],
61
- :perc_change => item[1]["finance"]["perc_change"]["data"],
62
- :y_close => item[1]["finance"]["y_close"]["data"],
63
- :open => item[1]["finance"]["open"]["data"]
64
- }
37
+ stock_data = Hash.new
38
+ item[1]["finance"].each do |key, val|
39
+ stock_data[:"#{key}"] = (!val["data"].nil?) ? val["data"] : val
40
+ end
41
+ output = [stock_data]
65
42
  end
66
43
  end
67
44
 
@@ -0,0 +1,65 @@
1
+ module Ystock
2
+ class Yahoo
3
+ @@service_uri = "http://download.finance.yahoo.com/d/quotes.csv"
4
+
5
+ def self.find(args)
6
+ stock_string = ""
7
+ last_stock = args.last.to_s
8
+ args.each do |stock|
9
+ if last_stock == stock.to_sym
10
+ stock_string += stock.to_s
11
+ else
12
+ stock_string += stock.to_s + ","
13
+ end
14
+ end
15
+ format(send_request(stock_string))
16
+ end
17
+
18
+ def self.get_quote(stock)
19
+ stock_data = Hash.new
20
+ s = send_request(stock)
21
+ a = s.chomp.split(",")
22
+ return [{:symbol => stock,
23
+ :price => a[0],
24
+ :change => a[1],
25
+ :volume => a[2]
26
+ }]
27
+ end
28
+
29
+ def self.format(results)
30
+ output = Array.new
31
+ results.each_line("\n") do |row|
32
+ stockdata = row.split(",")
33
+ if !stockdata.nil?
34
+ if !stockdata[3].nil?
35
+ stockdata[3] = stockdata[3].gsub("\r\n", "").gsub('"', '')
36
+
37
+ output << [{:symbol => stockdata[3],
38
+ :price => stockdata[0],
39
+ :change => stockdata[1],
40
+ :volume => stockdata[2]}]
41
+
42
+ end
43
+ end
44
+ end
45
+ return output
46
+ end
47
+
48
+ def self.send_request(args)
49
+ completed_path = @@service_uri + "?f=l1c1v1s&s=" + args
50
+ uri = URI.parse(completed_path)
51
+ response = Net::HTTP.start(uri.host, uri.port) do |http|
52
+ http.get completed_path
53
+ end
54
+ return response.body
55
+ end
56
+
57
+ def self.version
58
+ return Gem::VERSION
59
+ end
60
+
61
+ def self.test
62
+ return "Test is working 2"
63
+ end
64
+ end
65
+ end
data/lib/ystock.rb CHANGED
@@ -10,75 +10,6 @@ require 'cgi'
10
10
  require 'json'
11
11
  require 'net/http'
12
12
  require 'httparty'
13
- module Ystock
14
- def ensure_unique(name)
15
- begin
16
- self[name] = yield
17
- end while self.class.exists?(name => self[name])
18
- end
19
-
20
- @@service_uri = "http://download.finance.yahoo.com/d/quotes.csv"
21
-
22
- # Ystock.find(['stock', 'stock'])
23
- def self.find(args)
24
- stock_string = ""
25
- last_stock = args.last.to_s
26
- args.each do |stock|
27
- if last_stock == stock.to_s
28
- stock_string += stock.to_s
29
- else
30
- stock_string += stock.to_s + ","
31
- end
32
- end
33
- format(send_request(stock_string))
34
- end
35
-
36
- def self.get_stock(stock)
37
- output = Hash.new
38
- s = send_request(stock)
39
- a = s.chomp.split(",")
40
- output[stock.to_sym] = {:symbol => stock,
41
- :price => a[0],
42
- :change => a[1],
43
- :volume => a[2]
44
- }
45
- return output
46
- end
47
-
48
- def self.format(results)
49
- output = Hash.new
50
- results.each_line("\n") do |row|
51
- stockdata = row.split(",")
52
- if !stockdata.nil?
53
- if !stockdata[3].nil?
54
- stockdata[3] = stockdata[3].gsub("\r\n", "").gsub('"', '')
55
- output[stockdata[3].to_sym] = {:symbol => stockdata[3],
56
- :price => stockdata[0],
57
- :change => stockdata[1],
58
- :volume => stockdata[2]}
59
- end
60
- end
61
- end
62
- return output
63
- end
64
-
65
- def self.send_request(args)
66
- completed_path = @@service_uri + "?f=l1c1v1s&s=" + args
67
- uri = URI.parse(completed_path)
68
- response = Net::HTTP.start(uri.host, uri.port) do |http|
69
- http.get completed_path
70
- end
71
- return response.body
72
- end
73
-
74
- def self.version
75
- return Gem::VERSION
76
- end
77
-
78
- def self.test
79
- return "Test is working 2"
80
- end
81
-
82
- end
83
13
 
14
+ require 'ystock/yahoo'
84
15
  require 'ystock/google'
data/ystock.gemspec CHANGED
@@ -1,29 +1,18 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
1
  Gem::Specification.new do |s|
4
2
  s.name = %q{ystock}
5
- s.version = "0.3.8"
3
+ s.version = "0.4.0"
6
4
 
7
5
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
6
  s.authors = ["Greg Winn"]
9
7
  s.date = %q{2012-06-09}
10
- s.description = %q{Get stock information from Yahoo Finance and Google}
8
+ s.description = %q{Grab stock information from Yahoo Finance and Google}
11
9
  s.email = %q{greg@winn.ws}
12
10
  s.extra_rdoc_files = ["README.markdown", "lib/ystock.rb"]
13
- s.files = ["README.markdown", "lib/ystock.rb", "lib/ystock/google.rb", "ystock.gemspec"]
11
+ s.files = ["README.markdown", "lib/ystock.rb","lib/ystock/yahoo.rb", "lib/ystock/google.rb", "ystock.gemspec"]
14
12
  s.homepage = %q{http://github.com/gregwinn/ystock}
15
13
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ystock", "--main", "README.rdoc"]
16
14
  s.require_paths = ["lib"]
17
15
  s.rubyforge_project = %q{ystock}
18
16
  s.rubygems_version = %q{1.5.0}
19
- s.summary = %q{Get stock information from Yahoo Finance and Google}
20
-
21
- if s.respond_to? :specification_version then
22
- s.specification_version = 3
23
-
24
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
- else
26
- end
27
- else
28
- end
17
+ s.summary = %q{Grab stock information from Yahoo Finance and Google}
29
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ystock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-06-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Get stock information from Yahoo Finance and Google
14
+ description: Grab stock information from Yahoo Finance and Google
15
15
  email: greg@winn.ws
16
16
  executables: []
17
17
  extensions: []
@@ -21,6 +21,7 @@ extra_rdoc_files:
21
21
  files:
22
22
  - README.markdown
23
23
  - lib/ystock.rb
24
+ - lib/ystock/yahoo.rb
24
25
  - lib/ystock/google.rb
25
26
  - ystock.gemspec
26
27
  homepage: http://github.com/gregwinn/ystock
@@ -52,5 +53,5 @@ rubyforge_project: ystock
52
53
  rubygems_version: 1.8.25
53
54
  signing_key:
54
55
  specification_version: 3
55
- summary: Get stock information from Yahoo Finance and Google
56
+ summary: Grab stock information from Yahoo Finance and Google
56
57
  test_files: []