Class: Logan::Todo

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

Instance Attribute Summary collapse

Attributes included from HashConstructed

#attributes, #json_raw

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Todo

Returns a new instance of Todo.



22
23
24
25
26
27
28
29
30
# File 'lib/logan/todo.rb', line 22

def initialize h
  super

  unless app_url.nil? || app_url.blank?
    @project_id ||= app_url[/projects\/(\d*)\//, 1].to_i
  end

  self
end

Instance Attribute Details

#app_urlObject

Returns the value of attribute app_url.



17
18
19
# File 'lib/logan/todo.rb', line 17

def app_url
  @app_url
end

#assigneeObject

Returns the value of attribute assignee.



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

def assignee
  @assignee
end

#comments_countObject

Returns the value of attribute comments_count.



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

def comments_count
  @comments_count
end

#completedObject

Returns the value of attribute completed.



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

def completed
  @completed
end

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

#due_atObject

Returns the value of attribute due_at.



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

def due_at
  @due_at
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#positionObject

Returns the value of attribute position.



16
17
18
# File 'lib/logan/todo.rb', line 16

def position
  @position
end

#privateObject

Returns the value of attribute private.



20
21
22
# File 'lib/logan/todo.rb', line 20

def private
  @private
end

#project_idObject

Returns the value of attribute project_id.



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

def project_id
  @project_id
end

#trashedObject

Returns the value of attribute trashed.



19
20
21
# File 'lib/logan/todo.rb', line 19

def trashed
  @trashed
end

#urlObject

Returns the value of attribute url.



18
19
20
# File 'lib/logan/todo.rb', line 18

def url
  @url
end

Instance Method Details

#commentsArray<Logan::Comment] Array of comments on this todo

returns the array of comments - potentially synchronously downloaded from API

Returns:

  • (Array<Logan::Comment] Array of comments on this todo)

    Array<Logan::Comment] Array of comments on this todo



69
70
71
72
# File 'lib/logan/todo.rb', line 69

def comments
  refresh if (@comments.nil? || @comments.empty?) && @comments_count > 0
  @comments ||= Array.new
end

#comments=(comment_array) ⇒ Array<Logan::Comment>

assigns the #comments from the passed array

Parameters:

  • comment_array (Array<Object>)

    array of hash comments from API or <Logan::Comment> objects

Returns:



78
79
80
# File 'lib/logan/todo.rb', line 78

def comments=(comment_array)
  @comments = comment_array.map { |obj| obj = Logan::Comment.new obj if obj.is_a?(Hash) }
end

#create_comment(comment) ⇒ Logan::Comment

create a create in this todo list via the Basecamp API

Parameters:

  • todo (Logan::Comment)

    the comment instance to create in this todo lost

Returns:

  • (Logan::Comment)

    the created comment returned from the Basecamp API



94
95
96
97
98
99
100
101
102
# File 'lib/logan/todo.rb', line 94

def create_comment(comment)
  post_params = {
    :body => comment.post_json,
    :headers => Logan::Client.headers.merge({'Content-Type' => 'application/json'})
  }

  response = Logan::Client.post "/projects/#{@project_id}/todos/#{@id}/comments.json", post_params
  Logan::Comment.new response
end

#post_jsonObject



32
33
34
35
36
37
38
# File 'lib/logan/todo.rb', line 32

def post_json
  {
    :content => @content,
    :due_at => @due_at,
    :assignee => @assignee.nil? ? nil : @assignee.to_hash
  }.to_json
end

#put_jsonObject



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

def put_json
  {
    :content => @content,
    :due_at => @due_at,
    :assignee => @assignee.nil? ? nil : @assignee.to_hash,
    :position => (@position.nil? || @position.to_s.empty?) ? 99  : @position,
    :completed => @completed
  }.to_json
end

#refreshObject

refreshes the data for this todo from the API



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

def refresh
  response = Logan::Client.get "/projects/#{project_id}/todos/#{id}.json"
  initialize(response.parsed_response)
end

#saveObject



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

def save
  put_params = {
    :body => put_json,
    :headers => Logan::Client.headers.merge({'Content-Type' => 'application/json'})
  }

  response = Logan::Client.put url, put_params
  initialize response.parsed_response
end