class NiceJsonApi::Response
Response
from a friendly API
Constants
- NullResponse
Public Class Methods
new(url, method: :get, body: nil, auth: nil)
click to toggle source
# File lib/nice_json_api.rb, line 13 def initialize(url, method: :get, body: nil, auth: nil) @url = url.to_s @method = method @body = body @auth = auth end
Public Instance Methods
body()
click to toggle source
# File lib/nice_json_api.rb, line 20 def body JSON.parse(raw_body) || {} rescue JSON::ParserError {} end
code()
click to toggle source
# File lib/nice_json_api.rb, line 26 def code raw.code end
message()
click to toggle source
# File lib/nice_json_api.rb, line 30 def message raw.message end
raw()
click to toggle source
# File lib/nice_json_api.rb, line 34 def raw @raw ||= fetch(initial_uri, method: @method, body: @body, auth: @auth) end
raw_body()
click to toggle source
# File lib/nice_json_api.rb, line 38 def raw_body raw.body end
Private Instance Methods
fetch(uri, method: :get, body: nil, limit: 10, auth: nil)
click to toggle source
rubocop:disable Metrics/MethodLength
# File lib/nice_json_api.rb, line 45 def fetch(uri, method: :get, body: nil, limit: 10, auth: nil) return NullResponse.new('404', '{}', 'Too Many Redirects') if limit.zero? case response = SingleRequest.new(uri, method: method, body: body, auth: auth).response when Net::HTTPRedirection, Net::OpenTimeout fetch(response['location'], method: method, body: body, limit: --limit, auth: auth) else response end rescue Errno::ECONNREFUSED NullResponse.new('499', '{}', 'Host not found') rescue Errno::EHOSTUNREACH, Errno::ECONNRESET fetch(uri, method: method, body: body, limit: --limit, auth: auth) end
initial_uri()
click to toggle source
rubocop:enable Metrics/MethodLength
# File lib/nice_json_api.rb, line 64 def initial_uri @url = "https://#{@url}" unless @url[0..3] == 'http' URI(@url) end