Class: Statusbot::Api::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/statusbot/api/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_email) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
# File 'lib/statusbot/api/base.rb', line 6

def initialize(user_email)
  begin
    @user = User.find_by_email!(user_email)
  rescue ActiveRecord::RecordNotFound
    raise UserNotRegisteredError
  end
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'lib/statusbot/api/base.rb', line 4

def user
  @user
end

Instance Method Details

#add_goal(description = nil) ⇒ Object

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/statusbot/api/base.rb', line 37

def add_goal(description=nil)
  raise InvalidUpdateError if description.nil? or description.strip.empty?
  goal = Goal.new(
    :user => user, 
    :description => description
  )
  begin
    goal.save!
  rescue => e
    raise DatabaseConnectionError.new(e)
  end
end

#add_update(description = nil) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/statusbot/api/base.rb', line 14

def add_update(description=nil)
  raise InvalidUpdateError if description.nil? or description.strip.empty?
  done
  update = Update.new(
    :user => user, 
    :description => description, 
    :start_time => DateTime.now
  )
  begin
    update.save!
  rescue => e
    raise DatabaseConnectionError.new(e)
  end
end

#add_wait(description = nil) ⇒ Object

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/statusbot/api/base.rb', line 58

def add_wait(description=nil)
  raise InvalidUpdateError if description.nil? or description.strip.empty?
  wait = Wait.new(
    :user => user, 
    :description => description
  )
  user.waits << wait
  begin
    user.save!
  rescue => e
    raise DatabaseConnectionError.new(e)
  end
end

#doneObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/statusbot/api/base.rb', line 96

def done
  begin
    ActiveRecord::Base.transaction do
      user.updates.where(:stop_time => nil).each do |update|
        update.stop_time = DateTime.now
        update.save!
      end
    end
  rescue => e
    raise DatabaseConnectionError.new(e)
  end
  user.reload
end

#get_goalsObject



50
51
52
53
54
55
56
# File 'lib/statusbot/api/base.rb', line 50

def get_goals
  begin
    user.goals
  rescue => e
    raise DatabaseConnectionError.new(e)
  end
end

#get_updatesObject



29
30
31
32
33
34
35
# File 'lib/statusbot/api/base.rb', line 29

def get_updates
  begin
    user.updates
  rescue => e
    raise DatabaseConnectionError.new(e)
  end
end

#get_waitsObject



72
73
74
75
76
77
78
# File 'lib/statusbot/api/base.rb', line 72

def get_waits
  begin
    user.waits
  rescue => e
    raise DatabaseConnectionError.new(e)
  end
end

#remind(wait_id = nil, description = nil) ⇒ Object

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/statusbot/api/base.rb', line 80

def remind(wait_id=nil, description=nil)
  raise InvalidUpdateError unless wait_id.is_a? Integer
  begin
    wait = Wait.find(wait_id)
  rescue => e
    raise InvalidUpdateError.new(e)
  end
  wait.pings << Ping.new(:description => description)
  
  begin
    wait.save!
  rescue => e
    raise DatabaseConnectionError.new(e)
  end
end