Class: SecQuery::Filing

Inherits:
Object
  • Object
show all
Defined in:
lib/sec_query/filing.rb

Overview

> SecQuery::Filing

SecQuery::Filing requests and parses filings for any given SecQuery::Entity

Constant Summary collapse

COLUMNS =
:cik, :title, :summary, :link, :term, :date, :file_id

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filing) ⇒ Filing



10
11
12
13
14
# File 'lib/sec_query/filing.rb', line 10

def initialize(filing)
  COLUMNS.each do |column|
    instance_variable_set("@#{ column }", filing[column])
  end
end

Class Method Details

.fetch(uri, &blk) ⇒ Object



16
17
18
19
20
# File 'lib/sec_query/filing.rb', line 16

def self.fetch(uri, &blk)
  open(uri) do |rss|
    parse_rss(rss, &blk)
  end
end

.find(entity, start, count, limit) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sec_query/filing.rb', line 84

def self.find(entity, start, count, limit)
  start ||= 0
  count ||= 80
  url = uri_for_cik(entity[:cik], start, count)
  response = Entity.query(url)
  doc = Hpricot::XML(response)
  entries = doc.search(:entry)
  query_more = false
  entries.each do |entry|
    query_more = true
    filing = {}
    filing[:cik] = entity[:cik]
    filing[:title] = (entry/:title).innerHTML
    filing[:summary] = (entry/:summary).innerHTML
    filing[:link] =  (entry/:link)[0].get_attribute('href')
    filing[:term] = (entry/:category)[0].get_attribute('term')
    filing[:date] = (entry/:updated).innerHTML
    filing[:file_id] = (entry/:id).innerHTML.split('=').last
    entity[:filings] << Filing.new(filing)
  end
  if (query_more && limit.nil?) || (query_more && !limit)
    Filing.find(entity, start + count, count, limit)
  else
    return entity
  end
end

.for_cik(cik, options = {}, &blk) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sec_query/filing.rb', line 35

def self.for_cik(cik, options = {}, &blk)
  start = options.fetch(:start, 0)
  count = options.fetch(:count, 100)
  limit = options.fetch(:limit, 100)
  fetch(uri_for_cik(cik, start, count), &blk)
  start += count
  return if start >= limit
  for_cik(cik, { start: start, count: count, limit: limit }, &blk)
rescue OpenURI::HTTPError
  return
end

.parse_rss(rss, &blk) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sec_query/filing.rb', line 68

def self.parse_rss(rss, &blk)
  feed = RSS::Parser.parse(rss, false)
  feed.entries.each do |entry|
    filing = Filing.new({
      cik: entry.title.content.match(/\((\w{10})\)/)[1],
      file_id: entry.id.content.split('=').last,
      term:  entry.category.term,
      title: entry.title.content,
      summary: entry.summary.content,
      date: DateTime.parse(entry.updated.content.to_s),
      link: entry.link.href.gsub('-index.htm', '.txt')
    })
    blk.call(filing)
  end
end

.recent(options = {}, &blk) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sec_query/filing.rb', line 22

def self.recent(options = {}, &blk)
  start = options.fetch(:start, 0)
  count = options.fetch(:count, 100)
  limit = options.fetch(:limit, 100)
  limited_count = [limit - start, count].min
  fetch(uri_for_recent(start, limited_count), &blk)
  start += count
  return if start >= limit
  recent({ start: start, count: count, limit: limit }, &blk)
rescue OpenURI::HTTPError
  return
end

.uri_for_cik(cik, start = 0, count = 100) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/sec_query/filing.rb', line 57

def self.uri_for_cik(cik, start = 0, count = 100)
  SecURI.browse_edgar_uri(
    action: :getcompany,
    owner: :include,
    output: :atom,
    start: start,
    count: count,
    CIK: cik
  )
end

.uri_for_recent(start = 0, count = 100) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/sec_query/filing.rb', line 47

def self.uri_for_recent(start = 0, count = 100)
  SecURI.browse_edgar_uri(
    action: :getcurrent,
    owner: :include,
    output: :atom,
    start: start,
    count: count
  )
end