Class: EEML::Environment
- Inherits:
-
Object
- Object
- EEML::Environment
- Defined in:
- lib/eeml/environment.rb
Overview
An EEML environment object. Can contain a number of EEML::Data objects, as well as having other attributes.
Instance Attribute Summary collapse
-
#creator ⇒ Object
A string showing the creator of this environment object.
-
#description ⇒ Object
A description of this EEML feed.
-
#email ⇒ Object
The email address of the feed creator.
-
#feed ⇒ Object
The URL for this EEML feed.
-
#icon ⇒ Object
The URL of an icon for this feed.
-
#id ⇒ Object
The ID of the environment object.
-
#location ⇒ Object
The location of the feed.
-
#title ⇒ Object
The title for this EEML feed.
-
#updated_at ⇒ Object
Last updated time.
-
#website ⇒ Object
The URL of a website related to this feed.
Class Method Summary collapse
-
.from_eeml(eeml) ⇒ Object
Create from an EEML document.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Add an EEML::Data object to the environment.
-
#[](index) ⇒ Object
Access contained EEML::Data objects.
-
#initialize ⇒ Environment
constructor
Create new Environment object.
-
#set_updated! ⇒ Object
Set last updated time.
-
#size ⇒ Object
The number of EEML::Data objects in the environment.
-
#status ⇒ Object
The status of this EEML feed - can be :frozen or :live.
-
#status=(val) ⇒ Object
The status of this EEML feed - can be :frozen or :live.
-
#to_eeml(version = nil) ⇒ Object
Convert to EEML.
Constructor Details
#initialize ⇒ Environment
Create new Environment object
13 14 15 |
# File 'lib/eeml/environment.rb', line 13 def initialize @data_items = [] end |
Instance Attribute Details
#creator ⇒ Object
A string showing the creator of this environment object
238 239 240 |
# File 'lib/eeml/environment.rb', line 238 def creator @creator end |
#description ⇒ Object
A description of this EEML feed
199 200 201 |
# File 'lib/eeml/environment.rb', line 199 def description @description end |
#email ⇒ Object
The email address of the feed creator
208 209 210 |
# File 'lib/eeml/environment.rb', line 208 def email @email end |
#feed ⇒ Object
The URL for this EEML feed
184 185 186 |
# File 'lib/eeml/environment.rb', line 184 def feed @feed end |
#icon ⇒ Object
The URL of an icon for this feed
202 203 204 |
# File 'lib/eeml/environment.rb', line 202 def icon @icon end |
#id ⇒ Object
The ID of the environment object
241 242 243 |
# File 'lib/eeml/environment.rb', line 241 def id @id end |
#location ⇒ Object
The location of the feed
211 212 213 |
# File 'lib/eeml/environment.rb', line 211 def location @location end |
#title ⇒ Object
The title for this EEML feed
181 182 183 |
# File 'lib/eeml/environment.rb', line 181 def title @title end |
#updated_at ⇒ Object
Last updated time
222 223 224 |
# File 'lib/eeml/environment.rb', line 222 def updated_at @updated_at end |
#website ⇒ Object
The URL of a website related to this feed
205 206 207 |
# File 'lib/eeml/environment.rb', line 205 def website @website end |
Class Method Details
.from_eeml(eeml) ⇒ Object
Create from an EEML document
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/eeml/environment.rb', line 18 def self.from_eeml(eeml) # Parse EEML doc = REXML::Document.new(eeml) # Create environment object env = Environment.new # Read data items REXML::XPath.each(doc, "/eeml/environment/data") do |node| data = EEML::Data.new(node.elements['value'].text.to_f) # Read data tags REXML::XPath.each(node, "tag") do |tag| text = tag.text if(tag.text) data. << text end data.max_value = node.elements["value"].attributes["maxValue"] if(node.elements["value"].attributes["maxValue"]) data.min_value = node.elements["value"].attributes["minValue"] if(node.elements["value"].attributes["minValue"]) # Load tags into data object env << data end # Parse attributes # Read creator env.creator = doc.root.elements["environment"].attributes["creator"] if(doc.root.elements["environment"].attributes["creator"]) # Read id env.id = doc.root.elements["environment"].attributes["id"] if(doc.root.elements["environment"].attributes["creator"]) # Read created env.updated_at = Time.parse(doc.root.elements["environment"].attributes["updated"]) if(doc.root.elements["environment"].attributes["updated"]) # Parse environment elements # Read title env.title = doc.root.elements["//title"].text if(doc.root.elements["//title"]) # Read description env.description = doc.root.elements["//description"].text if(doc.root.elements["//description"]) # Read email env.email = doc.root.elements["//email"].text if(doc.root.elements["//email"]) # Read feed env.feed = doc.root.elements["//feed"].text if(doc.root.elements["//feed"]) # Read icon env.icon = doc.root.elements["//icon"].text if(doc.root.elements["//icon"]) # Read website env.website = doc.root.elements["//website"].text if(doc.root.elements["//website"]) # Get the location element if (doc.root.elements["///location"]) l_element = doc.root.elements["///location"] # Get the domain - :physical or :virtual if(l_element.attributes["domain"] == "physical") loc = EEML::Location.new(:physical) else loc = EEML::Location.new(:virtual) end # Get the exposure - :indoor or :outdoor if(l_element.attributes["exposure"] == "indoor") loc.exposure = :indoor else loc.exposure = :outdoor end # Get the disposition - :fixed or :mobile if(l_element.attributes["disposition"] == "mobile") loc.disposition = :mobile else loc.disposition = :fixed end # Get the location elements loc.name = doc.root.elements["///name"].text loc.lat = doc.root.elements["///lat"].text.to_i loc.lon = doc.root.elements["///lon"].text.to_i loc.ele = doc.root.elements["///ele"].text.to_f env.location = loc end # Done return env end |
Instance Method Details
#<<(value) ⇒ Object
Add an EEML::Data object to the environment
165 166 167 168 |
# File 'lib/eeml/environment.rb', line 165 def <<(value) raise TypeError.new('Only EEML::Data objects can be added to EEML::Environment objects') unless value.is_a?(EEML::Data) @data_items << value end |
#[](index) ⇒ Object
Access contained EEML::Data objects
176 177 178 |
# File 'lib/eeml/environment.rb', line 176 def [](index) @data_items[index] end |
#set_updated! ⇒ Object
Set last updated time
233 234 235 |
# File 'lib/eeml/environment.rb', line 233 def set_updated! @updated_at = Time.now end |
#size ⇒ Object
The number of EEML::Data objects in the environment
171 172 173 |
# File 'lib/eeml/environment.rb', line 171 def size @data_items.size end |
#status ⇒ Object
The status of this EEML feed - can be :frozen or :live
187 188 189 |
# File 'lib/eeml/environment.rb', line 187 def status @status end |
#status=(val) ⇒ Object
The status of this EEML feed - can be :frozen or :live
191 192 193 194 195 196 |
# File 'lib/eeml/environment.rb', line 191 def status=(val) unless [nil, :frozen, :live].include?(val) raise ArgumentError.new("Status must be :frozen or :live") end @status = val end |
#to_eeml(version = nil) ⇒ Object
Convert to EEML. Optional parameter describes the version of EEML to generate. Default (and currently only version implemented) is version 5.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/eeml/environment.rb', line 104 def to_eeml(version = nil) if version.nil? || version == 5 # Check that we have some data items if size < 1 raise EEML::NoData.new('EEML requires at least one data item') end # Create EEML eeml = Builder::XmlMarkup.new eeml.instruct! = {:xmlns => "http://www.eeml.org/xsd/005", :'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", :'xsi:schemaLocation' => "http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd"} [:version] = version if version eeml.eeml() do = {} [:updated] = @updated_at.xmlschema if @updated_at [:creator] = @creator if @creator [:id] = @id if @id eeml.environment() do |env| env.title @title if @title env.feed @feed if @feed env.status @status.to_s if @status env.description @description if @description env.icon @icon if @icon env.website @website if @website env.email @email if @email if @location = {} [:domain] = @location.domain [:exposure] = @location.exposure if @location.exposure [:disposition] = @location.disposition if @location.disposition env.location() do |loc| loc.name @location.name if @location.name loc.lat @location.lat if @location.lat loc.lon @location.lon if @location.lon loc.ele @location.ele if @location.ele end end @data_items.each_index do |i| env.data(:id => @data_items[i].id || i) do |data| @data_items[i]..each do |tag| data.tag tag end = {} [:maxValue] = @data_items[i].max_value if @data_items[i].max_value [:minValue] = @data_items[i].min_value if @data_items[i].min_value data.value @data_items[i].value, if @data_items[i].unit = {} [:symbol] = @data_items[i].unit.symbol if @data_items[i].unit.symbol [:type] = @data_items[i].unit.type if @data_items[i].unit.type data.unit @data_items[i].unit.name, end end end end end end end |