Class: Hibp::Parsers::Json

Inherits:
Object
  • Object
show all
Includes:
Helpers::JsonConversion
Defined in:
lib/hibp/parsers/json.rb

Overview

Hibp::Parsers::Json

Used to parse API JSON response and transform(convert) this
  raw data to the model specified by converter

Direct Known Subclasses

Breach, Paste

Instance Method Summary collapse

Instance Method Details

#parse_response(response) {|attributes| ... } ⇒ Object

Note:

If block with conversion not set than parser returns raw data (useful in cases when response returns array of strings)

Parse API response

Parameters:

  • response (Faraday::Response)

    - Response that contains raw data for conversion

Yields:

  • (attributes)
    • (optional, default: nil)

    Converter that used to convert complex raw response data to the particular model representation.

Raises:



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hibp/parsers/json.rb', line 27

def parse_response(response, &block)
  return nil if empty_response?(response)

  begin
    _, body = prepare_response(response)

    block_given? ? convert(body, &block) : body
  rescue Oj::ParseError
    raise_error(response.body)
  end
end