Class: Beeminder::User

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, opts = {}) ⇒ User

Returns a new instance of User.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/beeminder/user.rb', line 23

def initialize token, opts={}
  opts = {
    :auth_type => :personal,
    :enforce_timezone => true,
  }.merge(opts)
  
  @token            = token
  @auth_type        = opts[:auth_type]
  @enforce_timezone = opts[:enforce_timezone]

  @token_type =
    case @auth_type
    when :personal
      "auth_token"
    when :oauth
      "access_token"
    else
      raise ArgumentError, "Auth type not supported, must be :personal or :oauth."
    end
  
  info = get "users/me.json"

  @name       = info["username"]
  @timezone   = info["timezone"] || "UTC"
  @updated_at = DateTime.strptime(info["updated_at"].to_s, '%s').in_time_zone(@timezone)
end

Instance Attribute Details

#auth_typeSymbol (readonly)

Returns Type of user, can be ‘:personal` (default) or `:oauth`.

Returns:

  • (Symbol)

    Type of user, can be ‘:personal` (default) or `:oauth`.



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

def auth_type
  @auth_type
end

#enforce_timezonetrue|false

Returns Enforce user timezone for all passed times? Should be true unless you know what you’re doing. (Default: ‘true`.).

Returns:

  • (true|false)

    Enforce user timezone for all passed times? Should be true unless you know what you’re doing. (Default: ‘true`.)



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

def enforce_timezone
  @enforce_timezone
end

#nameString (readonly)

Returns User name.

Returns:

  • (String)

    User name.



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

def name
  @name
end

#timezoneString (readonly)

Returns Timezone.

Returns:

  • (String)

    Timezone.



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

def timezone
  @timezone
end

#tokenString (readonly)

Returns Auth token.

Returns:

  • (String)

    Auth token.



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

def token
  @token
end

#updated_atDateTime (readonly)

Returns Last time user made any changes.

Returns:

  • (DateTime)

    Last time user made any changes.



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

def updated_at
  @updated_at
end

Instance Method Details

#akrasia_horizonTime

Returns the current akrasia horizon.

Returns:

  • (Time)

    The first instant changes can be made for.



154
155
156
157
158
# File 'lib/beeminder/user.rb', line 154

def akrasia_horizon
  Time.use_zone(@timezone) do
    -8.days.ago.beginning_of_day
  end
end

#convert_to_timezone(time) ⇒ Time

Converts time object to one with user’s timezone.

Parameters:

  • time (Date|DateTime|Time)

    Time to convert.

Returns:

  • (Time)

    Converted time.



142
143
144
145
146
147
148
149
# File 'lib/beeminder/user.rb', line 142

def convert_to_timezone time
  Time.use_zone(@timezone){
  
  time = time.to_time unless time.is_a?(Time)
  Time.local(time.year, time.month, time.day, time.hour, time.min, time.sec)
  
  }
end

#create_goal(opts = {}) ⇒ Object

Create new goal.

Parameters:

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

    Goal options.



94
95
96
# File 'lib/beeminder/user.rb', line 94

def create_goal opts={}
  post "users/#{@name}/goals.json", opts
end

#delete(cmd, data = {}) ⇒ Object

Send DELETE request to API.

Parameters:

  • cmd (String)

    the API command, like ‘users/#Beeminder::User.useruser.name.json`

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

    data to send; auth_token is included by default (optional)



118
119
120
# File 'lib/beeminder/user.rb', line 118

def delete cmd, data={}
  _connection :delete, cmd, data
end

#enforce_timezone?true|false

Enforce timezone for all passed times?

Returns:

  • (true|false)


53
54
55
# File 'lib/beeminder/user.rb', line 53

def enforce_timezone?
  !!@enforce_timezone
end

#get(cmd, data = {}) ⇒ Object

Send GET request to API.

Parameters:

  • cmd (String)

    the API command, like ‘users/#Beeminder::User.useruser.name.json`

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

    data to send; auth_token is included by default (optional)



102
103
104
# File 'lib/beeminder/user.rb', line 102

def get cmd, data={}
  _connection :get, cmd, data
end

#goal(name) ⇒ Beeminder::Goal

Return specific goal.

Parameters:

  • name (String)

    Name of the goal.

Returns:



76
77
78
# File 'lib/beeminder/user.rb', line 76

def goal name
  Beeminder::Goal.new self, name
end

#goals(filter = :all) ⇒ Array<Beeminder::Goal>

List of goals.

Parameters:

  • filter (Symbol) (defaults to: :all)

    filter goals, can be ‘:all` (default), `:frontburner` or `:backburner`

Returns:



61
62
63
64
65
66
67
68
69
70
# File 'lib/beeminder/user.rb', line 61

def goals filter=:all
  raise "invalid goal filter: #{filter}" unless [:all, :frontburner, :backburner].include? filter

  goals = get("users/#{@name}/goals.json", :filter => filter.to_s) || []
  goals.map! do |info|
    Beeminder::Goal.new self, info
  end

  goals || []
end

#post(cmd, data = {}) ⇒ Object

Send POST request to API.

Parameters:

  • cmd (String)

    the API command, like ‘users/#Beeminder::User.useruser.name.json`

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

    data to send; auth_token is included by default (optional)



110
111
112
# File 'lib/beeminder/user.rb', line 110

def post cmd, data={}
  _connection :post, cmd, data
end

#put(cmd, data = {}) ⇒ Object

Send PUT request to API.

Parameters:

  • cmd (String)

    the API command, like ‘users/#Beeminder::User.useruser.name.json`

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

    data to send; auth_token is included by default (optional)



126
127
128
# File 'lib/beeminder/user.rb', line 126

def put cmd, data={}
  _connection :put, cmd, data
end

#put_document(cmd, data = {}) ⇒ Object

Send PUT request with a JSON document to API.

Parameters:

  • cmd (String)

    the API command, like ‘users/#Beeminder::User.useruser.name.json`

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

    data to send



134
135
136
# File 'lib/beeminder/user.rb', line 134

def put_document cmd, data = {}
  _connection :put_json, cmd, data
end

#send(name, value, comment = "") ⇒ Object

Convenience function to add datapoint to a goal.

Parameters:

  • name (String)

    Goal name.

  • value (Numeric)

    Datapoint value.

  • comment (String) (defaults to: "")

    Optional comment.



85
86
87
88
89
# File 'lib/beeminder/user.rb', line 85

def send name, value, comment=""
  goal = self.goal name
  dp = Beeminder::Datapoint.new :value => value, :comment => comment
  goal.add dp
end