rails_google_fonts 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63c16bf3c801ed93c064597b11e1f845a56ce75a
4
+ data.tar.gz: 263a4fa532dc5ab095236ab87900d51be6759a19
5
+ SHA512:
6
+ metadata.gz: 2910a6589694e55a4c23cc900fcc6b227373dd414a5045ce58f3c8d88a4eb809cee5f198ac041eb69e0e94c5c13b42740d8fe0bcc015e4dbbe8252664c0aeb8c
7
+ data.tar.gz: 2fb3d4e79a62f27a68b0cd3026f81ef673caf5398a90db372f7ee6d42b650052627cfa03149b2516a1bd0f3a89adf6f21f39f1242ce2e05bbd7458c68dd67365
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.gem
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_google_fonts.gemspec
4
+ gemspec
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2015 syaffers
2
+
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,96 @@
1
+ # RailsGoogleFonts
2
+
3
+ Rails Google Fonts is a gem/wrapper to ease the use of Google Fonts with your Rails app. It's more of an API client sort of thing.
4
+
5
+ ## Requirements
6
+
7
+ Your Google Developer API key. You can get one at [https://console.developers.google.com/](https://console.developers.google.com/) (you will need to create a project, if you haven't already!).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'rails_google_fonts'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install rails_google_fonts
24
+
25
+ ## Usage
26
+
27
+ ### Setup
28
+
29
+ Include your Google Developer API key. Tip: Store your API key in the environment variable for extra security!
30
+
31
+ ```ruby
32
+ RailsGoogleFonts.key(ENV['GOOGLE_API_KEY'])
33
+ ```
34
+ or (unsafe!)
35
+
36
+ ```ruby
37
+ RailsGoogleFonts.key('YOUR_GOOGLE_API_KEY')
38
+ ```
39
+
40
+ ### Get all the fonts
41
+
42
+ Simply:
43
+
44
+ ```ruby
45
+ RailsGoogleFonts.all # Returns a list of object containing all the fonts
46
+ ```
47
+
48
+ This will raise an error if the API key is not set
49
+
50
+ ### Get all fonts sorted by a parameter
51
+
52
+ Get a sorted list of all the fonts, sorted by a parameter. Possible parameters are (shamelessly copied from [https://developers.google.com/fonts/docs/developer_api](https://developers.google.com/fonts/docs/developer_api)):
53
+
54
+ + alpha: Sort the list alphabetically
55
+ + date: Sort the list by date added (most recent font added or updated first)
56
+ + popularity: Sort the list by popularity (most popular family first)
57
+ + style: Sort the list by number of styles available (family with most styles first)
58
+ + trending: Sort the list by families seeing growth in usage (family seeing the most growth first)
59
+
60
+ Example:
61
+
62
+ ```ruby
63
+ RailsGoogleFonts.all_by('trending')
64
+ ```
65
+
66
+ Setting an invalid parameter will raise an error
67
+
68
+ ### Filter by font category
69
+
70
+ Filter the fonts using the `category` function. Known font categories are:
71
+
72
+ + serif
73
+ + sans-serif
74
+ + monospace
75
+ + handwriting
76
+ + display
77
+
78
+ Example:
79
+
80
+ ```ruby
81
+ RailsGoogleFonts.category('sans-serif') # Returns a list of objects in the sans-serif category
82
+ ```
83
+
84
+ Returns an empty array for invalid categories
85
+
86
+ ## Testing
87
+
88
+ Test the build by running `rake test`
89
+
90
+ ## Contributing
91
+
92
+ 1. Fork it ( https://github.com/[my-github-username]/rails_google_fonts/fork )
93
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
94
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
95
+ 4. Push to the branch (`git push origin my-new-feature`)
96
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
10
+
@@ -0,0 +1,86 @@
1
+ require "rails_google_fonts/version"
2
+ require "httparty"
3
+
4
+ module RailsGoogleFonts
5
+ include HTTParty
6
+
7
+ # Set Google API key for access to the fonts API
8
+ #
9
+ # Example:
10
+ # >> RailsGoogleFonts.key(ENV['GOOGLE_API_KEY'])
11
+ # => {:query {:key => "1234567890ASDasdASAsdasdASDASdad}}
12
+ #
13
+ # Arguments:
14
+ # key: (String)
15
+
16
+ def self.key(key)
17
+ @key = key
18
+ @options = { query: {key: @key} }
19
+ end
20
+
21
+ # Get all fonts from the Google Fonts API
22
+ #
23
+ # Example:
24
+ # >> RailsGoogleFonts.all
25
+ # => [..., {"family"=>"Reenie Beanie", "category"=>"handwriting", "variants"=>["regular"], ...}, ...]
26
+
27
+ def self.all
28
+ if @key
29
+ @options[:query][:sort] = 'alpha'
30
+ response = HTTParty.get('https://www.googleapis.com/webfonts/v1/webfonts', @options).parsed_response
31
+
32
+ if response['error']
33
+ raise "Unexpected error: #{response['error']['errors'][0]['reason']}"
34
+ return response['error']
35
+ else
36
+ return response['items']
37
+ end
38
+
39
+ else
40
+ raise "Google API key is not set"
41
+ end
42
+ end
43
+
44
+ # Get all fonts sorted by a parameter
45
+ #
46
+ # Example:
47
+ # >> RailsGoogleFonts.all_by('trending')
48
+ # => [{..., "family"=>"Amita", ...}, ...]
49
+ #
50
+ # Arguments:
51
+ # sort: (String)
52
+
53
+ def self.all_by(sort='alpha')
54
+ if @key
55
+ @options[:query][:sort] = sort
56
+ response = HTTParty.get('https://www.googleapis.com/webfonts/v1/webfonts', @options).parsed_response
57
+
58
+ if response['error']
59
+ raise "Unexpected error: #{response['error']['errors'][0]['reason']}"
60
+ return response['error']
61
+ else
62
+ return response['items']
63
+ end
64
+
65
+ else
66
+ raise "Google API key is not set"
67
+ end
68
+ end
69
+
70
+ # Get all fonts from a category
71
+ #
72
+ # Example:
73
+ # >> RailsGoogleFonts.category('display')
74
+ # => [..., {..., "family"=>"Sniglet", "category"=>"display", ...}, ...]
75
+ #
76
+ # Arguments:
77
+ # category: (String)
78
+
79
+ def self.category(category)
80
+ if @key
81
+ return RailsGoogleFonts.all.select { |a| a['category'] == category }
82
+ else
83
+ raise "Google API key is not set"
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module RailsGoogleFonts
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_google_fonts/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_google_fonts"
8
+ spec.version = RailsGoogleFonts::VERSION
9
+ spec.authors = ["Syafiq Kamarul Azman"]
10
+ spec.email = ["syafiqkamarulazman@hotmail.com"]
11
+ spec.summary = %q{Rails Google Fonts is a gem/wrapper to ease the use of Google Fonts with your Rails app}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/syaffers/rails_google_fonts"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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 "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_dependency "httparty", "~> 0.13.5"
25
+ end
@@ -0,0 +1,92 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/rails_google_fonts'
3
+
4
+ describe "setting key" do
5
+ describe "no key being input" do
6
+ it "raises an argument error" do
7
+ proc { RailsGoogleFonts.key }.must_raise ArgumentError
8
+ end
9
+ end
10
+
11
+ describe "key is input" do
12
+ it "sets the key sucessfully" do
13
+ RailsGoogleFonts.key('ABC123')[:query][:key].must_equal 'ABC123'
14
+ end
15
+ end
16
+ end
17
+
18
+ describe "key not set" do
19
+ describe "get all fonts" do
20
+ it "raises a key not set error" do
21
+ proc { RailsGoogleFonts.all }.must_raise RuntimeError
22
+ end
23
+ end
24
+
25
+ describe "get all fonts sorted in a given order" do
26
+ it "raises an invalid key error" do
27
+ proc { RailsGoogleFonts.all_by("alpha") }.must_raise RuntimeError
28
+ end
29
+ end
30
+
31
+ describe "get all fonts filtered by category" do
32
+ it "raises an invalid key error" do
33
+ proc { RailsGoogleFonts.category("serif") }.must_raise RuntimeError
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "invalid key" do
39
+ before do
40
+ RailsGoogleFonts.key("abcABC123")
41
+ end
42
+
43
+ describe "get all fonts" do
44
+ it "raises an invalid key error" do
45
+ proc { RailsGoogleFonts.all }.must_raise RuntimeError
46
+ end
47
+ end
48
+
49
+ describe "get all fonts sorted in a given order" do
50
+ it "raises an invalid key error" do
51
+ proc { RailsGoogleFonts.all_by("alpha") }.must_raise RuntimeError
52
+ end
53
+ end
54
+
55
+ describe "get all fonts filtered by category" do
56
+ it "raises an invalid key error" do
57
+ proc { RailsGoogleFonts.category("serif") }.must_raise RuntimeError
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "valid key" do
63
+ before do
64
+ RailsGoogleFonts.key(ENV["GOOGLE_API_KEY"])
65
+ end
66
+
67
+ describe "get all fonts" do
68
+ it "returns all fonts" do
69
+ RailsGoogleFonts.all.must_be_instance_of Array
70
+ end
71
+ end
72
+
73
+ describe "get all fonts sorted in a given order" do
74
+ it "returns all fonts sorted in alphabetical order" do
75
+ RailsGoogleFonts.all_by("alpha").must_be_instance_of Array
76
+ end
77
+
78
+ it "raises an invalid parameter error" do
79
+ proc { RailsGoogleFonts.all_by("invalid_parameter") }.must_raise RuntimeError
80
+ end
81
+ end
82
+
83
+ describe "get all fonts filtered by category" do
84
+ it "returns all fonts in serif category" do
85
+ RailsGoogleFonts.category("serif")[0]["category"].must_equal 'serif'
86
+ end
87
+
88
+ it "returns empty array for an invalid category" do
89
+ RailsGoogleFonts.category("invalid_category").must_be_empty
90
+ end
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_google_fonts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Syafiq Kamarul Azman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.13.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.13.5
55
+ description: ''
56
+ email:
57
+ - syafiqkamarulazman@hotmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/rails_google_fonts.rb
68
+ - lib/rails_google_fonts/version.rb
69
+ - rails_google_fonts.gemspec
70
+ - test/test_rails_google_fonts.rb
71
+ homepage: https://github.com/syaffers/rails_google_fonts
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.4.7
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Rails Google Fonts is a gem/wrapper to ease the use of Google Fonts with
95
+ your Rails app
96
+ test_files:
97
+ - test/test_rails_google_fonts.rb