module GreenButtonData::Utilities

Public Instance Methods

attributes_to_hash(obj) click to toggle source

Returns a hash representation of object instance’s attributes

Arguments

  • obj - an object

# File lib/green-button-data/utilities.rb, line 131
def attributes_to_hash(obj)
  attributes_hash = {}

  obj.instance_variables.each do |var|
    attr_name = var.to_s.delete('@').to_sym
    attributes_hash[attr_name] = obj.instance_variable_get(var)
  end

  return attributes_hash
end
class_from_name(class_name) click to toggle source
# File lib/green-button-data/utilities.rb, line 142
def class_from_name(class_name)
  class_name or raise ArgumentError.new "Class name is required"
  class_name.split('::').inject(Object) { |obj, cls| obj.const_get cls }
end
epoch_to_time(epoch, kwargs = {}) click to toggle source
# File lib/green-button-data/utilities.rb, line 37
def epoch_to_time(epoch, kwargs = {})
  time = Time.at normalize_epoch(epoch)

  if kwargs[:local] == true
    return time.localtime
  else
    return time.utc
  end
end
first_sunday_of(year = Time.now.year, month = Time.now.month) click to toggle source

Retrieves the first Sunday of the month

Arguments

  • year - year

  • month - month

# File lib/green-button-data/utilities.rb, line 54
def first_sunday_of(year = Time.now.year, month = Time.now.month)
  first_day = DateTime.new(year, month, 1)
  first_weekday = first_day.wday

  # If today is Sunday, no offset, otherwise, calculate number of days that
  # need to be added before hitting the first Sunday of month
  day_offset = first_weekday == 0 ? 0 : 7 - first_weekday

  # Return first Sunday of the month
  first_day + day_offset
end
last_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0) click to toggle source

Returns the last weekday in the given month

Arguments

  • year - year

  • month - month

  • weekday - day of week; 0 = Sunday, 6 = Saturday

Examples

To retrieve the last Wednesday of September 2015,

last_weekday_of 2015, 9, 3
# File lib/green-button-data/utilities.rb, line 101
def last_weekday_of(year = Time.now.year, month = Time.now.month,
                    weekday = 0)

  # Get the last day of month
  last_day = DateTime.new year, month, -1
  last_weekday = last_day.wday

  day_offset = if last_weekday >= weekday
    last_weekday - weekday
  else
    7 + last_weekday - weekday
  end

  last_day - weekday_offset(weekday, last_weekday)
end
normalize_epoch(epoch) click to toggle source

Normalizes UNIX epoch ticks to seconds.

If the number of digits for epoch are greater than or equal to 15 decimal digits, it assumes that the epoch is in microseconds.

If the number of digits for epoch are less than 15 but greater than or equal to 13 decimal digits, it assumes that epoch is in milliseconds.

Less than 13 digits is assumed to be in seconds.

Arguments

  • epoch - Amount of seconds/milliseconds/microseconds since 1970-01-01

# File lib/green-button-data/utilities.rb, line 27
def normalize_epoch(epoch)
  if epoch.num_digits >= 15
    epoch / 100000
  elsif epoch.num_digits >= 13
    epoch / 1000
  else
    epoch
  end
end
nth_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0, week = 1) click to toggle source

Returns the Nth weekday in the given month

Arguments

  • year - year

  • month - month

  • weekday - day of week; 0 = Sunday, 6 = Saturday

  • week - Nth week of month

Examples

To retrieve third Friday of July 2015,

nth_weekday_of 2015, 7, 5, 3
# File lib/green-button-data/utilities.rb, line 80
def nth_weekday_of(year = Time.now.year, month = Time.now.month,
                   weekday = 0, week = 1)
  first_day = DateTime.new year, month, 1

  # Day offset needed for Nth day of the week
  first_day + weekday_offset(first_day.wday, weekday) + (week - 1) * 7
end
parse_datetime(string) click to toggle source
# File lib/green-button-data/utilities.rb, line 4
def parse_datetime(string)
  begin
    DateTime.parse(string).utc
  rescue
    warn "Parsing failed for string: #{string.inspect}"
    nil
  end
end
weekday_offset(first_weekday, second_weekday) click to toggle source
# File lib/green-button-data/utilities.rb, line 117
def weekday_offset(first_weekday, second_weekday)
  if second_weekday >= first_weekday
    second_weekday - first_weekday
  else
    7 + second_weekday - first_weekday
  end
end