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
- #create_task(list_name, attrs = {}) ⇒ Object
- #get(url, options = {}) ⇒ Object
- #get_list_ids(list_names = []) ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
- #lists ⇒ 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.
14 15 16 17 18 19 20 21 22 |
# File 'lib/wunderlist/api.rb', line 14 def initialize( = {}) @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 = [:access_token] @client_id = [:client_id] end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
9 10 11 |
# File 'lib/wunderlist/api.rb', line 9 def access_token @access_token end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
10 11 12 |
# File 'lib/wunderlist/api.rb', line 10 def client_id @client_id end |
Instance Method Details
#create_task(list_name, attrs = {}) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/wunderlist/api.rb', line 46 def create_task(list_name, attrs = {}) 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) @task.save end |
#get(url, options = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/wunderlist/api.rb', line 63 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
115 116 117 118 119 120 121 |
# File 'lib/wunderlist/api.rb', line 115 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 |
#lists ⇒ Object
24 25 26 |
# File 'lib/wunderlist/api.rb', line 24 def lists self.request :get, 'api/v1/lists' end |
#post(url, options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/wunderlist/api.rb', line 82 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/wunderlist/api.rb', line 98 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
55 56 57 58 59 60 61 |
# File 'lib/wunderlist/api.rb', line 55 def request(method, url, = {}) case method when :get then self.get(url, ) when :post then self.post(url, ) when :put then self.put(url, ) end end |
#tasks(list_names = [], completed = false) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/wunderlist/api.rb', line 28 def tasks(list_names = [], completed = false) list_ids = get_list_ids(list_names) tasks = [] list_ids.each do |list_id| list_tasks = self.request :get, 'api/v1/tasks', {:list_id => list_id, :completed => completed} if !list_tasks.empty? list_tasks.each do |t| task = Wunderlist::Task.new(t) task.api = self tasks << task end end end tasks end |