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.



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

#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

#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

#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

#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

Instance Method Details

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

Add one or more datapoints to the goal.

Parameters:



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

#datapointsArray<Beeminder::Datapoint>

List of datapoints.

Returns:



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.

Parameters:



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.

Parameters:

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

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



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

#reloadObject

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_hashHash

Convert goal to hash for POSTing.

Returns:

  • (Hash)


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

#updateObject

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