Class: Net::HTTPResponse

Inherits:
Object show all
Defined in:
lib/okay/http.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#from_jsonObject

Converts the result from JSON if okay? returns true; otherwise returns nil.

Can be combined with or_raise! to get a JSON result or raise an exception:

Okay::HTTP.get("https://example.org/blah.json").or_raise!.from_json


34
35
36
37
38
39
40
# File 'lib/okay/http.rb', line 34

def from_json
  return nil unless okay?

  require "json"

  JSON.parse(body)
end

#okay?Boolean

Returns false if the server encountered an error, true otherwise.

Returns:

  • (Boolean)


14
15
16
17
18
19
# File 'lib/okay/http.rb', line 14

def okay?
  self.value
  true
rescue Net::HTTPExceptions
  false
end

#or_raise!Object

Raises an exception if the request failed. (A fatal equivalent of okay?)



22
23
24
25
# File 'lib/okay/http.rb', line 22

def or_raise!
  self.value
  self
end