Module: GreenButton::Parser
- Defined in:
- lib/ce-greenbutton/parser.rb,
lib/ce-greenbutton/elements/gb_entry.rb,
lib/ce-greenbutton/elements/gb_content.rb,
lib/ce-greenbutton/elements/gb_interval.rb,
lib/ce-greenbutton/elements/gb_data_feed.rb,
lib/ce-greenbutton/elements/gb_usage_point.rb,
lib/ce-greenbutton/elements/gb_reading_type.rb,
lib/ce-greenbutton/elements/gb_interval_block.rb,
lib/ce-greenbutton/elements/gb_interval_reading.rb,
lib/ce-greenbutton/elements/gb_local_time_parameters.rb,
lib/ce-greenbutton/elements/gb_application_information.rb
Overview
Public: Defines a parse method. It accepts both strings and io inputs.
Author ahmed.seddiq Version: 1.0
Defined Under Namespace
Classes: GbApplicationInformation, GbContent, GbDataFeed, GbEntry, GbInterval, GbIntervalBlock, GbIntervalReading, GbLocalTimeParameters, GbReadingType, GbUsagePoint
Class Method Summary collapse
-
.parse(input) ⇒ Object
Public: Parses the given input to an instance of GbDataFeed.
Class Method Details
.parse(input) ⇒ Object
Public: Parses the given input to an instance of GbDataFeed. After parsing the GbDataFeed.entries will contain a hash of defined GbEntries. The key of the hash is the “self” link identifies each entry. the “up” links are also added to the hash with an array value aggregating all sub entries.
For example:
if we have a feed with one UsagePoint, one MeterReading, three IntervalBlocks
the entries hash will be some thing like
{"http://url.of.usage.point.self" => GbEntry(of type UsagePoint)}
{"http://url.of.usage.point.up" => [GbEntry(of type UsagePoint)]}
{"http://url.of.meter.reading.self" => GbEntry(of type MeterReading)}
{"http://url.of.meter.reading.up" => [GbEntry(of type MeterReading)]}
{"http://url.of.interval.block.1.self" => GbEntry(of type IntervalBlock)}
{"http://url.of.interval.block.2.self" => GbEntry(of type IntervalBlock)}
{"http://url.of.interval.block.3.self" => GbEntry(of type IntervalBlock)}
{"http://url.of.interval.block.[1|2|3].up" =>
[GbEntry(of type IntervalBlock), GbEntry(of type IntervalBlock),
GbEntry(of type IntervalBlock)]
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ce-greenbutton/parser.rb', line 30 def self.parse(input) feed = Parser::GbDataFeed.parse(input) entries = {} feed.entries.each do |entry| # map self link to this entry entries[entry.self] = entry # add this entry to the up link # the "up" link will be the "self" link after removing the id part # if there is no id part in the "self" link, the "up" link is used. up_link = /(.*)\/\d+$/.match(entry.self)[1] up_link = entry.up if up_link.nil? if entries[up_link].nil? entries[up_link] = [] end entries[up_link] << entry entry.link = nil end feed.entries = entries feed end |