Class: Fluttrly::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/fluttrly-gem/command.rb

Class Method Summary collapse

Class Method Details

.delete(list, num = nil) ⇒ Object

Command used to delete one or all items in a list

list - List to delete items from. num - Number in the list to update if already known.

Returns a refreshed list showing the changes



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fluttrly-gem/command.rb', line 46

def delete(list, num=nil)
  response = form_response(list)[1]
  page = list(list)
  if page
    puts "Which item would you like to delete?"
    num = STDIN.gets.chomp.to_i if num.nil?
    id = page[num-1]["task"]["id"]
    delete_task(id, response)
  #list(list)
  end
end

.execute(*args) ⇒ Object

Takes arguments from command line

args - The list of arguments passed in. If they are

in the wrong order, the proper command will not be
executed correctly when passed to parse_arguments()


16
17
18
19
20
21
22
# File 'lib/fluttrly-gem/command.rb', line 16

def execute(*args)
  command = args.shift
  list = args.shift
  message = args.empty? ? nil : args.join(' ')

  parse_arguments(command, list, message)
end

.helpObject

Command used to print out some helpful hints!

Returns nothing.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/fluttrly-gem/command.rb', line 122

def help
  text = %{
    Fluttrly is a nice web app to help you collaborate to-do lists
    with your friends, this is the 'help' for its command line
    interpreter.

    Usage:
      fluttrly -h/help
      fluttrly <command> <list> <message for posting>

    Examples:
      fluttrly list bouverdafs             Output the list "Bouverdafs"
      fluttrly post bouverdafs "winning"   Add to the list bouverdafs the message
                                           "winning"
      fluttrly update bouverdafs           Presents the user with a prompt to
                                           allow them to update a specific item
                                           on that list.
      fluttrly delete bouverdafs           Just like `update`,except allows user
                                           to delete a specific item from list. 

    For more information please go to:
    http://github.com/brntbeer/fluttrly-gem

  }.gsub(/^ {8}/, '') #to strip 8 lines of whitespace
  puts text
end

.list(list) ⇒ Object

Command used to list the items within a list

list - List to show contents of.

Returns a list of tasks for the given list.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fluttrly-gem/command.rb', line 81

def list(list)
  response = form_response(list, true)[1]
  if Net::HTTPSuccess === response
    i = 1
    page = JSON.parse(response.body)
    puts ""
    page.each do |task|
      puts("#{i}: Task => \"#{task["task"]["content"]}\" at => #{Time.parse(task["task"]["created_at"]).strftime("%m-%d-%Y %H:%M %p")} (Completed? #{task["task"]["completed"]})") 
      i = i+1
    end
    if page.empty? 
      puts "Oops, that list doesn't have any items yet!"
      puts "Try: fluttrly post #{list} <message> first."
      return false
    end
    page
  else 
    response.error! 
  end
end

.parse_arguments(command, list, message) ⇒ Object

Receives arguments and delegates them appropriately

command - First argument used to decide what additional method

to call.

list - The list to use execute the command on. message - Used to post to a list.

Returns output based on the method called.



32
33
34
35
36
37
38
# File 'lib/fluttrly-gem/command.rb', line 32

def parse_arguments(command, list, message)
  return list(list) if command == 'list' || command == 'l'
  return post(list, message) if command == 'post' || command == 'p'
  return update(list) if command == 'update' || command == 'c'
  return delete(list) if command == 'delete' || command == 'd'
  return help if command == 'help' || command == '-h'  
end

.post(list, message) ⇒ Object

Used to post a message to a given list.

list - List to send a message to. message - Message to pass to the list.

Returns a newly updated list.



108
109
110
111
112
113
114
115
116
# File 'lib/fluttrly-gem/command.rb', line 108

def post(list, message)
  http, response = form_response(list)
  if Net::HTTPSuccess === response
    response = post_request(list, message, response,http)
  else
    response.error!
  end
  #list(list)
end

.update(list, num = nil) ⇒ Object

Command used if wanting to mark an item to edit

list - List to update num - Number in list to update if already known.

Returns the list with its items to show the change.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fluttrly-gem/command.rb', line 64

def update(list, num=nil)
  response = form_response(list)[1]
  page = list(list)
  if page
    puts 'Which item would you like to edit?'
    num = STDIN.gets.chomp.to_i if num.nil?
    id = page[num-1]["task"]["id"]
    update_task(id,response) 
    #list(list)
  end
end