Class: HttpMimic::Response
- Inherits:
-
Object
- Object
- HttpMimic::Response
- Defined in:
- lib/http_mimic/response.rb
Instance Attribute Summary collapse
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
(also: #status)
readonly
Returns the value of attribute code.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
-
#fallback_triggered ⇒ Object
Returns the value of attribute fallback_triggered.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#http_version ⇒ Object
readonly
Returns the value of attribute http_version.
-
#mode_used ⇒ Object
(also: #strategy_used)
Returns the value of attribute mode_used.
-
#parsed_response ⇒ Object
readonly
Returns the value of attribute parsed_response.
-
#raw_headers ⇒ Object
readonly
Returns the value of attribute raw_headers.
-
#request_url ⇒ Object
readonly
Returns the value of attribute request_url.
-
#status_message ⇒ Object
readonly
Returns the value of attribute status_message.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #binary? ⇒ Boolean
- #blank? ⇒ Boolean
- #client_error? ⇒ Boolean
- #error? ⇒ Boolean
- #extract_images(base_url: nil) ⇒ Object
- #fallback_triggered? ⇒ Boolean
-
#initialize(attributes = {}) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
- #meta_description ⇒ Object
- #og_image ⇒ Object
- #present? ⇒ Boolean
- #redirect? ⇒ Boolean
- #save_to_file(filepath) ⇒ Object (also: #save)
- #server_error? ⇒ Boolean
- #success? ⇒ Boolean (also: #ok?)
- #title ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(attributes = {}) ⇒ Response
Returns a new instance of Response.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/http_mimic/response.rb', line 26 def initialize(attributes = {}) @code = attributes[:code] || 0 @http_version = attributes[:http_version] @status_message = attributes[:status_message] @headers = attributes[:headers] || Headers.new @cookies = attributes[:cookies] || Cookies.new @body = attributes[:body] || '' @parsed_response = attributes[:parsed_response] @raw_headers = attributes[:raw_headers] || '' @history = attributes[:history] || [] @request_url = attributes[:request_url] @stderr = attributes[:stderr] || '' @exit_code = attributes[:exit_code] || 0 @command = attributes[:command] @mode_used = attributes[:mode_used] || :impersonate @fallback_triggered = attributes[:fallback_triggered] || false @attempts = attributes[:attempts] || [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
179 180 181 182 183 184 185 186 187 |
# File 'lib/http_mimic/response.rb', line 179 def method_missing(method_name, *args, &block) if parsed_response.respond_to?(method_name) parsed_response.public_send(method_name, *args, &block) elsif body.respond_to?(method_name) body.public_send(method_name, *args, &block) else super end end |
Instance Attribute Details
#attempts ⇒ Object
Returns the value of attribute attempts.
19 20 21 |
# File 'lib/http_mimic/response.rb', line 19 def attempts @attempts end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def body @body end |
#code ⇒ Object (readonly) Also known as: status
Returns the value of attribute code.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def code @code end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def command @command end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def @cookies end |
#exit_code ⇒ Object (readonly)
Returns the value of attribute exit_code.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def exit_code @exit_code end |
#fallback_triggered ⇒ Object
Returns the value of attribute fallback_triggered.
19 20 21 |
# File 'lib/http_mimic/response.rb', line 19 def fallback_triggered @fallback_triggered end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def headers @headers end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def history @history end |
#http_version ⇒ Object (readonly)
Returns the value of attribute http_version.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def http_version @http_version end |
#mode_used ⇒ Object Also known as: strategy_used
Returns the value of attribute mode_used.
19 20 21 |
# File 'lib/http_mimic/response.rb', line 19 def mode_used @mode_used end |
#parsed_response ⇒ Object (readonly)
Returns the value of attribute parsed_response.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def parsed_response @parsed_response end |
#raw_headers ⇒ Object (readonly)
Returns the value of attribute raw_headers.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def raw_headers @raw_headers end |
#request_url ⇒ Object (readonly)
Returns the value of attribute request_url.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def request_url @request_url end |
#status_message ⇒ Object (readonly)
Returns the value of attribute status_message.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def @status_message end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def stderr @stderr end |
Instance Method Details
#[](key) ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/http_mimic/response.rb', line 140 def [](key) if parsed_response.is_a?(Hash) || parsed_response.is_a?(Array) parsed_response[key] else nil end end |
#binary? ⇒ Boolean
70 71 72 73 74 |
# File 'lib/http_mimic/response.rb', line 70 def binary? content_type = headers['content-type'].to_s.downcase ResponseParser::BINARY_MIME_KEYWORDS.any? { |kw| content_type.include?(kw) } || body.to_s.b[0, 1024]&.include?("\x00".b) end |
#blank? ⇒ Boolean
153 154 155 |
# File 'lib/http_mimic/response.rb', line 153 def blank? body.nil? || body.strip.empty? end |
#client_error? ⇒ Boolean
58 59 60 |
# File 'lib/http_mimic/response.rb', line 58 def client_error? code >= 400 && code < 500 end |
#error? ⇒ Boolean
66 67 68 |
# File 'lib/http_mimic/response.rb', line 66 def error? client_error? || server_error? end |
#extract_images(base_url: nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/http_mimic/response.rb', line 101 def extract_images(base_url: nil) return [] if binary? base = base_url || request_url || '' images = [] # 1. Match img tags (src, data-src, data-zoom-image, data-original, srcset) body.scan(/<img\s+[^>]*>/i).each do |img_tag| %w[src data-src data-zoom-image data-original data-high-res-src data-full-size-image-url].each do |attr| if match = img_tag.match(/#{attr}=["']([^"']+)["']/i) images << match[1].strip end end if match = img_tag.match(/srcset=["']([^"']+)["']/i) match[1].split(',').each do |item| url = item.strip.split(/\s+/).first images << url if url && !url.empty? end end end # 2. Match og:image meta tag if og = og_image images << og end # 3. Match raw image URLs found in document / script payloads body.scan(/https?:[^\s"'<>]+\.(?:jpg|jpeg|png|webp|avif|gif)/i).each do |raw_url| images << raw_url end # Clean, resolve relative URLs to absolute, and deduplicate images.map do |img| clean_url = img.gsub(/&/, '&').strip resolve_url(clean_url, base) end.compact.reject(&:empty?).uniq end |
#fallback_triggered? ⇒ Boolean
45 46 47 |
# File 'lib/http_mimic/response.rb', line 45 def fallback_triggered? !!@fallback_triggered end |
#inspect ⇒ Object
161 162 163 |
# File 'lib/http_mimic/response.rb', line 161 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} @code=#{code} @status_message=#{.inspect} @headers=#{headers.to_h.inspect} @parsed_response=#{parsed_response.inspect}>" end |
#meta_description ⇒ Object
95 96 97 98 99 |
# File 'lib/http_mimic/response.rb', line 95 def return nil if binary? body[/<meta\s+[^>]*name=["']description["'][^>]*content=["']([^"']+)["']/im, 1] || body[/<meta\s+[^>]*content=["']([^"']+)["'][^>]*name=["']description["']/im, 1] end |
#og_image ⇒ Object
89 90 91 92 93 |
# File 'lib/http_mimic/response.rb', line 89 def og_image return nil if binary? body[/<meta\s+[^>]*property=["']og:image["'][^>]*content=["']([^"']+)["']/im, 1] || body[/<meta\s+[^>]*content=["']([^"']+)["'][^>]*property=["']og:image["']/im, 1] end |
#present? ⇒ Boolean
157 158 159 |
# File 'lib/http_mimic/response.rb', line 157 def present? !blank? end |
#redirect? ⇒ Boolean
54 55 56 |
# File 'lib/http_mimic/response.rb', line 54 def redirect? code >= 300 && code < 400 end |
#save_to_file(filepath) ⇒ Object Also known as: save
76 77 78 79 80 81 |
# File 'lib/http_mimic/response.rb', line 76 def save_to_file(filepath) require 'fileutils' FileUtils.mkdir_p(File.dirname(filepath)) File.binwrite(filepath, body) filepath end |
#server_error? ⇒ Boolean
62 63 64 |
# File 'lib/http_mimic/response.rb', line 62 def server_error? code >= 500 && code < 600 end |
#success? ⇒ Boolean Also known as: ok?
49 50 51 |
# File 'lib/http_mimic/response.rb', line 49 def success? code >= 200 && code < 300 end |
#title ⇒ Object
84 85 86 87 |
# File 'lib/http_mimic/response.rb', line 84 def title return nil if binary? body[/<title[^>]*>(.*?)<\/title>/im, 1]&.strip end |
#to_s ⇒ Object Also known as: to_str
148 149 150 |
# File 'lib/http_mimic/response.rb', line 148 def to_s body.to_s end |