Class: Wunderlist::API

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

Overview

The API class provides access to the Wunderlist API over HTTP.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of API.



53
54
55
56
57
58
# File 'lib/wunderlist/api.rb', line 53

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)

Domain of the Wunderlist API



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

def domain
  @domain
end

#emailObject (readonly)

Your email address from login



47
48
49
# File 'lib/wunderlist/api.rb', line 47

def email
  @email
end

#pathObject (readonly)

Path of the Wunderlist API



43
44
45
# File 'lib/wunderlist/api.rb', line 43

def path
  @path
end

#sessionObject (readonly)

Wunderlist Session ID



51
52
53
# File 'lib/wunderlist/api.rb', line 51

def session
  @session
end

Instance Method Details

#create_list(name) ⇒ Object

Create new empty List



132
133
134
# File 'lib/wunderlist/api.rb', line 132

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

#destroy(obj) ⇒ Object

Destroy List or Task



148
149
150
151
152
153
154
# File 'lib/wunderlist/api.rb', line 148

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

Delete internal list caching



85
86
87
# File 'lib/wunderlist/api.rb', line 85

def flush
  @lists = nil
end

#inboxObject

Get INBOX list



98
99
100
# File 'lib/wunderlist/api.rb', line 98

def inbox
  lists.values.detect { |list| list.inbox }
end

#listsObject

Return all lists



91
92
93
94
# File 'lib/wunderlist/api.rb', line 91

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

#login(email, password) ⇒ Object

Request new session and connects it with your login credentials



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wunderlist/api.rb', line 62

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

#login_by_session(sessid) ⇒ Object

Login with a session ID without login credentials



77
78
79
80
81
# File 'lib/wunderlist/api.rb', line 77

def (sessid)
  return if @logged_in
  @logged_in = true
  @session = sessid
end

#save(obj) ⇒ Object

Save List or Task



138
139
140
141
142
143
144
# File 'lib/wunderlist/api.rb', line 138

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

Load and parse tasks from Wunderlist API



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/wunderlist/api.rb', line 104

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