2016-02-08 19:06:07 +00:00
|
|
|
# encoding: utf-8
|
2016-08-25 18:22:01 +00:00
|
|
|
# frozen_string_literal: true
|
2016-02-08 19:06:07 +00:00
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
2016-02-22 01:18:02 +00:00
|
|
|
require 'net/http'
|
2017-03-29 16:42:24 +00:00
|
|
|
require 'addressable/uri'
|
2016-02-22 01:18:02 +00:00
|
|
|
|
2016-02-08 19:06:07 +00:00
|
|
|
module Supermarket
|
|
|
|
class API
|
2017-11-16 16:26:19 +00:00
|
|
|
SUPERMARKET_URL = 'https://supermarket.chef.io'
|
2016-02-08 19:06:07 +00:00
|
|
|
|
|
|
|
# displays a list of profiles
|
2016-09-09 09:21:54 +00:00
|
|
|
def self.profiles(supermarket_url = SUPERMARKET_URL)
|
2016-11-09 15:43:15 +00:00
|
|
|
url = "#{supermarket_url}/api/v1/tools-search"
|
2016-12-21 11:27:59 +00:00
|
|
|
_success, data = get(url, { type: 'compliance_profile', items: 100 })
|
2016-02-08 19:06:07 +00:00
|
|
|
if !data.nil?
|
|
|
|
profiles = JSON.parse(data)
|
2016-11-09 15:43:15 +00:00
|
|
|
profiles['items'].map { |x|
|
2016-09-09 09:21:54 +00:00
|
|
|
m = %r{^#{supermarket_url}/api/v1/tools/(?<slug>[\w-]+)(/)?$}.match(x['tool'])
|
2016-03-01 12:26:36 +00:00
|
|
|
x['slug'] = m[:slug]
|
|
|
|
x
|
|
|
|
}
|
2016-02-08 19:06:07 +00:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.profile_name(profile)
|
2017-03-29 16:42:24 +00:00
|
|
|
# We use Addressable::URI here because URI has a bug in Ruby 2.1.x where it doesn't allow underscore in host
|
|
|
|
uri = Addressable::URI.parse profile
|
2016-02-08 19:06:07 +00:00
|
|
|
[uri.host, uri.path[1..-1]]
|
2017-03-29 16:42:24 +00:00
|
|
|
rescue
|
2016-02-08 19:06:07 +00:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# displays profile infos
|
2016-09-09 09:21:54 +00:00
|
|
|
def self.info(profile, supermarket_url = SUPERMARKET_URL)
|
2016-02-08 19:06:07 +00:00
|
|
|
_tool_owner, tool_name = profile_name("supermarket://#{profile}")
|
2016-03-01 12:26:36 +00:00
|
|
|
return if tool_name.nil? || tool_name.empty?
|
2018-07-25 19:59:19 +00:00
|
|
|
# Tool name in Supermarket URL is downcased so we need to downcase
|
|
|
|
url = "#{supermarket_url}/api/v1/tools/#{tool_name.downcase}"
|
2016-02-08 19:06:07 +00:00
|
|
|
_success, data = get(url, {})
|
2016-03-01 12:26:36 +00:00
|
|
|
JSON.parse(data) if !data.nil?
|
2016-02-08 19:06:07 +00:00
|
|
|
rescue JSON::ParserError
|
2016-03-01 12:26:36 +00:00
|
|
|
nil
|
2016-02-08 19:06:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# compares a profile with the supermarket tool info
|
2016-09-09 09:21:54 +00:00
|
|
|
def self.same?(profile, supermarket_tool, supermarket_url = SUPERMARKET_URL)
|
2016-02-08 19:06:07 +00:00
|
|
|
tool_owner, tool_name = profile_name(profile)
|
2018-07-25 19:59:19 +00:00
|
|
|
|
|
|
|
raise "Could not parse tool name from #{profile}" if tool_name.nil?
|
|
|
|
|
|
|
|
# Tool name in Supermarket URL is downcased so we need to downcase
|
|
|
|
tool = "#{supermarket_url}/api/v1/tools/#{tool_name.downcase}"
|
2016-02-08 19:06:07 +00:00
|
|
|
supermarket_tool['tool_owner'] == tool_owner && supermarket_tool['tool'] == tool
|
|
|
|
end
|
|
|
|
|
2017-03-29 16:42:24 +00:00
|
|
|
def self.find(profile, supermarket_url = SUPERMARKET_URL)
|
|
|
|
profiles = Supermarket::API.profiles(supermarket_url)
|
2017-11-21 07:49:41 +00:00
|
|
|
return if profiles.empty?
|
|
|
|
|
|
|
|
index = profiles.index { |t| same?(profile, t, supermarket_url) }
|
|
|
|
# return profile or nil
|
|
|
|
profiles[index] if !index.nil? && index >= 0
|
2016-02-08 19:06:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# verifies that a profile exists
|
2016-09-09 09:21:54 +00:00
|
|
|
def self.exist?(profile, supermarket_url = SUPERMARKET_URL)
|
|
|
|
!find(profile, supermarket_url).nil?
|
2016-02-08 19:06:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.get(url, params)
|
|
|
|
uri = URI.parse(url)
|
|
|
|
uri.query = URI.encode_www_form(params)
|
|
|
|
req = Net::HTTP::Get.new(uri)
|
|
|
|
send_request(uri, req)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.send_request(uri, req)
|
|
|
|
# send request
|
2017-11-21 07:49:41 +00:00
|
|
|
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
2016-02-08 19:06:07 +00:00
|
|
|
http.request(req)
|
2017-11-21 07:49:41 +00:00
|
|
|
end
|
2016-02-08 19:06:07 +00:00
|
|
|
[res.is_a?(Net::HTTPSuccess), res.body]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|