kleya 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 272b77784c8f74811be0db9402ff4694220c0a212ca4d6af53e6b61ff2398df0
4
- data.tar.gz: bb4451a8bb5bea56482ef1c48ae02b321da627218569fcca09ecd5c3d0b18b69
3
+ metadata.gz: f9d67fbb3a7754b0af20ff18f877da9f7fe8eb1c1f2885c94a17ab8f8c0ab85c
4
+ data.tar.gz: 6ead13010ce66ca8d0f96e07b5df9abab103c3014ad66d1584d22d16981a5e13
5
5
  SHA512:
6
- metadata.gz: 74262f4d700dc1d2d1c3e5e224801e8e95bdb55d47326167ba6a4d8154139e89a08d92a2a8ddd95b3e4af2ef01d5bf0ff12d5529213f57f963771864ffb363c4
7
- data.tar.gz: e1635c52e75051a00528aa59f4b70d58a28d7993ab1f1d414f60de869ed569198b3ccd726182393fc5169d31d1af4317aa0ab2a7d1b67c4f6c8b0a3d8db524da
6
+ metadata.gz: c9a46f780e038d591dd60d2172258cbd12128ce2342c672a4fdb5d92d93efc903f1e045cbc5375379f5beb7b696e6723a845fb3a3368ef8fed3dcc44ed290be1
7
+ data.tar.gz: cf575c64b78b13c15f5a654725c7b4779b2ce15f0a71a8b641f01ff54d19500e59f7c6b1695f571566f6b92d7b62bb9328ca50d9ea0478ee1b49cef1832d50fb
data/README.md CHANGED
@@ -30,6 +30,12 @@ The simplest way to capture a screenshot and save it can look like this,
30
30
  Kleya.capture('https://www.hellotext.com').save
31
31
  ```
32
32
 
33
+ Or directly via the CLI
34
+
35
+ ```bash
36
+ kleya https://www.hellotext.com
37
+ ```
38
+
33
39
  ## Usage
34
40
 
35
41
  ```ruby
@@ -52,11 +58,32 @@ puts artifact.content_type # "image/jpeg"
52
58
  browser.quit
53
59
  ```
54
60
 
61
+ ## CLI Usage
62
+
63
+ Kleya includes a command-line interface for quick screenshot captures:
64
+
65
+ ```bash
66
+ # Basic usage
67
+ kleya https://www.hellotext.com
68
+
69
+ # With options
70
+ kleya https://www.hellotext.com --format png --quality 95 --area page
71
+ ```
72
+
73
+ CLI Options
74
+
75
+ - `--format`, `-f` - Image format (jpeg, png). Default: jpeg
76
+ - `--quality`, `-q` - Image quality (1-100). Default: 90
77
+ - `--area`, `-a` - Capture area (viewport, page). Default: viewport
78
+ - `--encoding`, `-e` - Output encoding (binary, base64). Default: base64
79
+ - `--output`, `-o` - Output destination, defaults to the current directory.
80
+ - `--help`, `-h` - Show help message
81
+
55
82
  ### Presets
56
83
 
57
84
  Kleya includes convenient viewport presets for social media platforms and common devices. You can pass any of the following values when initializing a browser instance.
58
85
 
59
- - `desktop`: default (1920x1080)
86
+ - `desktop` default (1920x1080)
60
87
 
61
88
  - `x` (1200x675)
62
89
  - `facebook` (1200x630)
@@ -109,9 +136,10 @@ Kleya.capture('https://www.hellotext.com')
109
136
 
110
137
  Alongside the options you pass for the instance, there's some extra configurable settings you can tweak to your usecase.
111
138
 
112
- - `format`: specifies the format of the image captures, i.e `jpeg` or `png`.
113
- - `encoding`: specifies the encoding of the image, possible options is `binary` or `base64` (default). Regardless, the `Kleya::Artifact` object responds to `#binary` and `base64` when needed.
114
- - `quality`: an integer between 1 - 100 that determines the quality of the final image, higher quality images result in bigger sizes and may not work correctly in some situations such as the Open Graph (OG) protocol, you can tweak and test this. Defaults to `90`.
139
+ - `format` specifies the format of the image captures, i.e `jpeg` or `png`.
140
+ - `encoding` specifies the encoding of the image, possible options is `binary` or `base64` (default). Regardless, the `Kleya::Artifact` object responds to `#binary` and `base64` when needed.
141
+ - `quality` an integer between 1 - 100 that determines the quality of the final image, higher quality images result in bigger sizes and may not work correctly in some situations such as the Open Graph (OG) protocol, you can tweak and test this. Defaults to `90`.
142
+ - `area` specifies the area of the browser to capture, defaults to `viewport` for capturing only the visible part of the page. Supported values are `viewport` and `page` for full-page screenshots.
115
143
 
116
144
  ```ruby
117
145
  artifact = browser.capture('https://example.com', format: :jpeg, quality: 85, encoding: :base64)
@@ -181,14 +209,11 @@ end
181
209
  ## Roadmap
182
210
 
183
211
  - Wait strategies (`wait_for: '.element'`, `wait_until: :network_idle`)
184
- - Full page screenshots (not just viewport)
185
212
  - Built-in retry mechanism with configurable delays
186
213
 
187
214
  - Memory usage optimization for large batches
188
215
  - Request blocking (ads, analytics, fonts)
189
- - Custom user agents for mobile rendering
190
216
 
191
- - CLI tool for quick captures (`kleya capture https://example.com`)
192
217
  - Debug mode with browser preview
193
218
  - Capture metrics (timing, size, errors)
194
219
 
data/bin/kleya ADDED
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'kleya'
5
+
6
+ options = {
7
+ format: :jpeg,
8
+ quality: 90,
9
+ area: :viewport,
10
+ encoding: :base64,
11
+ output: nil
12
+ }
13
+
14
+ parser = OptionParser.new do |opts|
15
+ opts.banner = 'Usage: kleya [url] [options]'
16
+
17
+ opts.on('-f', '--format FORMAT', [:jpeg, :png], 'Image format (jpeg, png)') do |format|
18
+ options[:format] = format
19
+ end
20
+
21
+ opts.on('-q', '--quality QUALITY', Integer, 'Image quality (0-100)') do |quality|
22
+ options[:quality] = quality
23
+ end
24
+
25
+ opts.on('-a', '--area AREA', [:viewport, :full], 'Area to capture (viewport, full)') do |area|
26
+ options[:area] = area
27
+ end
28
+
29
+ opts.on('-e', '--encoding ENCODING', [:base64, :binary], 'Encoding (base64, binary)') do |encoding|
30
+ options[:encoding] = encoding
31
+ end
32
+
33
+ opts.on('-o', '--output OUTPUT', String, 'Output file path') do |output|
34
+ options[:output] = output
35
+ end
36
+
37
+ opts.on('-h', '--help', 'Show this message') do
38
+ puts opts
39
+ exit
40
+ end
41
+ end
42
+
43
+ parser.parse!
44
+
45
+ if ARGV.empty?
46
+ puts "Error: URL is required"
47
+ puts parser
48
+ exit 1
49
+ end
50
+
51
+ Kleya.capture(ARGV[0], **options).save(options[:output])
@@ -39,6 +39,8 @@ module Kleya
39
39
  path = filename
40
40
  elsif File.directory?(path)
41
41
  path = File.join(path, filename)
42
+ elsif File.extname(path).empty?
43
+ path = "#{path}.#{@format}"
42
44
  end
43
45
 
44
46
  File.write(path, binary, mode: 'wb').then { path }
data/lib/kleya/browser.rb CHANGED
@@ -30,6 +30,7 @@ module Kleya
30
30
  # @option options [Symbol] :format (:jpeg) image format (:jpeg, :png)
31
31
  # @option options [Integer] :quality (90) JPEG quality (1-100)
32
32
  # @option options [Symbol] :encoding (:base64) output encoding
33
+ # @option options [Symbol] :area (:viewport) the area to capture (:viewport, :page)
33
34
  # @return [Artifact] the screenshot artifact
34
35
  # @example Taking a X-optimized screenshot
35
36
  # browser = Kleya::Browser.new(
@@ -43,12 +44,9 @@ module Kleya
43
44
  format = options[:format] || :jpeg
44
45
  quality = options[:quality] || 90
45
46
  encoding = options[:encoding] || :base64
47
+ full = options[:area] == :page
46
48
 
47
- data = browser.screenshot(
48
- format: format,
49
- quality: quality,
50
- encoding: encoding
51
- )
49
+ data = browser.screenshot(format:, quality:, encoding:, full:)
52
50
 
53
51
  Artifact.new(data:, url:, viewport: @viewport, format:, quality:, encoding:)
54
52
  rescue Ferrum::TimeoutError
@@ -11,6 +11,11 @@ class ArtifactTest < Minitest::Test
11
11
  )
12
12
  end
13
13
 
14
+ def teardown
15
+ FileUtils.rm_f('test.jpg')
16
+ FileUtils.rm_f('appends_extension.jpeg')
17
+ end
18
+
14
19
  def test_artifact_size
15
20
  assert_equal(4, @artifact.size)
16
21
  end
@@ -19,6 +24,16 @@ class ArtifactTest < Minitest::Test
19
24
  assert_runs_without_errors do
20
25
  @artifact.save('test.jpg')
21
26
  end
27
+
28
+ assert File.exist?('test.jpg')
29
+ end
30
+
31
+ def test_artifact_save_with_content_type_extension
32
+ assert_runs_without_errors do
33
+ @artifact.save('appends_extension')
34
+ end
35
+
36
+ assert File.exist?('appends_extension.jpeg')
22
37
  end
23
38
 
24
39
  def test_artifact_base64
data/test/browser_test.rb CHANGED
@@ -61,7 +61,7 @@ class BrowserTest < Minitest::Test
61
61
  @mock_ferrum.expect :goto, nil, ['https://example.com']
62
62
  # The screenshot method receives keyword arguments as a hash
63
63
  @mock_ferrum.expect :screenshot, 'fake_image_data' do |args|
64
- args == { format: :jpeg, quality: 90, encoding: :base64 }
64
+ args == { format: :jpeg, quality: 90, encoding: :base64, full: false }
65
65
  end
66
66
 
67
67
  artifact = browser.capture('https://example.com')
@@ -84,7 +84,7 @@ class BrowserTest < Minitest::Test
84
84
  @mock_ferrum.expect :goto, nil, ['https://example.com']
85
85
  # The screenshot method receives keyword arguments as a hash
86
86
  @mock_ferrum.expect :screenshot, 'fake_png_data' do |args|
87
- args == { format: :png, quality: 100, encoding: :binary }
87
+ args == { format: :png, quality: 100, encoding: :binary, full: false }
88
88
  end
89
89
 
90
90
  artifact = browser.capture('https://example.com',
@@ -100,6 +100,25 @@ class BrowserTest < Minitest::Test
100
100
 
101
101
  @mock_ferrum.verify
102
102
  end
103
+
104
+ def test_capture_with_full_area
105
+ browser = Kleya::Browser.new
106
+
107
+ Ferrum::Browser.stub :new, @mock_ferrum do
108
+ @mock_ferrum.expect :goto, nil, ['https://example.com']
109
+ # The screenshot method receives keyword arguments as a hash
110
+ @mock_ferrum.expect :screenshot, 'fake_full_image_data' do |args|
111
+ args == { format: :jpeg, quality: 90, encoding: :base64, full: true }
112
+ end
113
+
114
+ artifact = browser.capture('https://example.com', area: :page)
115
+
116
+ assert_instance_of Kleya::Artifact, artifact
117
+ assert_equal('fake_full_image_data', artifact.instance_variable_get(:@data))
118
+ end
119
+
120
+ @mock_ferrum.verify
121
+ end
103
122
 
104
123
  def test_capture_handles_timeout_error
105
124
  browser = Kleya::Browser.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kleya
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hellotext
8
8
  - Ahmed Khattab
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-07-21 00:00:00.000000000 Z
11
+ date: 2025-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -81,12 +81,14 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.17'
83
83
  description: Screenshots, made easy.
84
- executables: []
84
+ executables:
85
+ - kleya
85
86
  extensions: []
86
87
  extra_rdoc_files: []
87
88
  files:
88
89
  - README.md
89
90
  - Rakefile
91
+ - bin/kleya
90
92
  - lib/kleya.rb
91
93
  - lib/kleya/artifact.rb
92
94
  - lib/kleya/browser.rb