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'.freeze
Class Method Summary collapse
-
.exist?(profile) ⇒ Boolean
verifies that a profile exists.
- .find(profile) ⇒ Object
- .get(url, params) ⇒ Object
-
.info(profile) ⇒ Object
displays profile infos.
- .profile_name(profile) ⇒ Object
-
.profiles ⇒ Object
displays a list of profiles.
-
.same?(profile, supermarket_tool) ⇒ Boolean
compares a profile with the supermarket tool info.
- .send_request(uri, req) ⇒ Object
- .supermarket_url ⇒ Object
Class Method Details
.exist?(profile) ⇒ Boolean
verifies that a profile exists
66 67 68 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 66 def self.exist?(profile) !find(profile).nil? end |
.find(profile) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 56 def self.find(profile) profiles = Supermarket::API.profiles if !profiles.empty? index = profiles.index { |t| same?(profile, t) } # return profile or nil profiles[index] if !index.nil? && index >= 0 end end |
.get(url, params) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 70 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) ⇒ Object
displays profile infos
39 40 41 42 43 44 45 46 47 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 39 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, {}) JSON.parse(data) if !data.nil? rescue JSON::ParserError nil end |
.profile_name(profile) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 31 def self.profile_name(profile) uri = URI(profile) [uri.host, uri.path[1..-1]] rescue URI::Error => _e nil end |
.profiles ⇒ Object
displays a list of profiles
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 16 def self.profiles url = "#{SUPERMARKET_URL}/api/v1/tools-search" _success, data = get(url, { q: 'compliance_profile' }) if !data.nil? profiles = JSON.parse(data) 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 end |
.same?(profile, supermarket_tool) ⇒ Boolean
compares a profile with the supermarket tool info
50 51 52 53 54 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 50 def self.same?(profile, supermarket_tool) tool_owner, tool_name = profile_name(profile) tool = "#{SUPERMARKET_URL}/api/v1/tools/#{tool_name}" supermarket_tool['tool_owner'] == tool_owner && supermarket_tool['tool'] == tool end |
.send_request(uri, req) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 77 def self.send_request(uri, req) # send request res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') {|http| http.request(req) } [res.is_a?(Net::HTTPSuccess), res.body] end |
.supermarket_url ⇒ Object
11 12 13 |
# File 'lib/bundles/inspec-supermarket/api.rb', line 11 def self.supermarket_url SUPERMARKET_URL end |