Module: Wunderlist::Helper

Included in:
List, Note, Reminder, Subtask, Task, TaskComment, User, Webhook
Defined in:
lib/wunderlist/helper.rb

Instance Method Summary collapse

Instance Method Details

#createObject



18
19
20
# File 'lib/wunderlist/helper.rb', line 18

def create
  self.api.request :post, path, self.to_hash
end

#destroyObject



31
32
33
34
35
36
# File 'lib/wunderlist/helper.rb', line 31

def destroy
  self.api.request :delete, resource_path, {:revision => self.revision}
  self.id = nil

  self
end

#model_nameObject



46
47
48
49
50
51
# File 'lib/wunderlist/helper.rb', line 46

def model_name
  self.class.to_s.gsub('Wunderlist::','').
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z])([A-Z])/, '\1_\2').
    downcase
end

#pathObject



42
43
44
# File 'lib/wunderlist/helper.rb', line 42

def path
  "api/v1/#{plural_model_name}"
end

#plural_model_nameObject



53
54
55
# File 'lib/wunderlist/helper.rb', line 53

def plural_model_name
  "#{model_name}s"
end

#resource_pathObject



38
39
40
# File 'lib/wunderlist/helper.rb', line 38

def resource_path
  "api/v1/#{plural_model_name}/#{self.id}"
end

#saveObject



22
23
24
25
26
27
28
29
# File 'lib/wunderlist/helper.rb', line 22

def save
  if self.id.nil?
    res = self.create
  else
    res = self.update
  end
  set_attrs(res)
end

#to_hashObject



4
5
6
7
8
9
10
11
12
# File 'lib/wunderlist/helper.rb', line 4

def to_hash
  i_vs = self.instance_variables
  i_vs.delete_if {|i_v| i_v.to_s == '@api'}
  hash = {}
  i_vs.each {|var| hash[var.to_s.delete("@")] = self.instance_variable_get(var) }

  hash

end

#updateObject



14
15
16
# File 'lib/wunderlist/helper.rb', line 14

def update
  self.api.request :put, resource_path, self.to_hash
end