Class: CONTENTdmAPI::Response

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

Overview

A class to parse API responses.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data: '') ⇒ Void

Parameters:

  • raw_data (String) (defaults to: '')

    A string of data received from the CONTENTdm API


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

def initialize(raw_data: '')
  @raw_data = raw_data
end

Instance Attribute Details

#raw_dataObject

Returns the value of attribute raw_data


4
5
6
# File 'lib/contentdm_api/response.rb', line 4

def raw_data
  @raw_data
end

Instance Method Details

#parsedHash

A parsed JSON string response that handles cases where the API does not return valid JSON (Response only supports JSON responses for now).

Returns:

  • (Hash)

    A ruby hash of a CONTENTdm response


16
17
18
19
20
21
# File 'lib/contentdm_api/response.rb', line 16

def parsed
  JSON.parse(raw_data)
rescue => e
  # The contentdm_api api spits out HTML when it can't find a collection
  { 'code' => '-2', 'message' => e.to_s }
end