Class: SecQuery::Entity

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

Overview

> SecQuery::Entity

SecQuery::Entity is the root class which is responsible for requesting, parsing and initializing SecQuery::Entity intances from SEC Edgar.

Constant Summary collapse

COLUMNS =
[:first, :middle, :last, :name, :symbol, :cik, :url, :type, :sic,
:location, :state_of_inc, :formerly, :mailing_address,
:business_address, :relationships, :transactions, :filings]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity) ⇒ Entity

Returns a new instance of Entity.



14
15
16
17
18
# File 'lib/sec_query/entity.rb', line 14

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

Class Method Details

.business_address(doc) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/sec_query/entity.rb', line 147

def self.business_address(doc)
  addie = doc.search('//table').search('//td')
    .search("b[text()*='Business Address']")
  if !addie.empty?
    business_address = addie[0].parent.innerHTML
      .gsub('<b class="blue">Business Address</b>', '').gsub('<br />', ' ')
    return business_address
  else
    return false
  end
end

.cik(url, entity) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sec_query/entity.rb', line 59

def self.cik(url, entity)
  response = Entity.query(url.output_atom.to_s)
  doc = Hpricot::XML(response)
  data = doc.search('//feed/title')[0]
  if data.inner_text == 'EDGAR Search Results'
    tbl = doc.search("//span[@class='companyMatch']")
    if tbl && tbl.innerHTML != ''
      tbl = tbl[0].parent.search('table')[0].search('tr')
      tbl.each do |tr|
        td = tr.search('td')
        if td[1] && entity[:middle] && td[1].innerHTML.downcase == ("#{entity[:last]} #{entity[:first]} #{entity[:middle]}").downcase || td[1] && td[1].innerHTML.downcase == ("#{ entity[:last] } #{ entity[:first] }").downcase
          cik = td[0].search('a').innerHTML
          return cik
        end
      end
    else
      return false
    end
  else
    cik = data.inner_text.scan(/\(([^)]+)\)/).to_s
    cik = cik.gsub('[["', '').gsub('"]]', '')
    return cik
  end
end

.details(temp, options) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/sec_query/entity.rb', line 189

def self.details(temp, options)
  ## Get Relationships for entity
  if options[:relationships] == true
    relationships = Relationship.find(temp)
    temp[:relationships] = relationships
  end

  ## Get Transactions for entity
  if options[:transactions] && options[:transactions].is_a?(Hash)
    temp = Transaction.find(
      temp,
      options[:transactions][:start],
      options[:transactions][:count],
      options[:transactions][:limit])
  elsif options[:transactions] && options[:transactions] == true
    temp = Transaction.find(temp, nil, nil, nil)
  end

  ## Get Filings for entity
  if options[:filings] && options[:filings].is_a?(Hash)
    temp = Filing.find(
      temp,
      options[:filings][:start],
      options[:filings][:count],
      options[:filings][:limit])
  elsif options[:filings] && options[:filings] == true
    temp = Filing.find(temp, nil, nil, nil)
  end

  temp
end

.document(cik) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sec_query/entity.rb', line 84

def self.document(cik)
  url = SecURI.ownership_display_uri(action: :getissuer, CIK: cik)
  response = query(url)
  doc = Hpricot(response)
  text = 'Ownership Reports from:'
  type = 'issuer'
  entity = doc.search('//table').search('//td').search("b[text()*='#{text}']")
  if entity.empty?
    url = SecURI.ownership_display_uri(action: :getowner, CIK: cik)
    response = query(url)
    doc = Hpricot(response)
    text = 'Ownership Reports for entitys:'
    type = 'owner'
    entity = doc.search('//table').search('//td').search("b[text()*='#{text}']")
  end
  [doc, type]
end

.find(entity_args, *options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sec_query/entity.rb', line 20

def self.find(entity_args, *options)
  temp = {}
  temp[:url] = SecURI.browse_edgar_uri(entity_args)
  temp[:url][:action] = :getcompany
  temp[:cik] = Entity.cik(temp[:url], entity_args)

  if !temp[:cik] || temp[:cik] == ''
    puts "No Entity found for query: #{ temp[:url] }"
    return false
  end

  ### Get Document and Entity Type
  doc = Entity.document(temp[:cik])
  temp = Entity.parse_document(temp, doc)

  ### Get Additional Arguments and Query Additional Details
  unless options.empty?
    temp[:transactions] = []
    temp[:filings] = []
    options = Entity.options(temp, options)
    temp = Entity.details(temp, options)
  end

  ###  Return entity Object
  @entity = Entity.new(temp)
  @entity
end

.info(doc) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sec_query/entity.rb', line 120

def self.info(doc)
  info = {}
  lines = doc.search("//td[@bgcolor='#E6E6E6']")[0].parent.parent.search('//tr')
  td = lines[0].search('//td//b').innerHTML
  info[:name] = td.gsub(td.scan(/\(([^)]+)\)/).to_s, '').gsub('()', '').gsub('\n', '')
  lines = lines[1].search('//table')[0].search('//tr//td')

  if lines[0].search('a')[0]
    info[:sic] = lines[0].search('a')[0].innerHTML
  end

  if lines[0].search('a')[1]
    info[:location] = lines[0].search('a')[1].innerHTML
  end

  if lines[0].search('b')[0] && lines[0].search('b')[0].innerHTML.squeeze(' ') != ' '
    info[:state_of_inc] = lines[0].search('b')[0].innerHTML
  end

  if lines[1] && lines[1].search('font')
    info[:formerly] = lines[1].search('font').innerHTML
      .gsub('formerly: ', '').gsub('<br />', '').gsub('\n', '; ')
  end

  info
end

.mailing_address(doc) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sec_query/entity.rb', line 159

def self.mailing_address(doc)
  addie = doc.search('//table').search('//td')
    .search("b[text()*='Mailing Address']")
  if !addie.empty?
    mailing_address = addie[0].parent.innerHTML
      .gsub('<b class="blue">Mailing Address</b>', '').gsub('<br />', ' ')
    return mailing_address
  else
    return false
  end
end

.options(temp, options) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/sec_query/entity.rb', line 171

def self.options(temp, options)
  args = {}
  if options.is_a?(Array) && options.length == 1 && options[0] == true
    args[:relationships] = true
    args[:transactions] = true
    args[:filings] = true
  elsif options.is_a?(Array) && options.length > 1
    args[:relationships] = options[0]
    args[:transactions] = options[1]
    args[:filings] = options[2]
  elsif options[0].is_a?(Hash)
    args[:relationships] = options[0][:relationships]
    args[:transactions] = options[0][:transactions]
    args[:filings] = options[0][:filings]
  end
  args
end

.parse_document(temp, doc) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sec_query/entity.rb', line 102

def self.parse_document(temp, doc)
  info = Entity.info(doc[0])
  temp[:type] = doc[1]
  temp[:name] = info[:name]
  temp[:location] = info[:location]
  temp[:sic] = info[:sic]
  temp[:state_of_inc] = info[:state_of_inc]
  temp[:formerly] = info[:formerly]

  ### Get Mailing Address
  temp[:mailing_address] = Entity.mailing_address(doc[0])

  ### Get Business Address
  temp[:business_address] = Entity.business_address(doc[0])

  temp
end

.query(url) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/sec_query/entity.rb', line 48

def self.query(url)
  RestClient.get(url.to_s) do |response, request, result, &block|
    case response.code
    when 200
      return response
    else
      response.return!(request, result, &block)
    end
  end
end