module EhcTarget
EhcTarget — resolve !ehc args to a recon URL (direct URL or !eh list index). Public: resolve Depends: EhSearch, EhcUrl, UrlLink Tests: test/lib/ehc_target_test.rb
Public Instance Methods
Source
# File lib/ehc_target.rb, line 15 def resolve(args:, eh_fetch: nil, now: Time.now.utc) stripped = args.to_s.strip return { error: :usage } if stripped.empty? return resolve_url(stripped) if stripped.match?(%r{\Ahttps?://}i) if (match = stripped.match(/\A(\d+)(?:\s+(.+))?\z/)) index = match[1].to_i focus = match[2]&.strip focus = nil if focus&.empty? return resolve_index(index, focus: focus, eh_fetch: eh_fetch, now: now) end { error: :usage } end
Source
# File lib/ehc_target.rb, line 46 def resolve_index(index, focus:, eh_fetch:, now:) return { error: :usage } if index < 1 result = EhSearch.dispatch_command(index.to_s, fetch: eh_fetch, now: now) return { error: :eh_fetch } if result[:error] == :fetch if result[:error] == :not_found return { error: :not_found, index: result[:index], listed: result[:listed] } end item = result[:item] url = item[:url].to_s.strip url = item[:ctftime_url].to_s.strip if url.empty? url = UrlLink.normalize_url(url) return { error: :usage } unless UrlLink.valid_url?(url) return { error: :unsafe } unless EhcUrl.safe_url?(url) { url: url, host: EhcUrl.host_from_url(url), focus: focus, challenge: { index: result[:index], title: item[:title].to_s.strip, prize_label: item[:prize_label].to_s.strip } } end
Source
# File lib/ehc_target.rb, line 31 def resolve_url(text) parsed = EhcUrl.parse_command(text) if parsed[:error] error = parsed[:error] == EhcUrl.unsafe_message ? :unsafe : :usage return { error: error } end { url: parsed[:url], host: parsed[:host], focus: parsed[:focus], challenge: nil } end