Class: Todoable::User

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

Constant Summary collapse

AUTH_PATH =
"/authenticate"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'lib/todoable.rb', line 13

def password
  @password
end

#tokenObject

Returns the value of attribute token.



13
14
15
# File 'lib/todoable.rb', line 13

def token
  @token
end

#usernameObject

Returns the value of attribute username.



13
14
15
# File 'lib/todoable.rb', line 13

def username
  @username
end

Class Method Details

.authenticate_user(username, password) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/todoable.rb', line 15

def self.authenticate_user(username, password)
    user = new
    user.username = username
    user.password = password
    user.token = self.get_token!(user)
    user
end

.get_token!(client) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/todoable.rb', line 23

def self.get_token!(client)
    req = RestClient::Request.new(
        method: :post,
        url: BASE_ROUTE+AUTH_PATH,
        user: client.username,
        password: client.password,
        headers: {
            content_type: :json,
            accept: :json
        }
    )
    resp = req.execute {|resp| resp} 
    if resp.code == 401
        return "Unauthorized"
    end 
    resp = JSON.parse(resp)
    
    resp['token']
end

Instance Method Details

#list_items(list_id) ⇒ Object



68
69
70
# File 'lib/todoable.rb', line 68

def list_items(list_id)
    Item.new(self, list_id)
end

#list_listsObject



64
65
66
# File 'lib/todoable.rb', line 64

def list_lists
    List.new(self)
end

#make_request(path, type, values = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/todoable.rb', line 43

def make_request(path, type, values = nil)
    data = {
        method: type,
        url: BASE_ROUTE+path,
        headers: {
            content_type: :json,
            accept: :json,
            authorization: "Token token=\"#{token}\""
        }
    }
    if values
        data.merge!(payload: values.to_json)
    end
    req = RestClient::Request.new(data)
    req = req.execute
    if type != :delete and type != :patch
        resp = JSON.parse(req)
        return resp
    end
end