Module: Tane::Helpers::ClassMethods
- Defined in:
- lib/tane/helpers.rb
Instance Method Summary collapse
- #authenticate_user ⇒ Object
- #base_url ⇒ Object
- #bushido_app_exists? ⇒ Boolean
- #bushido_dir ⇒ Object
- #bushido_envs ⇒ Object
- #bushido_url ⇒ Object
- #credentials ⇒ Object
- #credentials_file_path ⇒ Object
- #data_url ⇒ Object
- #destroy_credentials ⇒ Object
- #email_template_file_path(template_name) ⇒ Object
- #email_templates_path ⇒ Object
- #envs_url ⇒ Object
- #in_rails_dir? ⇒ Boolean
- #logged_in? ⇒ Boolean
- #mail_url ⇒ Object
- #make_app_bushido_dir ⇒ Object
- #make_global_bushido_dir ⇒ Object
- #opts ⇒ Object
- #password ⇒ Object
- #post(url, data) ⇒ Object
- #prompt_for_credentials ⇒ Object
- #save_credentials(username, password) ⇒ Object
- #support_url ⇒ Object
- #tane_file_path ⇒ Object
- #term ⇒ Object
- #username ⇒ Object
- #verbose_say(message) ⇒ Object
- #warn_if_credentials ⇒ Object
Instance Method Details
#authenticate_user ⇒ Object
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_url ⇒ Object
137 138 139 |
# File 'lib/tane/helpers.rb', line 137 def base_url "#{opts.scheme}://#{opts.host}:#{opts.port}" end |
#bushido_app_exists? ⇒ 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_dir ⇒ Object
30 31 32 |
# File 'lib/tane/helpers.rb', line 30 def bushido_dir "#{ENV['HOME']}/.bushido" end |
#bushido_envs ⇒ Object
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_url ⇒ Object
26 27 28 |
# File 'lib/tane/helpers.rb', line 26 def bushido_url ENV['BUSHIDO_URL'] || "http://bushi.do" end |
#credentials ⇒ Object
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_path ⇒ Object
46 47 48 |
# File 'lib/tane/helpers.rb', line 46 def credentials_file_path "#{bushido_dir}/credentials.yml" end |
#data_url ⇒ Object
141 142 143 |
# File 'lib/tane/helpers.rb', line 141 def data_url "#{base_url}/bushido/data" end |
#destroy_credentials ⇒ Object
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_path ⇒ Object
38 39 40 |
# File 'lib/tane/helpers.rb', line 38 def email_templates_path ".bushido/emails" end |
#envs_url ⇒ Object
149 150 151 |
# File 'lib/tane/helpers.rb', line 149 def envs_url "#{base_url}/bushido/envs" end |
#in_rails_dir? ⇒ 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
58 59 60 |
# File 'lib/tane/helpers.rb', line 58 def logged_in? File.exists?(credentials_file_path) end |
#mail_url ⇒ Object
153 154 155 |
# File 'lib/tane/helpers.rb', line 153 def mail_url "#{base_url}/bushido/mail" end |
#make_app_bushido_dir ⇒ Object
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_dir ⇒ Object
88 89 90 |
# File 'lib/tane/helpers.rb', line 88 def make_global_bushido_dir FileUtils.mkdir_p bushido_dir end |
#opts ⇒ Object
22 23 24 |
# File 'lib/tane/helpers.rb', line 22 def opts @opts ||= OpenStruct.new end |
#password ⇒ Object
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_credentials ⇒ Object
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_url ⇒ Object
145 146 147 |
# File 'lib/tane/helpers.rb', line 145 def support_url "#{bushido_url}/support/v1/message" end |
#tane_file_path ⇒ Object
42 43 44 |
# File 'lib/tane/helpers.rb', line 42 def tane_file_path ".bushido/tane.yml" end |
#term ⇒ Object
14 15 16 |
# File 'lib/tane/helpers.rb', line 14 def term @hl ||= HighLine.new end |
#username ⇒ Object
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() term.say() if opts.verbose end |
#warn_if_credentials ⇒ Object
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 |