Skip to content

Commit

Permalink
Merge branch 'feature/determine-path-of-executable' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ezkl committed Jan 13, 2012
2 parents 2dc41ce + 3b7a8ec commit b315c66
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
1 change: 1 addition & 0 deletions capit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- spec/*`.split("\n")

s.require_paths = ["lib"]
s.add_dependency("hike")
s.add_development_dependency('rspec')
end
35 changes: 15 additions & 20 deletions lib/capit/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Capture
# The URL of the page to be captured
attr_reader :url

attr_accessor :folder, :filename, :user_agent, :max_wait, :delay, :output
attr_accessor :folder, :filename, :user_agent, :max_wait, :delay, :output, :cutycapt_path

# Initialize a new Capture
# @param [String] url The URL we want to capture.
Expand All @@ -45,14 +45,14 @@ class Capture
# @raise InvalidExtensionError
#
def initialize url, options = {}
cutycapt_installed?
@url = url
@folder = options[:folder] || Dir.pwd
@filename = options[:filename] || "capit.jpeg"
@user_agent = options[:user_agent] || "CapIt! [http://github.com/meadvillerb/capit]"
@max_wait = options[:max_wait] || 15000
@delay = options[:delay]

@url = url
@folder = options[:folder] || Dir.pwd
@filename = options[:filename] || "capit.jpeg"
@user_agent = options[:user_agent] || "CapIt! [http://github.com/meadvillerb/capit]"
@max_wait = options[:max_wait] || 15000
@delay = options[:delay]
@cutycapt_path = options[:cutycapt_path] || determine_cutycapt_path
valid_extension?(@filename)
end

Expand Down Expand Up @@ -106,7 +106,7 @@ def successful?
# @return [String]
#
def capture_command
cmd = "CutyCapt --url='#{@url}'"
cmd = "#{@cutycapt_path} --url='#{@url}'"
cmd += " --out='#{@folder}/#{@filename}'"
cmd += " --max-wait=#{@max_wait}"
cmd += " --delay=#{@delay}" if @delay
Expand All @@ -120,6 +120,10 @@ def capture_command
end
end

def determine_cutycapt_path
`which CutyCapt`.strip or `which cutycapt`.strip
end

# Uses RUBY_PLATFORM to determine the operating system.
# Not foolproof, but good enough for the time being.
#
Expand All @@ -133,14 +137,5 @@ def determine_os
else raise InvalidOSError, "CapIt currently only works on the Mac and Linux platforms"
end
end

# Checks to see if CutyCapt is available in PATH.
# Raises CutyCaptError if not.
#
# @return
#
def cutycapt_installed?
raise CutyCaptError, "CutyCapt must be installed and available on PATH" if `which CutyCapt`.empty?
end
end
end
end
26 changes: 16 additions & 10 deletions spec/capit/capture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
subject { @capit }

describe "#initialize" do
it { should respond_to :url, :folder, :filename, :user_agent, :max_wait, :delay }
it { should respond_to :url, :folder, :filename, :user_agent, :max_wait, :delay, :cutycapt_path }

context "defaults" do
specify { @capit.folder.should == Dir.pwd }
Expand Down Expand Up @@ -53,6 +53,20 @@
end
end

describe "#cutycapt_path=" do
it "should allow the user to set CutyCapt's path" do
capit = CapIt::Capture.new("http://mdvlrb.com/")
capit.cutycapt_path = "/usr/local/sbin/CutyCapt"
capit.cutycapt_path.should == "/usr/local/sbin/CutyCapt"
end
end

describe "#determine_cutycapt_path" do
it "should determine the appropriate path for the executable if possible" do
@capit.determine_cutycapt_path.should == "/usr/local/bin/CutyCapt"
end
end

describe "#capture_command" do
before { @capit = CapIt::Capture.new("http://mdvlrb.com/") }
subject { @capit }
Expand All @@ -68,7 +82,7 @@
context "when platform is Mac" do
it "should not add the xvfb prefix" do
with_constants :RUBY_PLATFORM => "darwin" do
@capit.capture_command.should match /^CutyCapt/
@capit.capture_command.should match /CutyCapt/
end
end
end
Expand All @@ -82,14 +96,6 @@
end
end
end

context "when CutyCapt executable is unavailable" do
it "should raise an error" do
with_environment_variable 'PATH' => "" do
expect { CapIt::Capture.new("http://mdvlrb.com/") }.to raise_error(/CutyCapt/)
end
end
end
end
end
end

0 comments on commit b315c66

Please sign in to comment.