class JsonChecker::JSONFetcher

Public Class Methods

fetch_response(response) click to toggle source
# File lib/json_checker/json_fetcher.rb, line 43
def self.fetch_response(response)
  case response
    when Net::HTTPSuccess then
      return JSONFetcher.json_from_content(response.body)
    else
      puts"[ERROR] Connection error"
  end
end
json_from_content(content) click to toggle source
# File lib/json_checker/json_fetcher.rb, line 30
def self.json_from_content(content)
  if content.nil? || (!content.is_a?(String) && (!content.is_a?(Hash)))
    return nil
  end

  begin
    return content.is_a?(String) ? JSON.parse(content) : JSON.parse(content.to_json)
  rescue JSON::ParserError => e
    puts "[ERROR] Invalid json"
  end
  return nil
end
json_from_path(path) click to toggle source
# File lib/json_checker/json_fetcher.rb, line 8
def self.json_from_path(path)
  begin
    jsonFile = open(path)
    jsonContent = jsonFile.read
    return JSONFetcher.json_from_content(jsonContent)
  rescue
    puts "[ERROR] #{path} not found"
    return nil
  end
end
json_from_url(url) click to toggle source
# File lib/json_checker/json_fetcher.rb, line 19
def self.json_from_url(url)
  begin
    uri = URI(url)
    response = Net::HTTP.get_response(uri)
    return JSONFetcher.fetch_response(response)  
  rescue
  end
  
  return nil
end