Class: Gist::User

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

Overview

Class to hold user information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil) ⇒ User

Create the GistUser instance



9
10
11
12
13
14
15
# File 'lib/gist/user.rb', line 9

def initialize(username = nil, password = nil)
  @username = username
  @password = password
  @http = Net::HTTP.new(Gist::API_URL, Gist::API_PORT)
  @http.use_ssl = true
  @access_token = load_token if saved?
end

Instance Attribute Details

#access_tokenObject

Personal access token



6
7
8
# File 'lib/gist/user.rb', line 6

def access_token
  @access_token
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/gist/user.rb', line 5

def password
  @password
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/gist/user.rb', line 4

def username
  @username
end

Instance Method Details

#authenticateObject

Authentication method, will set the authentication token for the user



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gist/user.rb', line 18

def authenticate
  # If we already have authenticated, don't let it happen again
  return unless @access_token.nil?
  # If not, let's authenticate
  @http.start do |http|
    response = http.request(auth_obj)
    json_response = JSON.parse(response.body)
    @access_token = json_response['token']
  end
  # Then save
  save
  populate_methods()
end