GoogleWebfonts 0.3.0

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: 4d0ad2495f84477a076a21ea776ee490181de4e6
4
+ data.tar.gz: 4f92b21c09fb4cfdda54a87558476bcb155234df
5
+ SHA512:
6
+ metadata.gz: dc76a8c7d42ed429dbc987d0149551f9c719ae20f2d5dc5511b473aecdc50fcdcfd5644907d4912c9b6753ce7115fe4cfe531adaabddcad4538e4bb3be9a7e06
7
+ data.tar.gz: '02719ce2aa40ebfc36f9559910829bc4a92bd3e324548654fc7faea75034adb385b431150db950c88de2c36416875d0b5fa0f2b91bbef4346d91cfdfc267b796'
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google-webfonts.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Travis Haynes
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,95 @@
1
+ # Google::Webfonts
2
+
3
+ Provides a helper for using [Google Webfonts](http://www.google.com/webfonts) in
4
+ Rails or Sinatra.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'google-webfonts'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install google-webfonts
19
+
20
+ ## Usage
21
+
22
+ ### Syntax
23
+
24
+ google_webfonts_link_tag :font_name => [sizes], ...
25
+
26
+ ### Examples
27
+
28
+ Basic usage:
29
+
30
+ google_webfonts_link_tag :droid_sans => [400, 700],
31
+ :yanone_kaffeesatz => [300, 400]
32
+
33
+ The sizes are optional, and do not have to be in an Array if you are only
34
+ including one size. For example:
35
+
36
+ google_webfonts_link_tag :droid_sans
37
+ # => generates a tag for Droid+Sans without specifying the font weight
38
+
39
+ google_webfonts_link_tag :droid_sans => 400
40
+ # => generates a tag for Droid+Sans with 400 weight
41
+
42
+ google_webfonts_link_tag :droid_sans => [400, 700]
43
+ # => generates a tag for Droid+Sans with 400 and 700 weights
44
+
45
+ You can also use a String instead of a Symbol if you'd prefer. For example:
46
+
47
+ google_webfonts_link_tag "Droid Sans", "Yanone Kaffeesatz" => 400
48
+ # includes Droid+Sans without a specified weight
49
+ # and Yanone+Kaffeesatz with weight 400
50
+
51
+ ### Using in Rails
52
+
53
+ No additional work required to use this gem in a Rails application. Just add
54
+ it to your application's Gemfile, and it is automatically available in your
55
+ views.
56
+
57
+ You will, however, need to `include Google::Webfonts::Helper` if you want to use
58
+ it outside of a view.
59
+
60
+ ### Using in Sinatra
61
+
62
+ Here is a simple "Hello World" example for using Google::Webfonts in a Sinatra
63
+ app:
64
+
65
+ # app.rb
66
+ require 'rubygems'
67
+ require 'sinatra'
68
+ require 'google-webfonts' # <= this must be required after 'sinatra'
69
+
70
+ get '/' do
71
+ erb :index
72
+ end
73
+
74
+ ###
75
+
76
+ # views/index.erb
77
+ <html>
78
+ <head>
79
+ <%= google_webfonts_link_tag "Droid Sans" %>
80
+ </head>
81
+ <body>
82
+ <p style="font-family: 'Droid+Sans', sans-serif;">
83
+ Hello World!
84
+ </p>
85
+ </body>
86
+ </html>
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Ensure what your code is well tested, and all the tests pass. (`rspec spec`)
95
+ 6. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
8
+
9
+ task :default => [:test]
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'google-webfonts/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "GoogleWebfonts"
8
+ spec.version = Google::Webfonts::VERSION
9
+ spec.authors = ["Travis Haynes", "Evgeniy Kulikov"]
10
+ spec.email = ["travis.j.haynes@gmail.com", "im@kulikov.im"]
11
+
12
+ spec.summary = "Provides a helper for using Google Webfonts in Rails or " +
13
+ "Sinatra, although it can be used outside of those " +
14
+ "frameworks as well."
15
+
16
+ spec.description = "Google Webfonts helper for Rails or Sinatra applications."
17
+
18
+ spec.homepage = "https://github.com/im-kulikov/Google-Webfonts-Helper"
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.executables = spec.files.grep(%r{^bin/}) {|f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^test/})
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_runtime_dependency 'actionpack', '>= 3.0.0'
25
+
26
+ spec.add_development_dependency 'bundler', '>= 1.3.0', '< 2.0'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'minitest', '~> 5.3.3'
29
+ spec.add_development_dependency 'sinatra', '~> 1.3.2'
30
+ end
@@ -0,0 +1,22 @@
1
+ require 'uri'
2
+ require 'action_view'
3
+ require 'active_support/core_ext'
4
+ require 'action_dispatch/http/mime_type'
5
+
6
+ # Public: Module for Google helpers
7
+ module Google
8
+ # Public: Module for Google Webfonts helpers
9
+ module Webfonts
10
+ HOST = 'fonts.googleapis.com'
11
+ PATH = '/css'
12
+ end
13
+ end
14
+
15
+ require 'google-webfonts/version'
16
+ require 'google-webfonts/link_tag'
17
+ require 'google-webfonts/helper'
18
+
19
+ require 'google-webfonts/sinatra' if defined? Sinatra
20
+
21
+ # include the webfonts helper methods in the Rails view helpers
22
+ ActionView::Base.send :include, Google::Webfonts::Helper
@@ -0,0 +1,44 @@
1
+ module Google
2
+ module Webfonts
3
+
4
+ # Public: Helper module that includes the google_webfonts_link_tag method.
5
+ # This module is automatically included in your Rails view helpers.
6
+ module Helper
7
+ # Public: Generates a Google Webfonts link tag
8
+ #
9
+ # If you need to specify a font size, use a Hash. Symbols will be
10
+ # titleized, so if the font you're using uses a specialized case
11
+ # (e.g: "PT Sans"), then you'll need to use a String.
12
+ #
13
+ # You can specify subsets by using the :subset option. Note that this must
14
+ # be a Symbol. "Subset" and "subset" would be turned into a font.
15
+ #
16
+ # options - The font options. This can be a String, Symbol, or Hash, or
17
+ # a combination of all three.
18
+ #
19
+ # Examples
20
+ #
21
+ # google_webfonts_link_tag "PT Sans"
22
+ #
23
+ # google_webfonts_link_tag :droid_sans
24
+ #
25
+ # google_webfonts_link_tag droid_sans: [400, 700]
26
+ #
27
+ # google_webfonts_link_tag droid_sans: [400, 700],
28
+ # yanone_kaffeesatz: 700
29
+ #
30
+ # google_webfonts_link_tag :droid_sans,
31
+ # yanone_kaffeesatz: 400,
32
+ # "PT Sans" => [400, 700]
33
+ #
34
+ # google_webfonts_link_tag :droid_sans, subset: %w[latin cyrillic]
35
+ #
36
+ # Returns a <link> tag for the Google Webfonts stylesheet.
37
+ # Raises ArgumentError if no options are passed.
38
+ def google_webfonts_link_tag(*opts)
39
+ raise ArgumentError, 'expected at least one font', caller if opts.empty?
40
+ LinkTag.new(request, *opts).result
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,78 @@
1
+ module Google
2
+ module Webfonts
3
+ class LinkTag
4
+ include ActionView::Helpers::TagHelper
5
+
6
+ attr_reader :result
7
+
8
+ alias_method :to_s, :result
9
+
10
+ def initialize(request, *opts)
11
+ @request = request
12
+ @subsets = []
13
+ @fonts = fonts_from_options(opts)
14
+ @result = tag(:link, link_options, false, false)
15
+ end
16
+
17
+ private
18
+
19
+ def fonts_from_options(opts)
20
+ opts.map {|option| parse_google_webfont_option(option) }.flatten
21
+ end
22
+
23
+ def parse_google_webfont_option(option)
24
+ case option
25
+ when Symbol, String then parse_google_webfont_name(option)
26
+ when Hash then parse_google_webfont_hash(option)
27
+ else ""
28
+ end
29
+ end
30
+
31
+ def parse_google_webfont_hash(hash)
32
+ add_subsets(hash.delete(:subset))
33
+
34
+ hash.inject([]) do |result, (font_name, sizes)|
35
+ font_name = parse_google_webfont_name(font_name)
36
+ result << "#{font_name}:#{Array(sizes).join(",")}"
37
+ end
38
+ end
39
+
40
+ def parse_google_webfont_name(name)
41
+ name = name.to_s.titleize if name.is_a?(Symbol)
42
+ name.gsub("_", " ")
43
+ end
44
+
45
+ def link_options
46
+ { rel: 'stylesheet',
47
+ type: type_css,
48
+ href: uri.to_s }
49
+ end
50
+
51
+ def add_subsets(subsets)
52
+ @subsets += Array(subsets)
53
+ end
54
+
55
+ def uri
56
+ u = @request.ssl? ? URI::HTTPS : URI::HTTP
57
+ u.build(host: HOST, path: PATH, query: uri_query)
58
+ end
59
+
60
+ def uri_query
61
+ {}.tap {|q|
62
+ q['family'] = @fonts.join("|")
63
+ q['subset'] = @subsets.join(",") if @subsets.any?
64
+ }.to_query
65
+ end
66
+
67
+ private
68
+
69
+ def type_css
70
+ if ActionPack::VERSION::MAJOR < 5
71
+ return Mime::CSS
72
+ end
73
+
74
+ return Mime[:css]
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module Sinatra
2
+ helpers Google::Webfonts::Helper
3
+ end
@@ -0,0 +1,6 @@
1
+ module Google
2
+ module Webfonts
3
+ # Public: Current version of the gem
4
+ VERSION = "0.3.0"
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ module Google
4
+ module Webfonts
5
+ class HelperTest < Minitest::Test
6
+ include Helper
7
+
8
+ def test_google_webfonts_link_tag
9
+ options = { droid_sans: [400, 700] }
10
+ link_tag = LinkTag.new(request, options)
11
+ assert_equal link_tag.result, google_webfonts_link_tag(options)
12
+ end
13
+
14
+ private
15
+
16
+ def request
17
+ OpenStruct.new(:ssl? => false)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ module Google
4
+ module Webfonts
5
+ class LinkTagTest < Minitest::Test
6
+ def setup
7
+ @http_request = OpenStruct.new(:ssl? => false)
8
+ @https_request = OpenStruct.new(:ssl? => true)
9
+ end
10
+
11
+ def test_subsets
12
+ tag = LinkTag.new(@http_request, droid_sans: [400, 700],
13
+ subset: %w[latin cyrillic]).result
14
+ assert_tag tag, 'link',
15
+ href: 'http://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700&subset=latin%2Ccyrillic'
16
+ end
17
+
18
+ def test_uri_protocol
19
+ tag = LinkTag.new(@http_request, droid_sans: [400, 700]).result
20
+ assert_tag tag, 'link',
21
+ href: 'http://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700'
22
+
23
+ tag = LinkTag.new(@https_request, droid_sans: [400, 700]).result
24
+ assert_tag tag, 'link',
25
+ href: 'https://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700'
26
+ end
27
+
28
+ def test_with_font_sizes
29
+ tag = LinkTag.new(@http_request, droid_sans: [400, 700]).result
30
+ assert_tag tag, 'link',
31
+ href: 'http://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700'
32
+
33
+ tag = LinkTag.new(@http_request, droid_sans: [400, 700],
34
+ 'PT Sans' => [400, 700]).result
35
+ assert_tag tag, 'link',
36
+ href: 'http://fonts.googleapis.com/css?family=Droid+Sans%3A400%2C700%7CPT+Sans%3A400%2C700'
37
+ end
38
+
39
+ def test_strings_are_not_titleized
40
+ tag = LinkTag.new(@http_request, 'PT Sans').result
41
+ assert_tag tag, 'link',
42
+ href: 'http://fonts.googleapis.com/css?family=PT+Sans'
43
+ end
44
+
45
+ def test_symbols_are_titleized
46
+ tag = LinkTag.new(@http_request, :droid_sans).result
47
+ assert_tag tag, 'link',
48
+ href: 'http://fonts.googleapis.com/css?family=Droid+Sans'
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ # create a Sinatra route for testing
4
+ get("/") { google_webfonts_link_tag droid_sans: [400, 700] }
5
+
6
+ class SinatraTest < Minitest::Test
7
+ include Rack::Test::Methods
8
+
9
+ def app
10
+ Sinatra::Application
11
+ end
12
+
13
+ def test_google_webfonts_link_tag_helper_loaded_in_sinatra
14
+ get "/"
15
+ options = { droid_sans: [400, 700] }
16
+ link_tag = Google::Webfonts::LinkTag.new(request, options)
17
+ assert_equal link_tag.result, last_response.body
18
+ end
19
+
20
+ private
21
+
22
+ def request
23
+ OpenStruct.new(:ssl? => false)
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class Google::WebfontsTest < Minitest::Test
4
+ def test_helper_is_included_in_action_view_base
5
+ assert ActionView::Base.ancestors.include?(Google::Webfonts::Helper)
6
+ end
7
+
8
+ def test_helper_is_included_in_sinatra
9
+ assert Sinatra::Application.ancestors.include?(Google::Webfonts::Helper)
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'ostruct'
4
+ require 'sinatra'
5
+ require 'google-webfonts'
6
+ require 'minitest/autorun'
7
+ require 'minitest/unit'
8
+ require 'rack/test'
9
+
10
+ class Minitest::Test
11
+ def assert_tag(el, tag, props={})
12
+ assert el =~ /<#{tag}.*\/>/ , "expected a #{tag} tag"
13
+ assert props.all? {|key, value| el.include?("#{key}=\"#{value}\"") },
14
+ "tag's properties do not match"
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: GoogleWebfonts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Travis Haynes
8
+ - Evgeniy Kulikov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-03-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 3.0.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.0.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.0
35
+ - - "<"
36
+ - !ruby/object:Gem::Version
37
+ version: '2.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 1.3.0
45
+ - - "<"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: minitest
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 5.3.3
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 5.3.3
76
+ - !ruby/object:Gem::Dependency
77
+ name: sinatra
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.3.2
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.2
90
+ description: Google Webfonts helper for Rails or Sinatra applications.
91
+ email:
92
+ - travis.j.haynes@gmail.com
93
+ - im@kulikov.im
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - ".gitignore"
99
+ - Gemfile
100
+ - LICENSE
101
+ - README.md
102
+ - Rakefile
103
+ - google-webfonts.gemspec
104
+ - lib/google-webfonts.rb
105
+ - lib/google-webfonts/helper.rb
106
+ - lib/google-webfonts/link_tag.rb
107
+ - lib/google-webfonts/sinatra.rb
108
+ - lib/google-webfonts/version.rb
109
+ - test/lib/google-webfonts/helper_test.rb
110
+ - test/lib/google-webfonts/link_tag_test.rb
111
+ - test/lib/google-webfonts/sinatra_test.rb
112
+ - test/lib/google-webfonts_test.rb
113
+ - test/test_helper.rb
114
+ homepage: https://github.com/im-kulikov/Google-Webfonts-Helper
115
+ licenses: []
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.6.11
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Provides a helper for using Google Webfonts in Rails or Sinatra, although
137
+ it can be used outside of those frameworks as well.
138
+ test_files:
139
+ - test/lib/google-webfonts/helper_test.rb
140
+ - test/lib/google-webfonts/link_tag_test.rb
141
+ - test/lib/google-webfonts/sinatra_test.rb
142
+ - test/lib/google-webfonts_test.rb
143
+ - test/test_helper.rb