Class: ThirteenF::SecRequest

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

Constant Summary collapse

EFTS_HEADERS =
{
  'User-Agent' => 'S Fischer [email protected]',
  'Accept-Encoding' => 'gzip, deflate',
  'Host' => 'efts.sec.gov'
}

Class Method Summary collapse

Class Method Details

.data_headersObject



6
7
8
9
10
11
12
# File 'lib/thirteen_f/sec_request.rb', line 6

def self.data_headers
  {
    'User-Agent' => "ThirteenF/v#{::ThirteenF::VERSION} (Open Source Ruby Gem) [email protected]",
    'Host' => 'data.sec.gov',
    'Accept-Encoding' => 'gzip, deflate'
  }
end

.get(url, response_type: :json) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/thirteen_f/sec_request.rb', line 27

def self.get(url, response_type: :json)
  case response_type
  when :json
    response = HTTP.use(:auto_inflate).headers(data_headers).get(url)
    handle_response response, response_type: response_type
  else
    response = HTTP.use(:auto_inflate).headers(www_headers).get(url)
    handle_response response, response_type: response_type
  end
rescue
  raise "Request failed at this url: #{url} \n With this response #{response.to_s}"
end

.handle_response(response, response_type: :json) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/thirteen_f/sec_request.rb', line 45

def self.handle_response(response, response_type: :json)
  case response.status
  when 200, 201, 202, 203, 204, 206
    handle_response_type response.to_s, response_type
  else
    raise "Request failed with response #{response.status}, request url: #{response.uri.to_s}"
  end
end

.handle_response_type(body, response_type) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/thirteen_f/sec_request.rb', line 54

def self.handle_response_type(body, response_type)
  case response_type
  when :html
    Nokogiri::HTML body
  when :json
    JSON.parse body, symbolize_names: true
  when :xml
    xml_doc = Nokogiri::XML body
    xml_doc.remove_namespaces!
  end
end

.post(url, json) ⇒ Object



40
41
42
43
# File 'lib/thirteen_f/sec_request.rb', line 40

def self.post(url, json)
  response = HTTP.use(:auto_inflate).headers(EFTS_HEADERS).post(url, json: json)
  handle_response response
end

.www_headersObject



14
15
16
17
18
19
# File 'lib/thirteen_f/sec_request.rb', line 14

def self.www_headers
  {
    'User-Agent' => "ThirteenF/v#{::ThirteenF::VERSION} (Open Source Ruby Gem) [email protected]",
    'Host' => 'www.sec.gov'
  }
end