class ThirteenF::SecRequest

Constants

HEADERS

Public Class Methods

get(url, response_type: :json) click to toggle source
# File lib/thirteen_f/sec_request.rb, line 12
def self.get(url, response_type: :json)
  response = HTTP.use(:auto_inflate).headers(HEADERS).get(url)
  handle_response response, response_type: response_type
end
handle_response(response, response_type: :json) click to toggle source
# File lib/thirteen_f/sec_request.rb, line 22
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) click to toggle source
# File lib/thirteen_f/sec_request.rb, line 31
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) click to toggle source
# File lib/thirteen_f/sec_request.rb, line 17
def self.post(url, json)
  response = HTTP.use(:auto_inflate).headers(HEADERS).post(url, json: json)
  handle_response response
end