Class: Beeminder::User
- Inherits:
-
Object
- Object
- Beeminder::User
- Defined in:
- lib/beeminder/user.rb
Instance Attribute Summary collapse
-
#auth_type ⇒ Symbol
readonly
Type of user, can be ‘:personal` (default) or `:oauth`.
-
#enforce_timezone ⇒ true|false
Enforce user timezone for all passed times? Should be true unless you know what you’re doing.
-
#name ⇒ String
readonly
User name.
-
#timezone ⇒ String
readonly
Timezone.
-
#token ⇒ String
readonly
Auth token.
-
#updated_at ⇒ DateTime
readonly
Last time user made any changes.
Instance Method Summary collapse
-
#akrasia_horizon ⇒ Time
Returns the current akrasia horizon.
-
#convert_to_timezone(time) ⇒ Time
Converts time object to one with user’s timezone.
-
#create_goal(opts = {}) ⇒ Object
Create new goal.
-
#delete(cmd, data = {}) ⇒ Object
Send DELETE request to API.
-
#enforce_timezone? ⇒ true|false
Enforce timezone for all passed times?.
-
#get(cmd, data = {}) ⇒ Object
Send GET request to API.
-
#goal(name) ⇒ Beeminder::Goal
Return specific goal.
-
#goals(filter = :all) ⇒ Array<Beeminder::Goal>
List of goals.
-
#initialize(token, opts = {}) ⇒ User
constructor
A new instance of User.
-
#post(cmd, data = {}) ⇒ Object
Send POST request to API.
-
#put(cmd, data = {}) ⇒ Object
Send PUT request to API.
-
#put_document(cmd, data = {}) ⇒ Object
Send PUT request with a JSON document to API.
-
#send(name, value, comment = "") ⇒ Object
Convenience function to add datapoint to a goal.
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_type ⇒ Symbol (readonly)
Returns 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_timezone ⇒ true|false
Returns 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 |
#name ⇒ String (readonly)
Returns User name.
6 7 8 |
# File 'lib/beeminder/user.rb', line 6 def name @name end |
#timezone ⇒ String (readonly)
Returns Timezone.
15 16 17 |
# File 'lib/beeminder/user.rb', line 15 def timezone @timezone end |
#token ⇒ String (readonly)
Returns Auth token.
9 10 11 |
# File 'lib/beeminder/user.rb', line 9 def token @token end |
#updated_at ⇒ DateTime (readonly)
Returns 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_horizon ⇒ Time
Returns the current akrasia horizon.
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.
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.
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.
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?
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.
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.
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.
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.
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.
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.
134 135 136 |
# File 'lib/beeminder/user.rb', line 134 def put_document cmd, data = {} _connection :put_json, cmd, data end |