Class: ClassLink::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/classlink_client/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(net_http_response) ⇒ Response

Returns a new instance of Response.

Raises:



9
10
11
12
13
# File 'lib/classlink_client/response.rb', line 9

def initialize(net_http_response)
  @net_http_response = net_http_response

  raise RequestError unless code.start_with?("2")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/classlink_client/response.rb', line 26

def method_missing(method_name, *args, &block)
  if parsed.respond_to?(method_name)
    parsed.public_send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#parsedObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/classlink_client/response.rb', line 15

def parsed
  @parsed ||= JSON.parse(body).then do |res|
    # API responses are keyed (somewhat randomly)
    res.is_a?(Hash) ? res.values[0] : res
  end
rescue JSON::ParserError
  warn "Error parsing response as JSON, returning raw body instead."

  body
end

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/classlink_client/response.rb', line 34

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