Class: SecQuery::Entity

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity) ⇒ Entity

Returns a new instance of Entity.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sec_query/entity.rb', line 6

def initialize(entity)
    @first = entity[:first]
    @middle = entity[:middle]
    @last = entity[:last]
    @name = entity[:name]
    @sic = entity[:sic]
    @url = entity[:url]
    @location = entity[:location]
    @state_of_inc = entity[:state_of_inc]
    @formerly = entity[:formerly]
    @symbol = entity[:symbol]
    @cik = entity[:cik]
    @type =entity[:type]
    @mailing_address = entity[:mailing_address]
    @business_address = entity[:business_address]
    @relationships = entity[:relationships]
    @transactions = entity[:transactions]
    @filings = entity[:filings]
end

Instance Attribute Details

#business_addressObject

Returns the value of attribute business_address.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def business_address
  @business_address
end

#cikObject

Returns the value of attribute cik.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def cik
  @cik
end

#filingsObject

Returns the value of attribute filings.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def filings
  @filings
end

#firstObject

Returns the value of attribute first.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def first
  @first
end

#formerlyObject

Returns the value of attribute formerly.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def formerly
  @formerly
end

#lastObject

Returns the value of attribute last.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def last
  @last
end

#locationObject

Returns the value of attribute location.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def location
  @location
end

#mailing_addressObject

Returns the value of attribute mailing_address.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def mailing_address
  @mailing_address
end

#middleObject

Returns the value of attribute middle.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def middle
  @middle
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def name
  @name
end

#relationshipsObject

Returns the value of attribute relationships.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def relationships
  @relationships
end

#sicObject

Returns the value of attribute sic.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def sic
  @sic
end

#state_of_incObject

Returns the value of attribute state_of_inc.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def state_of_inc
  @state_of_inc
end

#symbolObject

Returns the value of attribute symbol.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def symbol
  @symbol
end

#transactionsObject

Returns the value of attribute transactions.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def transactions
  @transactions
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def type
  @type
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/sec_query/entity.rb', line 4

def url
  @url
end

Class Method Details

.business_address(doc) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/sec_query/entity.rb', line 183

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



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

def self.cik(url, entity)
    response = Entity.query(url+"&output=atom")
    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")
            for tr in tbl
                td = tr.search("td")
                if td[1] != nil && entity[:middle] != nil && td[1].innerHTML.downcase == (entity[:last]+" "+entity[:first]+" "+entity[:middle]).downcase or td[1] != nil && 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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/sec_query/entity.rb', line 223

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] != nil and options[:transactions].is_a?(Hash)
        temp = Transaction.find(temp, options[:transactions][:start], options[:transactions][:count], options[:transactions][:limit])
    elsif options[:transactions] != nil && options[:transactions] == true
        temp = Transaction.find(temp, nil, nil, nil)
    end


    ## Get Filings for entity

    if options[:filings] != nil and options[:filings].is_a?(Hash)
        temp = Filing.find(temp, options[:filings][:start], options[:filings][:count], options[:filings][:limit])
    elsif options[:filings] != nil and options[:filings] == true 
        temp = Filing.find(temp, nil, nil, nil)
    end

    return temp;
end

.document(cik) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sec_query/entity.rb', line 122

def self.document(cik)
    url ="http://www.sec.gov/cgi-bin/own-disp?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= "http://www.sec.gov/cgi-bin/own-disp?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
    return [doc, type]

end

.find(entity_args, *options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sec_query/entity.rb', line 27

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

    if !temp[:cik] or 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
    if !options.empty?;
        temp[:transactions]=[]
        temp[:filings] =[]
        options = Entity.options(temp, options); 
        temp = Entity.details(temp, options);
    end                   
    
    ###  Return entity Object
    @entity = Entity.new(temp)

    return @entity

end

.info(doc) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/sec_query/entity.rb', line 160

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] != nil
        info[:sic] = lines[0].search("a")[0].innerHTML
    end
    if lines[0].search("a")[1] != nil
        info[:location] = lines[0].search("a")[1].innerHTML
    end

    if lines[0].search("b")[0] != nil and lines[0].search("b")[0].innerHTML.squeeze(" ") != " "
        info[:state_of_inc] = lines[0].search("b")[0].innerHTML
    end
    if lines[1] != nil and lines[1].search("font")
        info[:formerly] = lines[1].search("font").innerHTML.gsub("formerly: ", "").gsub("<br />", "").gsub("\n", "; ")
    end
    return info
end

.log(entity) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/sec_query/entity.rb', line 251

def self.log(entity)

    if entity != false
        puts "\n\t# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #\n\n"
        puts "\t"+entity.name
        puts "\t("+entity.cik+")"
        if entity.formerly && entity.formerly != ""
            puts "\tFormerly: "+entity.formerly
        end
        if entity.sic
            puts "\tSIC = "+entity.sic
        end
        if entity.location
            puts "\tLocation: "+entity.location+", "
        end
        if entity.state_of_inc
            puts "\tState of Incorporation: "+entity.state_of_inc
        end
        if entity.mailing_address
            puts "\tMailing Address:\t"+ entity.mailing_address.inspect.gsub('\n', ' ').squeeze(" ")
        end

        if entity.business_address
            puts "\tBusiness Address:\t"+ entity.business_address.inspect.gsub('\n', ' ').squeeze(" ")
        end
        
        
        if !entity.relationships
            puts "\n\tRELATIONSHIPS:\t0 Total"
        else
            puts "\n\tRELATIONSHIPS:\t"+ entity.relationships.count.to_s+" Total"
            printf("\t%-40s %-15s %-30s %-10s\n\n","Entity", "CIK", "Position", "Date")
            for relationship in entity.relationships
                printf("\t%-40s %-15s %-30s %-10s\n",relationship.name, relationship.cik, relationship.position, relationship.date)
            end
        end
        if entity.transactions
            puts "\n\tTRANSACTIONS:\t"+ entity.transactions.count.to_s+" Total"
            printf("\t%-20s %-10s %-5s %-10s %-10s %-10s %-15s %-10s\n\n","Owner", "CIK", "Modes", "Type","Shares","Price","Owned","Date")
            for transaction in entity.transactions
                printf("\t%-20s %-10s %-5s %-10s%-10s %-10s %-15s %-10s\n", transaction.reporting_owner,transaction.owner_cik,transaction.modes, transaction.type,transaction.shares,transaction.price,transaction.owned,transaction.date)
            end
        end
        if entity.filings
            puts "\n\tFILINGS:\t"+ entity.filings.count.to_s+" Total"
            printf("\t%-10s %-30s %-20s\n\n","Type", "File ID", "Date")
            for filing in entity.filings
                printf("\t%-10s %-30s %-20s\n",filing.term, filing.file_id, filing.date)
            end    
          
        end
        puts "\t"+entity.url+"\n\n"
    else
        return "No Entity found."
    end

end

.mailing_address(doc) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/sec_query/entity.rb', line 193

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



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/sec_query/entity.rb', line 204

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
    return args;
end

.parse_document(temp, doc) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/sec_query/entity.rb', line 141

def self.parse_document(temp, doc)

    temp[:type] = doc[1]
    info = Entity.info(doc[0])
    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])

    return temp;
end

.query(url) ⇒ Object



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

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

.url(args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sec_query/entity.rb', line 70

def self.url(args)
    if args.is_a?(Hash)
        if args[:symbol] != nil
            string = "CIK="+args[:symbol]
        elsif args[:cik] != nil
            string = "CIK="+args[:cik]
        elsif args[:first] != nil and args[:last]
            string = "company="+args[:last]+" "+args[:first]
        elsif args[:name] != nil
            string = "company="+args[:name].gsub(/[(,?!\''"":.)]/, '')
        end
    elsif args.is_a?(String)
        begin Float(args)
            string = "CIK="+args
        rescue
            if args.length <= 4
                string = "CIK="+args
            else
                string = "company="+args.gsub(/[(,?!\''"":.)]/, '')
            end
        end
    end
    string = string.to_s.gsub(" ", "+")
    url = "http://www.sec.gov/cgi-bin/browse-edgar?"+string+"&action=getcompany"
    return url
end