sample_file 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f164867285558f9f7193a2397a3987dcd2344c2
4
- data.tar.gz: 1442c5272e887e94065e0990c3c525cbde092829
3
+ metadata.gz: fdaf48797664f50a5c11170b2c258faf7d38a629
4
+ data.tar.gz: 7f7ba026781765ec2ad341b8552f8cbb755960e6
5
5
  SHA512:
6
- metadata.gz: ed734e265f79d85d4f982502d12e10550730a78ebfbe54e349ed688c24feab2751e608a1f5f8f7ba3d6f1d102dae89a323530dc77fe80c7a3d3fcdcf1cf38dc4
7
- data.tar.gz: 114d53567e8d7925a9750243b48d459d0504b4da6f415cfa6ed7a01f41666b07d1b4c9ec18e3228f6e288dc0e52ace85eaeabe89ead2d7f83957c7d7bc364ebf
6
+ metadata.gz: 6391e288725356c4444e41f7e639497a184aae0b3391b917deb03a771acc1748750e559d5a27af692f277d9524a43630556bdbc40b069fded6cd5b664433d5c1
7
+ data.tar.gz: a130a31921aa4547e2b9f2084ae8c5eece52817899f583d29003762b52cf334beedfe5286d06e3330dd81c0bdb8b80ef5a282e2ab321c6f6829a8c5ef4fd93bf
data/README.md CHANGED
@@ -24,12 +24,14 @@ SampleFile.image
24
24
  SampleFile.image :jpg
25
25
  SampleFile.image :gif
26
26
  SampleFile.image :png
27
+ SampleFile.image :jpg, width: 300, height: 50
27
28
 
28
29
  # absolute path to image
29
30
  SampleFile.image_path
30
31
  SampleFile.image_path :jpg
31
32
  SampleFile.image_path :gif
32
33
  SampleFile.image_path :png
34
+ SampleFile.image_path :jpg, width: 300, height: 50
33
35
 
34
36
  # returns a video file
35
37
  SampleFile.video
@@ -1,3 +1,7 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "mini_magick"
4
+
1
5
  require "sample_file/version"
2
6
  require "sample_file/base"
3
7
  require "sample_file/image"
@@ -5,20 +9,20 @@ require "sample_file/video"
5
9
 
6
10
  module SampleFile
7
11
  class << self
8
- def image(type='png')
9
- Image.file(type)
12
+ def image(type='png', opts={})
13
+ Image.new(type, opts).file
10
14
  end
11
15
 
12
- def image_path(type='png')
13
- Image.file_path(type)
16
+ def image_path(type='png', opts={})
17
+ Image.new(type, opts).file_path
14
18
  end
15
19
 
16
- def video(type='h264')
17
- Video.file(type)
20
+ def video(type='h264', opts={})
21
+ Video.new(type, opts).file
18
22
  end
19
23
 
20
- def video_path(type='h264')
21
- Video.file_path(type)
24
+ def video_path(type='h264', opts={})
25
+ Video.new(type, opts).file_path
22
26
  end
23
27
  end
24
28
  end
@@ -1,10 +1,10 @@
1
1
  module SampleFile
2
2
  class Base
3
- def file(type=nil)
4
- File.open file_path(type)
3
+ def file
4
+ File.open file_path
5
5
  end
6
6
 
7
- def file_path(type=nil)
7
+ def file_path
8
8
  raise NotImplementedError, "#{self.class} does not implement the file_path method"
9
9
  end
10
10
 
@@ -13,20 +13,5 @@ module SampleFile
13
13
  def base_path
14
14
  @base_path ||= File.expand_path File.join(File.dirname(__FILE__), '..', 'files')
15
15
  end
16
-
17
- class << self
18
- def method_missing method, *args
19
- instance.send method, *args
20
- end
21
-
22
- def respond_to? method
23
- return true if instance.respond_to? method
24
- super
25
- end
26
-
27
- def instance
28
- @@instance ||= self.new
29
- end
30
- end
31
16
  end
32
17
  end
@@ -1,22 +1,74 @@
1
+ require 'fileutils'
2
+
1
3
  module SampleFile
2
4
  class Image < Base
3
- def file_path(type='png')
4
- filename = filename_for_type(type)
5
- File.join(image_dir_path, filename)
5
+ attr_reader :type, :width, :height
6
+
7
+ def initialize(type='png', opts={})
8
+ @type = type
9
+ @width = opts[:width] || opts[:height]
10
+ @height = opts[:height] || opts[:width]
6
11
  end
7
12
 
8
- def file(type='png')
9
- super
13
+ def file_path
14
+ if width && height
15
+ path_to_resized_image
16
+ else
17
+ path_to_base_image
18
+ end
19
+ end
20
+
21
+ # Temporary directory where images will be stored
22
+ #
23
+ def tmpdir
24
+ @tmpdir ||= File.join(Dir.tmpdir, 'sample_file', 'image')
10
25
  end
11
26
 
12
27
  protected
13
28
 
14
- def image_dir_path
15
- File.join(base_path, 'image')
29
+ def path_to_base_image
30
+ @path_to_base_image ||= File.join(base_path, 'image', base_filename)
16
31
  end
17
32
 
18
- def filename_for_type(type)
33
+ def base_filename
19
34
  "sample.#{type}"
20
35
  end
36
+
37
+ def ensure_tmpdir_exists
38
+ FileUtils.mkdir_p(tmpdir) unless File.exists?(tmpdir)
39
+ end
40
+
41
+ def path_to_resized_image
42
+ @path_to_resized_image ||= determine_resized_image_path
43
+ end
44
+
45
+ def determine_resized_image_path
46
+ destination = File.join(tmpdir, filename)
47
+ ensure_tmpdir_exists
48
+ FileUtils.cp(create_resized_image.path, destination)
49
+ destination
50
+ end
51
+
52
+ def filename
53
+ @filename ||= determine_filename
54
+ end
55
+
56
+ def determine_filename
57
+ if width && height
58
+ filename_with_dimensions
59
+ else
60
+ base_filename
61
+ end
62
+ end
63
+
64
+ def filename_with_dimensions
65
+ "sample_#{width}x#{height}.#{type}"
66
+ end
67
+
68
+ def create_resized_image
69
+ image = ::MiniMagick::Image.open(path_to_base_image)
70
+ image.resize "#{width.to_i}x#{height.to_i}!"
71
+ File.open image.path
72
+ end
21
73
  end
22
74
  end
@@ -1,3 +1,3 @@
1
1
  module SampleFile
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,6 +1,10 @@
1
1
  module SampleFile
2
2
  class Video < Base
3
- def file_path(type='h264')
3
+ def initialize(type='h264', opts={})
4
+ # TODO Implement provide samples of multiple video codecs
5
+ end
6
+
7
+ def file_path
4
8
  File.join(video_dir_path, 'h264.mp4')
5
9
  end
6
10
 
@@ -22,4 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "ruby-imagespec", "~> 0.3.1"
25
+
26
+ spec.add_dependency "mini_magick", ">= 3.6.0"
25
27
  end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'fileutils'
2
3
 
3
4
  describe SampleFile::Image do
4
5
  it_behaves_like "a sample file", %w(gif png jpg)
@@ -11,13 +12,57 @@ describe SampleFile::Image do
11
12
 
12
13
  image_types.each_pair do |image_type, content_type|
13
14
  context "when requesting a '#{image_type}' image" do
15
+ subject { described_class.new(image_type) }
16
+
14
17
  describe :file do
15
18
  it "is of the correct content type" do
16
- data = ImageSpec.new(subject.file(image_type))
17
- data.content_type.should == content_type
19
+ image = MiniMagick::Image.open subject.file.path
20
+ image.mime_type.should == content_type
21
+ end
22
+
23
+ context "when passing :width and :height options" do
24
+ subject { described_class.new(image_type, width: 75, height: 200) }
25
+ it "returns an image of those dimensions" do
26
+ image = MiniMagick::Image.open subject.file.path
27
+ image['width'].should == 75
28
+ image['height'].should == 200
29
+ end
30
+ end
31
+
32
+ context "when only :width is set" do
33
+ subject { described_class.new(image_type, width: 35) }
34
+ it "returns a square image" do
35
+ image = MiniMagick::Image.open subject.file.path
36
+ image['width'].should == 35
37
+ image['height'].should eq image['width']
38
+ end
39
+ end
40
+
41
+ context "when only :height is set" do
42
+ subject { described_class.new(image_type, height: 45) }
43
+ it "returns a square image" do
44
+ image = MiniMagick::Image.open subject.file.path
45
+ image['width'].should == 45
46
+ image['height'].should eq image['width']
47
+ end
18
48
  end
19
49
  end
20
- end
21
- end
50
+
51
+ describe :file_path do
52
+ context "when passing :width and :height options" do
53
+ subject { described_class.new(image_type, width: 75, height: 200) }
54
+ before(:each) do
55
+ FileUtils.rm_r(subject.tmpdir)
56
+ end
57
+ its(:file_path) { should match(/sample_75x200\.#{image_type}$/) }
58
+ it "returns a path to an image that exists" do
59
+ path = subject.file_path
60
+ File.exists?(path).should be_true
61
+ end
62
+ end
63
+ end
64
+
65
+ end # context
66
+ end # each image_type
22
67
 
23
68
  end
@@ -1,32 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SampleFile do
4
+ %w(Image Video).each do |class_name|
5
+ klass = const_get("SampleFile::#{class_name}")
4
6
 
5
- describe :image do
6
- it "should call #file on a new instance of Image" do
7
- described_class::Image.should_receive(:file)
8
- described_class.image
7
+ file_method = class_name.downcase
8
+ describe file_method do
9
+ it "should call #file on an instance of #{class_name}" do
10
+ klass.any_instance.should_receive(:file)
11
+ described_class.send file_method
12
+ end
9
13
  end
10
- end
11
-
12
- describe :image_path do
13
- it "should call #file_path on Image" do
14
- described_class::Image.should_receive(:file_path)
15
- described_class.image_path
16
- end
17
- end
18
14
 
19
- describe :video do
20
- it "should call #file on Video" do
21
- described_class::Video.should_receive(:file)
22
- described_class.video
15
+ file_path_method = "#{file_method}_path"
16
+ describe file_path_method do
17
+ it "should call #file_path on an instance of #{class_name}" do
18
+ klass.any_instance.should_receive(:file_path)
19
+ described_class.send file_path_method
20
+ end
23
21
  end
24
- end
25
22
 
26
- describe :video_path do
27
- it "should call #file_path on Video" do
28
- described_class::Video.should_receive(:file_path)
29
- described_class.video_path
30
- end
31
23
  end
32
24
  end
@@ -1,39 +1,31 @@
1
1
  shared_examples_for 'a sample file' do |file_types=[]|
2
- subject { described_class }
2
+ subject { described_class.new }
3
3
 
4
4
  describe :file_path do
5
5
  it { should respond_to(:file_path) }
6
-
7
6
  context "when no file type is provided" do
8
7
  it "should point to an existing file" do
9
8
  File.exists?(subject.file_path).should be_true
10
9
  end
11
10
  end
12
-
13
- file_types.each do |type|
14
- context "when file_type is #{type}" do
15
- it "should point to an existing file" do
16
- File.exists?(subject.file_path(type)).should be_true
17
- end
18
- end
19
- end
20
11
  end
21
12
 
22
13
  describe :file do
23
14
  it { should respond_to(:file) }
24
-
25
15
  context "when no file type is provided" do
26
16
  it "opens a file using file_path" do
27
17
  File.should_receive(:open).with(subject.file_path)
28
18
  subject.file
29
19
  end
30
20
  end
21
+ end
31
22
 
32
- file_types.each do |type|
33
- context "when file_type is #{type}" do
34
- it "opens a file using file_path" do
35
- File.should_receive(:open).with(subject.file_path(type))
36
- subject.file(type)
23
+ file_types.each do |type|
24
+ context "when file_type is #{type}" do
25
+ subject { described_class.new(type) }
26
+ describe :file_path do
27
+ it "should point to an existing file" do
28
+ File.exists?(subject.file_path).should be_true
37
29
  end
38
30
  end
39
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sample_file
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maarten Claes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-25 00:00:00.000000000 Z
11
+ date: 2013-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.3.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: mini_magick
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.6.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.6.0
69
83
  description: Sample files for testing purposes
70
84
  email:
71
85
  - maartencls@gmail.com