Module: HttpMimic::Obscura
- Defined in:
- lib/http_mimic/obscura.rb
Defined Under Namespace
Classes: BinaryNotFoundError, ExecutionError
Class Method Summary collapse
-
.render(url, options = {}) ⇒ HttpMimic::Response
Renders a web page using the Obscura headless SPA engine and returns an HttpMimic::Response.
- .resolve_binary ⇒ Object
Class Method Details
.render(url, options = {}) ⇒ HttpMimic::Response
Renders a web page using the Obscura headless SPA engine and returns an HttpMimic::Response
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/http_mimic/obscura.rb', line 23 def render(url, = {}) bin = resolve_binary raise BinaryNotFoundError, "Obscura binary not found. Run HttpMimic.download_obscura! or configure obscura_path." unless bin args = [bin, 'fetch', url.to_s] # Dump format: default to html (can be 'html', 'text', 'links', 'markdown', 'original', 'cookies') dump_format = [:dump] || 'html' args << '--dump' << dump_format.to_s # Stealth mode (enabled by default) args << '--stealth' if .fetch(:stealth, true) # Wait condition if [:wait_until] args << '--wait-until' << [:wait_until].to_s end # Wait delay if [:wait] args << '--wait' << [:wait].to_i.to_s end # Timeout in seconds (integer) timeout = [:timeout] || HttpMimic.configuration.default_timeout args << '--timeout' << timeout.to_i.to_s if timeout # User Agent if [:user_agent] args << '--user-agent' << [:user_agent].to_s end # Proxy if [:proxy] args << '--proxy' << [:proxy].to_s end # Cookies: pass cookies string or hash (Two-Phase Pipeline support) if [:cookies] || [:cookie] = [:cookies] || [:cookie] = if .is_a?(Hash) .map { |k, v| "#{k}=#{v}" }.join('; ') elsif .is_a?(Array) .join('; ') else .to_s end args << '--cookie' << unless .empty? end # Optional JS evaluation args << '--eval' << [:eval].to_s if [:eval] # Selector args << '--selector' << [:selector].to_s if [:selector] log_debug("Executing Obscura SPA render: #{args.join(' ')}") stdout, stderr, status = Open3.capture3(*args) code = (status && status.success?) ? 200 : (status ? status.exitstatus : 500) raw_headers = "HTTP/2 200 OK\r\ncontent-type: text/html; charset=utf-8\r\nx-rendered-by: obscura\r\n\r\n" headers = Headers.new({ 'content-type' => 'text/html; charset=utf-8', 'x-rendered-by' => 'obscura' }) Response.new( code: code, http_version: 'HTTP/2', status_message: status&.success? ? 'OK (Obscura SPA Rendered)' : 'Error', headers: headers, cookies: Cookies.new, body: stdout, parsed_response: nil, raw_headers: raw_headers, history: [], request_url: url.to_s, stderr: stderr, exit_code: status ? status.exitstatus : 0, command: args.join(' ') ) end |
.resolve_binary ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/http_mimic/obscura.rb', line 105 def resolve_binary # 1. Configured custom path custom_path = HttpMimic.configuration.obscura_path if custom_path && (File.file?(custom_path) || File.executable?(custom_path)) return custom_path end # 2. Check install directory or system PATH installed_path = Downloader.obscura_path return installed_path if installed_path # 3. Auto-download if enabled if HttpMimic.configuration.auto_download begin return Downloader.download_obscura! rescue StandardError => e HttpMimic.configuration.logger&.warn("[HttpMimic::Obscura] Auto-download failed: #{e.}") end end nil end |