lingohub_shield 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: 606c2b83d7cd7310b1de2ea8171dc144cc0cbc62
4
+ data.tar.gz: f1d81c7074e780ffc3da0f25c7fc2f462d677ac6
5
+ SHA512:
6
+ metadata.gz: 69e45726e9bf906b5719404ca42190a2feab45dcc01d7458fcd305ffbc144f780b2e37c06cf438daf9c145e67d5bf66cfae880bcd247dd4f678e06719bdda173
7
+ data.tar.gz: 13cd8256be1d8b051ad17a34a69f9cf2f77b4f675e4cbc102eae07ca3d31858711719e0e1a4e6d0937659bf83546f1c0cecaa18ed06a30ec9240b45b4da19d5d
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ /.idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lingohub_shield.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lingohub_shield (0.0.1)
5
+ rmagick (~> 2.13.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.4)
11
+ rake (10.1.0)
12
+ rmagick (2.13.2)
13
+ rspec (2.14.1)
14
+ rspec-core (~> 2.14.0)
15
+ rspec-expectations (~> 2.14.0)
16
+ rspec-mocks (~> 2.14.0)
17
+ rspec-core (2.14.5)
18
+ rspec-expectations (2.14.3)
19
+ diff-lcs (>= 1.1.3, < 2.0)
20
+ rspec-mocks (2.14.3)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.3)
27
+ lingohub_shield!
28
+ rake
29
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 lingohub
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 lingohub GmbH
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,4 @@
1
+ lingohub-shield
2
+ ===============
3
+
4
+ Dynamic generator of Lingohub shields
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ require 'lingohub_shield/systems/github_shield'
2
+
3
+ class ShieldSystem
4
+
5
+ def initialize(output_path, name, locales)
6
+ @output_path = output_path
7
+ @name = name
8
+ @locales = locales
9
+ end
10
+
11
+ def github_shield
12
+ Systems::GithubShield.new(@output_path, @name, @locales).generate
13
+ end
14
+
15
+ end
@@ -0,0 +1,78 @@
1
+ require 'RMagick'
2
+
3
+ module Systems
4
+ class Entry
5
+ include Comparable
6
+
7
+ LH = '#575757'
8
+ GREEN = '#45cb14'
9
+ YELLOW = '#c0cc14'
10
+ RED = '#cc2114'
11
+ GREEN_THRESHOLD = 95
12
+ YELLOW_THRESHOLD = 90
13
+
14
+ attr_reader :label, :percentage, :font_size, :font_family, :font_style, :font_gravity, :font_weight, :stroke, :stroke_width, :color, :width
15
+
16
+ # Initializes the shield entry
17
+ # @param [Symbol] label, the text that should be written within the entries container in the shield
18
+ # @param [Integer] percentage, value that determines the container background color
19
+ # @param [Integer] padding, space from the container's left and right edges to the label
20
+ def initialize(label, percentage, padding = 18)
21
+ @label = label
22
+ @percentage = percentage
23
+ @padding = padding
24
+
25
+ @font_size = 15
26
+ @font_family = 'helvetica'
27
+ @font_style = Magick::NormalStyle
28
+ @font_gravity = Magick::WestGravity
29
+ @stroke = 'transparent'
30
+ @stroke_width = 1
31
+ @color = 'white'
32
+ @width = get_text_width
33
+
34
+ if percentage
35
+ @font_weight = Magick::NormalWeight
36
+ else
37
+ @font_weight = Magick::BoldWeight
38
+ end
39
+ end
40
+
41
+ # returns the appropriate background color depending on the percentage
42
+ def background_color
43
+ if percentage.nil?
44
+ LH
45
+ elsif percentage > GREEN_THRESHOLD
46
+ GREEN
47
+ elsif percentage > YELLOW_THRESHOLD
48
+ YELLOW
49
+ else
50
+ RED
51
+ end
52
+ end
53
+
54
+ # enables sorting of entries alphabetically by label
55
+ def <=>(another)
56
+ label <=> another.label
57
+ end
58
+
59
+
60
+ private
61
+
62
+ def get_text_width
63
+ canvas = Magick::Image.new(100, 100) { self.background_color = 'transparent' }
64
+ canvas.alpha(Magick::ActivateAlphaChannel)
65
+ label = Magick::Draw.new
66
+ label.font_family(@font_family)
67
+ label.pointsize = @font_size
68
+ label.text_antialias(true)
69
+ label.font_style(@font_style)
70
+ label.font_weight(@font_weight)
71
+ label.gravity(@font_gravity)
72
+ label.text(0, 0, @label)
73
+ metrics = label.get_type_metrics(canvas, @label)
74
+ metrics.width.to_i + @padding
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,98 @@
1
+ require 'lingohub_shield/systems/entry'
2
+
3
+ module Systems
4
+ class GithubShield
5
+
6
+ # Initializes the github shield
7
+ # @param [String] output_path
8
+ # @param [String] name
9
+ # @param [Array<Hash>] locales
10
+ def initialize(output_path, name, locales)
11
+ @name = name
12
+ @output_path = output_path
13
+
14
+ # number of pixels from both ends of the label to the edge of the container
15
+ @padding = 18
16
+
17
+ # width of the left and right strip that contains the rounded edges
18
+ @corner_width = 2
19
+
20
+ lh_entry = Entry.new('LH', nil, @padding)
21
+ @entries = [lh_entry]
22
+
23
+
24
+ @shield_height = 25
25
+ @shield_width = lh_entry.width
26
+
27
+ @label_keys = [:lh]
28
+
29
+ locales.each do |locale|
30
+ entry = Entry.new(locale[:iso_code], locale[:percentage], @padding)
31
+ @entries << entry
32
+ @shield_width += entry.width
33
+ end
34
+
35
+ @label_keys[1..-1].sort!
36
+ @shield_width += @corner_width*2
37
+
38
+ @canvas = Magick::Image.new(@shield_width, @shield_height) { self.background_color = 'transparent' }
39
+ @canvas.alpha(Magick::ActivateAlphaChannel)
40
+ @draw = Magick::Draw.new
41
+ end
42
+
43
+ # Generates the shield with previously initialized parameters
44
+ def generate
45
+ draw_right_corners(@entries.last) unless @entries.empty?
46
+ draw_left_corners(@entries.first)
47
+
48
+ offset = @corner_width
49
+
50
+ @entries.each do |entry|
51
+ draw_rectangle entry, offset
52
+ draw_label entry, offset
53
+
54
+ offset += entry.width
55
+ end
56
+
57
+ publish
58
+ end
59
+
60
+ private
61
+
62
+ def draw_right_corners(entry)
63
+ @draw.fill(entry.background_color)
64
+ @draw.roundrectangle(@shield_width/2+1, 0, @corner_width, @shield_height-1, @corner_width, @corner_width)
65
+ end
66
+
67
+ # only purpose is to draw corners
68
+ def draw_left_corners(lh_entry)
69
+ @draw.fill(lh_entry.background_color)
70
+ @draw.roundrectangle(0, 0, @shield_width-@corner_width, @shield_height-1, @corner_width, @corner_width)
71
+ end
72
+
73
+ def draw_rectangle(entry, offset)
74
+ @draw.fill(entry.background_color)
75
+ @draw.stroke(entry.stroke)
76
+ @draw.stroke_width(entry.stroke_width)
77
+ @draw.rectangle(offset, 0, entry.width + offset, @shield_height)
78
+ end
79
+
80
+ def draw_label(entry, offset)
81
+ @draw.fill(entry.color)
82
+ @draw.font_family(entry.font_family)
83
+ @draw.pointsize = entry.font_size
84
+ @draw.text_antialias(true)
85
+ @draw.font_style(entry.font_style)
86
+ @draw.font_weight(entry.font_weight)
87
+ @draw.gravity(entry.font_gravity)
88
+ @draw.stroke = entry.stroke
89
+ @draw.text(offset + @padding/2, 2, entry.label)
90
+ end
91
+
92
+ def publish
93
+ @draw.draw(@canvas)
94
+ @canvas.write(@output_path + '/' + @name + '.png')
95
+ end
96
+
97
+ end
98
+ end
@@ -0,0 +1,3 @@
1
+ module LingohubShield
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'lingohub_shield/version'
3
+ require 'lingohub_shield/systems/github_shield'
4
+ require 'lingohub_shield/shield_system'
5
+
6
+ module LingohubShield
7
+
8
+ def self.create(output_path, name, locales, system)
9
+ shield_system = ShieldSystem.new output_path, name, locales
10
+
11
+ if system == 'github'
12
+ shield_system.github_shield
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lingohub_shield/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'lingohub_shield'
8
+ spec.version = LingohubShield::VERSION
9
+ spec.authors = ['Helmut Juskewycz', 'Marko Bošković']
10
+ spec.email = ['helmut@lingohub.com', 'marko@lingohub.com']
11
+ spec.description = %q{Generate a PNG shield for locales for the Lingohub service.}
12
+ spec.summary = %q{Shield generator for Lingohub.}
13
+ spec.homepage = 'http://lingohub.com'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
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_development_dependency 'rspec'
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+
25
+ spec.add_dependency 'rmagick', '~> 2.13.1'
26
+ end
Binary file
Binary file
Binary file
@@ -0,0 +1,37 @@
1
+ require 'rspec'
2
+ require 'lingohub_shield/shield_system'
3
+ require 'spec_helper'
4
+
5
+ describe ShieldSystem do
6
+
7
+ before :each do
8
+ @output_path = File.dirname(__FILE__) + '/images'
9
+ @locales = [{ :iso_code => 'en', :percentage => 96 }, { :iso_code => 'de-AT', :percentage => 91 }, { :iso_code => 'es', :percentage => 79 }]
10
+ @locales2 = [{ :iso_code => 'en', :percentage => 96 }]
11
+ end
12
+
13
+ it 'should generate a github style status badge' do
14
+ name = 'github_shield'
15
+ shield = ShieldSystem.new(@output_path, name, @locales)
16
+
17
+ shield.github_shield
18
+ assert_file_exists?(name)
19
+ end
20
+
21
+ it 'should generate a github style status badge when there is a single locale' do
22
+ name = 'github_shield2'
23
+ shield = ShieldSystem.new(@output_path, name, @locales2)
24
+
25
+ shield.github_shield
26
+ assert_file_exists?(name)
27
+ end
28
+
29
+ it 'should generate a github style status badge when there are no locales' do
30
+ name = 'github_shield3'
31
+ shield = ShieldSystem.new(@output_path, name, [])
32
+
33
+ shield.github_shield
34
+ assert_file_exists?(name)
35
+ end
36
+
37
+ end
@@ -0,0 +1,15 @@
1
+ class VerifyImage
2
+
3
+ def self.is_same(actual,expected)
4
+ compare = `compare -metric rmse -subimage-search #{actual} #{expected} null: 2>&1`
5
+ compare.strip == "0 (0)" ? true : compare
6
+ end
7
+
8
+ end
9
+
10
+ def assert_file_exists?(name)
11
+ actual = @output_path + "/#{name}.png"
12
+ File.exists?(actual).should == true
13
+ expected = "spec/images/expected_#{name}.png"
14
+ VerifyImage.is_same(actual, expected).should == true
15
+ end
@@ -0,0 +1,38 @@
1
+ require 'rspec'
2
+ require 'lingohub_shield/systems/entry'
3
+
4
+ describe Systems::Entry do
5
+
6
+ it 'returns colors depending on percentage as expected' do
7
+ [1, 15, 89, 90, 94, 95, 96, 100].each do |percentage|
8
+ background_color = Systems::Entry.new('en', percentage).background_color
9
+
10
+ if percentage.nil?
11
+ background_color.should eq Systems::Entry::LH
12
+ elsif percentage > Systems::Entry::GREEN_THRESHOLD
13
+ background_color.should eq Systems::Entry::GREEN
14
+ elsif percentage > Systems::Entry::YELLOW_THRESHOLD
15
+ background_color.should eq Systems::Entry::YELLOW
16
+ else
17
+ background_color.should eq Systems::Entry::RED
18
+ end
19
+ end
20
+ end
21
+
22
+
23
+ it 'sorts entries alphabetically' do
24
+ entries = []
25
+ entries << Systems::Entry.new('en', 13)
26
+ entries << Systems::Entry.new('de-AT', 55)
27
+ entries << Systems::Entry.new('rs', 22)
28
+ entries << Systems::Entry.new('en-US', 99)
29
+
30
+ entries.sort!
31
+
32
+ entries[0].label.should eq 'de-AT'
33
+ entries[1].label.should eq 'en'
34
+ entries[2].label.should eq 'en-US'
35
+ entries[3].label.should eq 'rs'
36
+ end
37
+
38
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lingohub_shield
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Helmut Juskewycz
8
+ - Marko Bošković
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '1.3'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rmagick
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 2.13.1
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.13.1
70
+ description: Generate a PNG shield for locales for the Lingohub service.
71
+ email:
72
+ - helmut@lingohub.com
73
+ - marko@lingohub.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/lingohub_shield.rb
86
+ - lib/lingohub_shield/shield_system.rb
87
+ - lib/lingohub_shield/systems/entry.rb
88
+ - lib/lingohub_shield/systems/github_shield.rb
89
+ - lib/lingohub_shield/version.rb
90
+ - lingohub_shield.gemspec
91
+ - spec/images/expected_github_shield.png
92
+ - spec/images/expected_github_shield2.png
93
+ - spec/images/expected_github_shield3.png
94
+ - spec/images/github_shield.png
95
+ - spec/images/github_shield2.png
96
+ - spec/images/github_shield3.png
97
+ - spec/shield_system_spec.rb
98
+ - spec/spec_helper.rb
99
+ - spec/systems/entry_spec.rb
100
+ homepage: http://lingohub.com
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.0.3
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Shield generator for Lingohub.
124
+ test_files:
125
+ - spec/images/expected_github_shield.png
126
+ - spec/images/expected_github_shield2.png
127
+ - spec/images/expected_github_shield3.png
128
+ - spec/images/github_shield.png
129
+ - spec/images/github_shield2.png
130
+ - spec/images/github_shield3.png
131
+ - spec/shield_system_spec.rb
132
+ - spec/spec_helper.rb
133
+ - spec/systems/entry_spec.rb