Class: HackerToDo::ToDoList

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/hacker_todo_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeToDoList

Returns a new instance of ToDoList.



17
18
19
20
21
# File 'lib/hacker_todo_list.rb', line 17

def initialize
  @github_creds = Setup.new.auth
  @todo_gist = find_todo_entry
  @todo_id = @todo_gist.nil? ? nil : @todo_gist["id"]
end

Instance Attribute Details

#github_credsObject

Returns the value of attribute github_creds.



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

def github_creds
  @github_creds
end

#todo_idObject

Returns the value of attribute todo_id.



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

def todo_id
  @todo_id
end

Instance Method Details

#add(task_list) ⇒ Object



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

def add(task_list)
  content = task_list.split("\\n")
  route = @todo_id.nil? ? "/gists" : "/gists/#{@todo_id}"
  post_gist(route, append_todo_content(content))
  puts "ToDo added successfully."
  list
end

#delete(*indexes) ⇒ Object



48
49
50
51
52
# File 'lib/hacker_todo_list.rb', line 48

def delete(*indexes)
  post_gist("/gists/#{@todo_id}", get_todo_content.delete_indexes(indexes.collect{|index| index.to_i - 1}))
  puts "ToDo deleted successfully."
  list
end

#find_todo_entryObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hacker_todo_list.rb', line 54

def find_todo_entry
  todo_entry = self.class.get("/users/#{@github_creds[:username]}/gists", 
    {:basic_auth => @github_creds}).find do |gist|
      unless gist.is_a?(Array)
        gist["files"].has_key?(GIST_FILE_NAME) && gist["description"] == GIST_DESCRIPTION
      end
  end
  if todo_entry
    return self.class.get("/gists/#{todo_entry["id"]}", {:basic_auth => @github_creds}) 
  else
    puts "Your github credentials are incorrect"
    system "rm -f #{HackerToDo::Setup::CREDENTIAL_FILE}"
    return nil
  end
end

#get_todo_contentObject



23
24
25
# File 'lib/hacker_todo_list.rb', line 23

def get_todo_content
  YAML.load(@todo_gist["files"][GIST_FILE_NAME]["content"])
end

#listObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hacker_todo_list.rb', line 27

def list
  content = get_todo_content
  if @todo_id.nil? || content.empty?
    puts "You need to create a ToDo"
  else
    HackerToDo.list_formatter do
      content.each_with_index do |task, index|
        puts "#{index + 1}. #{task}"
      end
    end  
  end
end