Class: Wunderlist::API

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

Constant Summary collapse

API_URL =
"https://a.wunderlist.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



19
20
21
22
# File 'lib/wunderlist/api.rb', line 19

def initialize(options = {})
  @access_token = options[:access_token]
  @client_id = options[:client_id]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



12
13
14
# File 'lib/wunderlist/api.rb', line 12

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



13
14
15
# File 'lib/wunderlist/api.rb', line 13

def client_id
  @client_id
end

#connObject

Returns the value of attribute conn.



15
16
17
# File 'lib/wunderlist/api.rb', line 15

def conn
  @conn
end

Instance Method Details

#delete(url, options = {}) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/wunderlist/api.rb', line 183

def delete(url, options = {})

  response = conn.delete do |req|
    req.url url
    req.params[:revision] = options[:revision]
    req.headers = {
      'X-Access-Token' => self.access_token,
      'X-Client-ID' => self.client_id,
      'Content-Encoding' => 'UTF-8'
    }
  end

  response.status

end

#get(url, options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/wunderlist/api.rb', line 132

def get(url, options = {})

  response = conn.get do |req|
    req.url url
    if options
      options.each do |k, v|
        req.params[k] = v
      end
    end
    req.headers = {
      'X-Access-Token' => self.access_token,
      'X-Client-ID' => self.client_id
    }
  end

  JSON.parse(response.body)

end

#get_list_ids(list_names = []) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/wunderlist/api.rb', line 201

def get_list_ids(list_names = [])
  lists = self.lists
  if !list_names.empty?
    lists = lists.select{|elm| list_names.include?(elm.title)}
  end
  lists.map{|list| list.id}
end

#list(list_name) ⇒ Object



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

def list(list_name)
  list_name = [list_name]
  list_id = get_list_ids(list_name)[0]
  res_list = self.request :get, "api/v1/lists/#{list_id}"
  list = Wunderlist::List.new(res_list)
  list.api = self

  list

end

#listsObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wunderlist/api.rb', line 48

def lists
  res_lists = self.request :get, 'api/v1/lists'
  lists = []
  res_lists.each do |l|
    list = Wunderlist::List.new(l)
    list.api = self
    lists << list
  end

  lists

end

#new_list(list_name) ⇒ Object



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

def new_list(list_name)
  list = Wunderlist::List.new('title' => list_name)
  list.api = self
  list
end

#new_task(list_name, attrs = {}) ⇒ Object



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

def new_task(list_name, attrs = {})
  attrs.stringify_keys
  list_name = [list_name]
  list_id = get_list_ids(list_name)[0]
  attrs['list_id'] = list_id
  task = Wunderlist::Task.new(attrs)
  task.api = self

  task

end

#new_webhook(list_name, attrs = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/wunderlist/api.rb', line 111

def new_webhook(list_name, attrs = {})
  attrs.stringify_keys
  list_name = [list_name]
  list_id = get_list_ids(list_name)[0]
  attrs['list_id'] = list_id
  task = Wunderlist::Webhook.new(attrs)
  task.api = self

  task

end

#post(url, options = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/wunderlist/api.rb', line 151

def post(url, options = {})

  response = conn.post do |req|
    req.url url
    req.body = options.to_json
    req.headers = {
      'X-Access-Token' => self.access_token,
      'X-Client-ID' => self.client_id,
      'Content-Type' => 'application/json',
      'Content-Encoding' => 'UTF-8'
    }
  end

  JSON.parse(response.body)
end

#put(url, options = {}) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/wunderlist/api.rb', line 167

def put(url, options = {})

  response = conn.put do |req|
    req.url url
    req.body = options.to_json
    req.headers = {
      'X-Access-Token' => self.access_token,
      'X-Client-ID' => self.client_id,
      'Content-Type' => 'application/json',
      'Content-Encoding' => 'UTF-8'
    }
  end

  JSON.parse(response.body)
end

#request(method, url, options = {}) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/wunderlist/api.rb', line 123

def request(method, url, options = {})
  case method
  when :get then self.get(url, options)
  when :post then self.post(url, options)
  when :put then self.put(url, options)
  when :delete then self.delete(url, options)
  end
end

#tasks(list_names = [], completed = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wunderlist/api.rb', line 72

def tasks(list_names = [], completed = false)
  list_ids = get_list_ids(list_names)
  tasks = []
  list_ids.each do |list_id|
    res_tasks = self.request :get, 'api/v1/tasks', {:list_id => list_id, :completed => completed}
    if !res_tasks.empty?
      res_tasks.each do |t|
        task = Wunderlist::Task.new(t)
        task.api = self
        tasks << task
      end
    end
  end

  tasks

end

#userObject



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

def user()
  res_user = self.request :get, 'api/v1/user'
  user = Wunderlist::User.new(res_user)
  user.api = self

  user
end

#webhooks(list_name) ⇒ Object



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

def webhooks(list_name)
  list_id = get_list_ids([list_name]).first

  res_webhooks = self.request :get, 'api/v1/webhooks', { :list_id => list_id }
  res_webhooks.reduce([]) do |webhooks, webhook|
    webhook = Wunderlist::Webhook.new(webhook)
    webhook.api = self
    webhooks << webhook
  end
end