Sha256: 0cee07dc756e60307a03ee31c3545732d3fa46a920d46cf3e834859f3f3fea44

Contents?: true

Size: 1.75 KB

Versions: 9

Compression:

Stored size: 1.75 KB

Contents

# Copyright (C) 2015 TopCoder Inc., All Rights Reserved.
require 'sax-machine'
require 'ce-greenbutton/elements/gb_content'
module GreenButton
  module Parser
    # a sax-machine mapping for the Entry structure
    #
    # For example:
    #   entry = GbEntry.parse(open(entry.xml))
    #
    #
    # Author: ahmed.seddiq
    # Version: 1.0
    class GbEntry
      include SAXMachine

      # The related entries. It is a hash {entry_type => entry_url}
      #   entry_urls are the href value of the link element with type "related"
      attr_accessor :related

      # The entry type, e.g. UsagePoint, IntervalBlock,...etc
      attr_accessor :type

      # Extract the href of the parent Entry
      element :link, as: :up, value: :href,  with: {rel: 'up'}

      # Extract the href of this Entry
      # The type is inferred from this href
      element :link, as: :self, value: :href,  with: {rel: 'self'} do |url|
        self.type = entry_type url
        url
      end

      # Extract the related Entries to the :related hash.
      elements :link, value: :href,  with: {rel: 'related'} do |url|
        if self.related.nil?
          self.related = {}
        end
        type = entry_type url
        unless type.nil?
          self.related[type] = url
        end
      end

      element :content, class: GbContent

      element :updated do |updated|
        DateTime.parse(updated).to_time
      end

      # Helper method to infer the entry type from the given url.
      #
      # url - the url
      #
      # Returns the entry type, e.g. UsagePoint, IntervalBlock,...etc
      private
      def entry_type(url)
        match = /\/(\w+)(\/(\d+))*$/.match(url)
        if match.nil?
          nil
        else
          match[1]
        end
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ce-greenbutton-0.1.8 lib/ce-greenbutton/elements/gb_entry.rb
ce-greenbutton-0.1.7 lib/ce-greenbutton/elements/gb_entry.rb
ce-greenbutton-0.1.6 lib/ce-greenbutton/elements/gb_entry.rb
ce-greenbutton-0.1.5 lib/ce-greenbutton/elements/gb_entry.rb
ce-greenbutton-0.1.4 lib/ce-greenbutton/elements/gb_entry.rb
ce-greenbutton-0.1.3 lib/ce-greenbutton/elements/gb_entry.rb
ce-greenbutton-0.1.2 lib/ce-greenbutton/elements/gb_entry.rb
ce-greenbutton-0.1.1 lib/ce-greenbutton/elements/gb_entry.rb
ce-greenbutton-0.1.0 lib/ce-greenbutton/elements/gb_entry.rb