Module: Tane::Helpers::ClassMethods

Defined in:
lib/tane/helpers.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_userObject



104
105
106
107
108
# File 'lib/tane/helpers.rb', line 104

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

#base_urlObject



139
140
141
# File 'lib/tane/helpers.rb', line 139

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

#cloudfuji_app_exists?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/tane/helpers.rb', line 68

def cloudfuji_app_exists?
  File.exists?('.cloudfuji/tane.yml') and File.directory?('.cloudfuji/emails')
end

#cloudfuji_dirObject



32
33
34
# File 'lib/tane/helpers.rb', line 32

def cloudfuji_dir
  "#{ENV['HOME']}/.cloudfuji"
end

#cloudfuji_envsObject



135
136
137
# File 'lib/tane/helpers.rb', line 135

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

#cloudfuji_urlObject



28
29
30
# File 'lib/tane/helpers.rb', line 28

def cloudfuji_url
  ENV['CLOUDFUJI_URL'] || "http://cloudfuji.com"
end

#credentialsObject



52
53
54
# File 'lib/tane/helpers.rb', line 52

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

#credentials_file_pathObject



48
49
50
# File 'lib/tane/helpers.rb', line 48

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

#data_urlObject



143
144
145
# File 'lib/tane/helpers.rb', line 143

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

#destroy_credentialsObject



56
57
58
# File 'lib/tane/helpers.rb', line 56

def destroy_credentials
  File.delete(credentials_file_path)
end

#email_template_file_path(template_name) ⇒ Object



36
37
38
# File 'lib/tane/helpers.rb', line 36

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

#email_templates_pathObject



40
41
42
# File 'lib/tane/helpers.rb', line 40

def email_templates_path
  ".cloudfuji/emails"
end

#envs_urlObject



151
152
153
# File 'lib/tane/helpers.rb', line 151

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

#in_rails_dir?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/tane/helpers.rb', line 64

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

#logged_in?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/tane/helpers.rb', line 60

def logged_in?
  File.exists?(credentials_file_path)
end

#mail_urlObject



155
156
157
# File 'lib/tane/helpers.rb', line 155

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

#make_app_cloudfuji_dirObject



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

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

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

#make_global_cloudfuji_dirObject



90
91
92
# File 'lib/tane/helpers.rb', line 90

def make_global_cloudfuji_dir
  FileUtils.mkdir_p cloudfuji_dir
end

#optsObject



24
25
26
# File 'lib/tane/helpers.rb', line 24

def opts
  @opts ||= OpenStruct.new
end

#passwordObject



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

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

#post(url, data) ⇒ Object



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

def post(url, data)
  data['key'] = cloudfuji_envs["CLOUDFUJI_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



118
119
120
121
122
# File 'lib/tane/helpers.rb', line 118

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

#repeat_every(interval, &block) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/tane/helpers.rb', line 169

def repeat_every(interval, &block)
  Thread.new do
    loop do
      start_time = Time.now
      yield
      elapsed = Time.now - start_time
      sleep([interval - elapsed, 0].max)
    end
  end
end

#save_credentials(username, password) ⇒ Object



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

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

#should_throbObject



201
202
203
# File 'lib/tane/helpers.rb', line 201

def should_throb
  @should_throb
end

#should_throb=(value) ⇒ Object



205
206
207
# File 'lib/tane/helpers.rb', line 205

def should_throb=(value)
  @should_throb = value
end

#start_throbber!Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/tane/helpers.rb', line 180

def start_throbber!
  throbber_frames = ['|', '/', '-', '\\', 'X']
  frame_counter = 0

  @should_throb = true

  thread = repeat_every(0.5) do
    if @should_throb
      print "\x08\x08\x08"
      frame_counter += 1
      frame_counter = 0 if frame_counter == throbber_frames.length
      print "#{throbber_frames[frame_counter]}  "
    end
  end

end

#stop_throbber!Object



197
198
199
# File 'lib/tane/helpers.rb', line 197

def stop_throbber!
  @should_throb = false
end

#support_urlObject



147
148
149
# File 'lib/tane/helpers.rb', line 147

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

#suppress_env_vars(*vars, &block) ⇒ Object

Takes a list of ENV vars, records their value, sets them to null, runs the block, then ensures the ENV vars are restored at the end.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/tane/helpers.rb', line 212

def suppress_env_vars(*vars, &block)
  cache = {}
  vars.each do |var|
    cache[var] = ENV[var]
  end

  begin      
    vars.each do |var|
      ENV[var] = nil
    end
    yield block
  ensure
    cache.each_pair do |key, value|
      ENV[key] = value
    end
  end
end

#tane_file_pathObject



44
45
46
# File 'lib/tane/helpers.rb', line 44

def tane_file_path
  ".cloudfuji/tane.yml"
end

#termObject



16
17
18
# File 'lib/tane/helpers.rb', line 16

def term
  @hl ||= HighLine.new
end

#try_for(options, &block) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/tane/helpers.rb', line 230

def try_for(options, &block)
  options[:seconds] ||= 30
  options[:sleep]   ||= 5

  start = Time.now
  elapsed = 0

  while elapsed < options[:seconds]
    return true if yield elapsed
    sleep options[:sleep]
    elapsed = Time.now - start
  end

  return false
end

#usernameObject



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

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

#verbose_say(message) ⇒ Object



20
21
22
# File 'lib/tane/helpers.rb', line 20

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

#warn_if_credentialsObject



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

def warn_if_credentials
  if logged_in?
    if term.agree("This computer already has the Cloudfuji 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