brandeins 0.2.2 → 0.3.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -1
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +5 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +11 -0
  7. data/Gemfile +7 -4
  8. data/Gemfile.lock +47 -21
  9. data/NOTES.md +6 -0
  10. data/Rakefile +15 -8
  11. data/bin/brandeins +3 -1
  12. data/brandeins.gemspec +0 -1
  13. data/lib/brandeins.rb +3 -5
  14. data/lib/brandeins/cli.rb +46 -34
  15. data/lib/brandeins/config.rb +18 -0
  16. data/lib/brandeins/kiosk.rb +100 -0
  17. data/lib/brandeins/merger/external/base.rb +16 -6
  18. data/lib/brandeins/merger/pdf_tools.rb +3 -6
  19. data/lib/brandeins/pages/archive.rb +91 -0
  20. data/lib/brandeins/pages/article.rb +37 -0
  21. data/lib/brandeins/pages/cover.rb +67 -0
  22. data/lib/brandeins/pages/magazine.rb +149 -0
  23. data/lib/brandeins/utils/cli_option_parser.rb +40 -0
  24. data/lib/brandeins/utils/cli_output.rb +100 -0
  25. data/lib/brandeins/utils/fetcher.rb +115 -0
  26. data/lib/brandeins/utils/merger.rb +41 -0
  27. data/lib/brandeins/version.rb +1 -1
  28. data/rubocop-todo.yml +141 -0
  29. data/spec/lib/brandeins/kiosk_spec.rb +66 -0
  30. data/spec/lib/brandeins/pages/archive_spec.rb +40 -0
  31. data/spec/lib/brandeins/pages/article_spec.rb +23 -0
  32. data/spec/lib/brandeins/pages/magazine_spec.rb +91 -0
  33. data/spec/lib/brandeins/utils/fetcher_spec.rb +8 -0
  34. data/spec/lib/brandeins_spec.rb +19 -0
  35. data/spec/spec_helper.rb +23 -0
  36. data/spec/support/capture_stdout.rb +12 -0
  37. data/spec/support/fixtures/archive.html +2365 -0
  38. data/spec/support/fixtures/artikel-masskonfektion-aus-plastik.html +254 -0
  39. data/spec/support/fixtures/artikel-schauspieler-daenemark.html +247 -0
  40. data/{test_support → spec/support}/fixtures/cover.jpg +0 -0
  41. data/spec/support/fixtures/editorial.html +236 -0
  42. data/spec/support/fixtures/just-a.pdf +0 -0
  43. data/spec/support/fixtures/magazine-1-2013.html +242 -0
  44. data/spec/support/fixtures/magazine-cover-fallback.html +1610 -0
  45. data/spec/support/fixtures/magazine-with-cover.html +1416 -0
  46. metadata +68 -61
  47. data/.rvmrc +0 -48
  48. data/lib/brandeins/downloader.rb +0 -111
  49. data/lib/brandeins/errors.rb +0 -5
  50. data/lib/brandeins/parser/archive_site.rb +0 -54
  51. data/lib/brandeins/parser/article_site.rb +0 -26
  52. data/lib/brandeins/parser/magazine_site.rb +0 -49
  53. data/lib/brandeins/setup.rb +0 -38
  54. data/specs/brandeins_spec.rb +0 -52
  55. data/specs/spec_helper.rb +0 -1
  56. data/test/brandeins_test.rb +0 -65
  57. data/test/helper.rb +0 -1
  58. data/test_support/capture_stdout.rb +0 -12
  59. data/test_support/fixtures/brandeins_archiv.html +0 -50
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c1c4cc751ae449333a24d1127535da306b200154
4
+ data.tar.gz: d2a62acfbdef5f2c7f563edc342579bd156523e2
5
+ SHA512:
6
+ metadata.gz: 5120d2ceaa37925d19fa43ca96cf8f5709526c5d7cc368cb19ea1e2c5a520de90670cd5d6c81987919b321967199358621b835a8f0e8c8fdb075d70623f5a9ad
7
+ data.tar.gz: f2a313a749fcd7cebb93822bace5242af17018ae64825e4f1c9d065ec13ec607633138cf6cdff29efe79bbdf3e663c93e985eef53e00d3b47c27ad96a5ee1185
data/.gitignore CHANGED
@@ -2,5 +2,9 @@
2
2
  *.gem
3
3
  .bundle
4
4
  .vagrant
5
- pkg/*
5
+ .yardoc
6
+ pkg
6
7
  tags
8
+ vendor/ruby
9
+ vendor/bundle
10
+ bin
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
@@ -0,0 +1,5 @@
1
+ AllCops:
2
+ Includes:
3
+ - Rakefile
4
+ Excludes:
5
+ - vendor/**
@@ -0,0 +1 @@
1
+ 2.0.0-p247
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ script: "rake spec"
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ gemfile:
8
+ - Gemfile
9
+ branches:
10
+ only:
11
+ - master
data/Gemfile CHANGED
@@ -1,12 +1,15 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  group :test do
4
- if ENV['RUBY_VERSION'][5,3] == '1.8'
5
- gem 'minitest'
6
- end
4
+ gem 'rspec'
7
5
  gem 'webmock'
8
- gem 'debugger'
6
+ gem 'rubocop'
9
7
  end
10
8
 
9
+ group :debug do
10
+ if RUBY_VERSION.split('.').first.to_i > 1
11
+ gem 'byebug'
12
+ end
13
+ end
11
14
 
12
15
  gemspec
@@ -1,47 +1,73 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- brandeins (0.2.2)
4
+ brandeins (0.3.0.pre)
5
5
  nokogiri
6
6
  prawn
7
7
  rake
8
- thor
9
8
 
10
9
  GEM
11
10
  remote: http://rubygems.org/
12
11
  specs:
13
12
  Ascii85 (1.0.2)
14
- addressable (2.3.2)
13
+ addressable (2.3.5)
14
+ afm (0.2.0)
15
+ ast (1.1.0)
16
+ byebug (2.5.0)
17
+ columnize (~> 0.3.6)
18
+ debugger-linecache (~> 1.2.0)
15
19
  columnize (0.3.6)
16
- crack (0.3.2)
17
- debugger (1.3.1)
18
- columnize (>= 0.3.1)
19
- debugger-linecache (~> 1.1.1)
20
- debugger-ruby_core_source (~> 1.1.8)
21
- debugger-linecache (1.1.2)
22
- debugger-ruby_core_source (>= 1.1.1)
23
- debugger-ruby_core_source (1.1.8)
24
- hashery (2.1.0)
25
- nokogiri (1.5.6)
26
- pdf-reader (1.2.0)
20
+ crack (0.4.1)
21
+ safe_yaml (~> 0.9.0)
22
+ debugger-linecache (1.2.0)
23
+ diff-lcs (1.2.5)
24
+ hashery (2.1.1)
25
+ mini_portile (0.5.2)
26
+ nokogiri (1.6.1)
27
+ mini_portile (~> 0.5.0)
28
+ parser (2.0.0)
29
+ ast (~> 1.1)
30
+ slop (~> 3.4, >= 3.4.5)
31
+ pdf-reader (1.3.3)
27
32
  Ascii85 (~> 1.0.0)
33
+ afm (~> 0.2.0)
28
34
  hashery (~> 2.0)
29
35
  ruby-rc4
30
- prawn (1.0.0.rc1)
31
- pdf-reader (>= 0.9.0)
36
+ ttfunk
37
+ powerpack (0.0.9)
38
+ prawn (0.13.0)
39
+ afm
40
+ pdf-reader (~> 1.2)
41
+ ruby-rc4
32
42
  ttfunk (~> 1.0.3)
33
- rake (10.0.3)
43
+ rainbow (1.1.4)
44
+ rake (10.1.0)
45
+ rspec (2.14.1)
46
+ rspec-core (~> 2.14.0)
47
+ rspec-expectations (~> 2.14.0)
48
+ rspec-mocks (~> 2.14.0)
49
+ rspec-core (2.14.7)
50
+ rspec-expectations (2.14.4)
51
+ diff-lcs (>= 1.1.3, < 2.0)
52
+ rspec-mocks (2.14.4)
53
+ rubocop (0.15.0)
54
+ parser (~> 2.0)
55
+ powerpack (~> 0.0.6)
56
+ rainbow (>= 1.1.4)
34
57
  ruby-rc4 (0.1.5)
35
- thor (0.17.0)
58
+ safe_yaml (0.9.7)
59
+ slop (3.4.7)
36
60
  ttfunk (1.0.3)
37
- webmock (1.9.0)
61
+ webmock (1.16.1)
38
62
  addressable (>= 2.2.7)
39
- crack (>= 0.1.7)
63
+ crack (>= 0.3.2)
40
64
 
41
65
  PLATFORMS
42
66
  ruby
43
67
 
44
68
  DEPENDENCIES
45
69
  brandeins!
46
- debugger
70
+ byebug
71
+ rspec
72
+ rubocop
47
73
  webmock
@@ -0,0 +1,6 @@
1
+ # BrandEins Client Design
2
+
3
+ * Usage
4
+ require 'brandeins/client'
5
+ client = BrandEins::Client.new
6
+
data/Rakefile CHANGED
@@ -1,18 +1,25 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
+ require 'rubocop/rake_task'
4
+ require 'rspec/autorun'
3
5
 
4
- Rake::TestTask.new do |t|
5
- t.test_files = FileList['test/*_test.rb', 'specs/*_spec.rb']
6
- t.verbose = true
6
+ Rubocop::RakeTask.new
7
+
8
+ Rake::TestTask.new(:spec) do |t|
9
+ t.pattern = 'spec/lib/**/*_spec.rb'
10
+ t.verbose = true
11
+ end
12
+
13
+ task :load_version_file do
14
+ require_relative './lib/brandeins/version'
7
15
  end
8
16
 
9
- task :install do
10
- require './lib/brandeins/version'
17
+ task install: [ :load_version_file ] do
18
+ require_relative './lib/brandeins/version'
11
19
  sh "gem install ./pkg/brandeins-#{BrandEins::VERSION}.gem"
12
20
  end
13
21
 
14
- task publish: [ :build ] do
15
- require './lib/brandeins/version'
22
+ task install: [ :load_version_file, :build ] do
16
23
  sh "gem push ./pkg/brandeins-#{BrandEins::VERSION}.gem"
17
24
  end
18
25
 
@@ -32,4 +39,4 @@ rule /^version:bump:(major|minor|patch)/ do |t|
32
39
  sh "git add #{file} Gemfile.lock && git commit -m 'bump version to #{new_version}'"
33
40
  end
34
41
 
35
- task :default => :test
42
+ task default: :spec
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'brandeins'
4
- BrandEins::CLI.start
4
+ require 'brandeins/cli'
5
+
6
+ BrandEins::Cli.run
@@ -12,7 +12,6 @@ Gem::Specification.new do |s|
12
12
  s.description = %q{BrandEins gem offers a download command to download a specific or all volumes. Use `brandeins help` to find out more about it}
13
13
 
14
14
  s.add_dependency "rake"
15
- s.add_dependency "thor"
16
15
  s.add_dependency "nokogiri"
17
16
  s.add_dependency "prawn"
18
17
 
@@ -1,9 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'brandeins/version'
4
- require 'brandeins/errors'
5
- require 'brandeins/downloader'
6
- require 'brandeins/setup'
7
- require 'brandeins/cli'
3
+ require_relative 'brandeins/version'
4
+ require_relative 'brandeins/config'
5
+ require_relative 'brandeins/kiosk'
8
6
 
9
7
  module BrandEins; end
@@ -1,47 +1,59 @@
1
1
  # encoding: utf-8
2
- require 'thor'
2
+
3
+ require 'optparse'
4
+ require 'ostruct'
5
+ require 'singleton'
6
+
7
+ require_relative '../brandeins'
8
+ require_relative '../brandeins/utils/cli_option_parser'
3
9
 
4
10
  module BrandEins
5
- class CLI < Thor
6
- map '--version' => :version
11
+ class CLI
12
+ include Singleton
7
13
 
8
- desc '--version', 'Displays current version'
9
- def version
10
- puts BrandEins::VERSION
14
+ def self.run(args)
15
+ @@args = args
16
+ instance.run
11
17
  end
12
18
 
13
- desc 'download', 'Download past brand eins magazines (use `brandeins help download` to learn more about options)'
14
- method_option :path, :type => :string
15
- method_option :volume, :type => :numeric
16
- method_option :all
17
- method_option :year, :type => :numeric
18
- def download
19
- path = options.path ? File.expand_path(options.path) : Dir.pwd
20
- year = options.year || Time.new.year
21
- all = options.all
22
- volume = options.volume
23
-
24
- if volume.nil? and all.nil?
25
- puts "If you want to download a specific volune use the --volume flag or use --all to download all volumes of a year"
26
- else
27
- downloader = BrandEins::Downloader.new(path, verbose: true)
28
- if !all.nil?
29
- downloader.get_magazines_of_year year
30
- else
31
- downloader.get_magazine year, volume
32
- end
19
+ def initialize(args = ARGV)
20
+ @args = (@@args || args).dup
21
+ end
22
+
23
+ def run
24
+ options = BrandEins::CliOptionParser.parse(@args)
25
+ if options.version
26
+ puts BrandEins::VERSION
27
+ return
28
+ end
29
+ if options.download
30
+ path = options.path
31
+ month = options.month
32
+ year = options.year
33
+ errors = validate_options(options)
34
+ puts errors.join("\n") && return unless error.empty?
35
+ download(path: path, month: month, yeaR: year)
33
36
  end
34
37
  end
35
38
 
36
- desc 'setup', 'Checks if all requirements for using brandeins gem are met'
37
- method_option :help
38
- def setup
39
- setup = BrandEins::Setup.new
40
- if !options.help.nil?
41
- setup.help
42
- else
43
- setup.run
39
+ def download(path: nil, month: nil, year: nil)
40
+ kiosk = BrandEins::Kiosk.new(path: path)
41
+ kiosk.fetch_magazine(month: month, year: year)
42
+ rescue BrandEins::Utils::Fetcher::ContentNotFetchedError => e
43
+ puts "Download Error: #{e}\n\n"
44
+ puts "#{e.backtrace.join('\n')}"
45
+ end
46
+
47
+ def validate_options
48
+ errors = []
49
+ if !(1..12).include? @options.month
50
+ errors.push("Invalid month: Must be between 1 and 12")
44
51
  end
52
+ if !(2000..Time.now.year).include? @options.year
53
+ errors.push("Invalid year: Must be between 2000 and #{Time.now.year}")
54
+ end
55
+ return errors
45
56
  end
57
+
46
58
  end
47
59
  end
@@ -0,0 +1,18 @@
1
+ require 'pathname'
2
+ # Access it like:
3
+ # BrandEins::Config['base_uri']
4
+ #
5
+ module BrandEins
6
+ class Config
7
+ BASE_URI = 'http://www.brandeins.de'
8
+ ARCHIVE_URI = BASE_URI + '/archiv.html'
9
+ BASE_PATH = Pathname.new(ENV['HOME']) + '.brandeins'
10
+ CACHE_PATH = BASE_PATH + 'cache'
11
+ TEMP_PATH = BASE_PATH + 'temp'
12
+ CACHE_LIMIT_BYTES = 400 * 1024 ** 2
13
+
14
+ def self.[](name)
15
+ const_get(name.upcase)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,100 @@
1
+ require 'pathname'
2
+ require 'fileutils'
3
+
4
+ require_relative 'config'
5
+ require_relative 'utils/fetcher'
6
+ require_relative 'pages/archive'
7
+ require_relative 'pages/archive'
8
+ require_relative 'pages/cover'
9
+ require_relative 'merger/pdf_tools'
10
+
11
+ module BrandEins
12
+ # Usage of
13
+ class Kiosk
14
+ attr_reader :target_path
15
+
16
+ class InvalidPathError < StandardError; end
17
+
18
+ def initialize(opts = {})
19
+ @target_path = opts.fetch(:path) { Pathname.new('.').realpath.to_s }
20
+ raise_if_path_inaccessible
21
+ set_opts_for_cli_output(opts)
22
+ create_directories_if_necessary
23
+ end
24
+
25
+ def create_directories_if_necessary
26
+ cache_path.mkpath unless cache_path.exist?
27
+ temp_path.mkpath unless temp_path.exist?
28
+ end
29
+
30
+ def set_opts_for_cli_output(opts)
31
+ cli_output_opts = {}
32
+ cli_output_opts[:debug] = !!opts[:verbose]
33
+ cli.set_options(cli_output_opts)
34
+ end
35
+
36
+ def raise_if_path_inaccessible
37
+ path = Pathname.new(@target_path)
38
+ if !path.writable?
39
+ raise InvalidPathError, 'Could not access the given path'
40
+ end
41
+ end
42
+
43
+ def download_magazine(month: nil, year: nil)
44
+ magazine = fetch_magazine(month: month, year: year)
45
+ cover_pdf_path = download_cover(magazine)
46
+ article_pdf_paths = download_article_pdfs(magazine)
47
+ magazine_pdf_files = article_pdfs.unshift(cover_pdf)
48
+ merge_pdf_files(magazine_pdf_files)
49
+ end
50
+
51
+ def merge_pdf_files(pdf_files)
52
+ magazine_file_path = magazine_file_path(month: month, year: year)
53
+ merger.merge_pdf_files(pdf_files, magazine_file_path)
54
+ clear_temp_path
55
+ magazine_file_path
56
+ end
57
+
58
+ def download_article_pdfs(magazine)
59
+ magazine.save_articles_to(temp_path)
60
+ end
61
+
62
+ def download_cover(magazine)
63
+ cover = BrandEins::Pages::Cover.new(magazine)
64
+ cover.save_to(temp_path)
65
+ end
66
+
67
+ def fetch_magazine(month: nil, year: nil)
68
+ archive.magazine_for(month: month, year: year)
69
+ end
70
+
71
+ def magazine_file_path(month: nil, year: nil)
72
+ Pathname.new(@target_path) + "brandeins-#{month}-#{year}.pdf"
73
+ end
74
+
75
+ def clear_temp_path
76
+ FileUtils.rm Dir["#{temp_path}/*.pdf"]
77
+ end
78
+
79
+ def merger
80
+ @merger ||= BrandEins::Merger::PdfTools.get_pdf_tool
81
+ end
82
+
83
+ def temp_path
84
+ BrandEins::Config['temp_path']
85
+ end
86
+
87
+ def cache_path
88
+ BrandEins::Config['cache_path']
89
+ end
90
+
91
+ def archive
92
+ @archive ||= BrandEins::Pages::Archive.new
93
+ end
94
+
95
+ def cli
96
+ @cli ||= BrandEins::Utils::CliOutput.instance
97
+ end
98
+
99
+ end
100
+ end