Class: WunderMarkdown::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wunder_markdown/client.rb

Constant Summary collapse

API_ENDPOINT =
'api.wunderlist.com'
API_URL =
"https://#{API_ENDPOINT}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil) ⇒ Client

Returns a new instance of Client.



12
13
14
15
# File 'lib/wunder_markdown/client.rb', line 12

def initialize(token = nil)
  @token = token
  @conn = Faraday.new(:url => API_URL)
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



10
11
12
# File 'lib/wunder_markdown/client.rb', line 10

def conn
  @conn
end

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'lib/wunder_markdown/client.rb', line 10

def token
  @token
end

Instance Method Details

#get(url) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/wunder_markdown/client.rb', line 45

def get(url)
  response = conn.get do |req|
    req.url url
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = token
  end
  JSON.parse(response.body)
end

#list(list_name) ⇒ Object



31
32
33
34
# File 'lib/wunder_markdown/client.rb', line 31

def list(list_name)
  json = lists.detect { |l| l['title'] == list_name }
  List.new(json['id'], json['title'])
end

#listsObject



27
28
29
# File 'lib/wunder_markdown/client.rb', line 27

def lists
  @lists ||= get('me/lists')
end

#login(email, password) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/wunder_markdown/client.rb', line 17

def (email, password)
   = conn.post do |req|
    req.url '/login'
    req.headers['Content-Type'] = 'application/json'
    req.body = '{ "email": "'+ email +'", "password": "'+ password +'"}'
  end
  @token = JSON.parse(.body)['token']
  [email, @token]
end

#tasks(list) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/wunder_markdown/client.rb', line 36

def tasks(list)
  json = get("#{list.id}/tasks")
  json.map do |task_json|
    if task_json['completed_at'] == nil
      Task.new(task_json['id'], task_json['title'], task_json['note'], task_json['parent_id'])
    end
  end.compact
end