Class: Gitcamp::Basecamp::Auth

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/gitcamp/basecamp/auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#usernameObject (readonly)

Returns the value of attribute username.



6
7
8
# File 'lib/gitcamp/basecamp/auth.rb', line 6

def username
  @username
end

Instance Method Details

#loginObject



9
10
11
12
13
14
15
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
# File 'lib/gitcamp/basecamp/auth.rb', line 9

def 
  say 'Authenticating with Basecamp', Thor::Shell::Color::YELLOW

  netrc = Netrc.read

  domain = 'simple-web.basecamphq.com'
  if netrc[domain]
    @username, token = netrc[domain]

    Basecamp::API::Classic.establish_connection!(domain, token, 'X', true)

    begin
      Basecamp::API::Classic::Person.me
      say "Authenticated as #{@username}", Thor::Shell::Color::GREEN
    rescue Octokit::Unauthorized => ex
      puts "Failed to authenticate you with those Basecamp credentials"
      exit false
    end
  else
    @username = ask 'Username:'
    password = ask_password 'Password:'

    Basecamp::API::Classic.establish_connection!(domain, @username, password, true)

    begin
      Basecamp::API::Classic::Person.me
      say "Authenticated as #{@username}", Thor::Shell::Color::GREEN
    rescue Octokit::Unauthorized => ex
      puts "Failed to authenticate you with those Basecamp credentials"
      exit false
    end

    token = Basecamp::API::Classic::Person.me.token

    unless netrc[domain]
      netrc[domain] = @username, token
      netrc.save
    end
  end
end