mr_yahoo_finance 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ec90687b5d0e20245c83a9b587e94913f298adb
4
+ data.tar.gz: 2cdec76c18c6ff9b1eb3eb6053dd8ce79fcb9566
5
+ SHA512:
6
+ metadata.gz: 37db3afe3ea1d09ab0151acc58dbdadc8b3069668d078a5481b1e86641d1e9ed8f176fa0d4a45eae142a926e8fe10a8b406774113e324842547fae47d184e5c7
7
+ data.tar.gz: 6d055a7574577b45940b6a264f54bdfa5916296be501e45c36ec95436e18d0ec70ab540ef2c875814712b639d652dd2fd647a3623e05887394315f63d1038a54
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at TODO: Write your email address. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mr_yahoo_finance.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 mr-myself
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ # MrYahooFinance
2
+
3
+ This gem enables you to get the company's settlement in annual and quarter.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mr_yahoo_finance'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mr_yahoo_finance
20
+
21
+ ## Usage
22
+
23
+ ```
24
+
25
+ yh = MrYahooFinance.get('YHOO') # pass the company symbol which you want
26
+
27
+ # example =>
28
+ {
29
+ :annual=>
30
+ [
31
+ {
32
+ :period=>"Dec 31, 2014",
33
+ :sale=>4618133,
34
+ :operating_profit=>142942,
35
+ :net_income=>7521731
36
+ },
37
+ {
38
+ :period=>"Dec 31, 2013",
39
+ :sale=>4680380,
40
+ :operating_profit=>589926,
41
+ :net_income=>1366281
42
+ },
43
+ {
44
+ :period=>"Dec 31, 2012",
45
+ :sale=>4986566,
46
+ :operating_profit=>566368,
47
+ :net_income=>3945479
48
+ }
49
+ ],
50
+ :quarterly=>
51
+ [
52
+ {
53
+ :period=>"Sep 30, 2015",
54
+ :sale=>1225673,
55
+ :operating_profit=>-86312,
56
+ :net_income=>76261
57
+ },
58
+ {
59
+ :period=>"Jun 30, 2015",
60
+ :sale=>1243265,
61
+ :operating_profit=>-44794,
62
+ :net_income=>-21554
63
+ },
64
+ {
65
+ :period=>"Mar 31, 2015",
66
+ :sale=>1225970,
67
+ :operating_profit=>-87354,
68
+ :net_income=>21198
69
+ },
70
+ {
71
+ :period=>"Dec 31, 2014",
72
+ :sale=>1253072,
73
+ :operating_profit=>32154,
74
+ :net_income=>166344
75
+ }
76
+ ]
77
+ }
78
+
79
+ ```
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mr_yahoo_finance"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ require 'mr_yahoo_finance/profit_and_loss'
2
+ require 'mr_yahoo_finance/builder/format'
3
+ require 'mr_yahoo_finance/builder/html'
4
+ require 'mr_yahoo_finance/builder/validator'
5
+ require 'mr_yahoo_finance/config/config'
6
+ require 'mr_yahoo_finance/config/indicator'
7
+ require 'mr_yahoo_finance/config/url'
8
+ require 'mr_yahoo_finance/config/version'
9
+
10
+
11
+ module MrYahooFinance
12
+
13
+ def self.get(symbol)
14
+ mr_yahoo_finance = MrYahooFinance.new(symbol)
15
+ mr_yahoo_finance.fetch
16
+ end
17
+
18
+ class MrYahooFinance
19
+ def initialize(symbol)
20
+ @symbol = symbol
21
+ end
22
+
23
+ def fetch
24
+ {}.tap do |data|
25
+ data[:annual] = ProfitAndLoss.get(@symbol, :annual)
26
+ data[:quarterly] = ProfitAndLoss.get(@symbol, :quarterly)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ module MrYahooFinance
2
+ module Builder
3
+ module Format
4
+ module Indicator
5
+
6
+ class << self
7
+ def periods(element, count)
8
+ [].tap do |data|
9
+ (2..(count+1)).each do |number|
10
+ data << build_text(element, number).to_i
11
+ end
12
+ end
13
+ end
14
+
15
+ private
16
+ def build_text(element, number)
17
+ if negative?(element, number)
18
+ '-'.concat(extract(element, number).to_i.to_s)
19
+ else
20
+ extract(element, number)
21
+ end
22
+ end
23
+
24
+ def negative?(element, number)
25
+ element.css("td[#{number}]").text.include?('(')
26
+ end
27
+
28
+ def extract(element, number)
29
+ element.css("td[#{number}]").text.delete(',').delete('(').delete(')')
30
+ end
31
+ end
32
+ end
33
+
34
+ module Period
35
+
36
+ class << self
37
+ def periods(element, count)
38
+ [].tap do |ary|
39
+ (0..(count-1)).map{|n| ary << element.css('th')[n].text}
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ module MrYahooFinance
5
+ module Builder
6
+ module Html
7
+
8
+ class << self
9
+ def row_html(path)
10
+ begin
11
+ Nokogiri::HTML(open(path))
12
+ rescue Net::HTTPError
13
+ end
14
+ end
15
+
16
+ def target_table_tr(html)
17
+ html.css('table.yfnc_tabledata1 > tr > td > table > tr'){|tr| yield tr }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module MrYahooFinance
2
+ module Builder
3
+ module Validator
4
+
5
+ def valid?(tr, key_word)
6
+ tr.css('td').text.include?(key_word)
7
+ end
8
+
9
+ def parseable?(html)
10
+ html and html.css('table.yfnc_tabledata1').any?
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module MrYahooFinance
2
+ module Config
3
+ TERMS_COUNT = {
4
+ annual: 3,
5
+ quarterly: 4
6
+ }.freeze
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module MrYahooFinance
2
+ module Config
3
+ module Indicator
4
+ ALL = {
5
+ period: 'Period',
6
+ sale: 'Total Revenue',
7
+ operating_profit: 'Operating Income or Loss',
8
+ net_income: 'Net Income'
9
+ }.freeze
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module MrYahooFinance
2
+ module Config
3
+ module Url
4
+
5
+ def self.annual(symbol)
6
+ "https://finance.yahoo.com/q/is?s=#{symbol}+Income+Statement&annual".freeze
7
+ end
8
+
9
+ def self.quarterly(symbol)
10
+ "https://finance.yahoo.com/q/is?s=#{symbol}".freeze
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module MrYahooFinance
2
+ module Config
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,66 @@
1
+ require 'mr_yahoo_finance/builder/validator'
2
+
3
+ module MrYahooFinance
4
+ class ProfitAndLoss
5
+ include MrYahooFinance::Builder::Validator
6
+
7
+ class << self
8
+ def get(symbol, term)
9
+ profit_and_loss = new(symbol, term)
10
+ profit_and_loss.fetch
11
+ profit_and_loss.data
12
+ end
13
+ end
14
+
15
+ attr_reader :data
16
+
17
+ def initialize(symbol, term)
18
+ @symbol = symbol
19
+ @term = term
20
+ @data = Array.new(Config::TERMS_COUNT[@term]){ Hash.new }
21
+ end
22
+
23
+ def fetch
24
+ build_html
25
+ parse_html if parseable?(@html)
26
+ end
27
+
28
+ private
29
+ def build_html
30
+ @html = Builder::Html.row_html Config::Url.send(@term.to_s, @symbol)
31
+ end
32
+
33
+ def parse_html
34
+ target_table_tr do |tr|
35
+ Config::Indicator::ALL.each do |key, value|
36
+ build_periods(tr, key) if valid?(tr, value)
37
+ end
38
+ end
39
+ end
40
+
41
+ def target_table_tr
42
+ Builder::Html.target_table_tr(@html).each{|tr| yield tr }
43
+ end
44
+
45
+ def build_periods(tr, base)
46
+ case base
47
+ when :period
48
+ format_periods(tr) do |hash|
49
+ @data.each_with_index{|data, i| data[base] = hash[i]}
50
+ end
51
+ else
52
+ format_indicators(tr) do |hash|
53
+ @data.each_with_index{|data, i| data[base] = hash[i]}
54
+ end
55
+ end
56
+ end
57
+
58
+ def format_periods(tr)
59
+ Builder::Format::Period.periods(tr, Config::TERMS_COUNT[@term]).tap{|array| yield array }
60
+ end
61
+
62
+ def format_indicators(tr)
63
+ Builder::Format::Indicator.periods(tr, Config::TERMS_COUNT[@term]).tap{|array| yield array }
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mr_yahoo_finance/config/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mr_yahoo_finance"
8
+ spec.version = MrYahooFinance::Config::VERSION
9
+ spec.authors = ["mr-myself"]
10
+ spec.email = ["contact@mr-myself.com"]
11
+
12
+ spec.summary = %q{Getting financial data of specific company from Yahoo finance.}
13
+ spec.description = %q{Getting financial data of specific company from Yahoo finance.}
14
+ spec.homepage = "https://github.com/mr-myself"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "nokogiri", "~> 1.6"
26
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mr_yahoo_finance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mr-myself
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ description: Getting financial data of specific company from Yahoo finance.
70
+ email:
71
+ - contact@mr-myself.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/mr_yahoo_finance.rb
87
+ - lib/mr_yahoo_finance/builder/format.rb
88
+ - lib/mr_yahoo_finance/builder/html.rb
89
+ - lib/mr_yahoo_finance/builder/validator.rb
90
+ - lib/mr_yahoo_finance/config/config.rb
91
+ - lib/mr_yahoo_finance/config/indicator.rb
92
+ - lib/mr_yahoo_finance/config/url.rb
93
+ - lib/mr_yahoo_finance/config/version.rb
94
+ - lib/mr_yahoo_finance/profit_and_loss.rb
95
+ - mr_yahoo_finance.gemspec
96
+ homepage: https://github.com/mr-myself
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.4.5
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Getting financial data of specific company from Yahoo finance.
120
+ test_files: []