Sha256: 216acd39f2823e16971c3a14a1de9d48053c2e3e6f152a5f73e3e4e43b7c49f2

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# The basecamp response parser
class Basecamp3::ResponseParser

  # Parses the given response to the given wrapper model.
  #
  # @param [Hash] response The hash with data which should be parsed to the wrapper model
  # @param [Class] model The class of the model
  #
  # @return [Basecamp3::Model, Array<Basecamp3::Model>, nil]
  # @raise [StandardError] raises an error for unsupported response type
  def self.parse(response, model)
    case response
    when Hash
      map_object(response, model)
    when Array
      response.map do |item|
        map_object(item, model)
      end
    when NilClass
      nil
    else
      raise 'Unsupported response type'
    end
  end

  private

  # Parses the given hash to the given wrapper model.
  #
  # @private
  #
  # @param [Hash] object The hash with data which should be parsed to the wrapper model
  # @param [Class] model The class of the model
  #
  # @return [Basecamp3::Model]
  # @raise [StandardError] raises an error for unsupported model type
  def self.map_object(object, model)
    begin
      model.new(object)
    rescue
      raise 'Unsupported model type. Try to call for a raw response'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
basecamp3-0.1.4 lib/basecamp3/response_parser.rb
basecamp3-0.1.3 lib/basecamp3/response_parser.rb