class HttpParser

Constants

VERSION

Attributes

response[RW]

Public Class Methods

new(response) click to toggle source
# File lib/http-parser.rb, line 8
def initialize(response)
  @response = response

  check_valid_response?(response)
end
parse(response) click to toggle source
# File lib/http-parser.rb, line 15
def parse(response)
  response = new(response)
  response.parse
end

Public Instance Methods

parse() click to toggle source
# File lib/http-parser.rb, line 21
def parse
  success       = (200..308).to_a.include?(response.code.to_i) ? true : false
  hash_response = JSON.parse(response.body)

  Struct.new(:success?, :hash_response).new(success, hash_response)
end

Private Instance Methods

check_valid_response?(response) click to toggle source
# File lib/http-parser.rb, line 30
def check_valid_response?(response)
  if response.respond_to?(:each_header)
    raise 'Not a valid response' unless response.each_header.to_h.dig('content-type').include?('application/json')
  elsif response.respond_to?(:headers)
    raise 'Not a valid response' unless response.headers.dig(:content_type).include?('application/json')
  else
    true
  end
end