class GreenButtonData::Entry

Attributes

id[R]
token[RW]
usage_point_id[R]

Public Class Methods

new(attributes) click to toggle source
# File lib/green-button-data/entry.rb, line 9
def initialize(attributes)
  # Automagically sets instance variables from attribute hash parsed from
  # the GreenButtonData::Parser classes
  init_instance_vars attributes

  # Handle relations via related_urls
  @related_urls.is_a?(Hash) and @related_urls.each do |key, value|
    self.instance_variable_set :"@#{key}_url", value
    singleton_class.class_eval do
      attr_accessor "#{key}_url".to_sym
    end

    # Define accessor methods from pluralized resource names
    define_attr_accessors key
  end
end

Protected Instance Methods

get_enum_symbol(enum, value) click to toggle source
# File lib/green-button-data/entry.rb, line 28
def get_enum_symbol(enum, value)
  if value.is_a? Numeric
    enum[value]
  else
    value
  end
end

Private Instance Methods

define_attr_accessors(resource) click to toggle source
# File lib/green-button-data/entry.rb, line 44
def define_attr_accessors(resource)
  self.class.send :define_method, "#{resource.to_s.pluralize}" do |*args|
    id = args[0]
    options = args[1]

    # Make the ID argument optional
    options ||= if id.is_a?(Hash)
      id
    else
      {}
    end

    if id.is_a?(Numeric) || id.is_a?(String) || id.is_a?(Symbol)
      get_or_fetch_entry id, resource, options
    else
      get_or_fetch_collection resource, options
    end
  end
end
get_or_fetch_collection(resource, options = {}) click to toggle source
# File lib/green-button-data/entry.rb, line 70
def get_or_fetch_collection(resource, options = {})
  klazz = class_from_name klazz_name(resource.to_s.camelize)
  url = self.instance_variable_get "@#{resource}_url"
  collection = self.instance_variable_get "@#{resource.to_s.pluralize}"

  collection = if !options[:reload] && collection
    collection
  else
    collection = klazz.all url, options
  end

  self.instance_variable_set :"@#{resource.to_s.pluralize}", collection

  return collection
end
get_or_fetch_entry(id, resource, options = {}) click to toggle source
# File lib/green-button-data/entry.rb, line 86
def get_or_fetch_entry(id, resource, options = {})
  klazz = class_from_name klazz_name(resource.to_s.camelize)
  url = self.instance_variable_get "@#{resource}_url"
  collection = self.instance_variable_get "@#{resource.to_s.pluralize}"

  # Try returning cached results first
  collection and instance = collection.find_by_id(id)
  cache_miss = instance.nil?

  # On cache miss or forced reload, send API request
  instance = if !options[:reload] && instance
    instance
  else
    klazz.find "#{url}/#{id}", options
  end

  # Cache the result
  collection ||= ModelCollection.new
  collection << instance if cache_miss

  self.instance_variable_set :"@#{resource.to_s.pluralize}", collection

  instance
end
init_instance_vars(attributes) click to toggle source
# File lib/green-button-data/entry.rb, line 38
def init_instance_vars(attributes)
  attributes.each do |key, value|
    self.instance_variable_set :"@#{key}", value
  end
end
klazz_name(name) click to toggle source
# File lib/green-button-data/entry.rb, line 64
def klazz_name(name)
  str = "GreenButtonData::#{name}"
  str.gsub! /ElectricPowerUsageSummary/, 'UsageSummary'
  return str
end