Module: MyShows::Auth

Included in:
CLI
Defined in:
lib/my_shows/auth.rb

Instance Method Summary collapse

Instance Method Details

#askObject



3
4
5
# File 'lib/my_shows/auth.rb', line 3

def ask
  $stdin.gets.to_s.strip
end

#ask_for_and_save_credentialsObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/my_shows/auth.rb', line 38

def ask_for_and_save_credentials
  begin
    @credentials = ask_for_credentials
    write_credentials
    check
  rescue Exception => e
    delete_credentials
    raise e
  end
  @credentials
end

#ask_for_credentialsObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/my_shows/auth.rb', line 61

def ask_for_credentials
  puts "Enter your Heroku credentials."

  print "Email: "
  user = ask

  print "Password (typing will be hidden): "
  password = ask_for_password

  [user, password]
end

#ask_for_passwordObject



73
74
75
76
77
78
79
# File 'lib/my_shows/auth.rb', line 73

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  return password
end

#checkObject



85
86
87
88
# File 'lib/my_shows/auth.rb', line 85

def check
  client = SidereelClient.new *self.credentials
  client.tracked_tv_shows
end

#credentialsObject



30
31
32
# File 'lib/my_shows/auth.rb', line 30

def credentials
  @credentials ||= (read_credentials || ask_for_and_save_credentials)
end

#delete_credentialsObject



50
51
52
53
54
# File 'lib/my_shows/auth.rb', line 50

def delete_credentials
  netrc.delete(host)
  netrc.save
  @credentials = nil
end

#echo_offObject



14
15
16
17
18
# File 'lib/my_shows/auth.rb', line 14

def echo_off
  with_tty do
    system "stty -echo"
  end
end

#echo_onObject



20
21
22
23
24
# File 'lib/my_shows/auth.rb', line 20

def echo_on
  with_tty do
    system "stty echo"
  end
end

#hostObject



81
82
83
# File 'lib/my_shows/auth.rb', line 81

def host
  'sidereel.com'
end

#netrcObject



26
27
28
# File 'lib/my_shows/auth.rb', line 26

def netrc
  @netrc ||= Netrc.read
end

#read_credentialsObject



34
35
36
# File 'lib/my_shows/auth.rb', line 34

def read_credentials
  netrc[host]
end

#with_tty(&block) ⇒ Object



7
8
9
10
11
12
# File 'lib/my_shows/auth.rb', line 7

def with_tty(&block)
  return unless $stdin.isatty
  yield
rescue
  logger.warn 'with_tty faild'
end

#write_credentialsObject



56
57
58
59
# File 'lib/my_shows/auth.rb', line 56

def write_credentials
  netrc[host] = self.credentials
  netrc.save
end