paperize 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Rakefile +6 -0
- data/lib/paperize.rb +8 -4
- data/lib/paperize/exceptions.rb +1 -0
- data/lib/paperize/spreadsheet.rb +14 -20
- data/lib/paperize/version.rb +1 -1
- data/lib/paperize/worksheet.rb +66 -0
- data/paperize.gemspec +1 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/worksheet_spec.rb +27 -0
- metadata +23 -4
- data/lib/paperize/adapter/google_drive.rb +0 -45
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37ee371bd3275e7459483b22a5b2eb24fd20ef23
|
4
|
+
data.tar.gz: c8856c44fc22ff74685b582575779a056d450783
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca5189fa54f4459202402da235480c5b56dc1a1d956d3ef1509b6b96f8d3ef5844eaae034f32966b500cc52451b552bf1d1e8fb1ae8ab9c19f874c50303f92ac
|
7
|
+
data.tar.gz: 451498847341aab98ac3fb984de6ba900ebb35c414d70c304269db76fcb63ffa7e919cf7747fb3b4991b52ad90efc26152ca850ecc10ccf8b6da0f9840fa9af1
|
data/Rakefile
CHANGED
data/lib/paperize.rb
CHANGED
@@ -6,14 +6,18 @@ require "prawn/measurement_extensions"
|
|
6
6
|
|
7
7
|
require "paperize/version"
|
8
8
|
require "paperize/spreadsheet"
|
9
|
+
require "paperize/worksheet"
|
9
10
|
require "paperize/template"
|
10
11
|
require "paperize/layout"
|
12
|
+
require "paperize/exceptions"
|
11
13
|
|
12
14
|
module Paperize
|
13
|
-
|
14
|
-
|
15
|
-
layout.document.render_file(filename)
|
15
|
+
class << self
|
16
|
+
attr_accessor :source
|
16
17
|
|
17
|
-
|
18
|
+
def render!(filename, card_items, layout)
|
19
|
+
layout.layout_cards(card_items)
|
20
|
+
layout.document.render_file(filename)
|
21
|
+
end
|
18
22
|
end
|
19
23
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
class Paperize::DuplicateColumnLabelException < Exception; end
|
data/lib/paperize/spreadsheet.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require "paperize/adapter/google_drive"
|
2
|
-
|
3
1
|
module Paperize
|
4
2
|
class Spreadsheet
|
5
3
|
attr_reader :spreadsheet
|
@@ -7,30 +5,26 @@ module Paperize
|
|
7
5
|
@spreadsheet = spreadsheet
|
8
6
|
end
|
9
7
|
|
10
|
-
def
|
11
|
-
|
12
|
-
|
8
|
+
def worksheets
|
9
|
+
spreadsheet.worksheets.map do |google_worksheet|
|
10
|
+
Worksheet.from_google_worksheet(google_worksheet)
|
13
11
|
end
|
12
|
+
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
row_struct = Struct.new(*attributes)
|
19
|
-
|
20
|
-
worksheet.rows[1..worksheet.num_rows].map do |row|
|
21
|
-
row_struct.new(*row)
|
14
|
+
def worksheet_by_title(title)
|
15
|
+
worksheets.find do |worksheet|
|
16
|
+
worksheet.title == title
|
22
17
|
end
|
23
18
|
end
|
24
19
|
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
20
|
+
def parse_worksheet(worksheet_name, options={})
|
21
|
+
self.worksheet_by_title(worksheet_name).rows
|
22
|
+
end
|
30
23
|
|
31
|
-
|
32
|
-
|
33
|
-
|
24
|
+
class << self
|
25
|
+
def find(key)
|
26
|
+
google_spreadsheet = Paperize.source.spreadsheet_by_key(key)
|
27
|
+
Paperize::Spreadsheet.new(google_spreadsheet)
|
34
28
|
end
|
35
29
|
end
|
36
30
|
end
|
data/lib/paperize/version.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module Paperize
|
4
|
+
class Worksheet
|
5
|
+
def self.find(options={})
|
6
|
+
spreadsheet = Paperize::Spreadsheet.find(options[:spreadsheet_key])
|
7
|
+
spreadsheet.worksheet_by_title(options[:worksheet_title])
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :title, :labels, :attributes, :rows
|
11
|
+
def initialize(title, labels, rows)
|
12
|
+
raise_on_duplicates(labels)
|
13
|
+
|
14
|
+
@title = title
|
15
|
+
@labels = labels
|
16
|
+
@attributes = @labels.map do |label|
|
17
|
+
unless label.nil?
|
18
|
+
label.downcase.gsub(' ', '_').gsub('/', '_').to_sym
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
@rows = rows.map do |row|
|
23
|
+
card = OpenStruct.new
|
24
|
+
|
25
|
+
@attributes.zip(row).each do |attribute_value_pair|
|
26
|
+
attribute = attribute_value_pair[0]
|
27
|
+
unless attribute.nil? or attribute.empty?
|
28
|
+
card.send("#{attribute}=".to_sym, attribute_value_pair[1])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
card
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def cards
|
37
|
+
rows
|
38
|
+
end
|
39
|
+
|
40
|
+
def as_json(option=nil)
|
41
|
+
{
|
42
|
+
title: @title,
|
43
|
+
columns: @labels
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def raise_on_duplicates(labels)
|
49
|
+
duplicates = labels.map do |label|
|
50
|
+
label if labels.index(label) != labels.rindex(label)
|
51
|
+
end.compact.uniq
|
52
|
+
|
53
|
+
unless duplicates.empty?
|
54
|
+
duplicate_sentence = "\"#{duplicates.join('", "')}\""
|
55
|
+
message = "Found duplicate columns: #{duplicate_sentence}"
|
56
|
+
raise Paperize::DuplicateColumnLabelException.new(message)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.from_google_worksheet(google_worksheet)
|
61
|
+
Worksheet.new(google_worksheet.title,
|
62
|
+
google_worksheet.rows[0],
|
63
|
+
google_worksheet.rows[1..google_worksheet.num_rows])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/paperize.gemspec
CHANGED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Paperize::Worksheet do
|
4
|
+
it "stores the labels" do
|
5
|
+
labels = ["Label 1", "label 2"]
|
6
|
+
ws = Paperize::Worksheet.new 'title', labels, []
|
7
|
+
ws.labels.must_equal labels
|
8
|
+
end
|
9
|
+
|
10
|
+
it "denies duplicate labels" do
|
11
|
+
proc {
|
12
|
+
Paperize::Worksheet.new('title', ['a', 'a'], [])
|
13
|
+
}.must_raise Paperize::DuplicateColumnLabelException
|
14
|
+
end
|
15
|
+
|
16
|
+
it "generates attributes from labels" do
|
17
|
+
labels = ["Casting Cost", "spell name", "oil/rope"]
|
18
|
+
ws = Paperize::Worksheet.new('title', labels, [])
|
19
|
+
ws.attributes.must_equal [:casting_cost, :spell_name, :oil_rope]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "detects and modifies bunk illegal attributes" do
|
23
|
+
ws = Paperize::Worksheet.new('title', ['one', nil, '', 'four'], [[1, 2, 3, 4]])
|
24
|
+
ws.cards.first.one.must_equal 1
|
25
|
+
ws.cards.first.four.must_equal 4
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paperize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Loren Norman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dotenv
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '10.1'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.3'
|
83
97
|
description: Allows a user to specify a list of gaming cards in a spreadsheet, then
|
84
98
|
generate a PDF suitable for print-and-play games and prototyping.
|
85
99
|
email:
|
@@ -94,12 +108,15 @@ files:
|
|
94
108
|
- README.md
|
95
109
|
- Rakefile
|
96
110
|
- lib/paperize.rb
|
97
|
-
- lib/paperize/
|
111
|
+
- lib/paperize/exceptions.rb
|
98
112
|
- lib/paperize/layout.rb
|
99
113
|
- lib/paperize/spreadsheet.rb
|
100
114
|
- lib/paperize/template.rb
|
101
115
|
- lib/paperize/version.rb
|
116
|
+
- lib/paperize/worksheet.rb
|
102
117
|
- paperize.gemspec
|
118
|
+
- spec/spec_helper.rb
|
119
|
+
- spec/worksheet_spec.rb
|
103
120
|
homepage: ''
|
104
121
|
licenses:
|
105
122
|
- MIT
|
@@ -124,4 +141,6 @@ rubygems_version: 2.2.2
|
|
124
141
|
signing_key:
|
125
142
|
specification_version: 4
|
126
143
|
summary: From spreadsheets, cards!
|
127
|
-
test_files:
|
144
|
+
test_files:
|
145
|
+
- spec/spec_helper.rb
|
146
|
+
- spec/worksheet_spec.rb
|
@@ -1,45 +0,0 @@
|
|
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
|