Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first working version

  • Loading branch information...
commit 9002a95b73bbe3bb076e9fa4b89a5885e6093d68 1 parent c4e7397
@sbfaulkner authored
View
3  .gitignore
@@ -1,3 +1,4 @@
*.sw?
.DS_Store
-coverage
+coverage
+rdoc/*
View
2  LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2008 S. Brent Faulkner
+Copyright (c) 2009 unwwwired.net
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
7 README
@@ -1,7 +0,0 @@
-sinatra-prawn=============
-Description goes here.
-
-COPYRIGHT
-=========
-
-Copyright (c) 2008 S. Brent Faulkner. See LICENSE for details.
View
27 README.rdoc
@@ -0,0 +1,27 @@
+= sinatra-prawn
+
+sinatra-prawn is an extension for sinatra to enable rendering of pdf files
+using prawn templates.
+
+== Example
+
+ require 'rubygems'
+ require 'sinatra'
+ require 'sinatra/prawn'
+
+ set :prawn, { :page_layout => :landscape }
+
+ get '/' do
+ content_type 'application/pdf'
+ prawn :pdf
+ end
+
+ __END__
+
+ @@ pdf
+ pdf.text "Hello world!!!!!"
+
+== Legal
+
+Author:: S. Brent Faulkner <brentf@unwwwired.net>
+License:: Copyright (c) 2009 unwwwired.net, released under the MIT license
View
10 Rakefile
@@ -7,8 +7,9 @@ begin
s.summary = %Q{Sinatra extension to add support for pdf rendering with Prawn templates.}
s.email = "brentf@unwwwired.net"
s.homepage = "http://github.com/sbfaulkner/sinatra-prawn"
- s.description = "TODO"
+ s.description = "Sinatra extension to add support for pdf rendering with Prawn templates."
s.authors = ["S. Brent Faulkner"]
+ s.add_dependency "prawn"
end
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
@@ -41,11 +42,4 @@ rescue LoadError
puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
end
-begin
- require 'cucumber/rake/task'
- Cucumber::Rake::Task.new(:features)
-rescue LoadError
- puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
-end
-
task :default => :test
View
4 VERSION.yml
@@ -0,0 +1,4 @@
+---
+:major: 0
+:minor: 9
+:patch: 1
View
9 features/sinatra_prawn.feature
@@ -1,9 +0,0 @@
-Feature: something something
- In order to something something
- A user something something
- something something something
-
- Scenario: something something
- Given inspiration
- When I create a sweet new gem
- Then everyone should see how awesome I am
View
0  features/steps/sinatra_prawn_steps.rb
No changes.
View
13 features/support/env.rb
@@ -1,13 +0,0 @@
-$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
-require 'sinatra_prawn'
-
-require 'test/unit/assertions'
-
-require 'test/unit/assertions'
-
-World do |world|
-
- world.extend(Test::Unit::Assertions)
-
- world
-end
View
31 lib/sinatra/prawn.rb
@@ -0,0 +1,31 @@
+require 'sinatra/base'
+
+module Sinatra
+ module Prawn
+ # Generate pdf file using Prawn.
+ # Takes the name of a template to render as a Symbol and returns a String with the rendered output.
+ #
+ # Options for prawn may be specified in Sinatra using set :prawn, { ... }
+ def prawn(template=nil, options={}, &block)
+ require 'prawn' unless defined? ::Prawn
+ options, template = template, nil if template.is_a?(Hash)
+ template = lambda { block } if template.nil?
+ options[:layout] = false
+ options[:options] ||= self.class.prawn if self.class.respond_to? :prawn
+ render :prawn, template, options
+ end
+
+ protected
+ def render_prawn(template, data, options, &block)
+ pdf = ::Prawn::Document.new(options[:options] || {})
+ if data.respond_to?(:to_str)
+ eval data.to_str, binding, '<PRAWN>', 1
+ elsif data.kind_of?(Proc)
+ data.call(pdf)
+ end
+ pdf.render
+ end
+ end
+
+ helpers Prawn
+end
View
0  lib/sinatra_prawn.rb
No changes.
View
32 sinatra-prawn.gemspec
@@ -0,0 +1,32 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{sinatra-prawn}
+ s.version = "0.9.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["S. Brent Faulkner"]
+ s.date = %q{2009-03-11}
+ s.description = %q{Sinatra extension to add support for pdf rendering with Prawn templates.}
+ s.email = %q{brentf@unwwwired.net}
+ s.files = ["README.rdoc", "VERSION.yml", "lib/sinatra", "lib/sinatra/prawn.rb", "test/sinatra_prawn_test.rb", "test/test_helper.rb", "test/views", "test/views/hello.prawn"]
+ s.has_rdoc = true
+ s.homepage = %q{http://github.com/sbfaulkner/sinatra-prawn}
+ s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.1}
+ s.summary = %q{Sinatra extension to add support for pdf rendering with Prawn templates.}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<prawn>, [">= 0"])
+ else
+ s.add_dependency(%q<prawn>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<prawn>, [">= 0"])
+ end
+end
View
48 test/sinatra_prawn_test.rb
@@ -1,7 +1,51 @@
require File.dirname(__FILE__) + '/test_helper'
class SinatraPrawnTest < Test::Unit::TestCase
- def test_something_for_real
- flunk "hey buddy, you should probably rename this file and start testing for real"
+ include Sinatra::Test
+
+ def prawn_app(&block)
+ mock_app {
+ helpers Sinatra::Prawn
+ set :views, File.dirname(__FILE__) + '/views'
+ get '/', &block
+ }
+ get '/'
+ end
+
+ def test_renders_inline_strings
+ prawn_app { prawn 'pdf.text "Hello shrimp!"' }
+ assert ok?
+ text = PDF::TextInspector.analyze(body)
+ assert_equal "Hello shrimp!", text.strings.first
+ end
+
+ def test_renders_inline_blocks
+ prawn_app {
+ @name = "Frank & Mary"
+ prawn do |pdf|
+ pdf.text "Hello #{@name}!"
+ end
+ }
+ assert ok?
+ text = PDF::TextInspector.analyze(body)
+ assert_equal "Hello Frank & Mary!", text.strings.first
+ end
+
+ def test_renders_prawn_files_in_views_path
+ prawn_app {
+ @name = "World"
+ prawn :hello
+ }
+ assert ok?
+ text = PDF::TextInspector.analyze(body)
+ assert_equal "Hello, World!", text.strings.first
+ end
+
+ def test_raises_error_if_template_not_found
+ mock_app {
+ helpers Sinatra::Prawn
+ get('/') { prawn :no_such_template }
+ }
+ assert_raise(Errno::ENOENT) { get('/') }
end
end
View
58 test/test_helper.rb
@@ -1,9 +1,63 @@
require 'rubygems'
require 'test/unit'
-require 'mocha'
+require 'sinatra/test'
$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'sinatra_prawn'
+require 'sinatra/prawn'
+
+require "pdf/reader"
+
+module PDF
+ class TextInspector
+ attr_accessor :font_settings, :size, :strings
+
+ def initialize
+ @font_settings = []
+ @fonts = {}
+ @strings = []
+ end
+
+ def resource_font(*params)
+ @fonts[params[0]] = params[1].basefont
+ end
+
+ def set_text_font_and_size(*params)
+ @font_settings << { :name => @fonts[params[0]], :size => params[1] }
+ end
+
+ def show_text(*params)
+ @strings << params[0]
+ end
+
+ def show_text_with_positioning(*params)
+ # ignore kerning information
+ @strings << params[0].reject { |e| Numeric === e }.join
+ end
+
+ def self.analyze(output,*args,&block)
+ obs = self.new(*args, &block)
+ PDF::Reader.string(output,obs)
+ obs
+ end
+
+ def self.analyze_file(filename,*args,&block)
+ analyze(File.open(filename, "rb") { |f| f.read },*args,&block)
+ end
+
+ def self.parse(obj)
+ PDF::Reader::Parser.new(
+ PDF::Reader::Buffer.new(StringIO.new(obj)), nil).parse_token
+ end
+ end
+end
class Test::Unit::TestCase
+ include Sinatra::Test
+
+ # Sets up a Sinatra::Base subclass defined with the block
+ # given. Used in setup or individual spec methods to establish
+ # the application.
+ def mock_app(base=Sinatra::Base, &block)
+ @app = Sinatra.new(base, &block)
+ end
end
View
1  test/views/hello.prawn
@@ -0,0 +1 @@
+pdf.text "Hello, #{@name}!"
Please sign in to comment.
Something went wrong with that request. Please try again.