Class: Logan::TodoList

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

Instance Attribute Summary collapse

Attributes included from HashConstructed

#attributes, #json_raw

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



21
22
23
24
25
# File 'lib/logan/todolist.rb', line 21

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_countObject

Returns the value of attribute completed_count.



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

def completed_count
  @completed_count
end

#completed_todosArray<Logan::Todo>

returns the array of completed todos - potentially synchronously downloaded from API

Returns:

  • (Array<Logan::Todo>)

    Array of completed todos for this todo list



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

def completed_todos
  if @completed_todos.empty? && @completed_count > 0
    refresh
  end

  return @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_countObject

Returns the value of attribute remaining_count.



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

def remaining_count
  @remaining_count
end

#remaining_todosArray<Logan::Todo>

returns the array of remaining todos - potentially synchronously downloaded from API

Returns:

  • (Array<Logan::Todo>)

    Array of remaining todos for this todo list



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

def remaining_todos
  if @remaining_todos.empty? && @remaining_count > 0
    refresh
  end

  return @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



90
91
92
93
94
95
96
97
98
# File 'lib/logan/todolist.rb', line 90

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



118
119
120
# File 'lib/logan/todolist.rb', line 118

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

#post_jsonObject



27
28
29
# File 'lib/logan/todolist.rb', line 27

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

#refreshObject

refreshes the data for this todo list from the API



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

def refresh
  response = Logan::Client.get "/projects/#{@project_id}/todolists/#{@id}.json"
  initialize(response.parsed_response)
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



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

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

#todosArray<Logan::Todo>

returns an array of all todos, completed and remaining - potentially synchronously downloaded from API

Returns:

  • (Array<Logan::Todo>)

    Array of completed todos for this todo list



62
63
64
# File 'lib/logan/todolist.rb', line 62

def todos
  remaining_todos + completed_todos
end

#todos=(todo_hash) ⇒ Array<Logan::Todo>

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

Returns:

  • (Array<Logan::Todo>)

    array of remaining and completed todos for this list



71
72
73
74
75
# File 'lib/logan/todolist.rb', line 71

def todos=(todo_hash)
  @remaining_todos = todo_hash['remaining'].map { |h| Logan::Todo.new h.merge({ :project_id => @project_id }) }
  @completed_todos = todo_hash['completed'].map { |h| Logan::Todo.new h.merge({ :project_id => @project_id }) }
  return @remaining_todos + @completed_todos
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



104
105
106
107
108
109
110
111
112
# File 'lib/logan/todolist.rb', line 104

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