google-webfonts-rails 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/README.md CHANGED
@@ -1,6 +1,12 @@
1
- # GoogleWebfonts
1
+ # GoogleWebfonts for Rails
2
2
 
3
- TODO: Write a gem description
3
+ A easiest way to introduce Google WebFonts to Rails application.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/google-webfonts-rails.png)](http://badge.fury.io/rb/google-webfonts-rails)
6
+
7
+ We provide an easy to use Google WebFonts Loader. See below about WebFonts Loader.
8
+
9
+ <https://developers.google.com/webfonts/docs/webfont_loader>
4
10
 
5
11
  ## Installation
6
12
 
@@ -18,7 +24,67 @@ Or install it yourself as:
18
24
 
19
25
  ## Usage
20
26
 
21
- TODO: Write usage instructions here
27
+ ### Syntax
28
+
29
+ Add following code to your layout file in the `<head>` tag
30
+
31
+ google_webfonts_init(config hash)
32
+
33
+ ### Configuration
34
+
35
+ Google WebFonts:
36
+
37
+ {
38
+ google: ['Nunito', 'Oxygen']
39
+ }
40
+
41
+ Adobe Typekit:
42
+
43
+ {
44
+ typekit: 'adobe'
45
+ }
46
+
47
+ Ascender:
48
+
49
+ {
50
+ ascender: {
51
+ testKey: ['AscenderSan:bold', 'Calibri']
52
+ }
53
+ }
54
+
55
+ fonts.com:
56
+
57
+ {
58
+ monotype: 'test'
59
+ }
60
+
61
+ Fontdeck:
62
+
63
+ {
64
+ fontdeck: "xxxxx"
65
+ }
66
+
67
+ Custom:
68
+
69
+ {
70
+ custom: {
71
+ 'OneFontFamily', 'http://example.com/stylesheet1.css',
72
+ 'AnotherFontFamily', 'http://example.com/stylesheet2.css'
73
+ }
74
+ }
75
+
76
+ The above configuration can be included at the same time.
77
+
78
+ Example:
79
+
80
+ {
81
+ google: ['Nunito', 'Oxygen'],
82
+ monotype: 'test',
83
+ custom: {
84
+ 'OneFontFamily', 'http://example.com/stylesheet1.css',
85
+ 'AnotherFontFamily', 'http://example.com/stylesheet2.css'
86
+ }
87
+ }
22
88
 
23
89
  ## Contributing
24
90
 
@@ -8,11 +8,14 @@ Gem::Specification.new do |gem|
8
8
  gem.version = GoogleWebfonts::VERSION
9
9
  gem.authors = ["URAMOTO Kazunori"]
10
10
  gem.email = ["kuprij@mac.com"]
11
- gem.description = %q{Rails 3 helpers to use google webfonts}
12
- gem.summary = %q{Rails 3 helpers to use google webfonts}
13
- gem.homepage = ""
11
+ gem.description = %q{A easiest way to introduce Google WebFonts to Rails application.}
12
+ gem.summary = %q{A easiest way to introduce Google WebFonts to Rails application.}
13
+ gem.homepage = "https://github.com/misopeso/google-webfonts-rails"
14
14
 
15
- gem.add_development_dependency 'rspec', '~> 2.8.0'
15
+ gem.add_runtime_dependency 'activesupport', '~> 3.2.0'
16
+
17
+ gem.add_development_dependency 'rake'
18
+ gem.add_development_dependency 'rspec', '~> 2.12.0'
16
19
 
17
20
  gem.files = `git ls-files`.split($/)
18
21
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -1,5 +1,5 @@
1
1
  module GoogleWebfonts
2
-
2
+ require 'google-webfonts/config_renderer'
3
3
  end
4
4
 
5
5
  if defined?(Rails)
@@ -0,0 +1,116 @@
1
+ module GoogleWebfonts
2
+ class ConfigRenderer
3
+ def initialize
4
+ @config = {}
5
+ end
6
+
7
+ def google= families
8
+ return if families.nil?
9
+
10
+ families = [families] unless families.is_a?(Array)
11
+ return if families.empty?
12
+
13
+ @config.merge!({
14
+ google: {
15
+ families: families
16
+ }
17
+ })
18
+ end
19
+
20
+ def typekit= id
21
+ return if id.nil?
22
+
23
+ @config.merge!({
24
+ typekit: {
25
+ id: id
26
+ }
27
+ })
28
+ end
29
+
30
+ def ascender= options
31
+ return if options.nil?
32
+ raise ArgumentError, "expected a Hash, got a #{options.class}" unless options.class == Hash
33
+ return if options.empty?
34
+
35
+ families = options.values.first
36
+ families = [families] unless families.is_a?(Array)
37
+
38
+ @config.merge!({
39
+ ascender: {
40
+ key: options.keys.first.to_s,
41
+ families: families
42
+ }
43
+ })
44
+ end
45
+
46
+ def monotype= id
47
+ return if id.nil?
48
+
49
+ @config.merge!({
50
+ monotype: {
51
+ projectId: id
52
+ }
53
+ })
54
+ end
55
+
56
+ def fontdeck= id
57
+ return if id.nil?
58
+
59
+ @config.merge!({
60
+ fontdeck: {
61
+ id: id
62
+ }
63
+ })
64
+ end
65
+
66
+ def custom= options
67
+ return if options.nil?
68
+ raise ArgumentError, "expected a Hash, got a #{options.class}" unless options.class == Hash
69
+ return if options.empty?
70
+
71
+ families, urls = [], []
72
+
73
+ options.each do |k, v|
74
+ families << k.to_s
75
+ urls << v
76
+ end
77
+
78
+ @config.merge!({
79
+ custom: {
80
+ families: families,
81
+ urls: urls,
82
+ }
83
+ })
84
+ end
85
+
86
+ def render
87
+ return "" if @config.empty?
88
+
89
+ <<-JAVASCRIPT
90
+ WebFontConfig = {
91
+ #{hash_to_js(@config, 2)}
92
+ };
93
+ JAVASCRIPT
94
+ end
95
+
96
+ private
97
+
98
+ def hash_to_js(h, level)
99
+ buffer = ""
100
+
101
+ h.each do |k, v|
102
+ buffer << ",\n" unless buffer.empty?
103
+ buffer << " " * level + "#{k}: "
104
+ buffer << "'#{v}'" if v.is_a?(String)
105
+ buffer << "[#{array_to_js(v)}]" if v.is_a?(Array)
106
+ buffer << "{\n#{hash_to_js(v, level + 1)}\n" + " " * level + "}" if v.is_a?(Hash)
107
+ end
108
+
109
+ buffer
110
+ end
111
+
112
+ def array_to_js(a)
113
+ a.map { |e| "'#{e}'" }.join(",")
114
+ end
115
+ end
116
+ end
@@ -10,28 +10,34 @@ module GoogleWebfonts
10
10
  # Initialize WebFont Loader javascript. Put it in the head elements.
11
11
  #
12
12
  # @param config [Hash]
13
- #
13
+ #
14
14
  #
15
15
  def google_webfonts_init(config = {})
16
- google_families = config.delete(:google)
17
- google_families = [google_families] unless google_families.is_a?(Array)
16
+ renderer = GoogleWebfonts::ConfigRenderer.new
17
+
18
+ renderer.google= config.delete(:google)
19
+ renderer.typekit= config.delete(:typekit)
20
+ renderer.ascender= config.delete(:ascender)
21
+ renderer.monotype= config.delete(:monotype)
22
+ renderer.fontdeck= config.delete(:fontdeck)
23
+ renderer.custom= config.delete(:custom)
18
24
 
19
- print(google_families).html_safe
25
+ specific_version = config.delete(:version) || "1"
26
+
27
+ construct(renderer, specific_version).html_safe
20
28
  end
21
29
 
22
30
  private
23
31
 
24
- def print(google)
32
+ def construct(renderer, version)
25
33
  <<-JAVASCRIPT
26
34
  <script type="text/javascript">
27
- WebFontConfig = {
28
- #{print_google(google)}
29
- };
35
+ #{renderer.render}
30
36
 
31
37
  (function() {
32
38
  var wf = document.createElement('script');
33
39
  wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
34
- '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
40
+ '#{loader_url(version)}';
35
41
  wf.type = 'text/javascript';
36
42
  wf.async = 'true';
37
43
  var s = document.getElementsByTagName('script')[0];
@@ -41,16 +47,8 @@ module GoogleWebfonts
41
47
  JAVASCRIPT
42
48
  end
43
49
 
44
- def print_google(config)
45
- return "" if config.nil?
46
-
47
- families = config.map { |c| "'#{c}'" }.join(",")
48
-
49
- <<-JAVASCRIPT
50
- google: {
51
- families: [#{families}]
52
- },
53
- JAVASCRIPT
50
+ def loader_url(version)
51
+ "://ajax.googleapis.com/ajax/libs/webfont/#{version}/webfont.js"
54
52
  end
55
53
  end
56
54
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleWebfonts
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,144 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleWebfonts::ConfigRenderer do
4
+ before do
5
+ @render = GoogleWebfonts::ConfigRenderer.new
6
+ end
7
+
8
+ context "with google webfonts settings" do
9
+ it "should process a argument" do
10
+ @render.google = 'Nunito'
11
+
12
+ h = @render.instance_variable_get(:@config)
13
+ h.should have_key(:google)
14
+ h[:google].should have_key(:families)
15
+ h[:google][:families].should be_is_a(Array)
16
+ h[:google][:families].should include('Nunito')
17
+ h[:google][:families].should have(1).items
18
+ end
19
+
20
+ it "should process an array arguments" do
21
+ @render.google = ['Nunito', 'Oxygen']
22
+
23
+ h = @render.instance_variable_get(:@config)
24
+ h.should have_key(:google)
25
+ h[:google].should have_key(:families)
26
+ h[:google][:families].should be_is_a(Array)
27
+ h[:google][:families].should include('Nunito')
28
+ h[:google][:families].should include('Oxygen')
29
+ h[:google][:families].should have(2).items
30
+ end
31
+
32
+ it "should ignore nil without error" do
33
+ @render.google = nil
34
+
35
+ h = @render.instance_variable_get(:@config)
36
+ h.should be_empty
37
+ end
38
+
39
+ it "should ignore empty array without error" do
40
+ @render.google = []
41
+
42
+ h = @render.instance_variable_get(:@config)
43
+ h.should be_empty
44
+ end
45
+ end
46
+
47
+ context "with adobe typekit settings" do
48
+ it "should process id string" do
49
+ @render.typekit = 'a12345'
50
+
51
+ h = @render.instance_variable_get(:@config)
52
+ h.should have_key(:typekit)
53
+ h[:typekit].should have_key(:id)
54
+ h[:typekit][:id].should == 'a12345'
55
+ end
56
+ end
57
+
58
+ context "with ascender settings" do
59
+ it "should process key and one family" do
60
+ @render.ascender = {testAscenderKey: 'AscenderSan:bold'}
61
+
62
+ h = @render.instance_variable_get(:@config)
63
+ h.should have_key(:ascender)
64
+ h[:ascender].should have_key(:key)
65
+ h[:ascender][:key].should == 'testAscenderKey'
66
+ h[:ascender].should have_key(:families)
67
+ h[:ascender][:families].should be_is_a(Array)
68
+ h[:ascender][:families].should include('AscenderSan:bold')
69
+ h[:ascender][:families].should have(1).items
70
+ end
71
+
72
+ it "should process key and multi families" do
73
+ @render.ascender = {testAscenderKey: ['AscenderSan:bold', 'Calibri']}
74
+
75
+ h = @render.instance_variable_get(:@config)
76
+ h.should have_key(:ascender)
77
+ h[:ascender].should have_key(:key)
78
+ h[:ascender][:key].should == 'testAscenderKey'
79
+ h[:ascender].should have_key(:families)
80
+ h[:ascender][:families].should be_is_a(Array)
81
+ h[:ascender][:families].should include('AscenderSan:bold')
82
+ h[:ascender][:families].should include('Calibri')
83
+ h[:ascender][:families].should have(2).items
84
+ end
85
+ end
86
+
87
+ context "with monotype(fonts.com) settings" do
88
+ it "should process project id" do
89
+ @render.monotype = 'testProjectId'
90
+
91
+ h = @render.instance_variable_get(:@config)
92
+ h.should have_key(:monotype)
93
+ h[:monotype].should have_key(:projectId)
94
+ h[:monotype][:projectId].should == 'testProjectId'
95
+ end
96
+ end
97
+
98
+ context "with fontdeck settings" do
99
+ it "should process id string" do
100
+ @render.fontdeck = 'xxxxx'
101
+
102
+ h = @render.instance_variable_get(:@config)
103
+ h.should have_key(:fontdeck)
104
+ h[:fontdeck].should have_key(:id)
105
+ h[:fontdeck][:id].should == 'xxxxx'
106
+ end
107
+ end
108
+
109
+ context "with custom settings" do
110
+ it "should process one entry" do
111
+ @render.custom = {'OneFont' => 'http://example.com/stylesheet1.css'}
112
+
113
+ h = @render.instance_variable_get(:@config)
114
+ h.should have_key(:custom)
115
+ h[:custom].should have_key(:families)
116
+ h[:custom][:families].should be_is_a(Array)
117
+ h[:custom][:families].should have(1).items
118
+ h[:custom][:families].should include('OneFont')
119
+ h[:custom].should have_key(:urls)
120
+ h[:custom][:urls].should be_is_a(Array)
121
+ h[:custom][:urls].should include('http://example.com/stylesheet1.css')
122
+ end
123
+
124
+ it "should process multi entries" do
125
+ @render.custom = {
126
+ 'OneFont' => 'http://example.com/stylesheet1.css',
127
+ 'AnotherFont' => 'http://example.com/stylesheet2.css'
128
+ }
129
+
130
+ h = @render.instance_variable_get(:@config)
131
+ h.should have_key(:custom)
132
+ h[:custom].should have_key(:families)
133
+ h[:custom][:families].should be_is_a(Array)
134
+ h[:custom][:families].should include('OneFont')
135
+ h[:custom][:families].should include('AnotherFont')
136
+ h[:custom][:families].should have(2).items
137
+ h[:custom].should have_key(:urls)
138
+ h[:custom][:urls].should be_is_a(Array)
139
+ h[:custom][:urls].should include('http://example.com/stylesheet1.css')
140
+ h[:custom][:urls].should include('http://example.com/stylesheet2.css')
141
+ h[:custom][:urls].should have(2).items
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleWebfonts::Rails::Helpers do
4
+ def self.helper_method(*methods)
5
+ # Pseudo method
6
+ end
7
+
8
+ include GoogleWebfonts::Rails::Helpers
9
+
10
+ describe "#google_webfonts_init" do
11
+ context "with no arguments" do
12
+ it "should output script without WebFontConfig" do
13
+ google_webfonts_init.should_not =~ /WebFontConfig/
14
+ end
15
+ end
16
+
17
+ context "with google webfonts" do
18
+ it "should output with Google WebFonts entry" do
19
+ google_webfonts_init({
20
+ google: ['Nunito', 'Oxygen']
21
+ }).should include <<-JAVASCRIPT
22
+ WebFontConfig = {
23
+ google: {
24
+ families: ['Nunito','Oxygen']
25
+ }
26
+ };
27
+ JAVASCRIPT
28
+ end
29
+ end
30
+
31
+ context "with adobe typekit" do
32
+ it "should output with Adobe Typekit entry" do
33
+ google_webfonts_init({
34
+ typekit: 'adobe'
35
+ }).should include <<-JAVASCRIPT
36
+ WebFontConfig = {
37
+ typekit: {
38
+ id: 'adobe'
39
+ }
40
+ };
41
+ JAVASCRIPT
42
+ end
43
+ end
44
+
45
+ context "with ascender" do
46
+ it "should output with Ascender entry" do
47
+ google_webfonts_init({
48
+ ascender: {testkey: ["AscenderSan:bold", "Calibri"]}
49
+ }).should include <<-JAVASCRIPT
50
+ WebFontConfig = {
51
+ ascender: {
52
+ key: 'testkey',
53
+ families: ['AscenderSan:bold','Calibri']
54
+ }
55
+ };
56
+ JAVASCRIPT
57
+ end
58
+ end
59
+
60
+ context "with monotype(fonts.com)" do
61
+ it "should output with Monotype entry" do
62
+ google_webfonts_init({
63
+ monotype: "test"
64
+ }).should include <<-JAVASCRIPT
65
+ WebFontConfig = {
66
+ monotype: {
67
+ projectId: 'test'
68
+ }
69
+ };
70
+ JAVASCRIPT
71
+ end
72
+ end
73
+
74
+ context "with fontdeck" do
75
+ it "should output with Fontdeck entry" do
76
+ google_webfonts_init({
77
+ fontdeck: "xxxxx"
78
+ }).should include <<-JAVASCRIPT
79
+ WebFontConfig = {
80
+ fontdeck: {
81
+ id: 'xxxxx'
82
+ }
83
+ };
84
+ JAVASCRIPT
85
+ end
86
+ end
87
+
88
+ context "with custom" do
89
+ it "should output with pseudo font provider" do
90
+ google_webfonts_init({
91
+ custom: {
92
+ 'OneFont' => 'http://example.com/stylesheet1.css',
93
+ 'AnotherFont' => 'http://example.com/stylesheet2.css'
94
+ }
95
+ }).should include <<-JAVASCRIPT
96
+ WebFontConfig = {
97
+ custom: {
98
+ families: ['OneFont','AnotherFont'],
99
+ urls: ['http://example.com/stylesheet1.css','http://example.com/stylesheet2.css']
100
+ }
101
+ };
102
+ JAVASCRIPT
103
+ end
104
+ end
105
+
106
+ context "with multiple font provider" do
107
+ it "should output with Google Webfonts and Monotype entries" do
108
+ google_webfonts_init({
109
+ google: ['Nunito', 'Oxygen'],
110
+ monotype: "test"
111
+ }).should include <<-JAVASCRIPT
112
+ WebFontConfig = {
113
+ google: {
114
+ families: ['Nunito','Oxygen']
115
+ },
116
+ monotype: {
117
+ projectId: 'test'
118
+ }
119
+ };
120
+ JAVASCRIPT
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'json'
4
+ require 'google-webfonts-rails'
5
+ require 'active_support/core_ext'
6
+ require 'google-webfonts/rails/helpers'
7
+
8
+ RSpec.configure do
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-webfonts-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,20 +9,42 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-20 00:00:00.000000000 Z
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70104508456600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70104508456600
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70104508456180 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70104508456180
14
36
  - !ruby/object:Gem::Dependency
15
37
  name: rspec
16
- requirement: &70195298503580 !ruby/object:Gem::Requirement
38
+ requirement: &70104508455640 !ruby/object:Gem::Requirement
17
39
  none: false
18
40
  requirements:
19
41
  - - ~>
20
42
  - !ruby/object:Gem::Version
21
- version: 2.8.0
43
+ version: 2.12.0
22
44
  type: :development
23
45
  prerelease: false
24
- version_requirements: *70195298503580
25
- description: Rails 3 helpers to use google webfonts
46
+ version_requirements: *70104508455640
47
+ description: A easiest way to introduce Google WebFonts to Rails application.
26
48
  email:
27
49
  - kuprij@mac.com
28
50
  executables: []
@@ -30,15 +52,20 @@ extensions: []
30
52
  extra_rdoc_files: []
31
53
  files:
32
54
  - .gitignore
55
+ - .rspec
33
56
  - Gemfile
34
57
  - LICENSE.txt
35
58
  - README.md
36
59
  - Rakefile
37
60
  - google-webfonts-rails.gemspec
38
61
  - lib/google-webfonts-rails.rb
62
+ - lib/google-webfonts/config_renderer.rb
39
63
  - lib/google-webfonts/rails/helpers.rb
40
64
  - lib/google-webfonts/version.rb
41
- homepage: ''
65
+ - spec/google-webfonts/config_renderer_spec.rb
66
+ - spec/google-webfonts/rails/helpers_spec.rb
67
+ - spec/spec_helper.rb
68
+ homepage: https://github.com/misopeso/google-webfonts-rails
42
69
  licenses: []
43
70
  post_install_message:
44
71
  rdoc_options: []
@@ -61,5 +88,8 @@ rubyforge_project:
61
88
  rubygems_version: 1.8.10
62
89
  signing_key:
63
90
  specification_version: 3
64
- summary: Rails 3 helpers to use google webfonts
65
- test_files: []
91
+ summary: A easiest way to introduce Google WebFonts to Rails application.
92
+ test_files:
93
+ - spec/google-webfonts/config_renderer_spec.rb
94
+ - spec/google-webfonts/rails/helpers_spec.rb
95
+ - spec/spec_helper.rb