Class: Fetchers::Url
- Inherits:
-
Object
- Object
- Fetchers::Url
- Defined in:
- lib/fetchers/url.rb
Direct Known Subclasses
Constant Summary collapse
- MIME_TYPES =
{ 'application/x-zip-compressed' => '.zip', 'application/zip' => '.zip', 'application/x-gzip' => '.tar.gz', 'application/gzip' => '.tar.gz', }.freeze
- GITHUB_URL_REGEX =
Transforms a browser github url to github tar url We distinguish between three different Github URL types:
- Master URL - Branch URL - Commit URL
master url: github.com/nathenharvey/tmp_compliance_profile/ is transformed to github.com/nathenharvey/tmp_compliance_profile/archive/master.tar.gz
github branch: github.com/hardening-io/tests-os-hardening/tree/2.0 is transformed to github.com/hardening-io/tests-os-hardening/archive/2.0.tar.gz
github commit: github.com/hardening-io/tests-os-hardening/tree/48bd4388ddffde68badd83aefa654e7af3231876 is transformed to github.com/hardening-io/tests-os-hardening/archive/48bd4388ddffde68badd83aefa654e7af3231876.tar.gz
%r{^https?://(www\.)?github\.com/(?<user>[\w-]+)/(?<repo>[\w-]+)(\.git)?(/)?$}
- GITHUB_URL_WITH_TREE_REGEX =
%r{^https?://(www\.)?github\.com/(?<user>[\w-]+)/(?<repo>[\w-]+)/tree/(?<commit>[\w\.]+)(/)?$}
Instance Attribute Summary collapse
-
#archive_path ⇒ Object
readonly
Returns the value of attribute archive_path.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
Class Method Summary collapse
- .resolve(target, opts = {}) ⇒ Object
- .resolve_from_string(target, opts) ⇒ Object
- .transform(target) ⇒ Object
Instance Method Summary collapse
- #fetch(path) ⇒ Object
-
#initialize(url, opts) ⇒ Url
constructor
A new instance of Url.
- #resolved_source ⇒ Object
Constructor Details
#initialize(url, opts) ⇒ Url
Returns a new instance of Url.
76 77 78 79 80 81 |
# File 'lib/fetchers/url.rb', line 76 def initialize(url, opts) @target = url @insecure = opts['insecure'] @token = opts['token'] @config = opts end |
Instance Attribute Details
#archive_path ⇒ Object (readonly)
Returns the value of attribute archive_path.
74 75 76 |
# File 'lib/fetchers/url.rb', line 74 def archive_path @archive_path end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
74 75 76 |
# File 'lib/fetchers/url.rb', line 74 def files @files end |
Class Method Details
.resolve(target, opts = {}) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/fetchers/url.rb', line 21 def self.resolve(target, opts = {}) if target.is_a?(Hash) && target.key?(:url) resolve_from_string(target[:url], opts) elsif target.is_a?(String) resolve_from_string(target, opts) end end |
.resolve_from_string(target, opts) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/fetchers/url.rb', line 29 def self.resolve_from_string(target, opts) uri = URI.parse(target) return nil if uri.nil? or uri.scheme.nil? return nil unless %{ http https }.include? uri.scheme target = transform(target) new(target, opts) rescue URI::Error nil end |
.transform(target) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fetchers/url.rb', line 59 def self.transform(target) transformed_target = if m = GITHUB_URL_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition "https://github.com/#{m[:user]}/#{m[:repo]}/archive/master.tar.gz" elsif m = GITHUB_URL_WITH_TREE_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition "https://github.com/#{m[:user]}/#{m[:repo]}/archive/#{m[:commit]}.tar.gz" end if transformed_target Inspec::Log.warn("URL target #{target} transformed to #{transformed_target}. Consider using the git fetcher") transformed_target else target end end |
Instance Method Details
#fetch(path) ⇒ Object
83 84 85 86 |
# File 'lib/fetchers/url.rb', line 83 def fetch(path) Inspec::Log.debug("Fetching URL: #{@target}") @archive_path = download_archive(path) end |
#resolved_source ⇒ Object
88 89 90 |
# File 'lib/fetchers/url.rb', line 88 def resolved_source { url: @target } end |