Sha256: 0fee56cf91cb7beb672d35fb5c82cf9dd054033263754aa1adb40f4efa7d0aa4

Contents?: true

Size: 563 Bytes

Versions: 1

Compression:

Stored size: 563 Bytes

Contents

require 'json'

module RestApiClient

  def self.parse_json(json, opts = {})
    json_response = JSON.parse json

    data_type = opts[:type]

    json_data = {}
    if json_response.kind_of?(Hash) && json_response.has_key?('data')
      json_data = json_response['data']
    end

    if json_data.kind_of?(Array) && data_type
      return json_data.map { |data| data_type.new data }
    elsif json_data.kind_of?(Hash) && data_type
      return data_type.new json_data
    else
      return json_data unless json_data.empty?
    end

    json_response
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rest-api-client-0.1.0 lib/rest/api/client/json_parser.rb