Class: JsonChecker::JSONFetcher
- Inherits:
-
Object
- Object
- JsonChecker::JSONFetcher
- Defined in:
- lib/json_checker/json_fetcher.rb
Class Method Summary collapse
- .fetch_response(response) ⇒ Object
- .json_from_content(content) ⇒ Object
- .json_from_path(path) ⇒ Object
- .json_from_url(url) ⇒ Object
Class Method Details
.fetch_response(response) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/json_checker/json_fetcher.rb', line 44 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) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/json_checker/json_fetcher.rb', line 31 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) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/json_checker/json_fetcher.rb', line 9 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) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/json_checker/json_fetcher.rb', line 20 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 |