Module: GreenButtonData::Utilities
- Included in:
- ApplicationInformation, Authorization, Dst, Entry, Fetchable::ClassMethods, IntervalBlock, Parser::ApplicationInformation, Parser::Authorization, Parser::Entry, Parser::Feed, Parser::Interval, Parser::LocalTimeParameters, Parser::UsageSummary, UsageSummary
- Defined in:
- lib/green-button-data/utilities.rb
Instance Method Summary collapse
-
#attributes_to_hash(obj) ⇒ Object
Returns a hash representation of object instance’s attributes.
- #class_from_name(class_name) ⇒ Object
- #epoch_to_time(epoch, kwargs = {}) ⇒ Object
-
#first_sunday_of(year = Time.now.year, month = Time.now.month) ⇒ Object
Retrieves the first Sunday of the month.
-
#last_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0) ⇒ Object
Returns the last weekday in the given month.
-
#normalize_epoch(epoch) ⇒ Object
Normalizes UNIX
epochticks to seconds. -
#nth_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0, week = 1) ⇒ Object
Returns the Nth weekday in the given month.
- #parse_datetime(string) ⇒ Object
- #weekday_offset(first_weekday, second_weekday) ⇒ Object
Instance Method Details
#attributes_to_hash(obj) ⇒ Object
Returns a hash representation of object instance’s attributes
Arguments
-
obj- an object
131 132 133 134 135 136 137 138 139 140 |
# 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) ⇒ Object
142 143 144 145 |
# 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 = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# 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) ⇒ Object
Retrieves the first Sunday of the month
Arguments
-
year- year -
month- month
54 55 56 57 58 59 60 61 62 63 64 |
# 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) ⇒ Object
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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# 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) ⇒ Object
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
27 28 29 30 31 32 33 34 35 |
# 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) ⇒ Object
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
80 81 82 83 84 85 86 |
# 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) ⇒ Object
4 5 6 7 8 9 10 11 |
# 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) ⇒ Object
117 118 119 120 121 122 123 |
# 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 |