imagemagick-identify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in imagemagick-identify.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dane Natoli
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # ImageMagick::Identify
2
+
3
+ A wrapper around ImageMagick's identify command
4
+
5
+ ## Installation
6
+
7
+ Install [ImageMagick](http://www.imagemagick.org/script/index.php) on your server using your favourite package manager.
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'imagemagick-identify'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install imagemagick-identify
20
+
21
+ ## Usage
22
+
23
+ Create an `ImageMagick::Identify` object using `ImageMagick::Identify(<filename>)`, then call `run` on it. It will return you an `ImageMagick::Result` which contains the attreibutes of that image.
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'imagemagick/identify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "imagemagick-identify"
8
+ spec.version = ImageMagick::Identify::VERSION
9
+ spec.authors = ["Dane Natoli"]
10
+ spec.email = ["dane.natoli@gmail.com"]
11
+ spec.description = %q{A wrapper around ImageMagick's identify command}
12
+ spec.summary = %q{A wrapper around ImageMagick's identify command}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,4 @@
1
+ module ImageMagick
2
+ class FileNotFoundException < Exception; end
3
+ class CommandNotFoundException < Exception; end
4
+ end
@@ -0,0 +1,44 @@
1
+ require "imagemagick/exceptions"
2
+ require "imagemagick/identify/version"
3
+ require "imagemagick/identify/command"
4
+ require "imagemagick/identify/parser"
5
+ require "imagemagick/identify/result"
6
+
7
+ module ImageMagick
8
+ class Identify
9
+
10
+ def initialize(file_path)
11
+ @file_path = file_path
12
+ validate_file_path!
13
+ end
14
+
15
+ class << self
16
+ def for(file_path)
17
+ new(file_path)
18
+ end
19
+
20
+ private :new
21
+ end
22
+
23
+ def run
24
+ output = command.execute(@file_path)
25
+ parsed_result = parser.parse(output)
26
+ Result.new(parsed_result)
27
+ end
28
+
29
+ private
30
+
31
+ def command
32
+ ImageMagick::Identify::Command
33
+ end
34
+
35
+ def parser
36
+ ImageMagick::Identify::Parser
37
+ end
38
+
39
+ def validate_file_path!
40
+ raise FileNotFoundException, "File doesn't exist!" unless File.exists?(@file_path)
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,23 @@
1
+ # This class is a wrapper around the identify command for ImageMagick.
2
+ # It will return the verbose information for the file.
3
+
4
+ require "utilities/which"
5
+
6
+ module ImageMagick
7
+ module Identify::Command
8
+
9
+ def self.execute(file_path)
10
+ command = "#{path_to_identify} -verbose -unique #{file_path.inspect}"
11
+ `#{command}`
12
+ end
13
+
14
+ private
15
+
16
+ def self.path_to_identify
17
+ Which.which("identify").tap{ |path|
18
+ raise CommandNotFoundException, "ImageMagick command 'identify' not found!" unless path
19
+ }
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ # This class is responsible for parsing the output of the identify
2
+ # command, and returning a nested hash structure.
3
+
4
+ module ImageMagick
5
+ module Identify::Parser
6
+
7
+ INDENT_PER_LEVEL = 2
8
+
9
+ def self.parse(input, spaces = INDENT_PER_LEVEL)
10
+ matcher = /\n\s{#{spaces}}\b/ #new line with n spaces until the next word
11
+
12
+ {}.tap{ |result|
13
+ input.split(matcher).each do |string|
14
+ if string.include?(":\n")
15
+ key, value = string.split(":\n", 2).map(&:strip)
16
+ result[key] = parse(value, spaces + INDENT_PER_LEVEL)
17
+ else
18
+ key, value = string.split(": ", 2).map(&:strip)
19
+ result[key] = value
20
+ end
21
+ end
22
+ }
23
+ end
24
+
25
+ private
26
+
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # This class represents the result of the ImageMagick identify command.
2
+ # It exposes only the results that we care about through methods.
3
+
4
+ module ImageMagick
5
+ class Identify::Result
6
+
7
+ def initialize(result={})
8
+ @result = result
9
+ end
10
+
11
+ def colors
12
+ @result["Colors"].to_i
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module ImageMagick
2
+ class Identify
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ # This module provides the equivalent of the unix which command.
2
+ # It is used by the ImageMagick module to check for the extistence
3
+ # of the ImageMagick command 'identify'.
4
+
5
+ module Which
6
+ def self.which(command)
7
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
8
+ command_path = File.join(path, command)
9
+ return command_path if File.executable? command_path
10
+ end
11
+
12
+ nil
13
+ end
14
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe ImageMagick::Identify::Command do
4
+
5
+ describe ".execute" do
6
+ context "when ImageMagick is installed" do
7
+ before :each do
8
+ Which.stub(:which).and_return("identify")
9
+ end
10
+
11
+ context "and given a file that exists" do
12
+ let(:file_name){ "spec/fixtures/images/test.jpg" }
13
+
14
+ example do
15
+ described_class.execute(file_name).should include("Colors")
16
+ end
17
+ end
18
+
19
+ context "and given a file that doesn't exist" do
20
+ let(:file_name){ "this/is/a/made_up_file.jpg" }
21
+
22
+ example do
23
+ described_class.execute(file_name).should be_empty
24
+ end
25
+ end
26
+ end
27
+
28
+ context "when ImageMagick is not installed" do
29
+ before :each do
30
+ Which.stub(:which).and_return(nil)
31
+ end
32
+
33
+ example do
34
+ expect{
35
+ described_class.execute("spec/fixtures/images/test.jpg")
36
+ }.to raise_error ImageMagick::CommandNotFoundException, "ImageMagick command 'identify' not found!"
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe ImageMagick::Identify::Parser do
4
+
5
+ describe ".parse" do
6
+
7
+ context "when it is given some valid input" do
8
+ let(:input) do
9
+ "Image: spec/fixtures/images/clock_in_1.jpg\n Format: JPEG (Joint Photographic Experts Group JFIF format)\n Geometry: 640x480+0+0\n Resolution: 72x72\n Channel depth:\n red: 8-bit\n green: 8-bit\n blue: 8-bit\n Channel statistics:\n Red:\n min: 0 (0)\n max: 255 (1)\n mean: 145.544 (0.570762)\n standard deviation: 75.8102 (0.297295)"
10
+ end
11
+
12
+ let(:expectation) do
13
+ {
14
+ "Image" => "spec/fixtures/images/clock_in_1.jpg",
15
+ "Format" => "JPEG (Joint Photographic Experts Group JFIF format)",
16
+ "Geometry" => "640x480+0+0",
17
+ "Resolution" => "72x72",
18
+ "Channel depth" => { "red" => "8-bit", "green" => "8-bit", "blue" => "8-bit" },
19
+ "Channel statistics" => { "Red" => { "min" => "0 (0)", "max" => "255 (1)", "mean" => "145.544 (0.570762)", "standard deviation" => "75.8102 (0.297295)" } }
20
+ }
21
+ end
22
+
23
+ example do
24
+ described_class.parse(input).should == expectation
25
+ end
26
+ end
27
+
28
+ context "when it is given no input" do
29
+ example do
30
+ described_class.parse("").should == {}
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe ImageMagick::Identify::Result do
4
+
5
+ describe "#colors" do
6
+ context "when given a result with colors specified" do
7
+ subject{ described_class.new("Colors" => 12345)}
8
+
9
+ its(:colors){ should == 12345 }
10
+ end
11
+
12
+ context "when given a result with colors not specified" do
13
+ subject{ described_class.new }
14
+
15
+ its(:colors){ should be_zero }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe ImageMagick::Identify do
4
+
5
+ describe ".new" do
6
+ example do
7
+ expect{
8
+ described_class.new
9
+ }.to raise_error NoMethodError
10
+ end
11
+ end
12
+
13
+ describe ".for" do
14
+ context "when given a file that exists" do
15
+ subject{ described_class.for("spec/fixtures/images/test.jpg") }
16
+
17
+ it{ should be_a(ImageMagick::Identify) }
18
+ end
19
+
20
+ context "when given a file that doesn't exist" do
21
+ example do
22
+ expect{
23
+ described_class.for("this/is/a/made_up_file.jpg")
24
+ }.to raise_error ImageMagick::FileNotFoundException, "File doesn't exist!"
25
+ end
26
+ end
27
+ end
28
+
29
+ describe ".run" do
30
+ context "when given a file that exists" do
31
+ subject{ described_class.for("spec/fixtures/images/test.jpg") }
32
+
33
+ example do
34
+ subject.run.should be_a(ImageMagick::Identify::Result)
35
+ end
36
+
37
+ example do
38
+ subject.run.colors.should == 50258
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'imagemagick/identify'
3
+
4
+ RSpec.configure do |c|
5
+ c.mock_with :rspec
6
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imagemagick-identify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dane Natoli
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A wrapper around ImageMagick's identify command
47
+ email:
48
+ - dane.natoli@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - imagemagick-identify.gemspec
59
+ - lib/imagemagick/exceptions.rb
60
+ - lib/imagemagick/identify.rb
61
+ - lib/imagemagick/identify/command.rb
62
+ - lib/imagemagick/identify/parser.rb
63
+ - lib/imagemagick/identify/result.rb
64
+ - lib/imagemagick/identify/version.rb
65
+ - lib/utilities/which.rb
66
+ - spec/fixtures/images/test.jpg
67
+ - spec/lib/imagemagick/identify/command_spec.rb
68
+ - spec/lib/imagemagick/identify/parser_spec.rb
69
+ - spec/lib/imagemagick/identify/result.rb
70
+ - spec/lib/imagemagick/identify_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.23
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: A wrapper around ImageMagick's identify command
97
+ test_files:
98
+ - spec/fixtures/images/test.jpg
99
+ - spec/lib/imagemagick/identify/command_spec.rb
100
+ - spec/lib/imagemagick/identify/parser_spec.rb
101
+ - spec/lib/imagemagick/identify/result.rb
102
+ - spec/lib/imagemagick/identify_spec.rb
103
+ - spec/spec_helper.rb