Class: ThirteenF::CusipSecurities

Inherits:
Object
  • Object
show all
Defined in:
lib/thirteen_f/cusip_securities.rb

Defined Under Namespace

Classes: ListEntry

Constant Summary collapse

BASE_URL =
'https://www.sec.gov'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_location) ⇒ CusipSecurities

Returns a new instance of CusipSecurities.



30
31
32
33
34
35
36
# File 'lib/thirteen_f/cusip_securities.rb', line 30

def initialize(file_location)
  @file_location = file_location
  @file_name = file_location.split('/').last
  @quarter = set_quarter_string file_name
  @period_end = set_period_end file_name
  true
end

Instance Attribute Details

#file_locationObject (readonly)

Returns the value of attribute file_location.



9
10
11
# File 'lib/thirteen_f/cusip_securities.rb', line 9

def file_location
  @file_location
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



9
10
11
# File 'lib/thirteen_f/cusip_securities.rb', line 9

def file_name
  @file_name
end

#list_entriesObject (readonly)

Returns the value of attribute list_entries.



9
10
11
# File 'lib/thirteen_f/cusip_securities.rb', line 9

def list_entries
  @list_entries
end

#period_endObject (readonly)

Returns the value of attribute period_end.



9
10
11
# File 'lib/thirteen_f/cusip_securities.rb', line 9

def period_end
  @period_end
end

#quarterObject (readonly)

Returns the value of attribute quarter.



9
10
11
# File 'lib/thirteen_f/cusip_securities.rb', line 9

def quarter
  @quarter
end

Class Method Details

.all_file_locationsObject



12
13
14
15
16
17
18
19
20
# File 'lib/thirteen_f/cusip_securities.rb', line 12

def self.all_file_locations
  index_url = "#{BASE_URL}/divisions/investment/13flists"
  page = SecRequest.get index_url, response_type: :html
  a_tags = page.search('a').select do |a_tag|
    href = a_tag.attributes['href']&.value.to_s
    href.include?('13flist') && href.include?('.pdf')
  end
  a_tags.map { |a_tag|"#{BASE_URL + a_tag.attributes['href'].value}" }
end

.most_recent_listObject



22
23
24
25
26
27
28
# File 'lib/thirteen_f/cusip_securities.rb', line 22

def self.most_recent_list
  index_url = "#{BASE_URL}/divisions/investment/13flists"
  page = SecRequest.get index_url, response_type: :html
  a_tag = page.search('a').find { |a| a.text.include?('Current List') }
  file_location = "#{BASE_URL + a_tag.attributes['href'].value}"
  new file_location
end

Instance Method Details

#get_list_entriesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/thirteen_f/cusip_securities.rb', line 38

def get_list_entries
  return false unless file_location
  io = URI.open(
    file_location, 
    'User-Agent' => "ThirteenF/v#{::ThirteenF::VERSION} (Open Source Ruby Gem) [email protected]",
    'Host' => 'www.sec.gov'
  )
  reader = PDF::Reader.new io
  valid_entries = []
  reader.pages[2..-1].each do |page|
    lines = page.text.split("\n").reject(&:empty?)[3..-1]
    line_arrs = lines.map do |line|
      next nil if line.include?('Total Coun')
      line.split(/\s{3}|( \* )/).reject(&:empty?).map(&:strip).reject { |text| text == '*' }
    end
    line_arrs.compact.each do |line_arr|
      next unless line_arr.count > 1
      valid_entries.push ListEntry.new(line_arr)
    end
  end
  @list_entries = valid_entries
  true
end