sigen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Matt Aimonetti
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = sigen
2
+
3
+ Sigen or Signature Generator is a collection of tools designed to allow easy creation of gamer card signatures.
4
+
5
+ Ruby 1.9 compatible and the backward compatibility is not guaranteed (not tested against 1.8).
6
+
7
+ == Note on Patches/Pull Requests
8
+
9
+ * Fork the project.
10
+ * Make your feature addition or bug fix.
11
+ * Add tests for it. This is important so I don't break it in a
12
+ future version unintentionally.
13
+ * Commit, do not mess with rakefile, version, or history.
14
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
15
+ * Send me a pull request. Bonus points for topic branches.
16
+
17
+ == Copyright
18
+
19
+ Copyright (c) 2010 Sony Computer Entertainment America. See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sigen"
8
+ gem.summary = %Q{Signature generator}
9
+ gem.description = %Q{This gem helps you build signatures like player cards etv..}
10
+ gem.email = "mattaimonetti@gmail.com"
11
+ gem.homepage = "http://github.com/mattetti/sigen"
12
+ gem.authors = ["Matt Aimonetti"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "sigen #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
Binary file
@@ -0,0 +1,125 @@
1
+ require 'rubygems'
2
+ require 'RMagick'
3
+ require_relative 'templater'
4
+
5
+
6
+ # Gamercard.new.generate({:name => 'basic', :player => 'mattetti')
7
+ class Gamercard
8
+ include Magick
9
+
10
+ attr_accessor :templater
11
+
12
+ def initialize(template_data)
13
+ unless template_data.respond_to?(:has_key?) && template_data.has_key?('name')
14
+ raise Template::OptionError, 'make sure your template info has a name key and that the key is a string'
15
+ end
16
+ @templater = Templater.new(template_data)
17
+
18
+ # @columns = 600.0
19
+ # @rows = 100.0
20
+ # @font_color = 'white'
21
+ # @font = File.expand_path(File.join(File.dirname(__FILE__), 'fonts', 'Vera.ttf'))
22
+ # @margin = 10.0
23
+ # @avatar_width = (@rows - @margin)
24
+ # @avatar_x = (@columns - (@avatar_width + (@margin/2)))
25
+ # @avatar_y = ((@rows - @avatar_width)/2)
26
+
27
+ end
28
+
29
+ # Returns the template object via the templater
30
+ def template
31
+ templater.template
32
+ end
33
+
34
+ def generate(username, stats={})
35
+ template.generate(username, stats)
36
+ end
37
+
38
+ def save(path=nil)
39
+ template.save
40
+ end
41
+
42
+ def to_io
43
+ template.to_io
44
+ end
45
+
46
+ # def generate(username)
47
+ # @username = username
48
+ # @base = render_gradiated_background
49
+ # insert_avatar
50
+ # label
51
+ # last_game
52
+ # stats
53
+ # insert_url
54
+ # @base.write("#{username}.png")
55
+ # end
56
+
57
+ def insert_avatar(avatar_file=nil)
58
+ if avatar_file
59
+ @avatar = Image.read('avatar.jpg')[0]
60
+ resized_avatar = @avatar.resize_to_fit(@avatar_width, @avatar_width)
61
+ @base.composite!(resized_avatar, @avatar_x, @avatar_y, OverCompositeOp)
62
+ end
63
+ end
64
+
65
+ def label
66
+ @d.fill = @font_color
67
+ @d.font = @font
68
+ @d.pointsize = 14.0
69
+ @d.stroke('transparent')
70
+ @d.font_weight = NormalWeight
71
+ @d.gravity = WestGravity
72
+ @d = @d.annotate_scaled(@base, @columns, 1.0, @margin, @margin, "MLB10 #{@username}", 1)
73
+ end
74
+
75
+ def last_game
76
+ @d.fill = @font_color
77
+ @d.font = @font
78
+ @d.pointsize = 8.0
79
+ @d.stroke('transparent')
80
+ @d.font_weight = NormalWeight
81
+ @d.gravity = WestGravity
82
+ @d = @d.annotate_scaled(@base, @columns, 1.0, @margin, (@rows/4), "Last Game: #{Time.now.to_s}", 1)
83
+ end
84
+
85
+ def stats
86
+ @d.fill = @font_color
87
+ @d.font = @font
88
+ @d.pointsize = 8.0
89
+ @d.stroke('transparent')
90
+ @d.font_weight = NormalWeight
91
+ @d.gravity = WestGravity
92
+ @d = @d.annotate_scaled(@base, @columns, 1.0, @margin, ((@rows/4)*2), "Stats: 1 | 2 | 3 | 4 | 5 | 6 |\nmore stats on this line", 1)
93
+ end
94
+
95
+ def insert_url
96
+ @d.fill = @font_color
97
+ @d.font = @font
98
+ @d.pointsize = 8.0
99
+ @d.stroke('transparent')
100
+ @d.font_weight = NormalWeight
101
+ @d.gravity = WestGravity
102
+ @d = @d.annotate_scaled(@base, @columns, 1.0, @margin, (@rows - @margin), "http://theshowcommunity.com/players/#{@username}", 1)
103
+ end
104
+
105
+ # Use with a theme definition method to draw a gradiated background.
106
+ def render_gradiated_background(top_color='#4a465a', bottom_color='black')
107
+ Image.new(@columns, @rows, GradientFill.new(0, 0, 100, 0, top_color, bottom_color))
108
+ end
109
+
110
+ end
111
+
112
+
113
+
114
+ # Magick HACK
115
+ module Magick
116
+ class Draw
117
+ # Additional method to scale annotation text since Draw.scale doesn't.
118
+ def annotate_scaled(img, width, height, x, y, text, scale)
119
+ scaled_width = (width * scale) >= 1 ? (width * scale) : 1
120
+ scaled_height = (height * scale) >= 1 ? (height * scale) : 1
121
+
122
+ self.annotate(img, scaled_width, scaled_height, x * scale, y * scale, text)
123
+ end
124
+ end
125
+ end # Magick
@@ -0,0 +1,54 @@
1
+ require_relative 'gamercard'
2
+
3
+ require 'fileutils'
4
+ class Sigen
5
+ @queue = :signatures
6
+ @default_save_dir = '/tmp/sigen/signatures'
7
+
8
+ # location when the signatures will be saved
9
+ def self.save_dir
10
+ if @save_dir
11
+ @save_dir
12
+ else
13
+ self.save_dir = @default_save_dir
14
+ end
15
+ end
16
+
17
+ def self.save_dir=(path)
18
+ @save_dir = path
19
+ FileUtils.mkdir_p(@save_dir) unless File.exist?(@save_dir)
20
+ @save_dir
21
+ end
22
+
23
+ # Creates a gamer card signature and saves it in `save_dir`
24
+ # which points to '/tmp/sigen/signatures' by default.
25
+ #
26
+ # ==== Parameters
27
+ # username<~to_s>:: Username of the player, used to fetch avatar and save the signature.
28
+ # stats<Hash>:: The stats used in the template. The template will look for the stats using hash keys.
29
+ # template<Hash>:: Template info used to generate the signature. Keys are expected to be strings and not symbols.
30
+ # Only the name key is required. Keys are expected to be strings and not symbols.
31
+ # output_path<String>:: Optional path ti save the output signature.
32
+ # If not passed, the `save_dir` value will be used.
33
+ #
34
+ # ==== Returns
35
+ # Boolean:: reports the success of the signature creation.
36
+ #
37
+ # ==== Examples
38
+ # Sigen.perform('mattetti',
39
+ # {'rank' => 123, 'team' => 'Cubs', 'last_win' => '2010-04-27'},
40
+ # {:name => 'mlb10', :style => 'Cubs'})
41
+ #
42
+ # Sigen.perform('mattetti',
43
+ # {'rank' => 123, 'team' => 'Cubs', 'last_win' => '2010-04-27'},
44
+ # {:name => 'mlb10', :style => 'Cubs'},
45
+ # '/tmp/mlb10')
46
+ #
47
+ def self.perform(username, stats, template, output_path=nil)
48
+ card = GamerCard.new(template)
49
+ card.generate(username, stats)
50
+ # path = output_path.nil? ? save_dir : output_path
51
+ # card.save(path)
52
+ end
53
+
54
+ end
@@ -0,0 +1,40 @@
1
+ # Gamercard::Template basic class, all templates should inherit from this class
2
+ class Gamercard
3
+ class Template
4
+ include Magick
5
+ class OptionError < StandardError; end
6
+
7
+ attr_reader :passed_settings
8
+ attr_reader :width, :height, :username, :stats, :generated, :base, :filename
9
+
10
+ def initialize(settings={})
11
+ @passed_settings = settings
12
+ @generated = false
13
+ process_settings(settings) if self.class.method_defined?(:process_settings)
14
+ end
15
+
16
+ def self.fonts_path
17
+ File.expand_path(File.join(File.dirname(__FILE__), 'fonts'))
18
+ end
19
+
20
+ # template helpers inherited by all templates
21
+
22
+ def save(path=nil)
23
+ file_path = path.nil? ? (filename || 'image.png') : File.join(path, (filename || 'image.png'))
24
+ base.write(file_path)
25
+ end
26
+
27
+ def to_io
28
+ if @generated
29
+ StringIO.new(base.to_blob{ self.format = 'PNG' })
30
+ else
31
+ nil
32
+ end
33
+ end
34
+
35
+ def gradient_background(top_color='#4a465a', bottom_color='black')
36
+ Image.new(width, height, GradientFill.new(0, 0, width, 0, top_color, bottom_color))
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'gamercard'
2
+ require_relative 'template'
3
+
4
+ class Gamercard
5
+ class Templater
6
+
7
+ attr_accessor :templates_path, :template
8
+
9
+ # Pass an optional template path in the option hash to use a custom path.
10
+ def initialize(template_data)
11
+ template_path = template_data.delete('template_path')
12
+ @templates_path = template_path || File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
13
+ @template = load_template(template_data)
14
+ self
15
+ end
16
+
17
+ def load_template(template_data)
18
+ name = template_data['name']
19
+ raise 'Your template settings need to include a name entry' if name.nil?
20
+ begin
21
+ require File.join(templates_path, "#{name}")
22
+ rescue LoadError
23
+ raise ::Gamercard::Template::OptionError, "the #{name}.rb template file could not be found in #{templates_path}"
24
+ else
25
+ # find and return template object
26
+ klass_name = name.capitalize
27
+ if Template.const_defined?(klass_name)
28
+ template_klass = Template.const_get(klass_name)
29
+ template_klass.new(template_data)
30
+ else
31
+ raise Template::OptionError, "The #{klass_name} template class was not found"
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,61 @@
1
+ class Basic < Gamercard::Template
2
+
3
+
4
+ # The super class already defined the following accessors:
5
+ # :width, :height, :username, :stats, :generated, :base, :filename
6
+
7
+ # method getting called when a new instance is initiated
8
+ # it's called directly from within initialize
9
+ def process_settings(passed_settings={})
10
+ # we could do something with the passed_settings but in this template
11
+ # only the default settings are used
12
+ @columns = @width = 600.0
13
+ @rows = @height = 100.0
14
+ @font_color = 'white'
15
+ @font = 'Vera.ttf'
16
+ @margin = 10.0
17
+ @avatar_width = (@rows - @margin)
18
+ @avatar_x = (@columns - (@avatar_width + (@margin/2)))
19
+ @avatar_y = ((@rows - @avatar_width)/2)
20
+ end
21
+
22
+ def settings
23
+ {
24
+ :width => @columns,
25
+ :height => @rows,
26
+ :font_color => @font_color,
27
+ :font => @font,
28
+ :margin => @margin,
29
+ :avatar_width => @avatar_width,
30
+ :avatar_x => @avatar_x,
31
+ :avatar_y => @avatar_y,
32
+ :class_name => self.class.name
33
+ }
34
+ end
35
+
36
+ def username_font
37
+ File.join(::Gamercard::Template.fonts_path, @font)
38
+ end
39
+
40
+ # Returns an image
41
+ def generate(username, stats={})
42
+ @username = username
43
+ @filename = "#{username}.png"
44
+ @stats = stats
45
+ @d = Draw.new
46
+ @base = gradient_background # uses the template helper with default values
47
+ username_label
48
+ @generated = true
49
+ end
50
+
51
+ def username_label
52
+ @d.fill = @font_color
53
+ @d.font = username_font
54
+ @d.pointsize = 14.0
55
+ @d.stroke('transparent')
56
+ @d.font_weight = NormalWeight
57
+ @d.gravity = WestGravity
58
+ @d = @d.annotate_scaled(@base, @columns, 1.0, @margin, @margin, "#{@username}", 1)
59
+ end
60
+
61
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Gamercard do
4
+ it "should raise if the passed template doesn't have a name" do
5
+ lambda{Gamercard.new({})}.should raise_error
6
+ end
7
+
8
+ it "should have a templater and a template" do
9
+ card = Gamercard.new({'name' => 'basic'})
10
+ card.template.class.name.should == 'Basic'
11
+ end
12
+
13
+ it "should have template settings" do
14
+ template = Gamercard.new({'name' => 'basic'}).template
15
+ template.settings.should_not be_nil
16
+ end
17
+
18
+ it "should be able to generate a gamer card" do
19
+ card = Gamercard.new({'name' => 'basic'})
20
+ card.generate('mattetti').should be_true
21
+ end
22
+
23
+ it "should respond to save" do
24
+ card = Gamercard.new({'name' => 'basic'})
25
+ card.generate('mattetti')
26
+ card.should respond_to(:save)
27
+ end
28
+
29
+ it "should let you save an image" do
30
+ card = Gamercard.new({'name' => 'basic'})
31
+ card.generate('mattetti')
32
+ card.save
33
+ File.exist?('./mattetti.png').should be_true
34
+ FileUtils.rm('./mattetti.png')
35
+ end
36
+
37
+ it "should give an IO version of the card" do
38
+ card = Gamercard.new({'name' => 'basic'})
39
+ card.generate('mattetti')
40
+ card.to_io.should be_an_instance_of(StringIO)
41
+ end
42
+
43
+ it "should return nil if asking for an IO of a not generated card" do
44
+ card = Gamercard.new({'name' => 'basic'})
45
+ card.to_io.should be_nil
46
+ end
47
+
48
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Sigen" do
4
+ it "should have a default save dir" do
5
+ Sigen.save_dir.should == '/tmp/sigen/signatures'
6
+ File.exist?(Sigen.save_dir).should be_true
7
+ end
8
+
9
+ it "should let you set a different save dir" do
10
+ Sigen.save_dir ='/tmp/sigen/signature_test'
11
+ Sigen.save_dir.should == '/tmp/sigen/signature_test'
12
+ File.exist?(Sigen.save_dir).should be_true
13
+ # some cleanup
14
+ FileUtils.rm_rf(Sigen.save_dir)
15
+ end
16
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'sigen'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'fileutils'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sigen
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Matt Aimonetti
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-13 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: This gem helps you build signatures like player cards etv..
36
+ email: mattaimonetti@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - VERSION
49
+ - lib/fonts/Vera.ttf
50
+ - lib/gamercard.rb
51
+ - lib/sigen.rb
52
+ - lib/template.rb
53
+ - lib/templater.rb
54
+ - lib/templates/basic.rb
55
+ - spec/gamercard_spec.rb
56
+ - spec/sigen_spec.rb
57
+ - spec/spec.opts
58
+ - spec/spec_helper.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/mattetti/sigen
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Signature generator
91
+ test_files:
92
+ - spec/gamercard_spec.rb
93
+ - spec/sigen_spec.rb
94
+ - spec/spec_helper.rb