Class: Blazemeter::Command::API

Inherits:
Blazemeter::Command show all
Defined in:
lib/blazemeter/command/api.rb

Overview

:nodoc:

Constant Summary

Constants inherited from Blazemeter::Command

Api

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#shift

Instance Attribute Details

#user_keyObject

Returns the value of attribute user_key.



4
5
6
# File 'lib/blazemeter/command/api.rb', line 4

def user_key
  @user_key
end

Instance Method Details

#ask_for_credentialsObject



66
67
68
69
70
71
# File 'lib/blazemeter/command/api.rb', line 66

def ask_for_credentials
      puts "Enter your BlazeMeter credentials. You can find this in https://a.blazemeter.com/user"
      print "API-Key: "
      apik = gets
      return apik.chomp.strip
end

#cmd_init(argv) ⇒ Object



6
7
8
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
# File 'lib/blazemeter/command/api.rb', line 6

def cmd_init argv
  @user_key = user_key
	#ENV['BLAZEMETER_APIKEY'] = '123'
	#get apikey stored on this computer
	if !@user_key
 @user_key = read_credentials[0]
	end
	
	if !@user_key
 #First get apikey from cmd line
 vars = Blazemeter::Command::Test.parse argv
 @user_key = vars["apikey"]
 
 #Secondly get apikey from heroku
 if !@user_key && ENV['BLAZEMETER_APIKEY']
puts "Heroku BlazeMeter APIKEY found. Use it? [y/n]"
use = gets.chomp.downcase
if use[0..0] == 'y'
  @user_key = ENV['BLAZEMETER_APIKEY']
end
 end
 #Thirdly get apikey by prompting user
 if !@user_key
   @user_key = ask_for_credentials
 end
 
 if @user_key
   write_credentials
   puts "Blazemeter API KEY stored."
 end
 else
    puts "BlazeMeter already initialized. Use blazemeter api:reset to change API KEY"	  
	end
end

#cmd_reset(argv) ⇒ Object



41
42
43
44
45
# File 'lib/blazemeter/command/api.rb', line 41

def cmd_reset argv
	@user_key = nil
	write_credentials
	puts "BlazeMeter is reset."
end

#cmd_validoptions(argv) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/blazemeter/command/api.rb', line 47

def cmd_validoptions argv
	user_key = Blazemeter::Common.get_user_key
	if !user_key
  puts "You must enter API Key. Use blazemeter api:init"
  return
end
	blaze = BlazemeterApi.new(user_key)
	puts "Valid options for your account: "
  puts blaze.getOptions()	
end

#credentials_fileObject



58
59
60
# File 'lib/blazemeter/command/api.rb', line 58

def credentials_file
    Blazemeter::Common.credentials_file
end

#read_credentialsObject



62
63
64
# File 'lib/blazemeter/command/api.rb', line 62

def read_credentials
    Blazemeter::Common.read_credentials
end

#set_credentials_permissionsObject



83
84
85
86
# File 'lib/blazemeter/command/api.rb', line 83

def set_credentials_permissions
    FileUtils.chmod 0700, File.dirname(credentials_file)
    FileUtils.chmod 0600, credentials_file        
end

#write_credentialsObject



75
76
77
78
79
80
81
# File 'lib/blazemeter/command/api.rb', line 75

def write_credentials
    FileUtils.mkdir_p(File.dirname(credentials_file))
    File.open(credentials_file, 'w') do |f|
      f.puts @user_key
    end
    set_credentials_permissions
end