Module: Tane::Helpers::ClassMethods

Defined in:
lib/tane/helpers.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_userObject



102
103
104
105
106
# File 'lib/tane/helpers.rb', line 102

def authenticate_user
  unless logged_in?
    Tane::Commands::Login.process(:exit => false)
  end
end

#base_urlObject



137
138
139
# File 'lib/tane/helpers.rb', line 137

def base_url
  "#{opts.scheme}://#{opts.host}:#{opts.port}"
end

#bushido_app_exists?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/tane/helpers.rb', line 66

def bushido_app_exists?
  File.exists?('.bushido/tane.yml') and File.exists?('.bushido/email.yml')
end

#bushido_dirObject



30
31
32
# File 'lib/tane/helpers.rb', line 30

def bushido_dir
  "#{ENV['HOME']}/.bushido"
end

#bushido_envsObject



133
134
135
# File 'lib/tane/helpers.rb', line 133

def bushido_envs
  YAML.load(ERB.new(File.read( '.bushido/tane.yml' )).result)
end

#bushido_urlObject



26
27
28
# File 'lib/tane/helpers.rb', line 26

def bushido_url
  ENV['BUSHIDO_URL'] || "http://bushi.do"
end

#credentialsObject



50
51
52
# File 'lib/tane/helpers.rb', line 50

def credentials
  YAML.load(ERB.new(File.read( credentials_file_path )).result)
end

#credentials_file_pathObject



46
47
48
# File 'lib/tane/helpers.rb', line 46

def credentials_file_path
  "#{bushido_dir}/credentials.yml"
end

#data_urlObject



141
142
143
# File 'lib/tane/helpers.rb', line 141

def data_url
  "#{base_url}/bushido/data"
end

#destroy_credentialsObject



54
55
56
# File 'lib/tane/helpers.rb', line 54

def destroy_credentials
  File.delete(credentials_file_path)
end

#email_template_file_path(template_name) ⇒ Object



34
35
36
# File 'lib/tane/helpers.rb', line 34

def email_template_file_path(template_name)
  "#{email_templates_path}/#{template_name}.yml"
end

#email_templates_pathObject



38
39
40
# File 'lib/tane/helpers.rb', line 38

def email_templates_path
  ".bushido/emails"
end

#envs_urlObject



149
150
151
# File 'lib/tane/helpers.rb', line 149

def envs_url
  "#{base_url}/bushido/envs"
end

#in_rails_dir?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/tane/helpers.rb', line 62

def in_rails_dir?
  Dir.exists?('./rails') || Dir.exists?('./script')
end

#logged_in?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/tane/helpers.rb', line 58

def logged_in?
  File.exists?(credentials_file_path)
end

#mail_urlObject



153
154
155
# File 'lib/tane/helpers.rb', line 153

def mail_url
  "#{base_url}/bushido/mail"
end

#make_app_bushido_dirObject



92
93
94
95
96
97
98
99
100
# File 'lib/tane/helpers.rb', line 92

def make_app_bushido_dir
  if bushido_app_exists?
    term.say("There's already a Bushido app created for this local rails app. If you'd like to create a new one, please remove the .bushido file in this directory")

    exit 1
  else
    FileUtils.mkdir_p '.bushido/emails'
  end
end

#make_global_bushido_dirObject



88
89
90
# File 'lib/tane/helpers.rb', line 88

def make_global_bushido_dir
  FileUtils.mkdir_p bushido_dir
end

#optsObject



22
23
24
# File 'lib/tane/helpers.rb', line 22

def opts
  @opts ||= OpenStruct.new
end

#passwordObject



79
80
81
82
83
84
85
86
# File 'lib/tane/helpers.rb', line 79

def password
  begin
    return credentials[:password]
  rescue
    term.say "Looks like your Bushido credentials file is corrupted - try deleting ~/.bushido/credentials.yml and then logging in again"
    exit 1
  end
end

#post(url, data) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/tane/helpers.rb', line 157

def post(url, data)
  data['key'] = bushido_envs["BUSHIDO_APP_KEY"]

  verbose_say("RestClient.put #{url}, #{data.inspect}, :content_type => :json, :accept => :json)")
  result = JSON(RestClient.put url, data, :content_type => :json, :accept => :json)
  verbose_say(result.inspect)

  result
end

#prompt_for_credentialsObject



116
117
118
119
120
# File 'lib/tane/helpers.rb', line 116

def prompt_for_credentials
  email = term.ask("Enter your email:     ") { |email| email }
  password = term.ask("Enter your password:  ") { |password| password.echo = false }
  return email.to_s, password.to_s
end

#save_credentials(username, password) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/tane/helpers.rb', line 108

def save_credentials(username, password)
  new_credentials = credentials rescue {}
  new_credentials[:username] = username
  new_credentials[:password] = password
  Dir.mkdir(bushido_dir) unless File.exists?(bushido_dir)
  File.open(credentials_file_path, 'w+') { |file| file.puts YAML.dump(new_credentials) }
end

#support_urlObject



145
146
147
# File 'lib/tane/helpers.rb', line 145

def support_url
  "#{bushido_url}/support/v1/message"
end

#tane_file_pathObject



42
43
44
# File 'lib/tane/helpers.rb', line 42

def tane_file_path
  ".bushido/tane.yml"
end

#termObject



14
15
16
# File 'lib/tane/helpers.rb', line 14

def term
  @hl ||= HighLine.new
end

#usernameObject



70
71
72
73
74
75
76
77
# File 'lib/tane/helpers.rb', line 70

def username
  begin
    return credentials[:username]
  rescue
    term.say "Looks like your Bushido credentials file is corrupted - try deleting ~/.bushido/credentials.yml and then logging in again"
    exit 1
  end
end

#verbose_say(message) ⇒ Object



18
19
20
# File 'lib/tane/helpers.rb', line 18

def verbose_say(message)
  term.say(message) if opts.verbose
end

#warn_if_credentialsObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/tane/helpers.rb', line 122

def warn_if_credentials
  if logged_in?
    if term.agree("This computer already has the Bushido user '#{username}' logged in, are you sure you would like to proceed? Y/N")
      term.say("Ok, continuing along like nothing happened")
    else
      term.say("Phew, I think we might have dodged a bullet there!")
      exit
    end
  end
end