module Eeml # A parser for json environments based on the EEML 5 specification. class JsonEnvironmentParserV005 # :nodoc: include Exceptions def make_environment_from_hash(env_hash) env = Environment.new(:title => env_hash["title"], :description => env_hash["description"], :feed_url => env_hash["feed"], :website => env_hash["website"], :email => env_hash["email"], :icon => env_hash["icon"], :status => env_hash["status"], :identifier => env_hash["id"], :private => env_hash["private"].to_s == "true") env.updated = Time.gm(*ParseDate.parsedate(env_hash['updated'])) unless env_hash['updated'].nil? env.location = buildLocation(env_hash) env.datastreams = buildDatastreams(env_hash) return env end def make_environment_from_json(json_str) env_hash = JSON.parse(strip_content(json_str)) return make_environment_from_hash(env_hash) end private def buildLocation(env_hash) location_hash = env_hash["location"] return if location_hash.nil? Location.new(:name => location_hash["name"], :latitude => location_hash["lat"], :longitude => location_hash["lon"], :elevation => location_hash["ele"], :domain => location_hash["domain"], :disposition => location_hash["disposition"], :exposure => location_hash["exposure"]) end def buildDatastreams(env_hash) datastreams_hash = env_hash["datastreams"] return [] if datastreams_hash.nil? datastreams = [] datastreams_hash.each do |datastream_hash| datastream = DataStream.new raise MissingAttribute.new('id', "data") if datastream_hash['id'].nil? datastream.identifier = datastream_hash['id'] datastream.tags = datastream_hash['tags'] unless datastream_hash['tags'].nil? value_hash = datastream_hash["value"] raise DataMissingValue if value_hash.nil? #raise DataHasMultipleValues if value_hash.size > 1 value = Value.new(:value => value_hash["current_value"], :min_value => value_hash["min_value"], :max_value => value_hash["max_value"]) datastream.add_value(value) unit_hash = datastream_hash["unit"] unless unit_hash.nil? datastream.unit_symbol = unit_hash["symbol"] datastream.unit_type = unit_hash["type"] datastream.unit_value = unit_hash["label"] end datastreams << datastream end return datastreams end end end