Class: JekyllImgFlow::Providers::BaseProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-imgflow/providers/base_provider.rb

Overview

Base provider class - all providers must inherit from this

Direct Known Subclasses

Flyimg, Imagemagick, Imgproxy, Libvips, Sharp, Weserv

Constant Summary collapse

SMARTCROP_POSITIONS =

Valid smartcrop position values

%w[attention entropy center centre].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ BaseProvider

Returns a new instance of BaseProvider.



16
17
18
19
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 16

def initialize(config = {})
  @config = config
  @operations = []
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 14

def config
  @config
end

Class Method Details

.provider_nameObject

Provider name for identification (override in subclasses)



183
184
185
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 183

def provider_name
  name.split("::").last.downcase
end

.supports_operation?(operation) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 192

def supports_operation?(operation)
  !unsupported_operations.include?(operation)
end

.unsupported_operationsObject

Provider capability methods



188
189
190
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 188

def unsupported_operations
  [] # Default: no unsupported operations
end

Instance Method Details

#add_watermark(watermark_path, options = {}) ⇒ Object



126
127
128
129
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 126

def add_watermark(watermark_path, options = {})
  @operations << { type: :watermark, watermark_path: watermark_path,
                   options: options }
end

#alpha_opacity=(opacity) ⇒ Object



131
132
133
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 131

def alpha_opacity=(opacity)
  @operations << { type: :alpha_opacity, opacity: opacity }
end

#available?Boolean

Check if this provider is available (must be implemented by subclasses)

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 22

def available?
  # Default: not available unless subclass implements actual check
  # This ensures providers explicitly check their availability
  false
end

#check_http_service(url) ⇒ Object

Helper method for HTTP providers to check service availability



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 29

def check_http_service(url)
  return false unless url

  begin
    require "net/http"
    require "uri"

    uri = URI(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = uri.scheme == "https"
    http.read_timeout = 2
    http.open_timeout = 2

    request = Net::HTTP::Get.new(uri)
    http.request(request)

    # Consider available if we get any response (even 404 means service is running)
    true
  rescue StandardError
    false
  end
end

#convert_format(format) ⇒ Object



106
107
108
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 106

def convert_format(format)
  @operations << { type: :format, format: format }
end

#crop(ratio_or_width, height_val = nil, x_val = nil, y_val = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 91

def crop(ratio_or_width, height_val = nil, x_val = nil, y_val = nil)
  if height_val.is_a?(Hash)
    @operations << { type: :crop, ratio: ratio_or_width, options: height_val }
  elsif height_val
    options = { width: ratio_or_width, height: height_val, x: x_val, y: y_val }
    @operations << options.merge(type: :crop, ratio: nil, options: options)
  else
    @operations << { type: :crop, ratio: ratio_or_width, options: {} }
  end
end

#encode_file_url(file_path) ⇒ Object

Helper method for HTTP providers to construct HTTP URLs from file paths



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 53

def encode_file_url(file_path)
  # If already a URL, return as-is
  return file_path if file_path.start_with?("http://", "https://")

  # For HTTP providers, construct proper HTTP URL
  site = @config.site

  # Get the relative path from site source using Pathname
  source_path = Pathname.new(site.source)
  file_pathname = Pathname.new(file_path)
  relative_path = if file_pathname.absolute? && file_path.to_s.start_with?(site.source)
                    file_pathname.relative_path_from(source_path).to_s
                  elsif file_path.start_with?("/")
                    file_path[1..]
                  else
                    file_path
                  end

  # URL-encode each path segment to handle spaces and special characters
  encoded_path = relative_path.split("/").map do |segment|
    URI.encode_www_form_component(segment)
  end.join("/")

  # Construct HTTP URL for the file
  # Use Jekyll server URL (localhost:4000 by default for development)
  # In production, this would be the actual site URL from config
  base_url = site.config["url"] || "http://localhost:4000"
  baseurl = site.config["baseurl"] || ""

  "#{base_url}#{baseurl}/#{encoded_path}"
end

#execute(input_path, output_path) ⇒ Object

Final execution method - providers must implement this

Raises:

  • (NotImplementedError)


144
145
146
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 144

def execute(input_path, output_path)
  raise NotImplementedError, "Provider must implement execute method"
end

#format(value) ⇒ Object



114
115
116
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 114

def format(value)
  convert_format(value)
end

#opacity(value) ⇒ Object



122
123
124
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 122

def opacity(value)
  self.alpha_opacity = value
end

#operationsObject

Get current operations (for testing/debugging)



154
155
156
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 154

def operations
  @operations.dup
end

#optimize(level = :medium) ⇒ Object



118
119
120
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 118

def optimize(level = :medium)
  @operations << { type: :optimize, level: level }
end

#quality(value) ⇒ Object



110
111
112
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 110

def quality(value)
  self.quality = value
end

#quality=(quality) ⇒ Object



102
103
104
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 102

def quality=(quality)
  @operations << { type: :quality, quality: quality }
end

#reset_operationsObject

Reset operations for next processing



149
150
151
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 149

def reset_operations
  @operations = []
end

#resize(width, height, options = {}) ⇒ Object

Operation collection methods - these should NOT execute immediately



86
87
88
89
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 86

def resize(width, height, options = {})
  @operations << { type: :resize, width: width, height: height,
                   options: options }
end

#supports_operation?(operation) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 139

def supports_operation?(operation)
  self.class.supports_operation?(operation)
end

#watermark(watermark_path, options = {}) ⇒ Object



135
136
137
# File 'lib/jekyll-imgflow/providers/base_provider.rb', line 135

def watermark(watermark_path, options = {})
  add_watermark(watermark_path, options)
end