Sha256: 2e42add5a5c87c92f1d2a2b2e2320bac8e69cc5a34370ccb7c1bb11415afcd6e

Contents?: true

Size: 762 Bytes

Versions: 7

Compression:

Stored size: 762 Bytes

Contents

require 'json'

module RestApiClient

  def self.parse_json(json, opts = {})
    begin
      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)
        return json_data.map { |data| data_type.new data } if data_type
        return json_data unless json_data.empty?

      elsif json_data.kind_of?(Hash)
        return data_type.new json_data if data_type
        return json_data unless json_data.empty?

      else
        return json_data
      end
      return json_response

    rescue Exception => e
      return (opts[:default_return] || nil)
    end
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rest-api-client-0.1.9 lib/rest/api/client/json_parser.rb
rest-api-client-0.1.8 lib/rest/api/client/json_parser.rb
rest-api-client-0.1.7 lib/rest/api/client/json_parser.rb
rest-api-client-0.1.6 lib/rest/api/client/json_parser.rb
rest-api-client-0.1.5 lib/rest/api/client/json_parser.rb
rest-api-client-0.1.4 lib/rest/api/client/json_parser.rb
rest-api-client-0.1.3 lib/rest/api/client/json_parser.rb