Class: Behave::FeatureDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/behave.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ FeatureDownloader

Returns a new instance of FeatureDownloader.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/behave.rb', line 16

def initialize(args)
  @dir = "features/"
  @manual = false
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil? or k == 'contents'
  end
  if @host.nil? or key.nil?
    puts '--------------------------------------------------------------'
    puts 'Must set at least Jira host URI and project key'
    puts 'if running from command line, use "behave --help"'
    puts '--------------------------------------------------------------'
    exit 1
  end
  @dir << '/' unless @dir[-1,1] == '/'
  @host << '/' unless @host[-1,1] == '/'
  output_vars()
  fetch()
  extract()
end

Instance Attribute Details

#bypass_sslObject

Returns the value of attribute bypass_ssl.



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

def bypass_ssl
  @bypass_ssl
end

#contentsObject

Returns the value of attribute contents.



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

def contents
  @contents
end

#dirObject

Returns the value of attribute dir.



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

def dir
  @dir
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#manualObject

Returns the value of attribute manual.



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

def manual
  @manual
end

#passObject

Returns the value of attribute pass.



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

def pass
  @pass
end

#proxyObject

Returns the value of attribute proxy.



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

def proxy
  @proxy
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#extractObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/behave.rb', line 102

def extract()
  return if @contents.nil?
  count = 0
  Dir.mkdir(@dir) unless File.exists?(@dir)
  File.open(@dir + 'features.zip', 'wb') {|f| f.write(@contents) }
  Zip::ZipFile::foreach("#{@dir}features.zip") {|feature|
    path = @dir + feature.to_s
    File.delete(path) if File.exists?(path)
    feature.extract(path)
    count += 1
  }
  File.delete(@dir + 'features.zip')
  puts '--------------------------------------------------------------'
  puts 'Successfully extracted ' + count.to_s + ' feature(s)'
  puts '--------------------------------------------------------------'
end

#fetchObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/behave.rb', line 47

def fetch()
  RestClient.proxy = @proxy unless @proxy.nil?
  ssl = if @bypass_ssl then OpenSSL::SSL::VERIFY_NONE else OpenSSL::SSL::VERIFY_PEER end
  path = @host + 'rest/cucumber/1.0/project/' + @key.to_s + '/features'
  if @user.nil? then
    resource = RestClient::Resource.new(path, :content_type => 'application/zip', :verify_ssl => ssl)
  else
    resource = RestClient::Resource.new(path, :user => @user, :password => @pass, :content_type => 'application/zip', :verify_ssl => ssl)
  end
  begin
    @contents = resource.get(:accept => 'application/zip', :params => {:manual => @manual})
    puts '--------------------------------------------------------------'
    puts 'Fetched file from server...'
  rescue => e
    puts '--------------------------------------------------------------'
    if e.respond_to?('response') then
      if e.response.respond_to?('code') then
        case e.response.code
          when 401
            puts 'User unauthorized (401)'
            puts 'Could not authenticate using supplied username and password'
          when 403
            puts 'User forbidden (403)'
            puts 'Too many login attempts with this user'
          when 404
            puts 'Not Found (404)'
            puts 'Could not find the project specified'
          when 405
            puts 'Behave running on server is outdated, cannot download features'
            puts 'Please upgrade your version of Behave for Jira and try again'
          when 406
            puts 'Behave running on server is outdated, cannot download features'
            puts 'Please upgrade your version of Behave for Jira and try again'
          when 500
            puts 'Jira server error (500)'
        end
      else
        if e.respond_to?('inspect')
          puts e.inspect
        else
          puts 'Unknown error occured (!)'
        end
      end
      puts 'Writing server response to error.log...'
      File.open('error.log', 'w') {|f| f.write(e.response) }
    else
      puts "Could not connect to host at '#{@host}'"
      puts e.message
    end
    puts 'ABORTING'
    puts '--------------------------------------------------------------'
    exit 1
  end
end

#output_varsObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/behave.rb', line 36

def output_vars()
  puts '--------------------------------------------------------------'
  puts 'HOST:           ' + @host
  puts 'PROJECT ID:     ' + @key.to_s
  puts 'USER:           ' + @user
  puts 'DIRECTORY:      ' + @dir
  puts 'INCLUDE MANUAL: ' + @manual.to_s
  puts 'PROXY:          ' + @proxy unless @proxy.nil?
  puts 'BYPASS SSL:     ' + @bypass_ssl.to_s unless @bypass_ssl.nil?
end