Class: Wunderlist::API
- Inherits:
-
Object
- Object
- Wunderlist::API
- Defined in:
- lib/wunderlist/api.rb
Constant Summary collapse
- API_URL =
"https://a.wunderlist.com"
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
Instance Method Summary collapse
- #delete(url, options = {}) ⇒ Object
- #get(url, options = {}) ⇒ Object
- #get_list_ids(list_names = []) ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
- #list(list_name) ⇒ Object
- #lists ⇒ Object
- #new_list(list_name) ⇒ Object
- #new_task(list_name, attrs = {}) ⇒ Object
- #post(url, options = {}) ⇒ Object
- #put(url, options = {}) ⇒ Object
- #request(method, url, options = {}) ⇒ Object
- #tasks(list_names = [], completed = false) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
15 16 17 18 19 20 21 22 |
# File 'lib/wunderlist/api.rb', line 15 def initialize( = {}) @conn = Faraday::Connection.new(:url => API_URL) do |builder| builder.use Faraday::Request::UrlEncoded builder.use Faraday::Adapter::NetHttp end @access_token = [:access_token] @client_id = [:client_id] end |
Instance Attribute Details
#access_token ⇒ Object (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_id ⇒ Object (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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/wunderlist/api.rb', line 146 def delete(url, = {}) response = @conn.delete do |req| req.url url req.params[:revision] = [: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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/wunderlist/api.rb', line 95 def get(url, = {}) response = @conn.get do |req| req.url url if .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
164 165 166 167 168 169 170 |
# File 'lib/wunderlist/api.rb', line 164 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
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wunderlist/api.rb', line 32 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 |
#lists ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/wunderlist/api.rb', line 43 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
24 25 26 27 28 29 30 |
# File 'lib/wunderlist/api.rb', line 24 def new_list(list_name) list = Wunderlist::List.new('title' => list_name) list.api = self list end |
#new_task(list_name, attrs = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/wunderlist/api.rb', line 74 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 |
#post(url, options = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/wunderlist/api.rb', line 114 def post(url, = {}) response = @conn.post do |req| req.url url req.body = .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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/wunderlist/api.rb', line 130 def put(url, = {}) response = @conn.put do |req| req.url url req.body = .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
86 87 88 89 90 91 92 93 |
# File 'lib/wunderlist/api.rb', line 86 def request(method, url, = {}) case method when :get then self.get(url, ) when :post then self.post(url, ) when :put then self.put(url, ) when :delete then self.delete(url, ) end end |
#tasks(list_names = [], completed = false) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/wunderlist/api.rb', line 56 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 |