Class: Supermarket::API
- Inherits:
-
Object
- Object
- Supermarket::API
- Defined in:
- lib/bundles/inspec-supermarket/api.rb
Constant Summary collapse
- SUPERMARKET_URL =
'https://supermarket.chef.io'
Class Method Summary collapse
-
.exist?(profile, supermarket_url = SUPERMARKET_URL) ⇒ Boolean
verifies that a profile exists.
- .find(profile, supermarket_url = SUPERMARKET_URL) ⇒ Object
- .get(url, params) ⇒ Object
-
.info(profile, supermarket_url = SUPERMARKET_URL) ⇒ Object
displays profile infos.
- .profile_name(profile) ⇒ Object
-
.profiles(supermarket_url = SUPERMARKET_URL) ⇒ Object
displays a list of profiles.
-
.same?(profile, supermarket_tool, supermarket_url = SUPERMARKET_URL) ⇒ Boolean
compares a profile with the supermarket tool info.
- .send_request(uri, req) ⇒ Object
Class Method Details
.exist?(profile, supermarket_url = SUPERMARKET_URL) ⇒ Boolean
verifies that a profile exists
70 71 72 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 70 def self.exist?(profile, supermarket_url = SUPERMARKET_URL) !find(profile, supermarket_url).nil? end |
.find(profile, supermarket_url = SUPERMARKET_URL) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 60 def self.find(profile, supermarket_url = SUPERMARKET_URL) profiles = Supermarket::API.profiles(supermarket_url) return if profiles.empty? index = profiles.index { |t| same?(profile, t, supermarket_url) } # return profile or nil profiles[index] if !index.nil? && index >= 0 end |
.get(url, params) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 74 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 |
.info(profile, supermarket_url = SUPERMARKET_URL) ⇒ Object
displays profile infos
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 38 def self.info(profile, supermarket_url = SUPERMARKET_URL) _tool_owner, tool_name = profile_name("supermarket://#{profile}") return if tool_name.nil? || tool_name.empty? # Tool name in Supermarket URL is downcased so we need to downcase url = "#{supermarket_url}/api/v1/tools/#{tool_name.downcase}" _success, data = get(url, {}) JSON.parse(data) if !data.nil? rescue JSON::ParserError nil end |
.profile_name(profile) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 29 def self.profile_name(profile) # 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 [uri.host, uri.path[1..-1]] rescue nil end |
.profiles(supermarket_url = SUPERMARKET_URL) ⇒ Object
displays a list of profiles
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 14 def self.profiles(supermarket_url = SUPERMARKET_URL) url = "#{supermarket_url}/api/v1/tools-search" _success, data = get(url, { type: 'compliance_profile', items: 100 }) if !data.nil? profiles = JSON.parse(data) profiles['items'].map { |x| m = %r{^#{supermarket_url}/api/v1/tools/(?<slug>[\w-]+)(/)?$}.match(x['tool']) x['slug'] = m[:slug] x } else [] end end |
.same?(profile, supermarket_tool, supermarket_url = SUPERMARKET_URL) ⇒ Boolean
compares a profile with the supermarket tool info
50 51 52 53 54 55 56 57 58 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 50 def self.same?(profile, supermarket_tool, supermarket_url = SUPERMARKET_URL) tool_owner, tool_name = profile_name(profile) 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}" supermarket_tool['tool_owner'] == tool_owner && supermarket_tool['tool'] == tool end |
.send_request(uri, req) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 81 def self.send_request(uri, req) # send request res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| http.request(req) end [res.is_a?(Net::HTTPSuccess), res.body] end |