Class: Wunderlist::API

Inherits:
Object
  • Object
show all
Defined in:
lib/wunderlist/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain = "www.wunderlist.com", path = "/") ⇒ API

Returns a new instance of API.



37
38
39
40
41
42
# File 'lib/wunderlist/api.rb', line 37

def initialize(domain = "www.wunderlist.com", path = "/")
  @domain = domain
  @path = path
  @http = Net::HTTP.new(@domain)
  @logged_in = false
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



35
36
37
# File 'lib/wunderlist/api.rb', line 35

def domain
  @domain
end

#emailObject (readonly)

Returns the value of attribute email.



35
36
37
# File 'lib/wunderlist/api.rb', line 35

def email
  @email
end

#pathObject (readonly)

Returns the value of attribute path.



35
36
37
# File 'lib/wunderlist/api.rb', line 35

def path
  @path
end

#sessionObject (readonly)

Returns the value of attribute session.



35
36
37
# File 'lib/wunderlist/api.rb', line 35

def session
  @session
end

Instance Method Details

#create_list(name) ⇒ Object



96
97
98
# File 'lib/wunderlist/api.rb', line 96

def create_list(name)
  Wunderlist::List.new(name, false, self).save
end

#destroy(obj) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/wunderlist/api.rb', line 108

def destroy(obj)
  if obj.is_a? Wunderlist::List
    return destroy_list obj
  elsif obj.is_a? Wunderlist::Task
    return destroy_task obj
  end
end

#flushObject



57
58
59
# File 'lib/wunderlist/api.rb', line 57

def flush
  @lists = nil
end

#inboxObject



66
67
68
# File 'lib/wunderlist/api.rb', line 66

def inbox
  lists.to_a.first[1]
end

#listsObject



61
62
63
64
# File 'lib/wunderlist/api.rb', line 61

def lists
  @lists = load_lists if @lists == nil
  @lists
end

#login(email, password) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wunderlist/api.rb', line 44

def (email, password)
  get_session if @session == nil
  return true if @logged_in
  @email = email

  req = prepare_request(Net::HTTP::Post.new "#{@path}/ajax/user")
  req.set_form_data({ "email" => @email, "password" => Digest::MD5.hexdigest(password) })
  res = JSON.parse(@http.request(req).body)

  @logged_in = true if res["code"] == 200
  @logged_in
end

#save(obj) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/wunderlist/api.rb', line 100

def save(obj)
  if obj.is_a? Wunderlist::List
    return save_list obj
  elsif obj.is_a? Wunderlist::Task
    return save_task obj
  end
end

#tasks(list) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/wunderlist/api.rb', line 70

def tasks(list)
  list_obj = list.is_a?(Wunderlist::List) ? list : lists[list]
  list = list.id if list.is_a? Wunderlist::List

  request = prepare_request(Net::HTTP::Get.new "#{@path}/ajax/lists/id/#{list}")
  response = @http.request request
  result = []

  Nokogiri::HTML(JSON.parse(response.body)["data"]).css("li.more").each do |html_task|
    task = Wunderlist::Task.new
    task.id = html_task.attributes["id"].value.to_i
    task.name = html_task.css("span.description").first.content
    task.important = html_task.css("span.fav").empty? ? false : true
    task.done = html_task.attributes["class"].value.split(" ").include?("done")
    html_timestamp = html_task.css("span.timestamp")
    task.date = Time.at(html_timestamp.first.attributes["rel"].
    value.to_i).to_date unless html_timestamp.empty?
    task.api = self
    task.list = list_obj

    result << task
  end

  result
end