Class: Beeminder::Goal
- Inherits:
-
Object
- Object
- Beeminder::Goal
- Defined in:
- lib/beeminder/goals.rb
Instance Attribute Summary collapse
-
#curday ⇒ DateTime
readonly
Date of the most recent data point.
-
#curval ⇒ Numeric
readonly
Value of the most recent data point.
-
#datapublic ⇒ true|false
Whether you have to be signed in as the owner of the goal to view the datapoints.
-
#ephem ⇒ true|false
Whether the graph was created in test mode.
-
#frozen ⇒ true|false
readonly
Whether the goal is currently frozen and therefore must be restarted before continuing to accept data.
-
#goal_type ⇒ Symbol
readonly
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.
-
#goaldate ⇒ DateTime
readonly
Goal date.
-
#goalval ⇒ Numeric
readonly
Goal value.
-
#graph_url ⇒ String
readonly
URL for the goal’s graph image.
-
#lane ⇒ Numeric
readonly
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).
-
#losedate ⇒ DateTime
readonly
Date of derailment.
-
#panic ⇒ Numeric
Panic threshold.
-
#queued ⇒ true|false
readonly
Whether the graph is currently being updated to reflect new data.
-
#rate ⇒ Numeric
readonly
The slope of the (final section of the) yellow brick road.
-
#roadall ⇒ Array<Integer, Float, Float>
All road settings over time.
-
#secret ⇒ true|false
Whether you have to be signed in as owner of the goal to view it.
-
#slug ⇒ String
The final part of the URL of the goal, used as an identifier.
-
#title ⇒ String
The title that the user specified for the goal.
-
#updated_at ⇒ DateTime
readonly
Last time this goal was updated.
-
#user ⇒ Beeminder::User
readonly
User that owns this goal.
-
#yaw ⇒ Numeric
readonly
Good side of the road.
Instance Method Summary collapse
-
#add(datapoints, opts = {}) ⇒ Object
Add one or more datapoints to the goal.
-
#datapoints ⇒ Array<Beeminder::Datapoint>
List of datapoints.
-
#delete(datapoints) ⇒ Object
Delete one or more datapoints from the goal.
-
#dial_road(dials = {}) ⇒ Object
Send new road setting to Beeminder.
-
#initialize(user, name_or_info) ⇒ Goal
constructor
A new instance of Goal.
-
#reload ⇒ Object
Reload data from Beeminder.
-
#schedule_break(start_time, end_time, rate = 0.0) ⇒ Object
Schedule a break.
-
#to_hash ⇒ Hash
Convert goal to hash for POSTing.
-
#update ⇒ Object
Send updated meta-data to Beeminder.
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
#curday ⇒ DateTime (readonly)
Returns Date of the most recent data point.
24 25 26 |
# File 'lib/beeminder/goals.rb', line 24 def curday @curday end |
#curval ⇒ Numeric (readonly)
Returns Value of the most recent data point.
21 22 23 |
# File 'lib/beeminder/goals.rb', line 21 def curval @curval end |
#datapublic ⇒ true|false
Returns 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 |
#ephem ⇒ true|false
Returns Whether the graph was created in test mode.
52 53 54 |
# File 'lib/beeminder/goals.rb', line 52 def ephem @ephem end |
#frozen ⇒ true|false (readonly)
Returns 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_type ⇒ Symbol (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.
37 38 39 |
# File 'lib/beeminder/goals.rb', line 37 def goal_type @goal_type end |
#goaldate ⇒ DateTime (readonly)
Returns Goal date.
15 16 17 |
# File 'lib/beeminder/goals.rb', line 15 def goaldate @goaldate end |
#goalval ⇒ Numeric (readonly)
Returns Goal value.
18 19 20 |
# File 'lib/beeminder/goals.rb', line 18 def goalval @goalval end |
#graph_url ⇒ String (readonly)
Returns URL for the goal’s graph image.
43 44 45 |
# File 'lib/beeminder/goals.rb', line 43 def graph_url @graph_url end |
#lane ⇒ Numeric (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).
70 71 72 |
# File 'lib/beeminder/goals.rb', line 70 def lane @lane end |
#losedate ⇒ DateTime (readonly)
Returns Date of derailment.
40 41 42 |
# File 'lib/beeminder/goals.rb', line 40 def losedate @losedate end |
#panic ⇒ Numeric
Returns Panic threshold. How long before derailment to panic.
46 47 48 |
# File 'lib/beeminder/goals.rb', line 46 def panic @panic end |
#queued ⇒ true|false (readonly)
Returns 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 |
#rate ⇒ Numeric (readonly)
Returns 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 |
#roadall ⇒ Array<Integer, Float, Float>
Returns All road settings over time.
64 65 66 |
# File 'lib/beeminder/goals.rb', line 64 def roadall @roadall end |
#secret ⇒ true|false
Returns 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 |
#slug ⇒ String
Returns 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 |
#title ⇒ String
Returns The title that the user specified for the goal.
12 13 14 |
# File 'lib/beeminder/goals.rb', line 12 def title @title end |
#updated_at ⇒ DateTime (readonly)
Returns Last time this goal was updated.
9 10 11 |
# File 'lib/beeminder/goals.rb', line 9 def updated_at @updated_at end |
#user ⇒ Beeminder::User (readonly)
Returns User that owns this goal.
61 62 63 |
# File 'lib/beeminder/goals.rb', line 61 def user @user end |
#yaw ⇒ Numeric (readonly)
Returns 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.
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 |
#datapoints ⇒ Array<Beeminder::Datapoint>
List of datapoints.
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.
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.
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 |
#reload ⇒ Object
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.
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_hash ⇒ Hash
Convert goal to hash for POSTing.
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 |
#update ⇒ Object
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 |