Sha256: fa99f565a04e24a67c1806c77510f1a726290926ffe5dedcbb1baf19317ea7c8

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module CartoJson
  class Parser
    def self.parse(input)
      p = new input
      p.parse
    end

    def initialize(input)
      @input = input
      @parsed = nil
    end

    def parse(input=@input)
      return nil if input.nil?

      case input
      when Array
        @parsed = []
        input.each {|element| @parsed << parse_element(element) }

      when Hash
        input = Utils.symbolize_keys(input)
        @parsed = parse_element input

      when String
        begin
          decoded = MultiJson.decode(input, :symbolize_keys => true)
        rescue MultiJson::DecodeError => e
          raise InputError, "unable to decode JSON: \"#{e.message}\""
        end
        @parsed = parse decoded
      else
        raise InputError, "expected Array, Hash or String, you provided #{input.class}"
      end

      @parsed
    end

    def parse_element(element)
      if element[:type].nil?
        if element[LAT] && element[LNG]
          element[:type] = 'point'
        else
          raise InputError, "cannot determine type for the provided element (type is missing or #{LAT}/#{LNG} are not present)"
        end
      end

      unless TYPES.include? element[:type].to_sym
        raise InvalidTypeError, "unsupported type: \"#{element[:type]}\", supported types: #{TYPES.join(', ')}"
      end

      CartoJson.const_get(element[:type].capitalize).parse element
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carto_json-0.0.6 lib/carto_json/parser.rb