module Eeml class JsonOutput # :nodoc: def to_json(environment, options = { :full => true }) hash = to_public_json_data(environment, options) hash.to_json end def to_public_json_data(environment, options = { :full => true }) environment_hash = { :id => Integer(environment.identifier), #we WANT this to fail early when we start using non-numeric identifiers :title => environment.title, :feed => environment.feed_url, :status => environment.status, :description => environment.description, :website => environment.website, :email => environment.email, :icon => environment.icon, :version => EEML_VERSION} environment_hash[:updated] = environment.updated.strftime("%Y-%m-%dT%H:%M:%SZ") unless environment.updated.nil? #TODO: was retrieved_at environment_hash.delete_if { |key, value| value.blank? } unless environment.location.nil? # add location data location_hash = { :domain => environment.location.domain, :exposure => environment.location.exposure, :disposition => environment.location.disposition, :name => environment.location.name, :ele => environment.location.elevation #currently, ele is output as a string } location_hash[:lat] = Float(environment.location.latitude) if environment.location.latitude location_hash[:lon] = Float(environment.location.longitude) if environment.location.longitude location_hash.delete_if { |key, value| value.blank? } environment_hash[:location] = location_hash unless location_hash.empty? end # if :full option given, add our datastream data into the returned json if options[:full] == true datastream_array = [] # add datastream data environment.datastreams.each do |datastream| datastream_array << datastream_to_json(datastream) end environment_hash[:datastreams] = datastream_array unless datastream_array.empty? end return environment_hash end protected def datastream_to_json(datastream) datastream_hash = { :id => datastream.identifier } datastream_hash[:tags] = datastream.tags unless datastream.tags.empty? value_hash = { :min_value => datastream.min_value.to_s, :max_value => datastream.max_value.to_s, :current_value => datastream.value.to_s } value_hash.delete_if { |key, value| value.blank? } datastream_hash[:value] = value_hash unless value_hash.empty? unit_hash = { :type => datastream.unit_type.to_s, :symbol => datastream.unit_symbol.to_s, :label => datastream.unit_value.to_s } unit_hash.delete_if { |key, value| value.blank? } datastream_hash[:unit] = unit_hash unless unit_hash.empty? return datastream_hash end end end