sec-firms 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: 497947b9cfb3ad25cdfb8a798d976dc10d615a28
4
+ data.tar.gz: 76a9e3cf60c05f3f9de9fe15e6ace31de6e65571
5
+ SHA512:
6
+ metadata.gz: b5e7891dc979c5b0df269790b9ec66b6741152e892c37f4bd174b5afa07efa92741918c524b92200708b6db4b7a53d55b6d4e24287147d01520328021a17479b
7
+ data.tar.gz: 64bf1ff42e6053b78bd881c2e441b97d691cbbe359ccd26062448d423e98847c54ea4bc43ee0701196ce606ca4a364a5cd2b13dd227d910a28c943ca41b0822d
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ script: "bundle exec rake spec"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sec-firms.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Josemar Luedke
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.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Sec::Firms [![Build Status](https://travis-ci.org/neighborly/sec-firms.svg?branch=master)](https://travis-ci.org/neighborly/sec-firms)
2
+
3
+ Firms XML parser for the U. S. Securities and Exchange Commission
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sec-firms'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ SEC maintains a list of the firms with the CIK ID that is updated every month. We have an API to access that list as csv with the following:
22
+
23
+ ```ruby
24
+ Sec::Firms::Lists.new.latest_as_csv
25
+ ```
26
+
27
+ To fetch all the data avaliable from a firm, you can use the following:
28
+
29
+ ```ruby
30
+ Sec::Firms::FirmParser.new(cik_id).to_hash
31
+ ```
32
+
33
+ Here's an exaple using both classes:
34
+
35
+ ```ruby
36
+ firms = CSV.read(Sec::Firms::Lists.new.latest_as_csv)[3..-1]
37
+
38
+ firms.each_with_index do |row, index|
39
+ cik_id = row[2].strip
40
+ firm = Sec::Firms::FirmParser.new(cik_id).to_hash
41
+
42
+ json = JSON.pretty_generate(firm.as_json)
43
+ file_name = Rails.root.join("data/#{cik_id}-#{firm['slug']}.json")
44
+ File.write(file_name, json)
45
+
46
+ puts "#{index + 1} out of #{firms.count}"
47
+ end
48
+ ```
49
+
50
+ ## Configuration
51
+
52
+ ```ruby
53
+ Sec::Firms.configure do |config|
54
+ config.root_path = "#{Rails.root}/data"
55
+ end
56
+ ```
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/[my-github-username]/sec-firms/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
71
+
72
+ ## License
73
+
74
+ Licensed under the [MIT license](LICENSE.txt).
75
+
76
+ ## About Neighborly
77
+
78
+ [![Neighborly](https://cloud.githubusercontent.com/assets/230476/10037857/285b76f6-6171-11e5-9af9-3ce60861e8c8.png)](https://neighborly.com)
79
+
80
+ **Neighborly is the nation's first Community Investment Marketplace.™**
81
+
82
+ By providing better ways for people to invest directly in the places and civic projects they care about, we create new options for communities to approach civic capital formation. Neighborly is democratizing the $3.8T municipal securities market, fostering a healthier relationship between global banks and our nation's places.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sec/firms"
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
@@ -0,0 +1,7 @@
1
+ module Sec
2
+ module Firms
3
+ class Configuration
4
+ attr_accessor :root_path
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,44 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'digest/md5'
4
+ require 'fileutils'
5
+
6
+ module Sec
7
+ module Firms
8
+ class Downloader
9
+ attr_reader :url, :root_path
10
+
11
+ def initialize(url)
12
+ @url = url
13
+ @root_path = "#{Sec::Firms.configuration.root_path}/downloads/"
14
+ end
15
+
16
+ def content
17
+ if File.exist?(file_name)
18
+ read_file
19
+ else
20
+ save_to_file
21
+ end
22
+ end
23
+
24
+ def file_name
25
+ "#{root_path}#{Digest::MD5.hexdigest(url)}.xml"
26
+ end
27
+
28
+ private
29
+
30
+ def read_file
31
+ IO.read(file_name)
32
+ end
33
+
34
+ def save_to_file
35
+ request = Net::HTTP.get_response(URI.parse(url))
36
+ if request.code == '200'
37
+ FileUtils.mkdir_p(root_path)
38
+ File.write(file_name, request.body)
39
+ request.body
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ module Sec
2
+ module Firms
3
+ class FirmEntryParser < XMLParser
4
+ attr_reader :updated_at
5
+
6
+ def initialize(xml_str, updated_at)
7
+ super(xml_str) if xml_str
8
+ @updated_at = updated_at
9
+ end
10
+
11
+ def to_hash
12
+ data = Hash.from_xml(doc.to_s)['edgarSubmission']['formData'] rescue nil
13
+
14
+ return {} unless data
15
+ data = Helpers::transform_hash(data) do |hash, key, value|
16
+ hash[key] = default_values(value)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def default_values(value)
23
+ {
24
+ value: value,
25
+ updated_at: updated_at
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,83 @@
1
+ module Sec
2
+ module Firms
3
+ class FirmParser < XMLParser
4
+ SEC_FIRM_DETAIL_FILE_NAME = 'primary_doc.xml'
5
+ SEC_FIRM_URL = 'http://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=:CIK_ID&output=atom'
6
+
7
+ def initialize(cik_id)
8
+ fetch_url = SEC_FIRM_URL.gsub(':CIK_ID', cik_id)
9
+ xml_str = Net::HTTP.get(URI.parse(fetch_url))
10
+ super(xml_str)
11
+ end
12
+
13
+ def id
14
+ doc.xpath('//xmlns:cik').text
15
+ end
16
+
17
+ def slug
18
+ Slugify.convert(name[:value], true)
19
+ end
20
+
21
+ def name
22
+ default_values doc.xpath('//xmlns:conformed-name').text
23
+ end
24
+
25
+ def to_hash
26
+ data = Hash.from_xml(doc.to_s)['feed']['company_info']
27
+ data = Helpers::transform_hash(data) do |hash, key, value|
28
+ hash[key.underscore] = default_values(value)
29
+ end
30
+
31
+ {
32
+ id: id,
33
+ slug: slug,
34
+ name: name
35
+ }.merge(data).merge(entries_attributes)
36
+ .deep_transform_keys { |key| key.to_s.underscore }
37
+ end
38
+
39
+ def entries
40
+ doc.xpath('//xmlns:entry').map do |entry|
41
+ filing_date = Date.parse(entry.at('filing-date').text)
42
+ accession_number = entry.at('accession-nunber').text
43
+ [
44
+ [filing_date, accession_number],
45
+ entry
46
+ ]
47
+ end.sort_by { |value| value[0] }
48
+ end
49
+
50
+ private
51
+
52
+ def updated_at
53
+ @updated_at ||= doc.xpath('//xmlns:feed').at('updated').text
54
+ end
55
+
56
+ def default_values(value)
57
+ {
58
+ value: value,
59
+ updated_at: updated_at
60
+ }
61
+ end
62
+
63
+ def entries_attributes
64
+ hash = {}
65
+ details = entries.map do |entry|
66
+ FirmEntryParser.new(
67
+ Downloader.new(entry_url(entry[1])).content, entry[0][0]
68
+ ).to_hash
69
+ end
70
+
71
+ details.each do |detail|
72
+ hash.merge!(detail)
73
+ end
74
+ hash
75
+ end
76
+
77
+ def entry_url(entry)
78
+ url = entry.at('filing-href').text
79
+ url.gsub(url.split('/')[-1], SEC_FIRM_DETAIL_FILE_NAME)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,17 @@
1
+ module Sec
2
+ module Firms
3
+ module Helpers
4
+ def self.transform_hash(original, options = {}, &block)
5
+ original.inject({}) do |result, (key, value)|
6
+ value = if (options[:deep] && Hash === value)
7
+ transform_hash(value, options, &block)
8
+ else
9
+ value
10
+ end
11
+ block.call(result, key, value)
12
+ result
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,70 @@
1
+ require 'roo-xls'
2
+ require 'zip'
3
+
4
+ module Sec
5
+ module Firms
6
+ class Lists < XMLParser
7
+ URL = 'https://www.sec.gov/foia/docs/muniadvisors-archive.htm'
8
+
9
+ def initialize
10
+ xml = Net::HTTP.get(URI.parse(URL))
11
+ super(xml)
12
+ end
13
+
14
+ def latest_as_csv
15
+ convert_to_csv(unzip(latest_report).first)
16
+ end
17
+
18
+ def urls
19
+ doc.css('table:last li a').map do |anchor|
20
+ [
21
+ Date.parse(anchor.text),
22
+ expand_url(anchor.attr('href'))
23
+ ]
24
+ end.sort.reverse
25
+ end
26
+
27
+ private
28
+
29
+ def latest_report
30
+ url = urls.first[1]
31
+ full_path = "/tmp/#{Digest::MD5.hexdigest(url)}.zip"
32
+
33
+ unless File.exist?(full_path)
34
+ remote_file = open(url)
35
+ File.open(full_path, 'wb') do |file|
36
+ file.write(remote_file.read)
37
+ end
38
+ end
39
+
40
+ full_path
41
+ end
42
+
43
+ def unzip(zip_path)
44
+ Zip::File.open(zip_path) do |zip_file|
45
+ zip_file.map do |entry|
46
+ file_path = File.join(File.dirname(zip_path), entry.name)
47
+ entry.extract(file_path) unless File.exist?(file_path)
48
+ file_path
49
+ end
50
+ end
51
+ end
52
+
53
+ def convert_to_csv(xls_path)
54
+ csv_path = File.join(File.dirname(xls_path),
55
+ File.basename(xls_path) + '.csv')
56
+ xls = Roo::Excel.new(xls_path)
57
+ xls.to_csv(csv_path)
58
+ csv_path
59
+ end
60
+
61
+ def latest_report_url
62
+ doc.css('table:nth-child(2) li a').first.attribute('href')
63
+ end
64
+
65
+ def expand_url(relative_path)
66
+ URI.join(URL, relative_path).to_s
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,5 @@
1
+ module Sec
2
+ module Firms
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require 'nokogiri'
2
+
3
+ module Sec
4
+ module Firms
5
+ class XMLParser
6
+ attr_accessor :doc
7
+
8
+ def initialize(xml_str)
9
+ @doc = Nokogiri::XML(xml_str, nil)
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/sec/firms.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'slugify'
2
+ require 'sec/firms/version'
3
+ require 'sec/firms/helpers'
4
+ require 'sec/firms/configuration'
5
+ require 'sec/firms/downloader'
6
+ require 'sec/firms/xml_parser'
7
+ require 'sec/firms/firm_entry_parser'
8
+ require 'sec/firms/firm_parser'
9
+ require 'sec/firms/lists'
10
+
11
+ module Sec
12
+ module Firms
13
+ class << self
14
+ attr_writer :configuration
15
+ end
16
+
17
+ def self.configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+
21
+ def self.configure
22
+ yield(configuration)
23
+ end
24
+ end
25
+ end
data/sec-firms.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sec/firms/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sec-firms"
8
+ spec.version = Sec::Firms::VERSION
9
+ spec.authors = ['Neighborly' 'Irio Musskopf', 'Josemar Luedke']
10
+ spec.email = %w(howdy@neighborly.com iirineu@gmail.com josemarluedke@gmail.com)
11
+
12
+ spec.summary = "Firms parser for sec.gov"
13
+ spec.description = "Firms parser for the U. S. Securities and Exchange Commission"
14
+ spec.homepage = "https://github.com/neighborly/sec-firms"
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_dependency "roo-xls", "~> 1.0"
23
+ spec.add_dependency "rubyzip", "~> 1.1"
24
+ spec.add_dependency "slugify", "~> 1.0"
25
+ spec.add_dependency "nokogiri", "> 1.5"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.7"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.3.0"
30
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sec-firms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - NeighborlyIrio Musskopf
8
+ - Josemar Luedke
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2015-09-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: roo-xls
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rubyzip
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.1'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.1'
42
+ - !ruby/object:Gem::Dependency
43
+ name: slugify
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: nokogiri
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.5'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.5'
70
+ - !ruby/object:Gem::Dependency
71
+ name: bundler
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '1.7'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.7'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '10.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '10.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rspec
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: 3.3.0
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 3.3.0
112
+ description: Firms parser for the U. S. Securities and Exchange Commission
113
+ email:
114
+ - howdy@neighborly.com
115
+ - iirineu@gmail.com
116
+ - josemarluedke@gmail.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".travis.yml"
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - lib/sec/firms.rb
131
+ - lib/sec/firms/configuration.rb
132
+ - lib/sec/firms/downloader.rb
133
+ - lib/sec/firms/firm_entry_parser.rb
134
+ - lib/sec/firms/firm_parser.rb
135
+ - lib/sec/firms/helpers.rb
136
+ - lib/sec/firms/lists.rb
137
+ - lib/sec/firms/version.rb
138
+ - lib/sec/firms/xml_parser.rb
139
+ - sec-firms.gemspec
140
+ homepage: https://github.com/neighborly/sec-firms
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.4.5
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Firms parser for sec.gov
164
+ test_files: []