Class: EEML::Environment

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Create new Environment object



13
14
15
# File 'lib/eeml/environment.rb', line 13

def initialize
  @data_items = []
end

Instance Attribute Details

#creatorObject

A string showing the creator of this environment object



238
239
240
# File 'lib/eeml/environment.rb', line 238

def creator
  @creator
end

#descriptionObject

A description of this EEML feed



199
200
201
# File 'lib/eeml/environment.rb', line 199

def description
  @description
end

#emailObject

The email address of the feed creator



208
209
210
# File 'lib/eeml/environment.rb', line 208

def email
  @email
end

#feedObject

The URL for this EEML feed



184
185
186
# File 'lib/eeml/environment.rb', line 184

def feed
  @feed
end

#iconObject

The URL of an icon for this feed



202
203
204
# File 'lib/eeml/environment.rb', line 202

def icon
  @icon
end

#idObject

The ID of the environment object



241
242
243
# File 'lib/eeml/environment.rb', line 241

def id
  @id
end

#locationObject

The location of the feed



211
212
213
# File 'lib/eeml/environment.rb', line 211

def location
  @location
end

#titleObject

The title for this EEML feed



181
182
183
# File 'lib/eeml/environment.rb', line 181

def title
  @title
end

#updated_atObject

Last updated time



222
223
224
# File 'lib/eeml/environment.rb', line 222

def updated_at
  @updated_at
end

#websiteObject

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.tags << 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

Raises:

  • (TypeError)


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

#sizeObject

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

#statusObject

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!
    eeml_options = {: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"}
    eeml_options[:version] = version if version
    eeml.eeml(eeml_options) do
      env_options = {}
      env_options[:updated] = @updated_at.xmlschema if @updated_at
      env_options[:creator] = @creator if @creator
      env_options[:id] = @id if @id
      eeml.environment(env_options) 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
          loc_options = {}
          loc_options[:domain] = @location.domain
          loc_options[:exposure] = @location.exposure if @location.exposure
          loc_options[:disposition] = @location.disposition if @location.disposition
          env.location(loc_options) 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].tags.each do |tag|
              data.tag tag
            end
            value_options = {}
            value_options[:maxValue] = @data_items[i].max_value if @data_items[i].max_value
            value_options[:minValue] = @data_items[i].min_value if @data_items[i].min_value
            data.value @data_items[i].value, value_options
            if @data_items[i].unit
              unit_options = {}
              unit_options[:symbol] = @data_items[i].unit.symbol if @data_items[i].unit.symbol
              unit_options[:type] = @data_items[i].unit.type if @data_items[i].unit.type
              data.unit @data_items[i].unit.name, unit_options
            end
          end
        end
      end
    end
  end
end