Class: Edgar::Filings
- Inherits:
-
Object
- Object
- Edgar::Filings
- Includes:
- Enumerable
- Defined in:
- lib/edgar/filings.rb
Overview
Enumerable collection of Filing objects with filter, sort, and summary.
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#original_state ⇒ Object
readonly
Returns the value of attribute original_state.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
Class Method Summary collapse
- .from_quarter(year, quarter, type: "company") ⇒ Object
- .from_years(years, quarters: [1, 2, 3, 4]) ⇒ Object
Instance Method Summary collapse
- #[](index) ⇒ Object
- #current_page ⇒ Object
- #date_range ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #filter(form: nil, filing_date: nil, date: nil, cik: nil, ticker: nil, exchange: nil, company_name: nil, amendments: nil, date_start: nil, date_end: nil) ⇒ Object
- #find_by_company(company_name) ⇒ Object
- #group_by_form ⇒ Object
- #head(count = 10) ⇒ Object
-
#initialize(entries = [], original_state: {}, page_index: 0, per_page: 100, all_entries: nil) ⇒ Filings
constructor
A new instance of Filings.
- #latest(count = 1) ⇒ Object
- #length ⇒ Object
- #next_page ⇒ Object
- #page(num = nil, per_page: nil) ⇒ Object
- #previous_page ⇒ Object
- #sample(count = 1) ⇒ Object
- #summary ⇒ Object
- #tail(count = 10) ⇒ Object
- #to_a ⇒ Object
- #to_dict ⇒ Object
- #to_hashes ⇒ Object
- #total_pages(per_page: nil) ⇒ Object
Constructor Details
#initialize(entries = [], original_state: {}, page_index: 0, per_page: 100, all_entries: nil) ⇒ Filings
Returns a new instance of Filings.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/edgar/filings.rb', line 10 def initialize(entries = [], original_state: {}, page_index: 0, per_page: 100, all_entries: nil) @page_index = page_index @per_page = per_page @all_entries = all_entries @entries = entries.map do |e| if e.is_a?(Filing) e elsif e.is_a?(Hash) || e.is_a?(FilingIndex::IndexEntry) Filing.new( cik: e[:cik] || e["cik"], company: e[:company] || e["company"], form: e[:form] || e["form"], filing_date: e[:filing_date] || e["filing_date"], accession_number: e[:accession_number] || e["accession_number"] ) end end.compact @all_entries ||= @entries @original_state = original_state end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
8 9 10 |
# File 'lib/edgar/filings.rb', line 8 def entries @entries end |
#original_state ⇒ Object (readonly)
Returns the value of attribute original_state.
8 9 10 |
# File 'lib/edgar/filings.rb', line 8 def original_state @original_state end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
8 9 10 |
# File 'lib/edgar/filings.rb', line 8 def per_page @per_page end |
Class Method Details
.from_quarter(year, quarter, type: "company") ⇒ Object
153 154 155 156 |
# File 'lib/edgar/filings.rb', line 153 def self.from_quarter(year, quarter, type: "company") entries = FilingIndex.for(year, quarter, type: type) new(entries) end |
.from_years(years, quarters: [1, 2, 3, 4]) ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/edgar/filings.rb', line 158 def self.from_years(years, quarters: [1, 2, 3, 4]) years = Array(years) all = years.flat_map do |y| quarters.flat_map { |q| FilingIndex.for(y, q) } end new(all) end |
Instance Method Details
#[](index) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/edgar/filings.rb', line 36 def [](index) if index.is_a?(Integer) @entries[index] else @entries.find { |f| f.accession_number == index } end end |
#current_page ⇒ Object
90 91 92 |
# File 'lib/edgar/filings.rb', line 90 def current_page @page_index + 1 end |
#date_range ⇒ Object
146 147 148 149 150 151 |
# File 'lib/edgar/filings.rb', line 146 def date_range return nil if @entries.empty? dates = @entries.map(&:filing_date) { min: dates.min, max: dates.max } end |
#each(&block) ⇒ Object
32 33 34 |
# File 'lib/edgar/filings.rb', line 32 def each(&block) @entries.each(&block) end |
#empty? ⇒ Boolean
48 49 50 |
# File 'lib/edgar/filings.rb', line 48 def empty? @entries.empty? end |
#filter(form: nil, filing_date: nil, date: nil, cik: nil, ticker: nil, exchange: nil, company_name: nil, amendments: nil, date_start: nil, date_end: nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/edgar/filings.rb', line 72 def filter(form: nil, filing_date: nil, date: nil, cik: nil, ticker: nil, exchange: nil, company_name: nil, amendments: nil, date_start: nil, date_end: nil) filtered = @entries.select do |f| matches_filters?(f, form: form, cik: cik, ticker: ticker, company_name: company_name, exchange: exchange, filing_date: filing_date || date, date_start: date_start, date_end: date_end, amendments: amendments) end Filings.new(filtered, original_state: @original_state) end |
#find_by_company(company_name) ⇒ Object
86 87 88 |
# File 'lib/edgar/filings.rb', line 86 def find_by_company(company_name) filter(company_name: company_name) end |
#group_by_form ⇒ Object
122 123 124 |
# File 'lib/edgar/filings.rb', line 122 def group_by_form @entries.group_by(&:form) end |
#head(count = 10) ⇒ Object
60 61 62 |
# File 'lib/edgar/filings.rb', line 60 def head(count = 10) Filings.new(@entries.first(count), original_state: @original_state) end |
#latest(count = 1) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/edgar/filings.rb', line 52 def latest(count = 1) sorted = @entries.sort_by(&:filing_date).reverse selected = sorted.first(count) return selected.first if count == 1 && selected.length == 1 Filings.new(selected, original_state: @original_state) end |
#length ⇒ Object
44 45 46 |
# File 'lib/edgar/filings.rb', line 44 def length @entries.length end |
#next_page ⇒ Object
104 105 106 107 108 109 |
# File 'lib/edgar/filings.rb', line 104 def next_page next_start = (@page_index + 1) * @per_page return self if next_start >= @all_entries.length page(@page_index + 2) end |
#page(num = nil, per_page: nil) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/edgar/filings.rb', line 94 def page(num = nil, per_page: nil) p = num || (@page_index + 1) pp = per_page || @per_page start_idx = (p - 1) * pp page_entries = @all_entries[start_idx, pp] || [] Filings.new(page_entries, original_state: @original_state, page_index: p - 1, per_page: pp, all_entries: @all_entries) end |
#previous_page ⇒ Object
111 112 113 114 115 |
# File 'lib/edgar/filings.rb', line 111 def previous_page return self if @page_index.zero? page(@page_index) end |
#sample(count = 1) ⇒ Object
68 69 70 |
# File 'lib/edgar/filings.rb', line 68 def sample(count = 1) Filings.new(@entries.sample(count), original_state: @original_state) end |
#summary ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/edgar/filings.rb', line 166 def summary form_counts = group_by_form.transform_values(&:length) date_range_str = if @entries.any? dates = @entries.map(&:filing_date) "#{dates.min} - #{dates.max}" else "N/A" end { count: @entries.length, date_range: date_range_str, form_counts: form_counts } end |
#tail(count = 10) ⇒ Object
64 65 66 |
# File 'lib/edgar/filings.rb', line 64 def tail(count = 10) Filings.new(@entries.last(count), original_state: @original_state) end |
#to_a ⇒ Object
126 127 128 |
# File 'lib/edgar/filings.rb', line 126 def to_a @entries.dup end |
#to_dict ⇒ Object
142 143 144 |
# File 'lib/edgar/filings.rb', line 142 def to_dict to_hashes end |
#to_hashes ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/edgar/filings.rb', line 130 def to_hashes @entries.map do |f| { cik: f.cik, company: f.company, form: f.form, filing_date: f.filing_date, accession_number: f.accession_number } end end |
#total_pages(per_page: nil) ⇒ Object
117 118 119 120 |
# File 'lib/edgar/filings.rb', line 117 def total_pages(per_page: nil) pp = per_page || @per_page pp.positive? ? (@all_entries.length.to_f / pp).ceil : 0 end |