ystock 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
data/lib/ystock.rb CHANGED
@@ -7,6 +7,7 @@
7
7
  # http://github.com/gregwinn/ystock
8
8
  #
9
9
  require 'cgi'
10
+ require 'json'
10
11
  require 'net/http'
11
12
  module Ystock
12
13
  def ensure_unique(name)
@@ -15,48 +16,101 @@ module Ystock
15
16
  end while self.class.exists?(name => self[name])
16
17
  end
17
18
 
18
- @@service_uri = "http://download.finance.yahoo.com/d/quotes.csv"
19
+ @@service_uri = "http://download.finance.yahoo.com/d/quotes.csv"
20
+
21
+ # => Ystock::Google
22
+ class Google
23
+ @@google_service = "http://finance.google.com/finance/info"
24
+ # => Ystock::Google.find(["aapl", "goog"])
25
+ def self.find(args)
26
+ last_stock = args.last
27
+ stock_string = ""
28
+ args.each do |stock|
29
+ if last_stock == stock
30
+ stock_string += stock
31
+ else
32
+ stock_string += stock + ","
33
+ end
34
+ end
35
+ # => Send Request to get quote then format
36
+ format(send_request(stock_string))
37
+ end
38
+
39
+ def self.get_quote(arg)
40
+ # => One stock
41
+ format(send_request(arg))
42
+ end
43
+
44
+ def self.format(results)
45
+ output = Hash.new
46
+ results.each do |result|
47
+ output[result["t"].to_sym] = {
48
+ :symbol => result["t"],
49
+ :market => result["e"],
50
+ :price => result["l"],
51
+ :change => result["c"]
52
+ }
53
+ end
54
+ return output
55
+ end
56
+
57
+ def self.send_request(args)
58
+ url = @@google_service + "?client=ig&q="
59
+ url += args
60
+ resp = Net::HTTP.get_response(URI.parse(url))
61
+ data = resp.body.gsub("//", "")
62
+ # => Result = Hash
63
+ result = JSON.parse(data)
64
+ end
65
+ end
19
66
 
20
- # Ystock.find(['stock', 'stock'])
67
+ # Ystock.find(['stock', 'stock'])
21
68
  def self.find(args)
22
- output = Hash.new
69
+ stock_string = ""
70
+ last_stock = args.last.to_s
23
71
  args.each do |stock|
24
- s = send_request(stock.to_s)
25
- a = s.chomp.split(",")
26
-
27
- output[stock.to_sym] = {:symbol => stock.to_s,
28
- :price => a[0],
29
- :change => a[1],
30
- :volume => a[2]}
31
-
72
+ if last_stock == stock.to_s
73
+ stock_string += stock.to_s
74
+ else
75
+ stock_string += stock.to_s + ","
76
+ end
32
77
  end
33
- return output
78
+ format(send_request(stock_string))
34
79
  end
35
-
80
+
36
81
  def self.get_stock(stock)
37
- output = Array.new
82
+ output = Hash.new
38
83
  s = send_request(stock)
39
84
  a = s.chomp.split(",")
40
- output = {:symbol => stock,
85
+ output[stock.to_sym] = {:symbol => stock,
41
86
  :price => a[0],
42
87
  :change => a[1],
43
88
  :volume => a[2]
44
89
  }
45
90
  return output
46
91
  end
92
+
93
+ def self.format(results)
94
+ output = Hash.new
95
+ results.each_line("\n") do |row|
96
+ stockdata = row.split(",")
97
+ stockdata[3] = stockdata[3].gsub("\r\n", "").gsub('"', '')
98
+ output[stockdata[3].to_sym] = {:symbol => stockdata[3],
99
+ :price => stockdata[0],
100
+ :change => stockdata[1],
101
+ :volume => stockdata[2]}
102
+ end
103
+ return output
104
+ end
47
105
 
48
- def self.send_request(*args)
49
- completed_path = @@service_uri + construct_args(*args)
106
+ def self.send_request(args)
107
+ completed_path = @@service_uri + "?f=l1c1v1s&s=" + args
50
108
  uri = URI.parse(completed_path)
51
109
  response = Net::HTTP.start(uri.host, uri.port) do |http|
52
110
  http.get completed_path
53
111
  end
54
112
  return response.body
55
113
  end
56
-
57
- def self.construct_args(*args)
58
- path = "?f=l1c1v&s=" + args.map{|x| CGI.escape(x)}.join(",")
59
- end
60
114
 
61
115
  end
62
116
  class ActiveRecord::Base
data/ystock.gemspec CHANGED
@@ -2,21 +2,21 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{ystock}
5
- s.version = "0.2.5"
5
+ s.version = "0.3.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Greg Winn"]
9
- s.date = %q{2012-05-18}
10
- s.description = %q{Get stock information from Yahoo Finance}
9
+ s.date = %q{2012-06-09}
10
+ s.description = %q{Get stock information from Yahoo Finance and Google}
11
11
  s.email = %q{greg@winn.ws}
12
- s.extra_rdoc_files = ["README.rdoc", "lib/ystock.rb"]
13
- s.files = ["Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/ystock.rb", "ystock.gemspec"]
12
+ s.extra_rdoc_files = ["README.markdown", "lib/ystock.rb"]
13
+ s.files = ["Manifest", "README.markdown", "Rakefile", "init.rb", "lib/ystock.rb", "ystock.gemspec"]
14
14
  s.homepage = %q{http://github.com/gregwinn/ystock}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ystock", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{ystock}
18
18
  s.rubygems_version = %q{1.5.0}
19
- s.summary = %q{Get stock information from Yahoo Finance}
19
+ s.summary = %q{Get stock information from Yahoo Finance and Google}
20
20
 
21
21
  if s.respond_to? :specification_version then
22
22
  s.specification_version = 3
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.2.5
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,18 +9,18 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-18 00:00:00.000000000 Z
12
+ date: 2012-06-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Get stock information from Yahoo Finance
14
+ description: Get stock information from Yahoo Finance and Google
15
15
  email: greg@winn.ws
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files:
19
- - README.rdoc
19
+ - README.markdown
20
20
  - lib/ystock.rb
21
21
  files:
22
22
  - Manifest
23
- - README.rdoc
23
+ - README.markdown
24
24
  - Rakefile
25
25
  - init.rb
26
26
  - lib/ystock.rb
@@ -54,5 +54,5 @@ rubyforge_project: ystock
54
54
  rubygems_version: 1.8.24
55
55
  signing_key:
56
56
  specification_version: 3
57
- summary: Get stock information from Yahoo Finance
57
+ summary: Get stock information from Yahoo Finance and Google
58
58
  test_files: []