paperize 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 159c719916881f2d6a2d3435b2bb5569f4a3aff3
4
+ data.tar.gz: 617392ecb5f294cbfc35085f629c36ce52f3ad5a
5
+ SHA512:
6
+ metadata.gz: ba16aa9f9c9c65df9daad7a06b438ba5377eaf911ec9d706604fe834db630842a6f95ba536c88f20aef8114c50e64ee81b2502ce1be9d18ea81e2f536de4c8b4
7
+ data.tar.gz: 9a5937d8e3790bfa6994f724277e1e115c8c0da16578a5c6f5f4965455475c9d779b9c79bf459459a94e124bfa9ae2e6a36e4e4d5440d376caf33e42c42b530f
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .env
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperize.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Loren Norman
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.
@@ -0,0 +1,29 @@
1
+ # Paperize
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'paperize'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install paperize
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/paperize/fork )
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
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ require "dotenv"
2
+ Dotenv.load
3
+
4
+ require "prawn"
5
+ require "prawn/measurement_extensions"
6
+
7
+ require "paperize/version"
8
+ require "paperize/spreadsheet"
9
+ require "paperize/template"
10
+ require "paperize/layout"
11
+
12
+ module Paperize
13
+ def self.render!(filename, card_items, layout)
14
+ layout.layout_cards(card_items)
15
+ layout.document.render_file(filename)
16
+
17
+ puts "Done! Rendered to #{filename}"
18
+ end
19
+ end
@@ -0,0 +1,45 @@
1
+ require 'google_drive'
2
+
3
+ module Paperize
4
+ module Adapter
5
+ class GoogleDrive
6
+ def self.open_spreadsheet(key)
7
+ client = OAuth2::Client.new(
8
+ ENV["GOOGLE_CLIENT_ID"],
9
+ ENV["GOOGLE_CLIENT_SECRET"],
10
+ :site => "https://accounts.google.com",
11
+ :token_url => "/o/oauth2/token",
12
+ :authorize_url => "/o/oauth2/auth")
13
+
14
+ begin
15
+ # try using a stored token
16
+ refresh_token = ENV["PAPERIZE_GOOGLE_DRIVE_REFRESH_TOKEN"]
17
+ auth_token = OAuth2::AccessToken.from_hash(client,
18
+ {:refresh_token => refresh_token})#, :expires_at => expires_at})
19
+ auth_token = auth_token.refresh!
20
+ rescue Exception => e
21
+ puts "Exception attempting to use refresh token:"
22
+ puts e
23
+
24
+ auth_url = client.auth_code.authorize_url(
25
+ :redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
26
+ :scope => "https://spreadsheets.google.com/feeds/")
27
+
28
+ puts "Opening authorization URL:\n#{auth_url}"
29
+ `open "#{auth_url}" -a 'Google Chrome'`
30
+ puts "Enter Authorization Code:"
31
+ authorization_code = gets.chomp
32
+
33
+ # Redirect the user to auth_url and get authorization code from redirect URL.
34
+ auth_token = client.auth_code.get_token(authorization_code, :redirect_uri => "urn:ietf:wg:oauth:2.0:oob")
35
+ puts "Refresh Token:"
36
+ puts auth_token.refresh_token
37
+ end
38
+
39
+ session = ::GoogleDrive.login_with_oauth(auth_token.token)
40
+
41
+ session.spreadsheet_by_key(key)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,79 @@
1
+ module Paperize
2
+ class Layout
3
+ attr_reader :total_columns, :total_rows, :layout
4
+ attr_accessor :document
5
+
6
+ def initialize(options={})
7
+ @layout = options[:layout]
8
+ @orientation = options[:orientation] || :portrait
9
+
10
+ if layout == :card_per_page
11
+
12
+ else
13
+ @total_columns, @total_rows = layout.split('x').map(&:to_i) || [3, 3]
14
+ @current_column = total_columns
15
+ @current_row = total_rows
16
+ end
17
+ @template = options[:template]
18
+
19
+ page_size = layout == :card_per_page ? [2.5.in, 3.5.in] : 'LETTER'
20
+ @document = Prawn::Document.new(
21
+ skip_page_creation: true,
22
+ page_size: page_size,
23
+ page_layout: @orientation
24
+ )
25
+ end
26
+
27
+ def shrink_bounds(margin, &block)
28
+ margin_left = margin
29
+ margin_top = document.bounds.top - margin
30
+ margin_width = document.bounds.width - 2*margin
31
+ margin_height = document.bounds.height - 2*margin
32
+
33
+ document.bounding_box [margin_left, margin_top], width: margin_width, height: margin_height, &block
34
+ end
35
+
36
+ def layout_cards(cards)
37
+ card_margin = 15
38
+ cards.each do |card|
39
+ next_cell do
40
+ shrink_bounds(2) do
41
+ document.line_width = 2
42
+ document.stroke_bounds
43
+ end
44
+
45
+ shrink_bounds(card_margin) do
46
+ @template.render_block.call(document, card)
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def next_cell &block
53
+ if layout == :card_per_page
54
+ document.start_new_page
55
+ document.canvas(&block)
56
+ else
57
+ increment_current_cell
58
+
59
+ document.grid(@current_row, @current_column).bounding_box(&block)
60
+ end
61
+ end
62
+
63
+ def increment_current_cell
64
+ @current_column += 1
65
+
66
+ if @current_column >= total_columns
67
+ @current_column = 0
68
+ @current_row += 1
69
+
70
+ if @current_row >= total_rows
71
+ @current_row = 0
72
+
73
+ document.start_new_page
74
+ document.define_grid(columns: total_columns, rows: total_rows)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,37 @@
1
+ require "paperize/adapter/google_drive"
2
+
3
+ module Paperize
4
+ class Spreadsheet
5
+ attr_reader :spreadsheet
6
+ def initialize(spreadsheet)
7
+ @spreadsheet = spreadsheet
8
+ end
9
+
10
+ def parse_worksheet(worksheet_name, options={})
11
+ worksheet = spreadsheet.worksheets.find do |worksheet|
12
+ worksheet.title == worksheet_name
13
+ end
14
+
15
+ labels = worksheet.rows[0]
16
+ attributes = labels.map { |label| label.downcase.gsub(' ', '_').to_sym }
17
+
18
+ row_struct = Struct.new(*attributes)
19
+
20
+ worksheet.rows[1..worksheet.num_rows].map do |row|
21
+ row_struct.new(*row)
22
+ end
23
+ end
24
+
25
+ class << self
26
+ def import(options={})
27
+ unless options[:google_drive] and options[:google_drive][:key]
28
+ raise 'Only Google Drive is supported at this time. Please provide { google_drive: { key: YOUR_KEY_HERE } } as parameters to Paperize::Spreadsheet.import'
29
+ end
30
+
31
+ if options[:google_drive]
32
+ Spreadsheet.new Paperize::Adapter::GoogleDrive.open_spreadsheet(options[:google_drive][:key])
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ module Paperize
2
+ class Template
3
+ attr_reader :render_block
4
+ def initialize(options={}, &block)
5
+ @render_block = block
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Paperize
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 'paperize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "paperize"
8
+ spec.version = Paperize::VERSION
9
+ spec.authors = ["Loren Norman"]
10
+ spec.email = ["lorennorman@gmail.com"]
11
+ spec.summary = %q{From spreadsheets, cards!}
12
+ spec.description = %q{Allows a user to specify a list of gaming cards in a spreadsheet, then generate a PDF suitable for print-and-play games and prototyping.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "dotenv", "~> 0.9"
22
+ spec.add_dependency "google_drive", "~> 0.3"
23
+ spec.add_dependency "prawn", "~> 0.13"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.5"
26
+ spec.add_development_dependency "rake", "~> 10.1"
27
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Loren Norman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google_drive
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: prawn
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.13'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.1'
83
+ description: Allows a user to specify a list of gaming cards in a spreadsheet, then
84
+ generate a PDF suitable for print-and-play games and prototyping.
85
+ email:
86
+ - lorennorman@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/paperize.rb
97
+ - lib/paperize/adapter/google_drive.rb
98
+ - lib/paperize/layout.rb
99
+ - lib/paperize/spreadsheet.rb
100
+ - lib/paperize/template.rb
101
+ - lib/paperize/version.rb
102
+ - paperize.gemspec
103
+ homepage: ''
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: From spreadsheets, cards!
127
+ test_files: []