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.



15
16
17
18
19
20
21
22
23
# File 'lib/wunderlist/api.rb', line 15

def initialize(options = {})
  @conn = Faraday::Connection.new(:url => API_URL) do |builder|
    builder.use Faraday::Request::UrlEncoded
    builder.use Faraday::Response::Logger
    builder.use Faraday::Adapter::NetHttp
  end
  @access_token = options[:access_token]
  @client_id = options[:client_id]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



10
11
12
# File 'lib/wunderlist/api.rb', line 10

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

Instance Method Details

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



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

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/wunderlist/api.rb', line 85

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



154
155
156
157
158
159
160
# File 'lib/wunderlist/api.rb', line 154

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



25
26
27
28
29
30
31
32
33
34
# File 'lib/wunderlist/api.rb', line 25

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



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

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_task(list_name, attrs = {}) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/wunderlist/api.rb', line 67

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
  attrs['api'] = self
  @task = Wunderlist::Task.new(attrs)
end

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/wunderlist/api.rb', line 104

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' => 'text/json',
      'Content-Encoding' => 'UTF-8'
    }
  end

  JSON.parse(response.body)
end

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/wunderlist/api.rb', line 120

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' => 'text/json',
      'Content-Encoding' => 'UTF-8'
    }
  end

  JSON.parse(response.body)
end

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



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

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wunderlist/api.rb', line 49

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