jspradlin-gmoney 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/Manifest +42 -0
  2. data/README.rdoc +29 -0
  3. data/Rakefile +25 -0
  4. data/gmoney.gemspec +30 -0
  5. data/lib/extensions/string.rb +10 -0
  6. data/lib/gmoney.rb +39 -0
  7. data/lib/gmoney/authentication_request.rb +52 -0
  8. data/lib/gmoney/feed_parser.rb +37 -0
  9. data/lib/gmoney/gf_request.rb +15 -0
  10. data/lib/gmoney/gf_response.rb +5 -0
  11. data/lib/gmoney/gf_service.rb +46 -0
  12. data/lib/gmoney/gf_session.rb +18 -0
  13. data/lib/gmoney/portfolio.rb +34 -0
  14. data/lib/gmoney/portfolio_feed_parser.rb +7 -0
  15. data/lib/gmoney/position.rb +33 -0
  16. data/lib/gmoney/position_feed_parser.rb +7 -0
  17. data/lib/gmoney/transaction.rb +21 -0
  18. data/lib/gmoney/transaction_feed_parser.rb +9 -0
  19. data/spec/authentication_request_spec.rb +101 -0
  20. data/spec/fixtures/cacert.pem +67 -0
  21. data/spec/fixtures/default_portfolios_feed.xml +45 -0
  22. data/spec/fixtures/empty_portfolio_feed.xml +24 -0
  23. data/spec/fixtures/portfolio_9_feed.xml +25 -0
  24. data/spec/fixtures/portfolio_feed_with_returns.xml +69 -0
  25. data/spec/fixtures/positions_feed_for_portfolio_14.xml +13 -0
  26. data/spec/fixtures/positions_feed_for_portfolio_9.xml +63 -0
  27. data/spec/fixtures/positions_feed_for_portfolio_9r.xml +130 -0
  28. data/spec/fixtures/transactions_feed_for_GOOG.xml +78 -0
  29. data/spec/gmoney_spec.rb +9 -0
  30. data/spec/portfolio_feed_parser_spec.rb +41 -0
  31. data/spec/portfolio_spec.rb +96 -0
  32. data/spec/position_feed_parser_spec.rb +39 -0
  33. data/spec/position_spec.rb +80 -0
  34. data/spec/request_spec.rb +29 -0
  35. data/spec/response_spec.rb +18 -0
  36. data/spec/service_spec.rb +127 -0
  37. data/spec/session_spec.rb +32 -0
  38. data/spec/spec.opts +2 -0
  39. data/spec/spec_helper.rb +1 -0
  40. data/spec/string_spec.rb +40 -0
  41. data/spec/transaction_feed_parser_spec.rb +29 -0
  42. data/spec/transaction_spec.rb +46 -0
  43. metadata +114 -0
@@ -0,0 +1,42 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ gmoney.gemspec
5
+ lib/extensions/string.rb
6
+ lib/gmoney.rb
7
+ lib/gmoney/authentication_request.rb
8
+ lib/gmoney/feed_parser.rb
9
+ lib/gmoney/gf_request.rb
10
+ lib/gmoney/gf_response.rb
11
+ lib/gmoney/gf_service.rb
12
+ lib/gmoney/gf_session.rb
13
+ lib/gmoney/portfolio.rb
14
+ lib/gmoney/portfolio_feed_parser.rb
15
+ lib/gmoney/position.rb
16
+ lib/gmoney/position_feed_parser.rb
17
+ lib/gmoney/transaction.rb
18
+ lib/gmoney/transaction_feed_parser.rb
19
+ spec/authentication_request_spec.rb
20
+ spec/fixtures/cacert.pem
21
+ spec/fixtures/default_portfolios_feed.xml
22
+ spec/fixtures/empty_portfolio_feed.xml
23
+ spec/fixtures/portfolio_9_feed.xml
24
+ spec/fixtures/portfolio_feed_with_returns.xml
25
+ spec/fixtures/positions_feed_for_portfolio_14.xml
26
+ spec/fixtures/positions_feed_for_portfolio_9.xml
27
+ spec/fixtures/positions_feed_for_portfolio_9r.xml
28
+ spec/fixtures/transactions_feed_for_GOOG.xml
29
+ spec/gmoney_spec.rb
30
+ spec/portfolio_feed_parser_spec.rb
31
+ spec/portfolio_spec.rb
32
+ spec/position_feed_parser_spec.rb
33
+ spec/position_spec.rb
34
+ spec/request_spec.rb
35
+ spec/response_spec.rb
36
+ spec/service_spec.rb
37
+ spec/session_spec.rb
38
+ spec/spec.opts
39
+ spec/spec_helper.rb
40
+ spec/string_spec.rb
41
+ spec/transaction_feed_parser_spec.rb
42
+ spec/transaction_spec.rb
@@ -0,0 +1,29 @@
1
+ = GMoney
2
+
3
+ A gem for interacting with the Google Finance API
4
+
5
+ == Install
6
+
7
+ gem install jspradlin-gmoney --source http://gems.github.com
8
+
9
+ == Usage
10
+
11
+ GMoney::GFSession.login('google username', 'password')
12
+
13
+ portfolios = GMoney::Portfolio.all #or GMoney::Portfolio.all(:with_returns => true)
14
+
15
+ #wait for a minute or so
16
+
17
+ portfolios will now contain a list of all of your google finance portfolios, positions, and transactions
18
+
19
+ == License
20
+
21
+ (The MIT License)
22
+
23
+ Copyright (c) 2009 Justin Spradlin
24
+
25
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
26
+
27
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
28
+
29
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+ require 'spec/rake/spectask'
5
+
6
+ task :default => :specs
7
+
8
+ desc "Run the gmoney specs"
9
+ task :specs do
10
+ Spec::Rake::SpecTask.new do |t|
11
+ t.spec_files = FileList['spec/**/*_spec.rb']
12
+ t.rcov = true
13
+ t.rcov_opts = ['--exclude', 'spec']
14
+ t.spec_opts = ['--options', "spec/spec.opts"]
15
+ end
16
+ end
17
+
18
+ Echoe.new('gmoney', '0.0.2') do |p|
19
+ p.description = "A gem for interacting with the Google Finance API"
20
+ p.url = "http://github.com/jspradlin/gmoney"
21
+ p.author = "Justin Spradlin"
22
+ p.email = "jspradlin@gmail.com"
23
+ p.ignore_pattern = ["coverage/*", "*~"]
24
+ p.development_dependencies = []
25
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{gmoney}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Justin Spradlin"]
9
+ s.date = %q{2009-09-15}
10
+ s.description = %q{A gem for interacting with the Google Finance API}
11
+ s.email = %q{jspradlin@gmail.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/extensions/string.rb", "lib/gmoney.rb", "lib/gmoney/authentication_request.rb", "lib/gmoney/feed_parser.rb", "lib/gmoney/gf_request.rb", "lib/gmoney/gf_response.rb", "lib/gmoney/gf_service.rb", "lib/gmoney/gf_session.rb", "lib/gmoney/portfolio.rb", "lib/gmoney/portfolio_feed_parser.rb", "lib/gmoney/position.rb", "lib/gmoney/position_feed_parser.rb", "lib/gmoney/transaction.rb", "lib/gmoney/transaction_feed_parser.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "gmoney.gemspec", "lib/extensions/string.rb", "lib/gmoney.rb", "lib/gmoney/authentication_request.rb", "lib/gmoney/feed_parser.rb", "lib/gmoney/gf_request.rb", "lib/gmoney/gf_response.rb", "lib/gmoney/gf_service.rb", "lib/gmoney/gf_session.rb", "lib/gmoney/portfolio.rb", "lib/gmoney/portfolio_feed_parser.rb", "lib/gmoney/position.rb", "lib/gmoney/position_feed_parser.rb", "lib/gmoney/transaction.rb", "lib/gmoney/transaction_feed_parser.rb", "spec/authentication_request_spec.rb", "spec/fixtures/cacert.pem", "spec/fixtures/default_portfolios_feed.xml", "spec/fixtures/empty_portfolio_feed.xml", "spec/fixtures/portfolio_9_feed.xml", "spec/fixtures/portfolio_feed_with_returns.xml", "spec/fixtures/positions_feed_for_portfolio_14.xml", "spec/fixtures/positions_feed_for_portfolio_9.xml", "spec/fixtures/positions_feed_for_portfolio_9r.xml", "spec/fixtures/transactions_feed_for_GOOG.xml", "spec/gmoney_spec.rb", "spec/portfolio_feed_parser_spec.rb", "spec/portfolio_spec.rb", "spec/position_feed_parser_spec.rb", "spec/position_spec.rb", "spec/request_spec.rb", "spec/response_spec.rb", "spec/service_spec.rb", "spec/session_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/string_spec.rb", "spec/transaction_feed_parser_spec.rb", "spec/transaction_spec.rb"]
14
+ s.homepage = %q{http://github.com/jspradlin/gmoney}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Gmoney", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{gmoney}
18
+ s.rubygems_version = %q{1.3.4}
19
+ s.summary = %q{A gem for interacting with the Google Finance API}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ class String
2
+ def camel_to_us
3
+ add_us = gsub(/(.)([A-Z][a-z]+)/, '\1_\2')
4
+ add_us.gsub(/([a-z0-9])([A-Z])/, '\1_\2').downcase
5
+ end
6
+
7
+ def is_numeric?
8
+ Float self rescue false
9
+ end
10
+ end
@@ -0,0 +1,39 @@
1
+ # Justin Spradlin
2
+ # www.fiascode.com
3
+ # (C) 2009
4
+
5
+
6
+ $:.unshift File.expand_path(File.dirname(__FILE__))
7
+
8
+ require 'date'
9
+ require 'net/http'
10
+ require 'net/https'
11
+ require 'rexml/document'
12
+
13
+ require 'extensions/string'
14
+
15
+ require 'gmoney/authentication_request'
16
+ require 'gmoney/feed_parser.rb'
17
+ require 'gmoney/gf_request'
18
+ require 'gmoney/gf_response'
19
+ require 'gmoney/gf_service'
20
+ require 'gmoney/gf_session'
21
+ require 'gmoney/portfolio'
22
+ require 'gmoney/portfolio_feed_parser'
23
+ require 'gmoney/position'
24
+ require 'gmoney/position_feed_parser'
25
+ require 'gmoney/transaction'
26
+ require 'gmoney/transaction_feed_parser'
27
+
28
+ module GMoney
29
+ VERSION = '0.0.2'
30
+ GF_URL = "https://finance.google.com/finance"
31
+ GF_FEED_URL = "#{GF_URL}/feeds/default"
32
+
33
+ HTTPOK = 200
34
+ HTTPCreated = 201
35
+
36
+ def self.version
37
+ VERSION
38
+ end
39
+ end
@@ -0,0 +1,52 @@
1
+ module GMoney
2
+ class AuthenticationRequest
3
+ class AuthError < StandardError;end
4
+
5
+ AUTH_URL = 'https://www.google.com/accounts/ClientLogin'
6
+
7
+ def initialize(email, password)
8
+ @email = email
9
+ @password = password
10
+ end
11
+
12
+ def parameters
13
+ {
14
+ 'Email' => @email,
15
+ 'Passwd' => @password,
16
+ 'accountType' => 'HOSTED_OR_GOOGLE',
17
+ 'service' => 'finance',
18
+ 'source' => 'gmoney-001'
19
+ }
20
+ end
21
+
22
+ def uri
23
+ URI.parse(AUTH_URL)
24
+ end
25
+
26
+ def send_request(ssl_mode)
27
+ http = Net::HTTP.new(uri.host, uri.port)
28
+ http.use_ssl = true
29
+ http.verify_mode = ssl_mode
30
+
31
+ if ssl_mode == OpenSSL::SSL::VERIFY_PEER
32
+ http.ca_file = CA_CERT_FILE
33
+ end
34
+
35
+ http.request(build_request) do |response|
36
+ raise AuthError unless response.is_a?(Net::HTTPOK)
37
+ end
38
+ end
39
+
40
+ def build_request
41
+ post = Net::HTTP::Post.new(uri.path)
42
+ post.set_form_data(parameters)
43
+ post
44
+ end
45
+
46
+ def auth_token(opts={})
47
+ ssl_mode = opts[:secure] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
48
+ send_request(ssl_mode).body.match(/^Auth=(.*)$/)[1]
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,37 @@
1
+ module GMoney
2
+ class FeedParser
3
+ def self.parse_feed(feed, feed_class, options = {:feed_link => true, :symbol => false})
4
+ doc = REXML::Document.new(feed)
5
+ finance_objects = []
6
+ feed_class_string = feed_class.to_s.gsub("GMoney::", "").downcase
7
+
8
+ set_ivar = lambda do |target, name, value|
9
+ value = value.to_f if value.is_numeric?
10
+ target.instance_variable_set("@#{name.camel_to_us}", value)
11
+ end
12
+
13
+ doc.elements.each('feed/entry') do |parsed_entry|
14
+ finance_object = feed_class.new
15
+ finance_data = parsed_entry.elements["gf:#{feed_class_string}Data"]
16
+
17
+ #TODO - have someone peer review this. Is it bad practice to use instance_variable_set because
18
+ #it breaks encapsulation? (Even though it actually enhances the encapsulation in the "domain" classes
19
+ #by not allowing users to set attributes that should be read only (i.e. id, updated, return1w))
20
+ finance_object.instance_variable_set("@id", parsed_entry.elements['id'].text)
21
+ finance_object.instance_variable_set("@title", parsed_entry.elements['title'].text)
22
+ finance_object.instance_variable_set("@updated", DateTime.parse(parsed_entry.elements['updated'].text))
23
+ finance_object.instance_variable_set("@feed_link", parsed_entry.elements['gd:feedLink'].attributes['href']) if options[:feed_link]
24
+
25
+ finance_data.attributes.each { |attr_name, attr_value| set_ivar.call(finance_object, attr_name, attr_value) }
26
+ parsed_entry.elements['gf:symbol'].each { |attr_name, attr_value| set_ivar.call(finance_object, attr_name, attr_value)} if options[:symbol]
27
+
28
+ #TODO - This is only going to work for USD for now. Might need to updated to make a "Money" object to store amount and currency code.
29
+ finance_data.elements.each do |cg|
30
+ finance_object.instance_variable_set("@#{cg.name.camel_to_us}", cg.elements['gd:money'].attributes['amount'].to_f)
31
+ end
32
+ finance_objects << finance_object
33
+ end
34
+ finance_objects
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ module GMoney
2
+ class GFRequest
3
+ attr_accessor :url, :body, :method, :headers
4
+
5
+ def initialize(url, options = {})
6
+ @url = url
7
+ options.each do |key, value|
8
+ self.send("#{key}=", value)
9
+ end
10
+
11
+ @method ||= :get
12
+ @headers ||= {}
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module GMoney
2
+ class GFResponse
3
+ attr_accessor :status_code, :body, :headers
4
+ end
5
+ end
@@ -0,0 +1,46 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'uri'
4
+
5
+ module GMoney
6
+ class GFService
7
+ def self.send_request(request)
8
+ url = URI.parse(request.url)
9
+ http = Net::HTTP.new(url.host, url.port)
10
+ http.use_ssl = (url.scheme == 'https')
11
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
12
+
13
+ case request.method
14
+ when :get
15
+ req = Net::HTTP::Get.new(url.request_uri)
16
+ when :put
17
+ req = Net::HTTP::Put.new(url.request_uri)
18
+ when :post
19
+ req = Net::HTTP::Post.new(url.request_uri)
20
+ when :delete
21
+ req = Net::HTTP::Delete.new(url.request_uri)
22
+ else
23
+ raise ArgumentError, "Unsupported HTTP method specified."
24
+ end
25
+
26
+ req.body = request.body.to_s
27
+
28
+ request.headers.each do |key, value|
29
+ req[key] = value
30
+ end
31
+
32
+ res = http.request(req)
33
+
34
+ response = GFResponse.new
35
+ response.body = res.body
36
+ response.headers = Hash.new
37
+ res.each do |key, value|
38
+ response.headers[key] = value
39
+ end
40
+
41
+ response.status_code = res.code.to_i
42
+
43
+ return response
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,18 @@
1
+ module GMoney
2
+ class GFSession
3
+
4
+ def self.login(email, password, opts={})
5
+ @email = email
6
+ auth_request = GMoney::AuthenticationRequest.new(email, password)
7
+ @auth_token = auth_request.auth_token(opts)
8
+ end
9
+
10
+ def self.auth_token
11
+ @auth_token
12
+ end
13
+
14
+ def self.email
15
+ @email
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ module GMoney
2
+ class Portfolio
3
+ class PortfolioRequestError < StandardError;end
4
+
5
+ attr_accessor :title, :currency_code, :positions
6
+
7
+ attr_reader :id, :feed_link, :updated, :gain_percentage, :return1w, :return4w, :return3m,
8
+ :return_ytd, :return1y, :return3y, :return5y, :return_overall,
9
+ :cost_basis, :days_gain, :gain, :market_value
10
+
11
+ def initialize()
12
+ @positions = []
13
+ end
14
+
15
+ def self.all(options = {})
16
+ url = "#{GF_FEED_URL}/portfolios"
17
+ url += "?returns=true" if options[:with_returns]
18
+ portfolios = []
19
+
20
+ response = GFService.send_request(GFRequest.new(url, :headers => {"Authorization" => "GoogleLogin auth=#{GFSession.auth_token}"}))
21
+
22
+ if response.status_code == HTTPOK
23
+ portfolios = PortfolioFeedParser.parse_portfolio_feed(response.body)
24
+ else
25
+ raise PortfolioRequestError
26
+ end
27
+
28
+ portfolios.each do |portfolio|
29
+ portfolio.positions = Position.find_by_url(portfolio.feed_link, {:with_returns => options[:with_returns]})
30
+ end
31
+ portfolios
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ module GMoney
2
+ class PortfolioFeedParser < FeedParser
3
+ def self.parse_portfolio_feed(portfolio_feed)
4
+ parse_feed(portfolio_feed, Portfolio)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ module GMoney
2
+ class Position
3
+ class PositionRequestError < StandardError; end
4
+ attr_accessor :transactions
5
+
6
+ attr_reader :id, :updated, :title, :feed_link, :exchange, :symbol, :shares,
7
+ :full_name, :gain_percentage, :return1w, :return4w, :return3m,
8
+ :return_ytd, :return1y, :return3y, :return5y, :return_overall,
9
+ :cost_basis, :days_gain, :gain, :market_value
10
+
11
+ def initialize
12
+ @transactions = []
13
+ end
14
+
15
+ def self.find_by_url(url, options = {})
16
+ positions = []
17
+ url += "?returns=true" if options[:with_returns]
18
+
19
+ response = GFService.send_request(GFRequest.new(url, :headers => {"Authorization" => "GoogleLogin auth=#{GFSession.auth_token}"}))
20
+
21
+ if response.status_code == HTTPOK
22
+ positions = PositionFeedParser.parse_position_feed(response.body) if response.status_code == 200
23
+ else
24
+ raise PositionRequestError
25
+ end
26
+
27
+ positions.each do |position|
28
+ position.transactions = Transaction.find_by_url(position.feed_link)
29
+ end
30
+ positions
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ module GMoney
2
+ class PositionFeedParser < FeedParser
3
+ def self.parse_position_feed(position_feed)
4
+ parse_feed(position_feed, Position, {:feed_link => true, :symbol => true})
5
+ end
6
+ end
7
+ end