capit 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/.gitignore CHANGED
@@ -3,5 +3,6 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  doc/*
6
+ coverage/*
6
7
  .yardoc
7
8
  .DS_STORE
data/Gemfile CHANGED
@@ -1,5 +1,14 @@
1
+ # encoding: UTF-8
2
+
1
3
  source "http://rubygems.org"
2
4
 
3
- gem 'postrank-uri', '~> 1.0.7'
5
+ group :test do
6
+ gem 'rspec', '~> 2.5.0'
7
+ gem 'simplecov', '~> 0.4.0', :require => false
8
+ gem 'guard', '~> 0.3.0'
9
+ gem 'growl', '~> 1.0.3'
10
+ gem 'guard-bundler', '~> 0.1.2'
11
+ gem 'guard-rspec', '~> 0.2.0'
12
+ end
4
13
 
5
14
  gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard 'rspec', :version => 2, :cli => '-c' do
2
+ watch(%r{^spec/.+_spec\.rb})
3
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
7
+ guard 'bundler', :notify => true do
8
+ watch('Gemfile')
9
+ watch(/^.+\.gemspec/)
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ezekiel Templin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -33,7 +33,6 @@ Usage
33
33
  require 'capit'
34
34
  capit = CapIt::Capture("http://www.github.com")
35
35
 
36
-
37
36
  Notes
38
37
  -----
39
38
  Don't use this yet.
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :test => :spec
8
+ task :default => :spec
data/capit.gemspec CHANGED
@@ -1,5 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
+
3
4
  require 'capit/version'
4
5
 
5
6
  Gem::Specification.new do |s|
@@ -8,14 +9,24 @@ Gem::Specification.new do |s|
8
9
  s.platform = Gem::Platform::RUBY
9
10
  s.authors = ["Ezekiel Templin"]
10
11
  s.email = ["ezkl@me.com"]
11
- s.date = %q{2011-03-24}
12
+ s.date = %q{2011-03-26}
12
13
  s.homepage = "http://github.com/meadvillerb/capit"
13
14
  s.summary = %q{Easy screen captures with the help of CutyCapt}
14
15
  s.description = %q{CapIt provides a simple Ruby interface to Björn Höhrmann's CutyCapt.}
16
+
15
17
  s.extra_rdoc_files = [
16
- "README.md"
18
+ "README.md",
19
+ "LICENSE"
17
20
  ]
21
+
18
22
  s.files = `git ls-files`.split("\n")
19
- s.add_runtime_dependency(%q<postrank-uri>, ["1.0.7"])
23
+ s.test_files = `git ls-files -- spec/*`.split("\n")
24
+
20
25
  s.require_paths = ["lib"]
26
+ s.add_development_dependency('rspec', '~> 2.5.0')
27
+ s.add_development_dependency('simplecov', '~> 0.4.0')
28
+ s.add_development_dependency('guard', '~> 0.3.0')
29
+ s.add_development_dependency('growl', '~> 1.0.3')
30
+ s.add_development_dependency('guard-bundler', '~> 0.1.2')
31
+ s.add_development_dependency('guard-rspec', '~> 0.2.0')
21
32
  end
data/lib/capit.rb CHANGED
@@ -1,10 +1,18 @@
1
1
  # encoding: UTF-8
2
2
 
3
- # To ensure clean URI's
4
- require 'postrank-uri'
5
-
6
3
  # Primary namespace of the capit gem
7
4
  module CapIt
5
+
6
+ # Raised when OS isn't Linux or Mac
7
+ class InvalidOSError < StandardError; end
8
+
9
+ # Raised when a filename with an invalid extension is entered.
10
+ # @abstract
11
+ class InvalidExtensionError < StandardError; end
12
+
13
+ # Raised when CutyCapt isn't installed.
14
+ # @abstract
15
+ class CutyCaptError < StandardError; end
8
16
  end
9
17
 
10
18
  require 'capit/version'
data/lib/capit/capture.rb CHANGED
@@ -14,6 +14,10 @@ module CapIt
14
14
  end
15
15
 
16
16
  class Capture
17
+
18
+ # All extensions CutyCapt can use to infer format of output
19
+ EXTENSIONS = /\A[\w]+\.(svg|ps|pdf|itext|html|rtree|png|jpeg|jpg|mng|tiff|gif|bmp|ppm|xvm|xpm)\z/i
20
+
17
21
  # The URL of the page to be captured
18
22
  attr_reader :url
19
23
 
@@ -29,12 +33,15 @@ module CapIt
29
33
  # capit.folder = "/home/user/screenshots"
30
34
  #
31
35
  def initialize url, options = {}
32
- @url = PostRank::URI.clean(url)
36
+ cutycapt_installed?
37
+ @url = url
33
38
  @folder = options[:folder] || Dir.pwd
34
39
  @filename = options[:filename] || "capit.jpg"
35
40
  @user_agent = options[:user_agent] || "CapIt! [http://github.com/meadvillerb/capit]"
36
41
  @max_wait = options[:max_wait] || 15000
37
42
  @delay = options[:delay]
43
+
44
+ valid_extension?(@filename)
38
45
  end
39
46
 
40
47
  # Performs the page capture.
@@ -43,61 +50,71 @@ module CapIt
43
50
  # capit = CapIt::Capture.new("http://mdvlrb.com", :filename => "mdvlrb.png", :folder => "/home/user/screenshots")
44
51
  # capit.capture
45
52
  #
46
- # @return [true, false]
53
+ # @return [String, false]
47
54
  #
48
55
  def capture
49
56
  `#{capture_command}`
50
57
  successful?
51
58
  end
52
-
53
- protected
54
59
 
55
- # Determines whether the capture was successful
56
- # by checking for the existence of the output file.
57
- # Sets {@output} if true.
58
- #
59
- # @return [true, false]
60
- #
61
- def successful?
62
- if FileTest.exists?("#{@folder}/#{@filename}")
63
- @output = "#{@folder}/#{@filename}"
64
- true
65
- else
66
- false
67
- end
68
- end
69
-
70
- # Produces the command used to run CutyCapt.
71
- #
72
- # @return [String]
73
- #
74
- def capture_command
75
- cmd = "CutyCapt"
76
- cmd += " --url='#{@url}'"
77
- cmd += " --out='#{@folder}/#{@filename}'"
78
- cmd += " --max-wait=#{@max_wait}"
79
- cmd += " --delay=#{@delay}" if @delay
80
- cmd += " --user-agent='#{@user_agent}'"
81
-
82
- if determine_os == :linux
83
- xvfb = 'xvfb-run --server-args="-screen 0, 1024x768x24" '
84
- xvfb.concat(cmd)
85
- else
86
- cmd
87
- end
60
+ def filename=(filename)
61
+ valid_extension?(filename)
62
+ @filename = filename
63
+ end
64
+
65
+ def valid_extension?(filename)
66
+ unless !filename[EXTENSIONS].nil?
67
+ raise InvalidExtensionError, "You must supply a valid extension!"
88
68
  end
69
+ end
70
+
71
+ # Determines whether the capture was successful
72
+ # by checking for the existence of the output file.
73
+ # Sets {@output} if true.
74
+ #
75
+ # @return [true, false]
76
+ #
77
+ def successful?
78
+ if FileTest.exists?("#{@folder}/#{@filename}")
79
+ @output = "#{@folder}/#{@filename}"
80
+ end
81
+ end
82
+
83
+ # Produces the command used to run CutyCapt.
84
+ #
85
+ # @return [String]
86
+ #
87
+ def capture_command
88
+ cmd = "CutyCapt"
89
+ cmd += " --url='#{@url}'"
90
+ cmd += " --out='#{@folder}/#{@filename}'"
91
+ cmd += " --max-wait=#{@max_wait}"
92
+ cmd += " --delay=#{@delay}" if @delay
93
+ cmd += " --user-agent='#{@user_agent}'"
89
94
 
90
- # Uses RUBY_PLATFORM to determine the operating system.
91
- # Not foolproof, but good enough for the time being.
92
- #
93
- # @return [Symbol]
94
- #
95
- def determine_os
96
- case RUBY_PLATFORM
97
- when /darwin/ then :mac
98
- when /linux/ then :linux
99
- else :error
100
- end
95
+ if determine_os == :linux
96
+ xvfb = 'xvfb-run --server-args="-screen 0, 1024x768x24" '
97
+ xvfb.concat(cmd)
98
+ else
99
+ cmd
100
+ end
101
+ end
102
+
103
+ # Uses RUBY_PLATFORM to determine the operating system.
104
+ # Not foolproof, but good enough for the time being.
105
+ #
106
+ # @return [Symbol]
107
+ #
108
+ def determine_os
109
+ case RUBY_PLATFORM
110
+ when /darwin/i then :mac
111
+ when /linux/i then :linux
112
+ else raise InvalidOSError, "CapIt currently only works on the Mac and Linux platforms"
101
113
  end
114
+ end
115
+
116
+ def cutycapt_installed?
117
+ raise CutyCaptError, "CutyCapt must be installed and available on PATH" if `which CutyCapt`.empty?
118
+ end
102
119
  end
103
120
  end
data/lib/capit/version.rb CHANGED
@@ -5,5 +5,5 @@ module CapIt
5
5
  #
6
6
  # The version number
7
7
  #
8
- VERSION = "0.1.2"
8
+ VERSION = "0.2.0"
9
9
  end
@@ -0,0 +1,92 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
+
4
+ describe CapIt::Capture do
5
+ describe "Convenience Method" do
6
+ it "should have a Convenience Method" do
7
+ @folder = Dir.pwd + '/spec/temporary'
8
+ `mkdir #{@folder}`
9
+ @capit = CapIt::Capture("http://mdvlrb.com/", :filename => 'mdvlrb.png', :folder => @folder).should == @folder + "/mdvlrb.png"
10
+ `rm -rf #{@folder}`
11
+ end
12
+ end
13
+
14
+ describe "Capture Class" do
15
+ subject { CapIt::Capture.new("http://mdvlrb.com/", :filename => 'mdvlrb.png') }
16
+
17
+ describe "#initialize" do
18
+ it "should have defaults" do
19
+ subject.folder.should == Dir.pwd
20
+ subject.filename.should == "mdvlrb.png"
21
+ subject.user_agent.should == "CapIt! [http://github.com/meadvillerb/capit]"
22
+ subject.max_wait.should == 15000
23
+ end
24
+
25
+ it "should respond to input" do
26
+ subject.should respond_to :url, :folder, :filename, :user_agent, :max_wait, :delay
27
+ end
28
+
29
+ it "should allow filename to be changed if extension is valid" do
30
+ @capit = CapIt::Capture.new("http://mdvlrb.com/")
31
+ @capit.filename = "mdvlrb.png"
32
+ @capit.filename.should == "mdvlrb.png"
33
+ end
34
+ end
35
+
36
+ describe "#capture" do
37
+ before(:all) do
38
+ @folder = Dir.pwd + '/spec/temporary'
39
+ @capit = CapIt::Capture.new("http://mdvlrb.com/", :filename => 'mdvlrb.jpeg', :folder => @folder)
40
+ `mkdir #{@folder}`
41
+ end
42
+
43
+ it "should return the full path to the screenshot" do
44
+ @capit.capture.should == @folder + "/mdvlrb.jpeg"
45
+ end
46
+
47
+ it "should capture the proper screenshot" do
48
+ @capit.output.should == @folder + "/mdvlrb.jpeg"
49
+ end
50
+
51
+ after(:all) do
52
+ `rm -rf #{@folder}`
53
+ end
54
+ end
55
+
56
+ describe "#capture_command" do
57
+ it "should prefix xvfb when platform is Linux" do
58
+ @capit = CapIt::Capture.new("http://mdvlrb.com/")
59
+ RUBY_PLATFORM = "linux"
60
+ @capit.capture_command.should match /^xvfb/
61
+ end
62
+
63
+ it "shouldn't prefix anything when platform is Mac" do
64
+ @capit = CapIt::Capture.new("http://mdvlrb.com/")
65
+ RUBY_PLATFORM = "darwin"
66
+ @capit.capture_command.should match /^CutyCapt/
67
+ end
68
+
69
+ end
70
+
71
+ describe "Errors" do
72
+
73
+ it "should raise an error if CutyCapt isn't available" do
74
+ path = ENV['PATH']
75
+ expect { ENV['PATH'] = ""; CapIt::Capture.new("http://mdvlrb.com/") }.to raise_error(/CutyCapt/)
76
+ ENV['PATH'] = path
77
+ end
78
+
79
+ it "should raise an error if OS isn't Linux or Mac" do
80
+ RUBY_PLATFORM = "mingw"
81
+ expect { subject.determine_os }.to raise_error(/platforms/)
82
+ RUBY_PLATFORM = "darwin"
83
+ end
84
+
85
+ it "should not accept filenames without a valid extension" do
86
+ expect { CapIt::Capture.new("http://mdvlrb.com/", :filename => 'capit.foo') }.to raise_error(/valid extension/)
87
+ expect { subject.filename = "capit.foo" }.to raise_error(/valid extension/)
88
+ end
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe CapIt do
5
+ describe "Basic Module" do
6
+ it "should have a VERSION" do
7
+ CapIt::VERSION.should_not be_nil
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+ require 'simplecov'
3
+
4
+ SimpleCov.start do
5
+ coverage_dir 'coverage'
6
+ add_filter '/spec/'
7
+ end
8
+
9
+ require 'rspec'
10
+ require 'capit'
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: capit
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ezekiel Templin
@@ -10,20 +10,75 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-24 00:00:00 -04:00
13
+ date: 2011-03-26 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
- name: postrank-uri
17
+ name: rspec
18
18
  prerelease: false
19
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
20
  none: false
21
21
  requirements:
22
- - - "="
22
+ - - ~>
23
23
  - !ruby/object:Gem::Version
24
- version: 1.0.7
25
- type: :runtime
24
+ version: 2.5.0
25
+ type: :development
26
26
  version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 0.4.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: guard
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 0.3.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: growl
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 1.0.3
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: guard-bundler
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.2
69
+ type: :development
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: guard-rspec
73
+ prerelease: false
74
+ requirement: &id006 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ version: 0.2.0
80
+ type: :development
81
+ version_requirements: *id006
27
82
  description: "CapIt provides a simple Ruby interface to Bj\xC3\xB6rn H\xC3\xB6hrmann's CutyCapt."
28
83
  email:
29
84
  - ezkl@me.com
@@ -33,15 +88,22 @@ extensions: []
33
88
 
34
89
  extra_rdoc_files:
35
90
  - README.md
91
+ - LICENSE
36
92
  files:
93
+ - .gemtest
37
94
  - .gitignore
38
95
  - Gemfile
96
+ - Guardfile
97
+ - LICENSE
39
98
  - README.md
40
99
  - Rakefile
41
100
  - capit.gemspec
42
101
  - lib/capit.rb
43
102
  - lib/capit/capture.rb
44
103
  - lib/capit/version.rb
104
+ - spec/capit/capture_spec.rb
105
+ - spec/capit_spec.rb
106
+ - spec/spec_helper.rb
45
107
  has_rdoc: true
46
108
  homepage: http://github.com/meadvillerb/capit
47
109
  licenses: []
@@ -70,5 +132,7 @@ rubygems_version: 1.6.2
70
132
  signing_key:
71
133
  specification_version: 3
72
134
  summary: Easy screen captures with the help of CutyCapt
73
- test_files: []
74
-
135
+ test_files:
136
+ - spec/capit/capture_spec.rb
137
+ - spec/capit_spec.rb
138
+ - spec/spec_helper.rb