brandeins 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/brandeins.gemspec CHANGED
@@ -22,9 +22,8 @@ Gem::Specification.new do |s|
22
22
  s.require_paths = ["lib"]
23
23
 
24
24
  s.post_install_message =<<-EOT
25
- BrandEins gem currently runs on unix systems only and depends on pdftk to merge downloaded pdfs.
25
+ BrandEins gem runs on windows and os x and depends on pdftk/ghostscript to merge downloaded pdfs.
26
26
  Run `brandeins setup` to check if all requirements are met and for informations on how to meet them.
27
27
 
28
-
29
28
  EOT
30
29
  end
data/lib/brandeins.rb CHANGED
@@ -50,25 +50,13 @@ module BrandEins
50
50
  end
51
51
 
52
52
  desc 'setup', 'Checks if all requirements for using brandeins gem are met'
53
+ method_option :help
53
54
  def setup
54
- BrandEinsSetup.new
55
- end
56
-
57
- desc 'test', 'test some stuff'
58
- method_option :input, :type => :string
59
- method_option :output, :type => :string
60
- def test
61
- gs = BrandEins::PdfTools::GhostscriptWin.new
62
- if gs.available?
63
- puts "GS is available"
64
- if options.input.nil? || options.output.nil?
65
- puts "need input/output to merge files"
66
- else
67
- puts "input: #{options.input}, output: #{options.output}"
68
- BrandEins::PdfTools::GhostscriptWin.merge_pdf_files(options.input, options.output)
69
- end
55
+ setup = BrandEins::Setup.new
56
+ if !options.help.nil?
57
+ setup.help
70
58
  else
71
- puts "GS not found"
59
+ setup.run
72
60
  end
73
61
  end
74
62
  end
@@ -2,18 +2,20 @@ module BrandEins
2
2
  module PdfTools
3
3
  attr_reader :pdf_tools, :pdf_tool
4
4
 
5
- def self.get_pdf_tool
5
+ def self.get_pdf_tool(env = nil)
6
+ @env = Hash.new
7
+ env = Hash.new if env.nil?
8
+ @env[:os] = env[:os] || RUBY_PLATFORM
9
+
6
10
  @pdf_tools ||= _init_pdf_tools
7
11
  @pdf_tool ||= @pdf_tools.first.new if @pdf_tools.length > 0
8
- return @pdf_tool
9
12
  end
10
13
 
11
14
  class Template
12
- attr_accessor :cmd, :args, :info
15
+ attr_accessor :cmd, :args, :noop
13
16
 
14
17
  def available?
15
- puts "trying to execute: #{@cmd}"
16
- _cmd_available? @cmd
18
+ _cmd_available? @cmd, @noop
17
19
  end
18
20
 
19
21
  def merge_pdf_files(pdf_files, target_pdf)
@@ -31,9 +33,9 @@ module BrandEins
31
33
  end
32
34
 
33
35
  private
34
- def _cmd_available? (cmd)
36
+ def _cmd_available? (cmd, args)
35
37
  begin
36
- open("|#{cmd}").close
38
+ open("|#{cmd} #{args}").close
37
39
  rescue Exception
38
40
  return false
39
41
  end
@@ -46,26 +48,26 @@ module BrandEins
46
48
 
47
49
  class PdftkOSX < TemplateOSX
48
50
  def initialize
49
- @cmd = 'pdftk2'
51
+ @cmd = 'pdftk'
50
52
  @args = '__pdf_files__ output __target_pdf__'
51
- @info = 'Visit http://test.com'
53
+ @noop = ' --version'
52
54
  end
53
55
  end
54
56
 
55
57
  class GhostscriptWin < TemplateWin
56
58
  def initialize
57
- @cmd = '"C:/Program Files/gs/gs9.06/bin/gswin64c.exe"'
59
+ @cmd = 'gswin64c.exe'
58
60
  @args = ' -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=__target_pdf__ __pdf_files__'
59
- @info = 'Visit me!'
61
+ @noop = ' --version'
60
62
  end
61
63
  end
62
64
 
63
65
  private
64
66
  def self._init_pdf_tools
65
67
  @pdf_tools = Array.new
66
- if RUBY_PLATFORM.include? 'w32'
68
+ if @env[:os].include? 'w32'
67
69
  return _get_subclasses TemplateWin
68
- elsif RUBY_PLATFORM.include? 'darwin'
70
+ elsif @env[:os].include? 'darwin'
69
71
  return _get_subclasses TemplateOSX
70
72
  else
71
73
  return nil
@@ -1,22 +1,40 @@
1
- class BrandEinsSetup
2
- attr_reader :pdf_tools
1
+ require File.expand_path '../pdf-tools', __FILE__
3
2
 
4
- def initialize
5
- @pdf_tools = Array.new
6
- end
3
+ module BrandEins
4
+ class Setup
5
+ attr_reader :pdf_tool
7
6
 
8
- def check_requirements
9
- puts 'Checking requirements for your system'
10
- if @pdf_tools.any? { |pdf_tool| pdf_tool.available? }
11
- puts 'It seems you have at least one supported pdf tool installed. You are ready to go!'
12
- else
13
- puts 'It seems you dont have any pdf tools installed or brandeins was not able to locate them.'
14
- puts pdf_tool_instructions
7
+ def initialize(env = nil)
8
+ env = Hash.new if env.nil?
9
+ @os = env[:os] || RUBY_PLATFORM
10
+ @pdf_tool = env[:pdf_tool] || BrandEins::PdfTools.get_pdf_tool(env)
15
11
  end
16
- end
17
12
 
18
- def add_pdf_tool(pdf_tool)
19
- @pdf_tools << pdf_tool
20
- end
13
+ def run
14
+ puts 'Checking requirements for your system'
15
+ if @pdf_tool.available?
16
+ puts "\nIt seems you have #{@pdf_tool.cmd} running on your system. You are ready to go!"
17
+ else
18
+ puts "\nIt seems you are missing a working pdf utility on your system. Use `brandeins setup --help` to get help."
19
+ end
20
+ end
21
21
 
22
+ def help
23
+ if @os.include? 'darwin'
24
+ puts 'In order to use brandeins gem on OS X you have to install pdftk.'
25
+ puts 'You can find the binaries for pdftk here: http://www.pdflabs.com/docs/install-pdftk/'
26
+ puts ''
27
+ puts 'After this, run `brandeins setup` again.'
28
+ elsif @os.include? 'w32'
29
+ puts 'In order to use brandeins gem on Windows you have to install ghostscript: http://www.ghostscript.com/download/'
30
+ puts 'After installing ghoscript you have to put the installation path of the bin folder containing "gswin64c.exe"'
31
+ puts 'into your PATH environment variable. Here a quick how-to for achieving this: http://www.computerhope.com/issues/ch000549.htm'
32
+ puts ''
33
+ puts 'After this, run `brandeins setup` again.'
34
+ else
35
+ puts 'Your operating system seems to be not supported. Contact the gem author for more information: me@grekko.de'
36
+ end
37
+ end
38
+
39
+ end
22
40
  end
@@ -1,3 +1,3 @@
1
1
  module BrandEins
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -1,4 +1,6 @@
1
+ require File.expand_path('../helper' , __FILE__)
1
2
  require File.expand_path('../../lib/brandeins' , __FILE__)
3
+ require File.expand_path('../../lib/brandeins/setup' , __FILE__)
2
4
  require 'minitest/autorun'
3
5
  require 'fakefs/safe'
4
6
 
@@ -55,4 +57,29 @@ class TestBrandEinsDownload < MiniTest::Unit::TestCase
55
57
  cover = archive_site.get_magazine_cover(2012, 4)
56
58
  assert_equal cover, { :title => "SCHWERPUNKT Kapitalismus", :img_url => "#{@base_url}/typo3temp/pics/08ff826417.jpg" }
57
59
  end
60
+
61
+ def test_brandeins_setup_output
62
+ pdf_tool = Object.new
63
+ pdf_tool.define_singleton_method :available? do true end
64
+ pdf_tool.define_singleton_method :cmd do 'magic' end
65
+
66
+ setup = BrandEins::Setup.new :os => 'x86_64-darwin12.2.0', :pdf_tool => pdf_tool
67
+ out = capture_stdout do
68
+ setup.run
69
+ end
70
+
71
+ assert_equal "Checking requirements for your system\n\nIt seems you have magic running on your system. You are ready to go!\n", out.string
72
+ end
73
+
74
+ def test_brandeins_setup_missing_pdf_tool_path
75
+ pdf_tool = Object.new
76
+ pdf_tool.define_singleton_method :available? do false end
77
+ pdf_tool.define_singleton_method :cmd do 'magic' end
78
+
79
+ capture_stdout do
80
+ BrandEins::Setup.new :os => 'x86_64-darwin12.2.0', :pdf_tool => pdf_tool
81
+ end
82
+
83
+ assert_equal "Checking requirements for your system\n\nIt seems you have magic running on your system. You are ready to go!\n", out.string
84
+ end
58
85
  end
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'stringio'
2
+
3
+ module Kernel
4
+ def capture_stdout
5
+ out = StringIO.new
6
+ $stdout = out
7
+ yield
8
+ return out
9
+ ensure
10
+ $stdout = STDOUT
11
+ end
12
+ end
13
+
14
+ # Use capture_stdout like this:
15
+ # out = capture_stdout do
16
+ # execute_code_that_puts 'some string'
17
+ # end
18
+ # assert_equal "some string", out.string
19
+ # http://thinkingdigitally.com/archive/capturing-output-from-puts-in-ruby/
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brandeins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-10 00:00:00.000000000 Z
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -97,16 +97,16 @@ files:
97
97
  - lib/brandeins/setup.rb
98
98
  - lib/brandeins/version.rb
99
99
  - test/brandeins_test.rb
100
+ - test/helper.rb
100
101
  homepage: http://www.grekko.de
101
102
  licenses: []
102
- post_install_message: ! 'BrandEins gem currently runs on unix systems only and depends
103
- on pdftk to merge downloaded pdfs.
103
+ post_install_message: ! 'BrandEins gem runs on windows and os x and depends on pdftk/ghostscript
104
+ to merge downloaded pdfs.
104
105
 
105
106
  Run `brandeins setup` to check if all requirements are met and for informations
106
107
  on how to meet them.
107
108
 
108
109
 
109
-
110
110
  '
111
111
  rdoc_options: []
112
112
  require_paths:
@@ -119,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  segments:
121
121
  - 0
122
- hash: 1148308714876892824
122
+ hash: -590436749218099656
123
123
  required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  none: false
125
125
  requirements:
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
128
  version: '0'
129
129
  segments:
130
130
  - 0
131
- hash: 1148308714876892824
131
+ hash: -590436749218099656
132
132
  requirements: []
133
133
  rubyforge_project:
134
134
  rubygems_version: 1.8.24
@@ -137,3 +137,4 @@ specification_version: 3
137
137
  summary: BrandEins gem allows you to download past volumes of the Brand Eins magazine
138
138
  test_files:
139
139
  - test/brandeins_test.rb
140
+ - test/helper.rb