Class: Manatoo::Task

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/manatoo/task.rb

Defined Under Namespace

Classes: ATTRIBUTES_STRUCT

Constant Summary collapse

TASK_KEYS =
[
  :id,
  :title,
  :data,
  :description,
  :slug,
  :status,
  :weight,
  :duration,
  :labels,
  :finished_at,
  :created_at,
  :due_at,
  :started_at,
  :list_id,
  :users,
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, snake_cased_json) ⇒ Task

Returns a new instance of Task.



78
79
80
81
82
83
# File 'lib/manatoo/task.rb', line 78

def initialize(data, snake_cased_json)
  @attributes = ATTRIBUTES_STRUCT.new
  set_attributes_from_hash(data)
  @last_json_response = snake_cased_json
  self
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



29
30
31
# File 'lib/manatoo/task.rb', line 29

def attributes
  @attributes
end

#last_json_responseObject (readonly)

Returns the value of attribute last_json_response.



29
30
31
# File 'lib/manatoo/task.rb', line 29

def last_json_response
  @last_json_response
end

Class Method Details

.add_labels(task_id, labels) ⇒ Object

labels should not be empty, should be array



112
113
114
115
116
117
118
# File 'lib/manatoo/task.rb', line 112

def self.add_labels(task_id, labels)
  url = "tasks/#{task_id}/labels"
  resp = Manatoo.post(url, {
    labels: labels
  })
  JSON.parse(resp.body).to_snake_keys
end

.add_users(task_id, users) ⇒ Object

users should not be empty, should be array



140
141
142
143
144
145
146
# File 'lib/manatoo/task.rb', line 140

def self.add_users(task_id, users)
  url = "tasks/#{task_id}/users"
  resp = Manatoo.post(url, {
    users: users
  })
  JSON.parse(resp.body).to_snake_keys
end

.add_weight(task_id, weight) ⇒ Object

weight REQUIRED, should be integer



126
127
128
129
130
131
132
# File 'lib/manatoo/task.rb', line 126

def self.add_weight(task_id, weight)
  url = "tasks/#{task_id}/weight"
  resp = Manatoo.post(url, {
    weight: weight
  })
  JSON.parse(resp.body).to_snake_keys
end

.create(attrs, return_task = false) ⇒ Object

list_id, title REQUIRED

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/manatoo/task.rb', line 48

def self.create(attrs, return_task=false)
  raise ArgumentError.new('Task attributes must be passed in as a Hash') unless attrs.is_a?(Hash)

  list_id = attrs[:list_id]
  title = attrs[:title]
  raise ArgumentError.new(
    'Both list_id and title must be passed in and not blank'
  ) if list_id.nil? or list_id.empty? or title.nil? or title.empty?

  url = "tasks"
  resp = Manatoo.post(url, attrs.merge({
      title: title,
      list_id: list_id,
    }))
  snake_cased_json = JSON.parse(resp.body).to_snake_keys
  attrs = snake_cased_json['data']
  return Task.new(attrs, snake_cased_json) if return_task
  return snake_cased_json
end

.find(task_id, return_task = false) ⇒ Object

task_id REQUIRED



69
70
71
72
73
74
75
76
# File 'lib/manatoo/task.rb', line 69

def self.find(task_id, return_task=false)
  url = "tasks/#{task_id}"
  resp = Manatoo.get(url)
  snake_cased_json = JSON.parse(resp.body).to_snake_keys
  attrs = snake_cased_json['data']
  return Task.new(attrs, snake_cased_json) if return_task
  return snake_cased_json
end

.remove_users(task_id, users) ⇒ Object

users should not be empty, should be array



154
155
156
157
158
159
160
# File 'lib/manatoo/task.rb', line 154

def self.remove_users(task_id, users)
  url = "tasks/#{task_id}/users"
  resp = Manatoo.delete(url, {
    users: users
  })
  JSON.parse(resp.body).to_snake_keys
end

.update(task_id, attrs) ⇒ Object

task_id REQUIRED



86
87
88
89
90
# File 'lib/manatoo/task.rb', line 86

def self.update(task_id, attrs)
  url = "tasks/#{task_id}"
  resp = Manatoo.put(url, attrs)
  JSON.parse(resp.body).to_snake_keys
end

.update_status(task_id, status) ⇒ Object

task_id, status REQUIRED



98
99
100
101
102
103
104
# File 'lib/manatoo/task.rb', line 98

def self.update_status(task_id, status)
  url = "tasks/#{task_id}/status"
  resp = Manatoo.put(url, {
    status: status
  })
  JSON.parse(resp.body).to_snake_keys
end

Instance Method Details

#add_labels(labels) ⇒ Object



120
121
122
123
# File 'lib/manatoo/task.rb', line 120

def add_labels(labels)
  snake_cased_json = Task.add_labels(id, labels)
  handle_snake_cased_json(snake_cased_json)
end

#add_users(users) ⇒ Object



148
149
150
151
# File 'lib/manatoo/task.rb', line 148

def add_users(users)
  snake_cased_json = Task.add_users(id, weight)
  handle_snake_cased_json(snake_cased_json)
end

#add_weight(weight) ⇒ Object



134
135
136
137
# File 'lib/manatoo/task.rb', line 134

def add_weight(weight)
  snake_cased_json = Task.add_weight(id, weight)
  handle_snake_cased_json(snake_cased_json)
end

#handle_snake_cased_json(snake_cased_json) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/manatoo/task.rb', line 32

def handle_snake_cased_json(snake_cased_json)
  # if a user wanted the last_response_json, I assume they would want it
  # in a snake_cased ruby usable fashion
  @last_json_response = snake_cased_json
  task_attrs = @last_json_response['data']
  set_attributes_from_hash(task_attrs)
  self
end

#remove_members(users) ⇒ Object



162
163
164
165
# File 'lib/manatoo/task.rb', line 162

def remove_members(users)
  snake_cased_json = Task.remove_users(id, weight)
  handle_snake_cased_json(snake_cased_json)
end

#set_attributes_from_hash(hash) ⇒ Object



41
42
43
44
45
# File 'lib/manatoo/task.rb', line 41

def set_attributes_from_hash(hash)
  hash.each do |k, v|
    attributes[k] = v
  end
end

#update(attrs) ⇒ Object



92
93
94
95
# File 'lib/manatoo/task.rb', line 92

def update(attrs)
  snake_cased_json = Task.update(id, attrs)
  handle_snake_cased_json(snake_cased_json)
end

#update_status(status) ⇒ Object



106
107
108
109
# File 'lib/manatoo/task.rb', line 106

def update_status(status)
  snake_cased_json = Task.update_status(id, status)
  handle_snake_cased_json(snake_cased_json)
end