Class: Logan::TodoList

Inherits:
Object
  • Object
show all
Includes:
HashConstructed
Defined in:
lib/logan/todolist.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ TodoList

intializes a todo list by calling the HashConstructed initialize method and setting both @remaining_todos and @completed_todos to empty arrays



19
20
21
22
23
# File 'lib/logan/todolist.rb', line 19

def initialize(h)
  @remaining_todos = []
  @completed_todos = []
  super
end

Instance Attribute Details

#completedObject

Returns the value of attribute completed.



12
13
14
# File 'lib/logan/todolist.rb', line 12

def completed
  @completed
end

#completed_todosObject

Returns the value of attribute completed_todos.



14
15
16
# File 'lib/logan/todolist.rb', line 14

def completed_todos
  @completed_todos
end

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/logan/todolist.rb', line 11

def description
  @description
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/logan/todolist.rb', line 8

def id
  @id
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/logan/todolist.rb', line 10

def name
  @name
end

#project_idObject

Returns the value of attribute project_id.



9
10
11
# File 'lib/logan/todolist.rb', line 9

def project_id
  @project_id
end

#remaining_todosObject

Returns the value of attribute remaining_todos.



13
14
15
# File 'lib/logan/todolist.rb', line 13

def remaining_todos
  @remaining_todos
end

#urlObject

Returns the value of attribute url.



15
16
17
# File 'lib/logan/todolist.rb', line 15

def url
  @url
end

Instance Method Details

#create_todo(todo) ⇒ Logan::Todo

create a todo in this todo list via the Basecamp API

Parameters:

  • todo (Logan::Todo)

    the todo instance to create in this todo lost

Returns:

  • (Logan::Todo)

    the created todo returned from the Basecamp API



52
53
54
55
56
57
58
59
60
# File 'lib/logan/todolist.rb', line 52

def create_todo(todo)
  post_params = {
    :body => todo.post_json,
    :headers => Logan::Client.headers.merge({'Content-Type' => 'application/json'})
  }
   
  response = Logan::Client.post "/projects/#{@project_id}/todolists/#{@id}/todos.json", post_params
  Logan::Todo.new response
end

#delete_todo(todo) ⇒ HTTParty::response

delete a todo in this todo list via delete call to Basecamp API

Parameters:

  • todo (Logan::Todo)

    the todo instance to be delete from this todo list

Returns:

  • (HTTParty::response)

    response from Basecamp for delete request



80
81
82
# File 'lib/logan/todolist.rb', line 80

def delete_todo(todo)
  response = Logan::Client.delete "/projects/#{@project_id}/todos/#{todo.id}.json"
end

#post_jsonObject



25
26
27
# File 'lib/logan/todolist.rb', line 25

def post_json
    { :name => @name, :description => @description }.to_json
end

#todo_with_substring(substring) ⇒ Logan::Todo

searches the remaining and completed todos for the first todo with the substring in its content

Parameters:

  • substring (String)

    substring to look for

Returns:

  • (Logan::Todo)

    the matched todo, or nil if there wasn’t one



43
44
45
46
# File 'lib/logan/todolist.rb', line 43

def todo_with_substring(substring)      
  issue_todo = @remaining_todos.detect{ |t| !t.content.index(substring).nil? }
  issue_todo ||= @completed_todos.detect { |t| !t.content.index(substring).nil? }
end

#todos=(todo_hash) ⇒ Object

assigns the #remaining_todos and #completed_todos from the associated keys in the passed hash

Parameters:

  • todo_hash (Hash)

    hash possibly containing todos under ‘remaining’ and ‘completed’ keys



33
34
35
36
37
# File 'lib/logan/todolist.rb', line 33

def todos=(todo_hash)
  @remaining_todos = todo_hash['remaining'].map { |h| Logan::Todo.new h }
  @completed_todos = todo_hash['completed'].map { |h| Logan::Todo.new h }
  return nil
end

#update_todo(todo) ⇒ Logan::Todo

update a todo in this todo list via the Basecamp API

Parameters:

  • todo (Logan::Todo)

    the todo instance to update in this todo list

Returns:

  • (Logan::Todo)

    the updated todo instance returned from the Basecamp API



66
67
68
69
70
71
72
73
74
# File 'lib/logan/todolist.rb', line 66

def update_todo(todo)
  put_params = {
    :body => todo.put_json,
    :headers => Logan::Client.headers.merge({'Content-Type' => 'application/json'})
  }
         
  response = Logan::Client.put "/projects/#{@project_id}/todos/#{todo.id}.json", put_params
  Logan::Todo.new response
end