Class: CommitLive::User

Inherits:
Object
  • Object
show all
Defined in:
lib/commit-live/user.rb

Constant Summary collapse

DEFAULT_EDITOR =
'atom'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUser



12
13
14
# File 'lib/commit-live/user.rb', line 12

def initialize()
    @netrc = CommitLive::NetrcInteractor.new()
end

Instance Attribute Details

#netrcObject (readonly)

Returns the value of attribute netrc.



8
9
10
# File 'lib/commit-live/user.rb', line 8

def netrc
  @netrc
end

Instance Method Details

#confirmAndResetObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/commit-live/user.rb', line 72

def confirmAndReset
    if confirmReset?
      netrc.delete!(machine: 'ga-config')
      netrc.delete!(machine: 'ga-extra')
      puts "Sorry to see you go!"
    else
      puts "Thanks for being there with us!"
    end

    exit
end

#confirmReset?Boolean



84
85
86
87
88
89
90
91
# File 'lib/commit-live/user.rb', line 84

def confirmReset?
    puts "This will remove your existing login configuration and reset.\n"
    print "Are you sure you want to do this? [yN]: "

    response = STDIN.gets.chomp.downcase

    !!(response == 'yes' || response == 'y')
end

#save(userDetails, token) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/commit-live/user.rb', line 51

def save(userDetails, token)
  user_data = userDetails.fetch('data')
  username = user_data['userName']
  userID = user_data['id']
  netrc.write(new_login: userID, new_password: token)
  netrc.write(machine: 'ga-extra', new_login: username, new_password: userID)
  welcome(username)
end

#setDefaultWorkspaceObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/commit-live/user.rb', line 60

def setDefaultWorkspace
  workspaceDir = File.expand_path('~/Workspace/code')
  configPath = File.expand_path('~/.ga-config')

  FileUtils.mkdir_p(workspaceDir)
  FileUtils.touch(configPath)

  data = YAML.dump({ workspace: workspaceDir, editor: DEFAULT_EDITOR })

  File.write(configPath, data)
end

#validate(userID, token) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/commit-live/user.rb', line 16

def validate(userID, token)
  puts "Authenticating..."
  begin
    Timeout::timeout(15) do
      response = CommitLive::API.new().get(
        "/v2/users/#{userID}",
        headers: { 'Authorization' => "#{token}" }
      )
      if response.status == 200
        # Save valid user details in netrc
        user = JSON.parse(response.body)
        , password = netrc.read
        if .nil? || password.nil?
          save(user, token)
        else
          username = user.fetch('data')['userName']
          welcome(username)
        end
      else
        case response.status
              when 401
                 puts "It seems your OAuth token is incorrect. Please retry with correct token."
                 exit 1
              else
                 puts "Something went wrong. Please try again."
                 exit 1
              end
      end
    end
  rescue Timeout::Error
    puts "Please check your internet connection."
    exit
  end
end

#welcome(username) ⇒ Object



93
94
95
# File 'lib/commit-live/user.rb', line 93

def welcome(username)
  puts "Welcome, #{username}!"
end