make supermarket command more robust

This commit is contained in:
Christoph Hartmann 2016-03-01 13:26:36 +01:00
parent 051ac89376
commit bc2cde6b29
2 changed files with 22 additions and 11 deletions

View file

@ -18,7 +18,11 @@ module Supermarket
_success, data = get(url, { q: 'compliance_profile' })
if !data.nil?
profiles = JSON.parse(data)
profiles['items']
profiles['items'].map { |x|
m = %r{^#{Supermarket::API.supermarket_url}/api/v1/tools/(?<slug>[\w-]+)(/)?$}.match(x['tool'])
x['slug'] = m[:slug]
x
}
else
[]
end
@ -34,15 +38,12 @@ module Supermarket
# displays profile infos
def self.info(profile)
_tool_owner, tool_name = profile_name("supermarket://#{profile}")
return if tool_name.nil? || tool_name.empty?
url = "#{SUPERMARKET_URL}/api/v1/tools/#{tool_name}"
_success, data = get(url, {})
if !data.nil?
JSON.parse(data)
else
{}
end
JSON.parse(data) if !data.nil?
rescue JSON::ParserError
{}
nil
end
# compares a profile with the supermarket tool info

View file

@ -13,8 +13,7 @@ module Supermarket
headline('Available profiles:')
supermarket_profiles.each { |p|
m = %r{^#{Supermarket::API.supermarket_url}/api/v1/tools/(?<slug>[\w-]+)(/)?$}.match(p['tool'])
li("#{p['tool_owner']}/#{m[:slug]}")
li("#{p['tool_owner']}/#{p['slug']}")
}
end
@ -23,7 +22,7 @@ module Supermarket
desc: 'Attach a profile ID to all test results'
target_options
option :format, type: :string
def exec(tests)
def exec(*tests)
# iterate over tests and add compliance scheme
tests = tests.map { |t| 'supermarket://' + t }
@ -34,8 +33,19 @@ module Supermarket
desc 'info PROFILE', 'display Supermarket profile details'
def info(profile)
info = Supermarket::API.info(profile)
# check that the profile is available
supermarket_profiles = Supermarket::API.profiles
found = supermarket_profiles.select { |p|
"#{p['tool_owner']}/#{p['slug']}" == profile
}
if found.length == 0
puts "#{mark_text(profile)} is not available on Supermarket"
return
end
# load details for the specific profile
info = Supermarket::API.info(profile)
puts "#{mark_text('name: ')} #{info['slug']}"
puts "#{mark_text('owner:')} #{info['owner']}"
puts "#{mark_text('url: ')} #{info['source_url']}"