diff --git a/README.md b/README.md index 5dd5bd3..94d4420 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,23 @@ Rails.application.configure do end ``` +You can even change placeholder image host, if for example you prefer [fillmurray](https://fillmurray.com): + +```ruby +Rails.application.configure do + config.middleware.use ImagePlaceholder::Middleware, size_pattern: { /.*/ => '320/320' }, host: 'fillmurray.com' +end +``` + +Last but not least, this middleware can be used with any Rack application: + +```ruby +# config.ru +use ImagePlaceholder::Middleware, size_pattern: { /.*/ => '320/320' }, host: 'fillmurray.com' +run YourRackApp +``` + + ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. diff --git a/config.ru b/config.ru index ead80fd..80f4d4c 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,4 @@ require 'image_placeholder' -use ImagePlaceholder::Middleware, size_pattern: { /.*/ => 320 } +use ImagePlaceholder::Middleware, size_pattern: { /.*/ => '320/320' }, host: 'fillmurray.com' run Rack::File.new(File.expand_path(File.join(__dir__, 'spec/support'))) diff --git a/lib/image_placeholder/middleware.rb b/lib/image_placeholder/middleware.rb index fe71ee4..0b69ff4 100644 --- a/lib/image_placeholder/middleware.rb +++ b/lib/image_placeholder/middleware.rb @@ -2,10 +2,11 @@ module ImagePlaceholder class Middleware - def initialize(app, image_extensions: %w(jpg png), size_pattern: {/.*/ => 100}) + def initialize(app, image_extensions: %w(jpg png), size_pattern: {/.*/ => 100}, host: 'via.placeholder.com') @app = app @image_extensions = image_extensions @size_pattern = size_pattern + @host = host end def call(env) @@ -22,7 +23,7 @@ def call(env) private def serve_placeholder_image(size = 100) - net_response = Net::HTTP.get_response(URI("https://via.placeholder.com/#{size}")) + net_response = Net::HTTP.get_response(URI("https://#{@host}/#{size}")) rack_response = Rack::Response.new(net_response.body, net_response.code.to_i) safe_headers = net_response.to_hash .reject { |key, _| hop_by_hop_header_fields.include?(key.downcase) } diff --git a/spec/middleware_spec.rb b/spec/middleware_spec.rb index 5b0779a..9d659c3 100644 --- a/spec/middleware_spec.rb +++ b/spec/middleware_spec.rb @@ -78,6 +78,7 @@ def app Rack::Lint.new( Middleware.new( Rack::File.new(static_path), + host: 'via.placeholder.com', image_extensions: %w(jpg), size_pattern: { %r{/uploads/.*/s_[0-9]+\.[a-z]{3}$} => 200,