g_web_font 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +40 -0
- data/g_web_font.gemspec +23 -0
- data/lib/g_web_font/railtie.rb +9 -0
- data/lib/g_web_font/version.rb +3 -0
- data/lib/g_web_font/view_helpers/tag_helper.rb +8 -0
- data/lib/g_web_font/view_helpers.rb +109 -0
- data/lib/g_web_font/web_font.rb +23 -0
- data/lib/g_web_font.rb +8 -0
- data/test/g_web_font_test.rb +31 -0
- metadata +124 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dakota Dux
|
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,29 @@
|
|
1
|
+
# GWebFont
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'g_web_font'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install g_web_font
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# #!/usr/bin/env rake
|
2
|
+
# require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
|
5
|
+
#!/usr/bin/env rake
|
6
|
+
begin
|
7
|
+
require 'bundler/setup'
|
8
|
+
rescue LoadError
|
9
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
10
|
+
end
|
11
|
+
begin
|
12
|
+
require 'rdoc/task'
|
13
|
+
rescue LoadError
|
14
|
+
require 'rdoc/rdoc'
|
15
|
+
require 'rake/rdoctask'
|
16
|
+
RDoc::Task = Rake::RDocTask
|
17
|
+
end
|
18
|
+
|
19
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'GWebFont'
|
22
|
+
# rdoc.options << '--line-numbers'
|
23
|
+
# rdoc.rdoc_files.include('README.rdoc')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
30
|
+
require 'rake/testtask'
|
31
|
+
|
32
|
+
Rake::TestTask.new(:test) do |t|
|
33
|
+
t.libs << 'lib'
|
34
|
+
t.libs << 'test'
|
35
|
+
t.pattern = 'test/**/*_test.rb'
|
36
|
+
t.verbose = false
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :test
|
data/g_web_font.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/g_web_font/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dakota Dux"]
|
6
|
+
gem.email = ["dakota.dux@gmail.com"]
|
7
|
+
gem.description = %q{ Easy access to Google's Web Fonts }
|
8
|
+
gem.summary = %q{ Access a list of Google's webfonts through their API and provide rails views helpers }
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "g_web_font"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = GWebFont::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rake"
|
19
|
+
# gem.add_development_dependency "rspec"
|
20
|
+
gem.add_dependency 'httparty'
|
21
|
+
gem.add_dependency 'oj'
|
22
|
+
gem.add_dependency 'hashie'
|
23
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'g_web_font/view_helpers/tag_helper'
|
2
|
+
|
3
|
+
module GWebFont
|
4
|
+
module Helper
|
5
|
+
include ActionView::Helpers::TagHelper
|
6
|
+
BASE_URL = "fonts.googleapis.com/css?family=".freeze
|
7
|
+
|
8
|
+
# Generates the options for select for the Google Web Fonts
|
9
|
+
def web_font_options_for_select
|
10
|
+
options_for_select([''] | GWebFont::WebFont.list_fonts)
|
11
|
+
end
|
12
|
+
|
13
|
+
# From Google Webfonts Helper
|
14
|
+
# https://github.com/travishaynes/Google-Webfonts-Helper
|
15
|
+
|
16
|
+
# Public: Generates a Google Webfonts link tag
|
17
|
+
#
|
18
|
+
# options - The font options. This can be a String, Symbol, or Hash, or
|
19
|
+
# a combination of all three. If you need to specify a font
|
20
|
+
# size, use a Hash, otherwise a String or Symbol will work.
|
21
|
+
#
|
22
|
+
# Examples
|
23
|
+
#
|
24
|
+
# google_webfonts_link_tag "Droid Sans"
|
25
|
+
# # => '<link href="http://fonts.googleapis.com/css?family=Droid+Sans" rel="stylesheet" type="text/css" />'
|
26
|
+
#
|
27
|
+
# google_webfonts_link_tag :droid_sans
|
28
|
+
# # => '<link href="http://fonts.googleapis.com/css?family=Droid+Sans" rel="stylesheet" type="text/css" />'
|
29
|
+
#
|
30
|
+
# google_webfonts_link_tag :droid_sans => [400, 700]
|
31
|
+
# # => '<link href="http://fonts.googleapis.com/css?family=Droid+Sans:400,700" rel="stylesheet" type="text/css" />'
|
32
|
+
#
|
33
|
+
# google_webfonts_link_tag :droid_sans => [400, 700],
|
34
|
+
# :yanone_kaffeesatz => [300, 400]
|
35
|
+
# # => '<link href="http://fonts.googleapis.com/css?family=Droid+Sans:400,700|Yanone+Kaffeesatz:300,400" rel="stylesheet" type="text/css" />'
|
36
|
+
#
|
37
|
+
# google_webfonts_link_tag "Droid Sans",
|
38
|
+
# :yanone_kaffeesatz => 400
|
39
|
+
# # => '<link href="http://fonts.googleapis.com/css?family=Droid+Sans|Yanone+Kaffeesatz:400" rel="stylesheet" type="text/css" />'
|
40
|
+
#
|
41
|
+
# Returns a <link> tag for the Google Webfonts stylesheet.
|
42
|
+
# Raises ArgumentError if no options are passed.
|
43
|
+
# Raises ArgumentError if an option is not a Symbol, String, or Hash.
|
44
|
+
# Raises ArgumentError if a size is not a String or Fixnum.
|
45
|
+
def google_webfonts_link_tag(*options)
|
46
|
+
raise ArgumentError, "expected at least one font" if options.empty?
|
47
|
+
|
48
|
+
fonts = []
|
49
|
+
|
50
|
+
options.each do |option|
|
51
|
+
case option.class.to_s
|
52
|
+
when "Symbol", "String"
|
53
|
+
# titleize the font name
|
54
|
+
font_name = option.to_s.titleize
|
55
|
+
|
56
|
+
# replace any spaces with pluses
|
57
|
+
font_name = font_name.gsub(" ", "+")
|
58
|
+
|
59
|
+
# include the font
|
60
|
+
fonts << font_name
|
61
|
+
when "Hash"
|
62
|
+
fonts += option.inject([]) do |result, (font_name, sizes)|
|
63
|
+
# ensure sizes is an Array
|
64
|
+
sizes = Array(sizes)
|
65
|
+
|
66
|
+
sizes.all? do |size|
|
67
|
+
unless size.class == Fixnum || size.class == String
|
68
|
+
raise ArgumentError, "expected a Fixnum or String, got a #{ size.class }"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# convert font name into a String
|
73
|
+
font_name = font_name.to_s
|
74
|
+
|
75
|
+
# replace underscores with spaces
|
76
|
+
# and titleize the font name
|
77
|
+
font_name = font_name.gsub("_", " ")
|
78
|
+
font_name = font_name.titleize
|
79
|
+
|
80
|
+
# convert the spaces into pluses
|
81
|
+
font_name = font_name.gsub(" ", "+")
|
82
|
+
|
83
|
+
# return font_name:sizes where
|
84
|
+
# sizes is a comma separated list
|
85
|
+
result << "#{ font_name }:#{ sizes.join(",") }"
|
86
|
+
end
|
87
|
+
else
|
88
|
+
raise ArgumentError, "expected a String, Symbol, or a Hash, got a #{ option.class }"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# the fonts are separated by pipes
|
93
|
+
family = fonts.join("|")
|
94
|
+
|
95
|
+
# generate https links if we are an https site
|
96
|
+
request.ssl? ? request_method = "https" : request_method = "http"
|
97
|
+
|
98
|
+
# return the link tag
|
99
|
+
tag 'link', {
|
100
|
+
:rel => :stylesheet,
|
101
|
+
:type => Mime::CSS,
|
102
|
+
:href => "http://#{ BASE_URL }#{ family }",
|
103
|
+
:href => "#{ request_method }://#{ BASE_URL }#{ family }"
|
104
|
+
},
|
105
|
+
false,
|
106
|
+
false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module GWebFont
|
2
|
+
BASE_URL = 'https://www.googleapis.com/webfonts/v1/webfonts'.freeze
|
3
|
+
|
4
|
+
class WebFont
|
5
|
+
class << self
|
6
|
+
def list_fonts
|
7
|
+
load_font_items.map{ |font| font['family'] }
|
8
|
+
end
|
9
|
+
|
10
|
+
def load_font_items
|
11
|
+
response = HTTParty.get(BASE_URL, :query => { :key => GWebFont.api_key })
|
12
|
+
font_items = Oj.load(response.body)['items']
|
13
|
+
|
14
|
+
font_items
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
attr_accessor :api_key
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/lib/g_web_font.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'g_web_font'
|
3
|
+
|
4
|
+
class GWebFontTest < Test::Unit::TestCase
|
5
|
+
def api_key
|
6
|
+
# Enter your API Key here
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_version
|
10
|
+
assert_equal GWebFont::VERSION, '0.0.1'
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_set_api_key
|
14
|
+
GWebFont.api_key = api_key
|
15
|
+
assert_equal GWebFont.api_key, api_key
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_load_font_items
|
19
|
+
GWebFont.api_key = api_key
|
20
|
+
font_items = GWebFont::WebFont.load_font_items
|
21
|
+
assert !font_items.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_list_fonts
|
25
|
+
GWebFont.api_key = api_key
|
26
|
+
fonts = GWebFont::WebFont.list_fonts
|
27
|
+
assert fonts.is_a?(Array)
|
28
|
+
assert !fonts.empty?
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: g_web_font
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dakota Dux
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: httparty
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: oj
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: hashie
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: ! ' Easy access to Google''s Web Fonts '
|
79
|
+
email:
|
80
|
+
- dakota.dux@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- g_web_font.gemspec
|
91
|
+
- lib/g_web_font.rb
|
92
|
+
- lib/g_web_font/railtie.rb
|
93
|
+
- lib/g_web_font/version.rb
|
94
|
+
- lib/g_web_font/view_helpers.rb
|
95
|
+
- lib/g_web_font/view_helpers/tag_helper.rb
|
96
|
+
- lib/g_web_font/web_font.rb
|
97
|
+
- test/g_web_font_test.rb
|
98
|
+
homepage: ''
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.24
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Access a list of Google's webfonts through their API and provide rails views
|
122
|
+
helpers
|
123
|
+
test_files:
|
124
|
+
- test/g_web_font_test.rb
|