Class: ScreenshotAPI::Client
- Inherits:
-
Object
- Object
- ScreenshotAPI::Client
- Defined in:
- lib/screenshotapi/client.rb
Constant Summary collapse
- QUERY_PARAM_MAP =
{ url: "url", width: "width", height: "height", full_page: "fullPage", type: "type", quality: "quality", color_scheme: "colorScheme", wait_until: "waitUntil", wait_for_selector: "waitForSelector", delay: "delay", block_ads: "blockAds", remove_cookie_banners: "removeCookieBanners", css_inject: "cssInject", js_inject: "jsInject", stealth_mode: "stealthMode", device_pixel_ratio: "devicePixelRatio", timezone: "timezone", locale: "locale", cache_ttl: "cacheTtl", preload_fonts: "preloadFonts", remove_elements: "removeElements", remove_popups: "removePopups", mockup_device: "mockupDevice", geo_latitude: "geoLatitude", geo_longitude: "geoLongitude", geo_accuracy: "geoAccuracy" }.freeze
- BODY_PARAM_MAP =
QUERY_PARAM_MAP.merge( html: "html", geo_location: "geoLocation" ).freeze
Instance Method Summary collapse
-
#initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) ⇒ Client
constructor
A new instance of Client.
- #save(path:, **options) ⇒ Object
- #screenshot(**options) ⇒ Object
Constructor Details
#initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) ⇒ Client
Returns a new instance of Client.
41 42 43 44 45 46 47 |
# File 'lib/screenshotapi/client.rb', line 41 def initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) raise ArgumentError, "API key is required" if blank?(api_key) @api_key = api_key.to_s @base_url = normalize_base_url(base_url) @timeout = timeout end |
Instance Method Details
#save(path:, **options) ⇒ Object
74 75 76 77 78 |
# File 'lib/screenshotapi/client.rb', line 74 def save(path:, **) result = screenshot(**) File.binwrite(path, result.image) result. end |
#screenshot(**options) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/screenshotapi/client.rb', line 49 def screenshot(**) validate_capture_target!() uri, request = build_request() request["x-api-key"] = @api_key response = perform_request(uri, request) unless response.is_a?(Net::HTTPSuccess) handle_error(response) end = Metadata.new( credits_remaining: integer_header(response, "x-credits-remaining"), screenshot_id: response["x-screenshot-id"] || "", duration_ms: integer_header(response, "x-duration-ms") ) Result.new( image: response.body, content_type: response["content-type"] || "image/png", metadata: ) end |