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.
-
#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.
-
#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.
-
#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.
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.
-
#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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/beeminder/goals.rb', line 63 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 |
#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 |
#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 |
#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 |
Instance Method Details
#add(datapoints, opts = {}) ⇒ Object
Add one or more datapoints to the goal.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/beeminder/goals.rb', line 133 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.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/beeminder/goals.rb', line 88 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.
152 153 154 155 156 157 |
# File 'lib/beeminder/goals.rb', line 152 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.
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/beeminder/goals.rb', line 118 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.
80 81 82 83 |
# File 'lib/beeminder/goals.rb', line 80 def reload info = @user.get "users/me/goals/#{@slug}.json" parse_info info end |
#to_hash ⇒ Hash
Convert goal to hash for POSTing.
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/beeminder/goals.rb', line 161 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.
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/beeminder/goals.rb', line 102 def update data = { "slug" => @slug, "title" => @title, "ephem" => @ephem || false, "panic" => @panic || 86400, "secret" => @secret || false, "datapublic" => @datapublic || false, } @user.put "users/me/goals/#{@slug}.json", data end |