Class: ApiAuth::Client::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/api_auth/client/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.

Parameters:

  • response (Hash | RestClient::Response)


11
12
13
14
15
16
# File 'lib/api_auth/client/response.rb', line 11

def initialize(response)
  fail ArgumentError, 'response is not a RestClient::Response' unless response.is_a?(RestClient::Response)

  @response ||= response
  parse
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



36
37
38
39
40
41
# File 'lib/api_auth/client/response.rb', line 36

def method_missing(meth, *args, &blk)
  return response.send(meth, *args, &blk) if response.respond_to?(meth)
  return parsed.send(meth) if parsed.respond_to?(meth)

  super
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



8
9
10
# File 'lib/api_auth/client/response.rb', line 8

def attrs
  @attrs
end

#parsedObject (readonly)

Returns the value of attribute parsed.



8
9
10
# File 'lib/api_auth/client/response.rb', line 8

def parsed
  @parsed
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/api_auth/client/response.rb', line 8

def response
  @response
end

Instance Method Details

#[](arg) ⇒ Object



18
19
20
# File 'lib/api_auth/client/response.rb', line 18

def [](arg)
  parsed.send(arg) if parsed.respond_to?(arg)
end

#inspectObject



32
33
34
# File 'lib/api_auth/client/response.rb', line 32

def inspect
  parsed.inspect.gsub('OpenStruct', 'ApiAuth::Client::Response')
end

#ok?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/api_auth/client/response.rb', line 28

def ok?
  response.code >= 200 && response.code < 400
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/api_auth/client/response.rb', line 43

def respond_to_missing?(meth, include_private = false)
  response.respond_to?(meth) || parsed.respond_to?(meth) || super
end

#to_jsonObject



22
23
24
25
26
# File 'lib/api_auth/client/response.rb', line 22

def to_json
  JSON.parse(response)
rescue JSON::ParserError
  { 'error' => 'Bad JSON', 'body' => response.body }
end