libreservice 0.0.1

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: cee8faaeacdc8c50c247c3873ef947740087f985
4
+ data.tar.gz: b4aa4f61c2b9b5883cfb3389e1d4c68dbdb8624d
5
+ SHA512:
6
+ metadata.gz: b805f10bb668f07bd90552ab9af157ae2999447987883b8d66b36c8935bfddb5601bc1a6e4733506cbeaadc21ea7f1d1eb5f5e7c2d7ab28d119033b1e13fd41c
7
+ data.tar.gz: 78f6173c6f55c20cb9e42dc671d873f33e30be174eeaa15eeae4787d88dca62bfc936987474d7cbe46907c24333fd46a807aca5391d43076f9e31dbdda6c0f11
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ tmp/*
data/.vagrant ADDED
@@ -0,0 +1 @@
1
+ {"active":{"default":"eb9bece8-eeee-4ff5-9f22-a73790bc37ed"}}
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in libreservice.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andre Bernardes
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Libreservice
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'libreservice'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install libreservice
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/Vagrantfile ADDED
@@ -0,0 +1,99 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ Vagrant::Config.run do |config|
5
+ # All Vagrant configuration is done here. The most common configuration
6
+ # options are documented and commented below. For a complete reference,
7
+ # please see the online documentation at vagrantup.com.
8
+
9
+ # Every Vagrant virtual environment requires a box to build off of.
10
+ config.vm.box = "precise64"
11
+
12
+ # The url from where the 'config.vm.box' box will be fetched if it
13
+ # doesn't already exist on the user's system.
14
+ # config.vm.box_url = "http://domain.com/path/to/above.box"
15
+
16
+ # Boot with a GUI so you can see the screen. (Default is headless)
17
+ # config.vm.boot_mode = :gui
18
+
19
+ # Assign this VM to a host-only network IP, allowing you to access it
20
+ # via the IP. Host-only networks can talk to the host machine as well as
21
+ # any other machines on the same network, but cannot be accessed (through this
22
+ # network interface) by any external networks.
23
+ # config.vm.network :hostonly, "192.168.33.10"
24
+
25
+ # Assign this VM to a bridged network, allowing you to connect directly to a
26
+ # network using the host's network device. This makes the VM appear as another
27
+ # physical device on your network.
28
+ # config.vm.network :bridged
29
+
30
+ # Forward a port from the guest to the host, which allows for outside
31
+ # computers to access the VM, whereas host only networking does not.
32
+ config.vm.forward_port 4567, 4567
33
+
34
+ # Share an additional folder to the guest VM. The first argument is
35
+ # an identifier, the second is the path on the guest to mount the
36
+ # folder, and the third is the path on the host to the actual folder.
37
+ # config.vm.share_folder "v-data", "/vagrant_data", "../data"
38
+
39
+ # Enable provisioning with Puppet stand alone. Puppet manifests
40
+ # are contained in a directory path relative to this Vagrantfile.
41
+ # You will need to create the manifests directory and a manifest in
42
+ # the file precise64.pp in the manifests_path directory.
43
+ #
44
+ # An example Puppet manifest to provision the message of the day:
45
+ #
46
+ # # group { "puppet":
47
+ # # ensure => "present",
48
+ # # }
49
+ # #
50
+ # # File { owner => 0, group => 0, mode => 0644 }
51
+ # #
52
+ # # file { '/etc/motd':
53
+ # # content => "Welcome to your Vagrant-built virtual machine!
54
+ # # Managed by Puppet.\n"
55
+ # # }
56
+ #
57
+ # config.vm.provision :puppet do |puppet|
58
+ # puppet.manifests_path = "manifests"
59
+ # puppet.manifest_file = "precise64.pp"
60
+ # end
61
+
62
+ # Enable provisioning with chef solo, specifying a cookbooks path, roles
63
+ # path, and data_bags path (all relative to this Vagrantfile), and adding
64
+ # some recipes and/or roles.
65
+ #
66
+ # config.vm.provision :chef_solo do |chef|
67
+ # chef.cookbooks_path = "../my-recipes/cookbooks"
68
+ # chef.roles_path = "../my-recipes/roles"
69
+ # chef.data_bags_path = "../my-recipes/data_bags"
70
+ # chef.add_recipe "mysql"
71
+ # chef.add_role "web"
72
+ #
73
+ # # You may also specify custom JSON attributes:
74
+ # chef.json = { :mysql_password => "foo" }
75
+ # end
76
+
77
+ # Enable provisioning with chef server, specifying the chef server URL,
78
+ # and the path to the validation key (relative to this Vagrantfile).
79
+ #
80
+ # The Opscode Platform uses HTTPS. Substitute your organization for
81
+ # ORGNAME in the URL and validation key.
82
+ #
83
+ # If you have your own Chef Server, use the appropriate URL, which may be
84
+ # HTTP instead of HTTPS depending on your configuration. Also change the
85
+ # validation key to validation.pem.
86
+ #
87
+ # config.vm.provision :chef_client do |chef|
88
+ # chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
89
+ # chef.validation_key_path = "ORGNAME-validator.pem"
90
+ # end
91
+ #
92
+ # If you're using the Opscode platform, your validator client is
93
+ # ORGNAME-validator, replacing ORGNAME with your organization name.
94
+ #
95
+ # IF you have your own Chef Server, the default validation client name is
96
+ # chef-validator, unless you changed the configuration.
97
+ #
98
+ # chef.validation_client_name = "ORGNAME-validator"
99
+ end
data/bin/libreservice ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ print "Starting daemonized service..."
3
+ if system 'rackup -E production -D -s thin'
4
+ puts "Done!"
5
+ else
6
+ puts "Error"
7
+ end
data/config.ru ADDED
@@ -0,0 +1,2 @@
1
+ require './lib/server'
2
+ run Libreservice::Server
data/lib/document.rb ADDED
@@ -0,0 +1,37 @@
1
+ require_relative 'document_converter'
2
+
3
+ module Libreservice
4
+ class Document
5
+ attr_reader :file
6
+
7
+ def initialize(file_param, options={})
8
+ @file_param = file_param
9
+ @converter = options[:converter] || DocumentConverter
10
+ end
11
+
12
+ def convert_to_pdf
13
+ create_tempfile
14
+ @converter.convert(filename)
15
+ end
16
+
17
+ private
18
+
19
+ def create_tempfile
20
+ File.open(filename, 'w') do |target_file|
21
+ target_file.write(file.read)
22
+ end
23
+ end
24
+
25
+ def filename
26
+ temp_path + @file_param[:filename]
27
+ end
28
+
29
+ def temp_path
30
+ "tmp/"
31
+ end
32
+
33
+ def file
34
+ @file_param[:tempfile]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,26 @@
1
+ module Libreservice
2
+ class FailedConversionError < StandardError; end
3
+
4
+ class DocumentConverter
5
+ def initialize(filename, converter = Libreconv)
6
+ @filename = filename
7
+ @converter = converter
8
+ end
9
+
10
+ def self.convert(filename)
11
+ new(filename).convert
12
+ end
13
+
14
+ def convert
15
+ @converter.convert(filename, "#{filename}.pdf")
16
+
17
+ raise FailedConversionError unless File.exist?("#{filename}.pdf")
18
+
19
+ "#{filename}.pdf"
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :filename, :file
25
+ end
26
+ end
data/lib/server.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'libreconv'
2
+ require 'sinatra/base'
3
+ require_relative 'document'
4
+
5
+
6
+ module Libreservice
7
+ class Server < Sinatra::Base
8
+ set :bind, '0.0.0.0'
9
+
10
+ post '/convert' do
11
+ document = Document.new(params['file'])
12
+
13
+ attachment document.convert_to_pdf
14
+
15
+ "Converted file enclosed"
16
+ end
17
+ end
18
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Libreservice
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "libreservice"
8
+ spec.version = Libreservice::VERSION
9
+ spec.executables << "libreservice"
10
+ spec.authors = ["Andre Bernardes"]
11
+ spec.email = ["abernardes@gmail.com"]
12
+ spec.description = %q{Adds a service layer on top of the Libreconv gem}
13
+ spec.summary = %q{Adds a service layer on top of the Libreconv gem for document to PDF conversion}
14
+ spec.homepage = "http://github.com/abernardes/libreservice"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "sinatra"
23
+ spec.add_dependency "libreservice"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ end
File without changes
Binary file
@@ -0,0 +1,41 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'rspec'
4
+ require 'rack/test'
5
+ require './lib/server'
6
+
7
+ describe "Requesting a document conversion" do
8
+ include Rack::Test::Methods
9
+
10
+ def app
11
+ Libreservice::Server
12
+ end
13
+
14
+ let(:filename) { "spec/fixtures/document.docx" }
15
+ let(:mime_type) { "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }
16
+
17
+ before do
18
+ Libreconv.stub(:convert)
19
+ end
20
+
21
+ after do
22
+ FileUtils.rm_r Dir.glob('tmp/*')
23
+ end
24
+
25
+ context "POST /convert" do
26
+ let(:file) { Rack::Test::UploadedFile.new(filename, mime_type) }
27
+
28
+ it "converts a given file to the PDF format." do
29
+ FileUtils.copy "spec/fixtures/document.docx.pdf", "tmp/"
30
+ post "/convert", :file => file
31
+
32
+ expect(last_response.header["Content-Disposition"]).to eq 'attachment; filename="document.docx.pdf"'
33
+ end
34
+
35
+ it "raises an error if conversion failed" do
36
+ expect {
37
+ post "/convert", :file => file
38
+ }.to raise_error(Libreservice::FailedConversionError)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,35 @@
1
+ require './lib/document_converter'
2
+
3
+ describe Libreservice::DocumentConverter do
4
+ describe "#convert" do
5
+ let(:converter) { double(:converter, convert: nil) }
6
+
7
+ before do
8
+ FileUtils.copy "spec/fixtures/document.docx.pdf", "tmp/"
9
+ end
10
+
11
+ subject { Libreservice::DocumentConverter.new("tmp/document.docx", converter) }
12
+
13
+ after do
14
+ FileUtils.rm_r Dir.glob('tmp/*')
15
+ end
16
+
17
+ it "converts a given file to PDF using Libreconv" do
18
+ converter.should_receive(:convert).with("tmp/document.docx", "tmp/document.docx.pdf")
19
+
20
+ subject.convert
21
+ end
22
+
23
+ it "raises an error if conversion fails" do
24
+ FileUtils.rm "tmp/document.docx.pdf"
25
+
26
+ expect {
27
+ subject.convert
28
+ }.to raise_error(Libreservice::FailedConversionError)
29
+ end
30
+
31
+ it "returns the converted filename" do
32
+ expect(subject.convert).to eq "tmp/document.docx.pdf"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ require './lib/document'
2
+
3
+ describe Libreservice::Document do
4
+ let(:filename) { "document.docx" }
5
+ let(:file) { File.open("spec/fixtures/document.docx") }
6
+ let(:converter) { double(:converter, convert: "tmp/document.docx.pdf") }
7
+
8
+ subject { Libreservice::Document.new({ filename: filename, tempfile: file }, converter: converter) }
9
+
10
+ after do
11
+ file.close
12
+ end
13
+
14
+ it "converts documents" do
15
+ converter.should_receive(:convert).with("tmp/document.docx")
16
+
17
+ subject.convert_to_pdf
18
+ end
19
+
20
+ it "creates a tempfile with the file contents" do
21
+ subject.convert_to_pdf
22
+
23
+ expect(File.exists?("tmp/document.docx")).to be_true
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libreservice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andre Bernardes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
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: libreservice
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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
+ description: Adds a service layer on top of the Libreconv gem
70
+ email:
71
+ - abernardes@gmail.com
72
+ executables:
73
+ - libreservice
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .vagrant
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - Vagrantfile
84
+ - bin/libreservice
85
+ - config.ru
86
+ - lib/document.rb
87
+ - lib/document_converter.rb
88
+ - lib/server.rb
89
+ - lib/version.rb
90
+ - libreservice.gemspec
91
+ - spec/fixtures/document.docx
92
+ - spec/fixtures/document.docx.pdf
93
+ - spec/requests/document_conversion_spec.rb
94
+ - spec/units/document_converter_spec.rb
95
+ - spec/units/document_spec.rb
96
+ homepage: http://github.com/abernardes/libreservice
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.0.7
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Adds a service layer on top of the Libreconv gem for document to PDF conversion
120
+ test_files:
121
+ - spec/fixtures/document.docx
122
+ - spec/fixtures/document.docx.pdf
123
+ - spec/requests/document_conversion_spec.rb
124
+ - spec/units/document_converter_spec.rb
125
+ - spec/units/document_spec.rb