class Wunderlist::API

Constants

API_URL

Attributes

access_token[R]
client_id[R]
conn[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/wunderlist/api.rb, line 19
def initialize(options = {})
  @access_token = options[:access_token]
  @client_id = options[:client_id]
end

Public Instance Methods

delete(url, options = {}) click to toggle source
# File lib/wunderlist/api.rb, line 182
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 = {}) click to toggle source
# File lib/wunderlist/api.rb, line 131
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 = []) click to toggle source
# File lib/wunderlist/api.rb, line 200
def get_list_ids(list_names = [])
  if list_names.is_a? Array and list_names.all? {|i| i.is_a?(Integer) }
    return list_names
  end
  return [list_names] if list_names.is_a? Integer
  list_names = [list_names] if list_names.is_a? String

  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_or_id) click to toggle source
# File lib/wunderlist/api.rb, line 37
def list(list_name_or_id)
  list_id = get_list_ids(list_name_or_id).first

  res_list = self.request :get, "api/v1/lists/#{list_id}"
  list = Wunderlist::List.new(res_list)
  list.api = self

  list

end
lists() click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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_or_id, attrs = {}) click to toggle source
# File lib/wunderlist/api.rb, line 111
def new_webhook(list_name_or_id, attrs = {})
  list_id = get_list_ids(list_name_or_id).first
  attrs.stringify_keys
  attrs['list_id'] = list_id
  task = Wunderlist::Webhook.new(attrs)
  task.api = self

  task

end
post(url, options = {}) click to toggle source
# File lib/wunderlist/api.rb, line 150
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 = {}) click to toggle source
# File lib/wunderlist/api.rb, line 166
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 = {}) click to toggle source
# File lib/wunderlist/api.rb, line 122
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_or_ids = [], completed = false) click to toggle source
# File lib/wunderlist/api.rb, line 72
def tasks(list_names_or_ids = [], completed = false)
  list_ids = get_list_ids(list_names_or_ids)
  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
user() click to toggle source
# 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_or_id) click to toggle source
# File lib/wunderlist/api.rb, line 61
def webhooks(list_name_or_id)
  list_id = get_list_ids(list_name_or_id).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