Skip to content

Commit

Permalink
Merge 9c815a5 into fa34e81
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermef committed Jan 28, 2022
2 parents fa34e81 + 9c815a5 commit a684aab
Show file tree
Hide file tree
Showing 14 changed files with 378 additions and 310 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ jobs:
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
rubocop:
name: runner / rubocop
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
- name: rubocop
uses: reviewdog/action-rubocop@v2
with:
rubocop_version: gemfile
reporter: github-pr-check
22 changes: 22 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Ruby

on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+'

jobs:
release:
name: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
- name: Install dependencies
run: bundle install
- name: push to RubyGems
env:
GEM_HOST_API_KEY: ${{ secrets.GEM_HOST_API_KEY }}
run: bundle exec rake release
29 changes: 29 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Naming/FileName:
Enabled: false

Style/Documentation:
Enabled: false

Gemspec/RequiredRubyVersion:
Enabled: false

Metrics/ClassLength:
Max: 130

Layout/LineLength:
Max: 161

Metrics/BlockLength:
Max: 255

Metrics/AbcSize:
Max: 74

Metrics/CyclomaticComplexity:
Max: 26

Metrics/MethodLength:
Max: 44

Metrics/PerceivedComplexity:
Max: 26
6 changes: 2 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

gem 'rspec'
gem 'simplecov', require: false
gem 'simplecov-lcov', require: false
18 changes: 15 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
Dir.glob('tasks/*.rake').each { |r| import r}

task :default => :spec
# frozen_string_literal: true

require 'rspec/core/rake_task'

begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

Bundler::GemHelper.install_tasks

RSpec::Core::RakeTask.new(:spec)

task default: :spec
2 changes: 2 additions & 0 deletions lib/ruby-thumbor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'thumbor/crypto_url'
require 'thumbor/cascade'

Expand Down
48 changes: 28 additions & 20 deletions lib/thumbor/cascade.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'forwardable'
require 'openssl'
require 'base64'
Expand All @@ -8,14 +10,17 @@ module Thumbor
class Cascade
attr_accessor :image, :crypto, :options, :filters

@available_options = [
:meta, :crop, :center,
:original_width, :original_height,
:width, :height, :flip,
:flop, :halign, :valign,
:smart, :fit_in, :adaptive_fit_in,
:full_fit_in, :adaptive_full_fit_in,
:trim, :debug]
FILTER_REGEX = Regexp.new('^(.+)_filter$')

@available_options = %i[
meta crop center
original_width original_height
width height flip
flop halign valign
smart fit_in adaptive_fit_in
full_fit_in adaptive_full_fit_in
trim debug
]

extend Forwardable

Expand All @@ -29,7 +34,7 @@ class Cascade
end
end

def initialize(key=false, image=nil)
def initialize(key = nil, image = nil)
@key = key
@image = image
@options = {}
Expand All @@ -38,36 +43,39 @@ def initialize(key=false, image=nil)
end

def generate
@crypto.generate prepare_options(@options).merge({image: @image, filters: @filters})
@crypto.generate prepare_options(@options).merge({ image: @image, filters: @filters })
end

def method_missing(m, *args)
if /^(.+)_filter$/.match(m.to_s)
@filters << "#{$1}(#{escape_args(args).join(',')})"
def method_missing(method, *args)
if method =~ FILTER_REGEX
@filters << "#{FILTER_REGEX.match(method)[1]}(#{escape_args(args).join(',')})"
self
else
super
end
end

def respond_to_missing?(method, include_private = false)
method =~ FILTER_REGEX || super
end

private

def escape_args(args)
args.map do |arg|
arg = CGI::escape(arg) if arg.is_a? String and arg.match(/^https?:\/\//)
arg = CGI.escape(arg) if arg.is_a?(String) && arg.match(%r{^https?://})
arg
end
end

def prepare_options(options)
options.reduce({}) do |final_options, item|
options.each_with_object({}) do |item, final_options|
value = if item[1].length == 1
item[1].first
else
item[1]
end
item[1].first
else
item[1]
end
final_options[item[0]] = value
final_options
end
end
end
Expand Down
Loading

0 comments on commit a684aab

Please sign in to comment.