psei 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 419a46c16e6473e81f1ad1e25e2e27c6284aa13e
4
+ data.tar.gz: 9d3c2d9225cd44352414665c58f2dcde944d3dd4
5
+ SHA512:
6
+ metadata.gz: 6d793f3554192fe2bbccec6719f9b48fd2598e7dc991e3a1c3cc2dd729a94932c1b1753bec0f95c2e857854e3289cc1417973f54a8ab31e72b89bc2617b44220
7
+ data.tar.gz: 5c15c8a83da144dca6592452d2c571fffa4a8d793b0a26bed6df7f29856aba0b799068e1b4b70bf0c0c9524d612900af4f74427e3fa457aac2e98bc945686a47
data/.gitignore ADDED
@@ -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/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in psei.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Marvin Baltazar
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # PSEI
2
+
3
+ This gem fetches the end-of-day values of the Philippine Stock Exchange (PSE).
4
+ More details available at their website [www.pse.com.ph](http://www.pse.com.ph/).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'psei'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install psei
21
+
22
+ ## Usage
23
+
24
+ There are two main components in this library, the `Security` module and the `Index` module.
25
+
26
+ ### Psei::Security
27
+
28
+ This module is used to get the end-of-day values of all securities in the PSE. The main identifier of these securities are their symbols or tickers. For example, the Philippine Long Distance Telephone Company is defined by their symbol `TEL`.
29
+
30
+ First, we need to initialize the module:
31
+
32
+ `@sec = Psei::Security.new`
33
+
34
+ To get the list of all symbols available:
35
+
36
+ `@sec.symbols`
37
+
38
+ This returns an array of all symbols (as strings), sorted by alphabetical order.
39
+
40
+ To get the values of a specific security:
41
+
42
+ `@sec.value('TEL')`
43
+
44
+ This returns a Hash containing the following information:
45
+
46
+ * symbol (String) - The security symbol
47
+ * alias (String) - The company identifier
48
+ * total_volume (Float) - Total number of traded volume of the day
49
+ * updown (String) - Identifies if the security has gone up or down in value
50
+ * percent_change (Float) - Percentage that the security has changed
51
+ * last_price (Float) - The last traded price of the security
52
+
53
+ To get the values of all securities in a single call:
54
+
55
+ `@sec.values`
56
+
57
+ This returns an array of Hashes, each containing the same information when getting the values of a single security.
58
+
59
+ Note that these values are only applicable to a specific trading day. To get the date of these values:
60
+
61
+ `@sec.date`
62
+
63
+
64
+ ### Psei::Index
65
+
66
+ This module is used to get the end-of-day values of all indices in the PSE. This includes the main index (PSEi), as well as industry-specific indices (Financial, Holdings, Mining, Services, etc).
67
+
68
+ First, we need to initialize the module:
69
+
70
+ `@ind = Psei::Index.new`
71
+
72
+ To get the list of all symbols available:
73
+
74
+ `@ind.symbols`
75
+
76
+ This returns an array of all symbols (as strings).
77
+
78
+ To get the values of a specific index:
79
+
80
+ `@ind.value('PSE')`
81
+
82
+ This returns a Hash containing the following information:
83
+
84
+ * symbol (String) - The index symbol
85
+ * alias (String) - The index identifier
86
+ * total_volume (Float) - Total number of traded volume of the day
87
+ * updown (String) - Identifies if the index has gone up or down in value
88
+ * percent_change (Float) - Percentage that the index has changed
89
+ * last_price (Float) - The last traded price of the index
90
+
91
+ To get the values of all indices in a single call:
92
+
93
+ `@ind.values`
94
+
95
+ This returns an array of Hashes, each containing the same information when getting the values of a single index.
96
+
97
+ Note that these values are only applicable to a specific trading day. To get the date of these values:
98
+
99
+ `@ind.date`
100
+
101
+
102
+ ## Development
103
+
104
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
105
+
106
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
107
+
108
+ ## Contributing
109
+
110
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marvs/psei. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
111
+
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
116
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "psei"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/psei.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "date"
2
+ require "psei/version"
3
+ require "psei/parser"
4
+ require "psei/formatter"
5
+ require "psei/security"
6
+ require "psei/index"
7
+ require "psei/date"
8
+
9
+ module Psei
10
+ SOURCE_URL = "http://pse.com.ph/stockMarket/home.html?method=getSecuritiesAndIndicesForPublic&ajax=true"
11
+ HEADING_ALIAS = "Stock Update As of"
12
+ INDEX_ALIASES = [ "PSEi", "All Shares", "Financials", "Industrial", "Holding Firms", "Property",
13
+ "Services", "Mining and Oil"].freeze
14
+ end
data/lib/psei/date.rb ADDED
@@ -0,0 +1,33 @@
1
+ # Based on the parsed request, gets the date of the values
2
+ class Psei::Date
3
+ DATE_IDENTIFIER = "DATE"
4
+ DATETIME_FORMAT = '%m/%d/%Y %H:%M %p%z'
5
+
6
+ def initialize parsed
7
+ @parsed = parsed
8
+ @parsed_time = nil
9
+ end
10
+
11
+ def get
12
+ parse_time.to_date
13
+ end
14
+
15
+ def get_datetime
16
+ parse_time
17
+ end
18
+
19
+ private
20
+
21
+ def date_filter
22
+ @parsed.select{|x| x["lastTradedPrice"] == DATE_IDENTIFIER}.first
23
+ end
24
+
25
+ def date_string
26
+ date_str = date_filter["securityAlias"]
27
+ date_str << "+0800"
28
+ end
29
+
30
+ def parse_time
31
+ @parsed_time ||= DateTime.strptime(date_string, DATETIME_FORMAT)
32
+ end
33
+ end
@@ -0,0 +1,49 @@
1
+ # Formats the parsed data
2
+ class Psei::Formatter
3
+
4
+ def initialize
5
+ @arr = []
6
+ end
7
+
8
+ def process arr
9
+ @arr = arr
10
+ @arr ? to_hash : {}
11
+ end
12
+
13
+ private
14
+
15
+ def int_format val
16
+ val.to_s.gsub(',', '').to_i
17
+ end
18
+
19
+ def float_format val
20
+ val.to_s.gsub(',','').to_f
21
+ end
22
+
23
+ def get_total_volume
24
+ float_format @arr['totalVolume']
25
+ end
26
+
27
+ def get_percent_change
28
+ float_format @arr['percChangeClose']
29
+ end
30
+
31
+ def get_last_price
32
+ float_format @arr['lastTradedPrice']
33
+ end
34
+
35
+ def up_or_down
36
+ @arr['indicator'] == "U" ? 'up' : 'down'
37
+ end
38
+
39
+ def to_hash
40
+ {
41
+ symbol: @arr['securitySymbol'],
42
+ alias: @arr['securityAlias'],
43
+ total_volume: get_total_volume,
44
+ updown: up_or_down,
45
+ percent_change: get_percent_change,
46
+ last_price: get_last_price
47
+ }
48
+ end
49
+ end
data/lib/psei/index.rb ADDED
@@ -0,0 +1,55 @@
1
+ class Psei::Index
2
+
3
+ def initialize(parser=nil)
4
+ @parser = parser || Psei::Parser.new(Psei::SOURCE_URL)
5
+ @parsed = @parser.process
6
+ @formatter = Psei::Formatter.new
7
+ end
8
+
9
+ def symbols
10
+ indices_hash.keys
11
+ end
12
+
13
+ # Returns a Hash of last values of indices
14
+ def values
15
+ symbols.collect{ |x| value x }
16
+ end
17
+
18
+ # Returns the last value of an index
19
+ def value symbol
20
+ index symbol
21
+ end
22
+
23
+ def date
24
+ Psei::Date.new(@parsed).get
25
+ end
26
+
27
+ private
28
+
29
+ def find_by_symbol symbol
30
+ indices = indices_filter
31
+ indices.detect{ |s| s['securitySymbol'] == symbol.to_s }
32
+ end
33
+
34
+ def indices_filter
35
+ @ind_filter ||= @parsed.select do |x|
36
+ (Psei::INDEX_ALIASES).include?(x['securityAlias']) && x['securitySymbol'] != Psei::HEADING_ALIAS
37
+ end
38
+ end
39
+
40
+ def indices_hash
41
+ @ind_hash ||= Hash[indices_array]
42
+ end
43
+
44
+ def indices_array
45
+ @ind_array ||= indices_filter.collect do |item|
46
+ [item['securitySymbol'], item['lastTradedPrice'].to_f]
47
+ end
48
+ end
49
+
50
+ def index sym
51
+ ind = indices_filter.select{|x| x['securitySymbol'] == sym }.first
52
+ @formatter.process ind
53
+ end
54
+
55
+ end
@@ -0,0 +1,23 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class Psei::Parser
5
+ attr_reader :url
6
+
7
+ def initialize url
8
+ @url = url
9
+ end
10
+
11
+ def process(response=nil)
12
+ resp = response || get_response
13
+ JSON.parse(resp)
14
+ end
15
+
16
+ private
17
+
18
+ def get_response
19
+ uri = URI(@url)
20
+ @response ||= Net::HTTP.get(uri)
21
+ end
22
+
23
+ end
@@ -0,0 +1,54 @@
1
+ class Psei::Security
2
+
3
+ def initialize(parser=nil)
4
+ @parser = parser || Psei::Parser.new(Psei::SOURCE_URL)
5
+ @parsed = @parser.process
6
+ @formatter = Psei::Formatter.new
7
+ end
8
+
9
+ def symbols
10
+ securities_hash.keys
11
+ end
12
+
13
+ def values
14
+ symbols.collect{ |x| value x }
15
+ end
16
+
17
+ # Returns the data of a specific security
18
+ def value symbol
19
+ security symbol
20
+ end
21
+
22
+ def date
23
+ Psei::Date.new(@parsed).get
24
+ end
25
+
26
+ private
27
+
28
+ def find_by_symbol symbol
29
+ securities = securities_filter
30
+ securities.detect{ |s| s['securitySymbol'] == symbol.to_s }
31
+ end
32
+
33
+ def securities_filter
34
+ @sec_filter ||= @parsed.reject do |x|
35
+ (Psei::INDEX_ALIASES).include?(x['securityAlias']) || x['securitySymbol'] == Psei::HEADING_ALIAS
36
+ end
37
+ end
38
+
39
+ def securities_hash
40
+ @sec_hash ||= Hash[securities_array]
41
+ end
42
+
43
+ def securities_array
44
+ @sec_array ||= securities_filter.collect do |item|
45
+ [item['securitySymbol'], item['lastTradedPrice'].to_f]
46
+ end
47
+ end
48
+
49
+ def security sym
50
+ sec = securities_filter.select{|x| x['securitySymbol'] == sym }.first
51
+ @formatter.process sec
52
+ end
53
+
54
+ end
@@ -0,0 +1,3 @@
1
+ module Psei
2
+ VERSION = "0.1.0"
3
+ end
data/psei.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'psei/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "psei"
8
+ spec.version = Psei::VERSION
9
+ spec.authors = ["Marvin Baltazar"]
10
+ spec.email = ["marvin.baltazar@gmail.com"]
11
+
12
+ spec.summary = %q{Fetch the PSE end-of-day values}
13
+ spec.description = %q{This returns the end-of-day values of the Philippine Stock Exchange}
14
+ spec.homepage = "https://github.com/marvs/psei"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ #else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ #end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "minitest", '~> 5'
33
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: psei
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marvin Baltazar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-08 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5'
55
+ description: This returns the end-of-day values of the Philippine Stock Exchange
56
+ email:
57
+ - marvin.baltazar@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/psei.rb
72
+ - lib/psei/date.rb
73
+ - lib/psei/formatter.rb
74
+ - lib/psei/index.rb
75
+ - lib/psei/parser.rb
76
+ - lib/psei/security.rb
77
+ - lib/psei/version.rb
78
+ - psei.gemspec
79
+ homepage: https://github.com/marvs/psei
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Fetch the PSE end-of-day values
103
+ test_files: []