Class: Beeminder::Datapoint

Inherits:
Object
  • Object
show all
Defined in:
lib/beeminder/goals.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info = {}) ⇒ Datapoint

Returns a new instance of Datapoint.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/beeminder/goals.rb', line 248

def initialize info={}
  # set variables
  info.each do |k,v|
    instance_variable_set "@#{k}", v
  end

  # defaults
  @timestamp ||= DateTime.now
  @comment   ||= ""

  # some conversions
  @timestamp  = DateTime.strptime(@timestamp.to_s,  '%s') unless @timestamp.is_a?(Date) || @timestamp.is_a?(Time)
  @updated_at = DateTime.strptime(@updated_at.to_s, '%s') unless @updated_at.nil?

  # set timezone if possible
  unless @goal.nil?
    @timestamp  = @timestamp.in_time_zone  @goal.user.timezone
    @updated_at = @updated_at.in_time_zone @goal.user.timezone
  end
end

Instance Attribute Details

#commentString

Returns An optional comment about the datapoint.

Returns:

  • (String)

    An optional comment about the datapoint.



236
237
238
# File 'lib/beeminder/goals.rb', line 236

def comment
  @comment
end

#goalBeeminder::Goal

Returns Goal this datapoint belongs to. Optional for new datapoints. Use ‘Goal#add` to add new datapoints to a goal.

Returns:

  • (Beeminder::Goal)

    Goal this datapoint belongs to. Optional for new datapoints. Use ‘Goal#add` to add new datapoints to a goal.



246
247
248
# File 'lib/beeminder/goals.rb', line 246

def goal
  @goal
end

#idString (readonly)

Returns A unique ID, used to identify a datapoint when deleting or editing it.

Returns:

  • (String)

    A unique ID, used to identify a datapoint when deleting or editing it.



239
240
241
# File 'lib/beeminder/goals.rb', line 239

def id
  @id
end

#timestampDateTime

Returns Time of the datapoint.

Returns:

  • (DateTime)

    Time of the datapoint.



230
231
232
# File 'lib/beeminder/goals.rb', line 230

def timestamp
  @timestamp
end

#updated_atDateTime (readonly)

Returns The time that this datapoint was entered or last updated.

Returns:

  • (DateTime)

    The time that this datapoint was entered or last updated.



242
243
244
# File 'lib/beeminder/goals.rb', line 242

def updated_at
  @updated_at
end

#valueNumeric

Returns Value of the datapoint.

Returns:

  • (Numeric)

    Value of the datapoint.



233
234
235
# File 'lib/beeminder/goals.rb', line 233

def value
  @value
end

Instance Method Details

#to_hashHash

Convert datapoint to hash for POSTing.

Returns:

  • (Hash)


271
272
273
274
275
276
277
278
279
280
281
# File 'lib/beeminder/goals.rb', line 271

def to_hash
  if not @goal.nil? and @goal.user.enforce_timezone?
    @timestamp = @goal.user.convert_to_timezone @timestamp
  end
  
  {
    "timestamp" => @timestamp.strftime('%s'),
    "value"     => @value || 0,
    "comment"   => @comment || "",
  }
end