Class: TdCLI::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(token = nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/todoist/client.rb', line 11

def initialize(token = nil)
    # initialize the client with a token
    if token.nil?
        @file_path = File.join(DATA_DIR, 'tdcli')
        if File.exist?(@file_path)
            @todoist_token = File.open(@file_path, &:readline).strip
        else
            newAPIToken()
        end
    else
        @todoist_token = token
    end
end

Instance Method Details

#newAPITokenObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/todoist/client.rb', line 25

def newAPIToken
    # make sure the data dir exists
    if !File.directory?(DATA_DIR)
        FileUtils.mkdir(DATA_DIR)
    end
    puts 'Enter an API token:'
    token = gets.chomp
    # save token to file for later use
    File.open(@file_path, 'w') { |file| file.write(token) }
    @todoist_token = token

end

#sync(resource_types) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/todoist/client.rb', line 38

def sync(resource_types)
    options = {}
    options['token'] = @todoist_token
    options['seq_no'] = 0
    options['resource_types'] = JSON.generate resource_types

    uri = URI.parse(TODOIST_API_URL)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(uri.request_uri)
    request.set_form_data(options)

    response = http.request(request)

    data = JSON.parse response.body
    return data
end