Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ The simplest way to capture a screenshot and save it can look like this,
Kleya.capture('https://www.hellotext.com').save
```

Or directly via the CLI

```bash
kleya https://www.hellotext.com
```

## Usage

```ruby
Expand All @@ -52,6 +58,27 @@ puts artifact.content_type # "image/jpeg"
browser.quit
```

## CLI Usage

Kleya includes a command-line interface for quick screenshot captures:

```bash
# Basic usage
kleya https://www.hellotext.com

# With options
kleya https://www.hellotext.com --format png --quality 95 --area page
```

CLI Options

- `--format`, `-f` - Image format (jpeg, png). Default: jpeg
- `--quality`, `-q` - Image quality (1-100). Default: 90
- `--area`, `-a` - Capture area (viewport, page). Default: viewport
- `--encoding`, `-e` - Output encoding (binary, base64). Default: base64
- `--output`, `-o` - Output destination, defaults to the current directory.
- `--help`, `-h` - Show help message

### Presets

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.
Expand Down Expand Up @@ -187,7 +214,6 @@ end
- Memory usage optimization for large batches
- Request blocking (ads, analytics, fonts)

- CLI tool for quick captures (`kleya capture https://example.com`)
- Debug mode with browser preview
- Capture metrics (timing, size, errors)

Expand Down
51 changes: 51 additions & 0 deletions bin/kleya
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env ruby

require 'optparse'
require 'kleya'

options = {
format: :jpeg,
quality: 90,
area: :viewport,
encoding: :base64,
output: nil
}

parser = OptionParser.new do |opts|
opts.banner = 'Usage: kleya [url] [options]'

opts.on('-f', '--format FORMAT', [:jpeg, :png], 'Image format (jpeg, png)') do |format|
options[:format] = format
end

opts.on('-q', '--quality QUALITY', Integer, 'Image quality (0-100)') do |quality|
options[:quality] = quality
end

opts.on('-a', '--area AREA', [:viewport, :full], 'Area to capture (viewport, full)') do |area|
options[:area] = area
end

opts.on('-e', '--encoding ENCODING', [:base64, :binary], 'Encoding (base64, binary)') do |encoding|
options[:encoding] = encoding
end

opts.on('-o', '--output OUTPUT', String, 'Output file path') do |output|
options[:output] = output
end

opts.on('-h', '--help', 'Show this message') do
puts opts
exit
end
end

parser.parse!

if ARGV.empty?
puts "Error: URL is required"
puts parser
exit 1
end

Kleya.capture(ARGV[0], **options).save(options[:output])
3 changes: 2 additions & 1 deletion kleya.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'kleya'
s.version = '0.0.2'
s.version = '0.0.3'
s.summary = 'Screenshots, made easy.'
s.description = 'Screenshots, made easy.'
s.authors = ['Hellotext', 'Ahmed Khattab']
Expand All @@ -17,4 +17,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'webmock', '~> 3.25'

s.add_dependency 'ferrum', '~> 0.17'
s.executables << 'kleya'
end
2 changes: 2 additions & 0 deletions lib/kleya/artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def save(path = nil)
path = filename
elsif File.directory?(path)
path = File.join(path, filename)
elsif File.extname(path).empty?
path = "#{path}.#{@format}"
end

File.write(path, binary, mode: 'wb').then { path }
Expand Down
15 changes: 15 additions & 0 deletions test/artifact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def setup
)
end

def teardown
FileUtils.rm_f('test.jpg')
FileUtils.rm_f('appends_extension.jpeg')
end

def test_artifact_size
assert_equal(4, @artifact.size)
end
Expand All @@ -19,6 +24,16 @@ def test_artifact_save
assert_runs_without_errors do
@artifact.save('test.jpg')
end

assert File.exist?('test.jpg')
end

def test_artifact_save_with_content_type_extension
assert_runs_without_errors do
@artifact.save('appends_extension')
end

assert File.exist?('appends_extension.jpeg')
end

def test_artifact_base64
Expand Down