Class: Resilientlink::Client
- Inherits:
-
Object
- Object
- Resilientlink::Client
- Defined in:
- lib/resilientlink.rb
Instance Method Summary collapse
-
#initialize(api_key:, base_url: DEFAULT_URL, timeout: DEFAULT_TIMEOUT) ⇒ Client
constructor
A new instance of Client.
-
#scrape(url, **opts) ⇒ Hash
Scrape a URL and return structured metadata.
Constructor Details
#initialize(api_key:, base_url: DEFAULT_URL, timeout: DEFAULT_TIMEOUT) ⇒ Client
Returns a new instance of Client.
27 28 29 30 31 32 33 |
# File 'lib/resilientlink.rb', line 27 def initialize(api_key:, base_url: DEFAULT_URL, timeout: DEFAULT_TIMEOUT) raise ArgumentError, 'api_key is required.' if api_key.nil? || api_key.empty? @api_key = api_key @base_url = base_url.chomp('/') @timeout = timeout end |
Instance Method Details
#scrape(url, **opts) ⇒ Hash
Scrape a URL and return structured metadata.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/resilientlink.rb', line 59 def scrape(url, **opts) raise ArgumentError, 'url is required.' if url.nil? || url.empty? body = { url: url } body[:return_html] = opts[:return_html] unless opts[:return_html].nil? body[:screenshot] = opts[:screenshot] unless opts[:screenshot].nil? body[:full_page] = opts[:full_page] unless opts[:full_page].nil? body[:pdf] = opts[:pdf] unless opts[:pdf].nil? body[:pdf_format] = opts[:pdf_format] unless opts[:pdf_format].nil? body[:pdf_background] = opts[:pdf_background] unless opts[:pdf_background].nil? body[:pdf_landscape] = opts[:pdf_landscape] unless opts[:pdf_landscape].nil? body[:bypass_cache] = opts[:bypass_cache] unless opts[:bypass_cache].nil? body[:js_render] = opts[:js_render] unless opts[:js_render].nil? body[:wait_for_selector] = opts[:wait_for_selector] unless opts[:wait_for_selector].nil? body[:wait_until] = opts[:wait_until] unless opts[:wait_until].nil? body[:wait_ms] = opts[:wait_ms] unless opts[:wait_ms].nil? body[:custom_headers] = opts[:custom_headers] unless opts[:custom_headers].nil? body[:custom_js] = opts[:custom_js] unless opts[:custom_js].nil? body[:return_cookies] = opts[:return_cookies] unless opts[:return_cookies].nil? body[:block_resources] = opts[:block_resources] unless opts[:block_resources].nil? body[:timeout] = opts[:timeout] unless opts[:timeout].nil? request('POST', '/api/scrape', body) end |