Skip to content

Commit

Permalink
Fix bug that did not allow remote files to be opened in Ruby 3
Browse files Browse the repository at this point in the history
  • Loading branch information
e-serranor authored May 30, 2023
2 parents fb13180 + 8b6e42c commit a7bf6e7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
11 changes: 6 additions & 5 deletions lib/miro/dominant_colors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def to_rgba
sorted_pixels.collect { |pixel| ChunkyPNG::Color.to_truecolor_alpha_bytes(pixel) }
end

def to_hsl
def to_hsl
histogram.map { |item| item[1].to_hsl.to_a } if Miro.histogram?
end

Expand All @@ -48,14 +48,15 @@ def sorted_pixels
end

def histogram
@histogram ||= downsample_and_histogram.sort_by { |item| item[0] }.reverse
@histogram ||= downsample_and_histogram.sort_by { |item| item[0] }.reverse
end

private

def downsample_and_histogram
@source_image = open_source_image
hstring = Terrapin::CommandLine.new(Miro.options[:image_magick_path], image_magick_params).
run(:in => Shellwords.escape(File.expand_path(@source_image.path)),
hstring = Terrapin::CommandLine.new(Miro.options[:image_magick_path], image_magick_params)
.run(:in => Shellwords.escape(File.expand_path(@source_image.path)),
:resolution => Miro.options[:resolution],
:colors => Miro.options[:color_count].to_s,
:quantize => Miro.options[:quantize])
Expand Down Expand Up @@ -94,7 +95,7 @@ def open_source_image
original_extension = @image_type || URI.parse(@src_image_path).path.split('.').last

tempfile = Tempfile.open(["source", ".#{original_extension}"])
remote_file_data = open(@src_image_path).read
remote_file_data = URI.parse(@src_image_path).read

tempfile.write(should_force_encoding? ? remote_file_data.force_encoding("UTF-8") : remote_file_data)
tempfile.close
Expand Down
2 changes: 1 addition & 1 deletion miro.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "oily_png" if RUBY_ENGINE != "jruby"

gem.add_development_dependency "rspec"
gem.add_development_dependency "fakeweb"
gem.add_development_dependency "webmock"

gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
Expand Down
3 changes: 2 additions & 1 deletion spec/miro/dominant_colors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
before do
subject.stub(:open_downsampled_image).and_return(mock_downsampled_image)
Tempfile.stub(:open).and_return(mock_source_image)
WebMock.stub_request(:get, 'http://domain.com/to/image.jpg').to_return(status: 200, body: 'image data')
end

it "opens for the file resource" do
Expand Down Expand Up @@ -202,7 +203,7 @@
it "should be #00FF02 at the first element" do
subject.to_hex.first.should == hex_colors.first
end
it "should be #8F0074 at the first element" do
it "should be #8F0074 at the last element" do
subject.to_hex.last.should == hex_colors.last
end
it "should have the right values" do
Expand Down
4 changes: 1 addition & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
#
require 'miro'
require 'fakeweb'
require 'webmock/rspec'

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
end

FakeWeb.register_uri(:get, "http://domain.com/to/image.jpg", :body => "image data")

0 comments on commit a7bf6e7

Please sign in to comment.