Class: Beeminder::Goal

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, name_or_info) ⇒ Goal

Returns a new instance of Goal.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/beeminder/goals.rb', line 75

def initialize user, name_or_info
  @user = user

  info =
    case name_or_info
    when String
      @user.get "users/me/goals/#{name_or_info}.json"
    when Hash
      name_or_info
    else
      raise ArgumentError, "`name_or_info` must be String (slug) or Hash (preloaded info)"
    end
  
  _parse_info info
end

Instance Attribute Details

#curdayDateTime (readonly)

Returns Date of the most recent data point.

Returns:

  • (DateTime)

    Date of the most recent data point.



24
25
26
# File 'lib/beeminder/goals.rb', line 24

def curday
  @curday
end

#curvalNumeric (readonly)

Returns Value of the most recent data point.

Returns:

  • (Numeric)

    Value of the most recent data point.



21
22
23
# File 'lib/beeminder/goals.rb', line 21

def curval
  @curval
end

#datapublictrue|false

Returns Whether you have to be signed in as the owner of the goal to view the datapoints.

Returns:

  • (true|false)

    Whether you have to be signed in as the owner of the goal to view the datapoints.



58
59
60
# File 'lib/beeminder/goals.rb', line 58

def datapublic
  @datapublic
end

#ephemtrue|false

Returns Whether the graph was created in test mode.

Returns:

  • (true|false)

    Whether the graph was created in test mode.



52
53
54
# File 'lib/beeminder/goals.rb', line 52

def ephem
  @ephem
end

#frozentrue|false (readonly)

Returns Whether the goal is currently frozen and therefore must be restarted before continuing to accept data.

Returns:

  • (true|false)

    Whether the goal is currently frozen and therefore must be restarted before continuing to accept data.



67
68
69
# File 'lib/beeminder/goals.rb', line 67

def frozen
  @frozen
end

#goal_typeSymbol (readonly)

Returns One of the following symbols:

  • ‘:fatloser`: Weight loss

  • ‘:hustler`: Do More

  • ‘:biker`: Odometer

  • ‘:inboxer`: Inbox Fewer

  • ‘:gainer`: Gain Weight

  • ‘:drinker`: Set a Limit

  • ‘:custom`: Full access to the underlying goal parameters.

Returns:

  • (Symbol)

    One of the following symbols:

    • ‘:fatloser`: Weight loss

    • ‘:hustler`: Do More

    • ‘:biker`: Odometer

    • ‘:inboxer`: Inbox Fewer

    • ‘:gainer`: Gain Weight

    • ‘:drinker`: Set a Limit

    • ‘:custom`: Full access to the underlying goal parameters



37
38
39
# File 'lib/beeminder/goals.rb', line 37

def goal_type
  @goal_type
end

#goaldateDateTime (readonly)

Returns Goal date.

Returns:

  • (DateTime)

    Goal date.



15
16
17
# File 'lib/beeminder/goals.rb', line 15

def goaldate
  @goaldate
end

#goalvalNumeric (readonly)

Returns Goal value.

Returns:

  • (Numeric)

    Goal value.



18
19
20
# File 'lib/beeminder/goals.rb', line 18

def goalval
  @goalval
end

#graph_urlString (readonly)

Returns URL for the goal’s graph image.

Returns:

  • (String)

    URL for the goal’s graph image.



43
44
45
# File 'lib/beeminder/goals.rb', line 43

def graph_url
  @graph_url
end

#laneNumeric (readonly)

Returns Where you are with respect to the yellow brick road (2 or more = above the road, 1 = top lane, -1 = bottom lane, -2 or less = below the road).

Returns:

  • (Numeric)

    Where you are with respect to the yellow brick road (2 or more = above the road, 1 = top lane, -1 = bottom lane, -2 or less = below the road).



70
71
72
# File 'lib/beeminder/goals.rb', line 70

def lane
  @lane
end

#losedateDateTime (readonly)

Returns Date of derailment.

Returns:

  • (DateTime)

    Date of derailment.



40
41
42
# File 'lib/beeminder/goals.rb', line 40

def losedate
  @losedate
end

#panicNumeric

Returns Panic threshold. How long before derailment to panic.

Returns:

  • (Numeric)

    Panic threshold. How long before derailment to panic.



46
47
48
# File 'lib/beeminder/goals.rb', line 46

def panic
  @panic
end

#queuedtrue|false (readonly)

Returns Whether the graph is currently being updated to reflect new data.

Returns:

  • (true|false)

    Whether the graph is currently being updated to reflect new data.



49
50
51
# File 'lib/beeminder/goals.rb', line 49

def queued
  @queued
end

#rateNumeric (readonly)

Returns The slope of the (final section of the) yellow brick road.

Returns:

  • (Numeric)

    The slope of the (final section of the) yellow brick road.



27
28
29
# File 'lib/beeminder/goals.rb', line 27

def rate
  @rate
end

#roadallArray<Integer, Float, Float>

Returns All road settings over time.

Returns:

  • (Array<Integer, Float, Float>)

    All road settings over time



64
65
66
# File 'lib/beeminder/goals.rb', line 64

def roadall
  @roadall
end

#secrettrue|false

Returns Whether you have to be signed in as owner of the goal to view it.

Returns:

  • (true|false)

    Whether you have to be signed in as owner of the goal to view it.



55
56
57
# File 'lib/beeminder/goals.rb', line 55

def secret
  @secret
end

#slugString

Returns The final part of the URL of the goal, used as an identifier.

Returns:

  • (String)

    The final part of the URL of the goal, used as an identifier.



6
7
8
# File 'lib/beeminder/goals.rb', line 6

def slug
  @slug
end

#titleString

Returns The title that the user specified for the goal.

Returns:

  • (String)

    The title that the user specified for the goal.



12
13
14
# File 'lib/beeminder/goals.rb', line 12

def title
  @title
end

#updated_atDateTime (readonly)

Returns Last time this goal was updated.

Returns:

  • (DateTime)

    Last time this goal was updated.



9
10
11
# File 'lib/beeminder/goals.rb', line 9

def updated_at
  @updated_at
end

#userBeeminder::User (readonly)

Returns User that owns this goal.

Returns:



61
62
63
# File 'lib/beeminder/goals.rb', line 61

def user
  @user
end

#yawNumeric (readonly)

Returns Good side of the road. I.e., the side of the road (+1/-1 = above/below) that makes you say “yay”.

Returns:

  • (Numeric)

    Good side of the road. I.e., the side of the road (+1/-1 = above/below) that makes you say “yay”.



73
74
75
# File 'lib/beeminder/goals.rb', line 73

def yaw
  @yaw
end

Instance Method Details

#add(datapoints, opts = {}) ⇒ Object

Add one or more datapoints to the goal.

Parameters:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/beeminder/goals.rb', line 160

def add datapoints, opts={}
  datapoints = [*datapoints]

  datapoints.each do |dp|
    # we grab these datapoints for ourselves
    dp.goal = self
    
    data = {
      "sendmail" => opts[:sendmail] || false
    }.merge(dp.to_hash)

    # TODO create_all doesn't work because Ruby's POST encoding of arrays is broken.
    @user.post "users/me/goals/#{@slug}/datapoints.json", data
  end
end

#datapointsArray<Beeminder::Datapoint>

List of datapoints.

Returns:



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/beeminder/goals.rb', line 100

def datapoints
  info = @user.get "users/me/goals/#{slug}/datapoints.json"
  datapoints = info.map do |d_info|
    d_info = {
      :goal => self,
    }.merge(d_info)

    Datapoint.new d_info
  end

  datapoints
end

#delete(datapoints) ⇒ Object

Delete one or more datapoints from the goal.

Parameters:



179
180
181
182
183
184
# File 'lib/beeminder/goals.rb', line 179

def delete datapoints
  datapoints = [*datapoints]
  datapoints.each do |dp|
    @user.delete "/users/me/goals/#{@slug}/datapoints/#{dp.id}.json"
  end
end

#dial_road(dials = {}) ⇒ Object

Send new road setting to Beeminder.

Parameters:

  • dials (Hash) (defaults to: {})

    Set exactly two of ‘“rate”`, `“goaldate”` and `“goalval”`. The third is implied.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/beeminder/goals.rb', line 131

def dial_road dials={}
  raise "Set exactly two dials." if dials.keys.size != 2

  # convert to proper timestamp
  unless dials["goaldate"].nil?
    dials["goaldate"] = @user.convert_to_timezone dials["goaldate"] if @user.enforce_timezone?
    dials["goaldate"] = dials["goaldate"].strftime('%s')
  end
    
  @user.post "users/me/goals/#{@slug}/dial_road.json", dials
end

#reloadObject

Reload data from Beeminder.



92
93
94
95
# File 'lib/beeminder/goals.rb', line 92

def reload
  info = @user.get "users/me/goals/#{@slug}.json"
  _parse_info info
end

#schedule_break(start_time, end_time, rate = 0.0) ⇒ Object

Schedule a break. Adds two new entries to ‘roadall` reflecting the break. Use #update to actually update the goal.

Parameters:

  • start_time (Time)

    when to start the break – must be after the akrasia horizon

  • end_time (Time)

    when to end the break

  • rate (Float) (defaults to: 0.0)

    the slope of the road during the break



150
151
152
153
154
155
# File 'lib/beeminder/goals.rb', line 150

def schedule_break start_time, end_time, rate = 0.0
  check_break start_time, end_time

  roadall.insert(-2, [start_time.to_i, nil, roadall.last.last])
  roadall.insert(-2, [end_time.to_i, nil, rate])
end

#to_hashHash

Convert goal to hash for POSTing.

Returns:

  • (Hash)


188
189
190
191
192
193
194
195
196
197
198
# File 'lib/beeminder/goals.rb', line 188

def to_hash
  {
    "slug"       => @slug,
    "title"      => @title,
    "goal_type"  => @goal_type.to_s,
    "ephem"      => @ephem || false,
    "panic"      => @panic || 86400,
    "secret"     => @secret || false,
    "datapublic" => @datapublic || false,
  }
end

#updateObject

Send updated meta-data to Beeminder.



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/beeminder/goals.rb', line 114

def update
  data = {
    "slug"       => @slug,
    "title"      => @title,
    "ephem"      => @ephem || false,
    "panic"      => @panic || 86400,
    "secret"     => @secret || false,
    "datapublic" => @datapublic || false,
  }
  data['roadall'] = @roadall if @roadall

  @user.put_document "users/me/goals/#{@slug}.json", data
end