Class: CartoJson::Parser
- Inherits:
-
Object
- Object
- CartoJson::Parser
- Defined in:
- lib/carto_json/parser.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(input) ⇒ Parser
constructor
A new instance of Parser.
- #parse(input = @input) ⇒ Object
- #parse_element(element) ⇒ Object
Constructor Details
#initialize(input) ⇒ Parser
Returns a new instance of Parser.
8 9 10 11 |
# File 'lib/carto_json/parser.rb', line 8 def initialize(input) @input = input @parsed = nil end |
Class Method Details
.parse(input) ⇒ Object
3 4 5 6 |
# File 'lib/carto_json/parser.rb', line 3 def self.parse(input) p = new input p.parse end |
Instance Method Details
#parse(input = @input) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/carto_json/parser.rb', line 13 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.}\"" end @parsed = parse decoded else raise InputError, "expected Array, Hash or String, you provided #{input.class}" end @parsed end |
#parse_element(element) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/carto_json/parser.rb', line 39 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.new(element[:type].to_sym, "unsupported type: \"#{element[:type]}\", supported types: #{TYPES.join(', ')}") end class_name = element[:type] == 'linestring' ? 'LineString' : element[:type].capitalize CartoJson.const_get(class_name).parse element end |