module Eeml # A parser for json environments based on the EEML 5 specification. class JsonEnvironmentParserV100 # :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"]) 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(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? values_arr = datastream_hash["values"] raise DataMissingValue if values_arr.nil? values_arr.each do |v| value = Value.new(:value => v["value"], :min_value => datastream_hash["min_value"], :max_value => datastream_hash["max_value"], :recorded_at => v["at"]) datastream.add_value(value) end 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