pdf_meta 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e9f12db955d00234ed51e48f58b1800e13ca3f2
4
+ data.tar.gz: 0f243667baf7db768ab67a67abab28802ffe66b7
5
+ SHA512:
6
+ metadata.gz: b42fd05c5a5d38c6be7374e204b32df299a884522335de7d9836cc49659ec46a8367bdcab208e05bcffeabcdecf48a1fb107ea1a42c6b021600c447a6d9d08e7
7
+ data.tar.gz: c35a3ce898f7b30c066812f0aca5523c1ee5931be6a2dd1772c7a0d2f51ce46f85a94ec60397de249d338f4d282bfe110c3f3b5d6dcbcb8403dc5b68cb788176
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pdf_meta.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Adam Carlile
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # PdfMeta
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pdf_meta`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'pdf_meta'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install pdf_meta
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/pdf_meta/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'spec'
6
+ t.pattern = "spec/*_spec.rb"
7
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pdf_meta"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ module PDFMeta
2
+ class Command
3
+
4
+ def initialize(path='', options={})
5
+ raise ArgumentError if path.empty?
6
+
7
+ @path = path
8
+ @options = default_options.merge(options)
9
+ end
10
+
11
+ def run!
12
+ runner = PDFMeta::Runner.new(command)
13
+ if runner.success?
14
+ PDFMeta::Parser.new(runner.output).results
15
+ else
16
+ raise runner.error
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def command
23
+ [@options[:command], @options[:switches], @path].compact.join(' ')
24
+ end
25
+
26
+ def default_options
27
+ {
28
+ command: 'pdfinfo',
29
+ switches: nil
30
+ }
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ module PDFMeta
2
+ class Parser
3
+
4
+ def initialize(output)
5
+ @output = output
6
+ end
7
+
8
+ def results
9
+ @results ||= PDFMeta::Results.new(parsed_output)
10
+ end
11
+
12
+ private
13
+
14
+ def parsed_output
15
+ out = @output.split("\n").map {|x| x.split(':', 2)}
16
+ hash = {}
17
+ out = out.each do |row|
18
+ hash[row.first.underscore.parameterize.underscore.to_sym] = row.last.strip
19
+ end
20
+ hash
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module PDFMeta
2
+ class Results
3
+ class FileSize < Virtus::Attribute
4
+
5
+ def coerce(value)
6
+ value.to_i
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ module PDFMeta
2
+ class Results
3
+ class PageSize
4
+ include Virtus.model
5
+
6
+ attribute :height, Float
7
+ attribute :width, Float
8
+ attribute :units, String
9
+
10
+ def initialize(value)
11
+ self.units = value.split(' ').last
12
+ self.height = value.split(' ')[0]
13
+ self.width = value.split(' ')[2]
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module PDFMeta
2
+ class Results
3
+ include Virtus.model
4
+
5
+ attribute :title, String
6
+ attribute :author, String
7
+ attribute :creator, String
8
+ attribute :producer, String
9
+ attribute :creation_date, DateTime
10
+ attribute :mod_date, DateTime
11
+ attribute :tagged, Boolean
12
+ attribute :user_properties, Boolean
13
+ attribute :suspects, Boolean
14
+ attribute :form, String
15
+ attribute :java_script, Boolean
16
+ attribute :pages, Integer
17
+ attribute :encrypted, Boolean
18
+ attribute :page_size, PDFMeta::Results::PageSize
19
+ attribute :page_rot, Integer
20
+ attribute :file_size, PDFMeta::Results::FileSize
21
+ attribute :optimized, Boolean
22
+ attribute :pdf_version, Float
23
+
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ module PDFMeta
2
+ class Runner
3
+
4
+ attr_reader :output
5
+ def initialize(command)
6
+ stdin, stdout, stderr, wait_thr = Open3.popen3(command)
7
+ @output = stdout.read
8
+ @error = stderr.read
9
+ @process = wait_thr.value
10
+ end
11
+
12
+ def success?
13
+ @process.success?
14
+ end
15
+
16
+ def error
17
+ error_mapping[@process.exitstatus].new(@error)
18
+ end
19
+
20
+ private
21
+
22
+ def error_mapping
23
+ @error_mapping ||= Hash.new(UnknownError).merge({
24
+ 1 => PDFMeta::UnableToReadFileError,
25
+ 2 => PDFMeta::UnableOpenOutputFileError,
26
+ 3 => PDFMeta::PDFPermissionError,
27
+ 99 => PDFMeta::UnknownPopplerError
28
+ })
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module PDFMeta
2
+ VERSION = "0.1.0"
3
+ end
data/lib/pdf_meta.rb ADDED
@@ -0,0 +1,58 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+ require "virtus"
4
+ require "open3"
5
+ require "pry"
6
+
7
+ require "pdf_meta/version"
8
+
9
+ require "pdf_meta/command"
10
+ require "pdf_meta/runner"
11
+ require "pdf_meta/parser"
12
+
13
+ require "pdf_meta/results/file_size"
14
+ require "pdf_meta/results/page_size"
15
+ require "pdf_meta/results"
16
+
17
+
18
+ module PDFMeta
19
+ PopplerMissingError = Class.new(StandardError)
20
+ UnknownError = Class.new(StandardError)
21
+ UnableToReadFileError = Class.new(StandardError)
22
+ UnableOpenOutputFileError = Class.new(StandardError)
23
+ PDFPermissionError = Class.new(StandardError)
24
+ UnknownPopplerError = Class.new(StandardError)
25
+
26
+ module_function
27
+
28
+ def config
29
+ @config ||= {
30
+ command_path: 'pdfinfo'
31
+ }
32
+ end
33
+
34
+ def configure &block
35
+ reset!
36
+ yield(config) if block_given?
37
+ end
38
+
39
+ def reset!
40
+ @config = nil
41
+ @available = nil
42
+ end
43
+
44
+ def read(file)
45
+ raise PopplerMissingError unless available?
46
+ out = case file
47
+ when File then file.path
48
+ else file
49
+ end
50
+ PDFMeta::Command.new(out).run!
51
+ end
52
+
53
+ def available?
54
+ @available ||= system("which #{config[:command_path]}")
55
+ end
56
+
57
+
58
+ end
data/pdf_meta.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pdf_meta/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pdf_meta"
8
+ spec.version = PDFMeta::VERSION
9
+ spec.authors = ["Adam Carlile"]
10
+ spec.email = ["adam.carlile@boardintelligence.co.uk"]
11
+
12
+ spec.summary = %q{A gem for reading file data from poppler pdfinfo}
13
+ spec.homepage = "http://github.com/boardiq/pdf_meta"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_dependency "virtus"
23
+
24
+ spec.add_development_dependency "minitest"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "awesome_print"
27
+ spec.add_development_dependency "bundler", "~> 1.9"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdf_meta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Carlile
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: virtus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: awesome_print
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ description:
112
+ email:
113
+ - adam.carlile@boardintelligence.co.uk
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - lib/pdf_meta.rb
127
+ - lib/pdf_meta/command.rb
128
+ - lib/pdf_meta/parser.rb
129
+ - lib/pdf_meta/results.rb
130
+ - lib/pdf_meta/results/file_size.rb
131
+ - lib/pdf_meta/results/page_size.rb
132
+ - lib/pdf_meta/runner.rb
133
+ - lib/pdf_meta/version.rb
134
+ - pdf_meta.gemspec
135
+ homepage: http://github.com/boardiq/pdf_meta
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.4.5
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: A gem for reading file data from poppler pdfinfo
159
+ test_files: []
160
+ has_rdoc: