crystalmeta 0.9.0

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.
Files changed (55) hide show
  1. data/.gitignore +6 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +130 -0
  5. data/Rakefile +1 -0
  6. data/Readme.markdown +210 -0
  7. data/crystalmeta.gemspec +23 -0
  8. data/lib/crystal/controller_extensions.rb +19 -0
  9. data/lib/crystal/hash_with_stringify_keys.rb +52 -0
  10. data/lib/crystal/interpolates_tags.rb +19 -0
  11. data/lib/crystal/meta.rb +29 -0
  12. data/lib/crystal/options_for_controller.rb +52 -0
  13. data/lib/crystal/railtie.rb +17 -0
  14. data/lib/crystal/tag.rb +26 -0
  15. data/lib/crystal/tags.rb +52 -0
  16. data/lib/crystal/version.rb +3 -0
  17. data/lib/crystal/view_helpers.rb +13 -0
  18. data/lib/crystalmeta.rb +24 -0
  19. data/spec/dummy/Rakefile +7 -0
  20. data/spec/dummy/app/assets/javascripts/application.js +12 -0
  21. data/spec/dummy/app/assets/stylesheets/application.css +11 -0
  22. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  23. data/spec/dummy/app/controllers/movies_controller.rb +5 -0
  24. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  25. data/spec/dummy/app/mailers/.gitkeep +0 -0
  26. data/spec/dummy/app/models/.gitkeep +0 -0
  27. data/spec/dummy/app/views/layouts/application.html.erb +11 -0
  28. data/spec/dummy/app/views/movies/index.html.erb +1 -0
  29. data/spec/dummy/app/views/movies/show.html.erb +0 -0
  30. data/spec/dummy/config.ru +4 -0
  31. data/spec/dummy/config/application.rb +64 -0
  32. data/spec/dummy/config/boot.rb +10 -0
  33. data/spec/dummy/config/environment.rb +5 -0
  34. data/spec/dummy/config/environments/development.rb +37 -0
  35. data/spec/dummy/config/environments/production.rb +67 -0
  36. data/spec/dummy/config/environments/test.rb +37 -0
  37. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  38. data/spec/dummy/config/initializers/inflections.rb +15 -0
  39. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  40. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  41. data/spec/dummy/config/initializers/session_store.rb +8 -0
  42. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/spec/dummy/config/locales/en.yml +18 -0
  44. data/spec/dummy/config/routes.rb +3 -0
  45. data/spec/dummy/lib/assets/.gitkeep +0 -0
  46. data/spec/dummy/log/.gitkeep +0 -0
  47. data/spec/dummy/script/rails +6 -0
  48. data/spec/features/meta_tags_spec.rb +52 -0
  49. data/spec/spec_helper.rb +14 -0
  50. data/spec/support/tag.rb +3 -0
  51. data/spec/unit/hash_with_stringify_keys_spec.rb +31 -0
  52. data/spec/unit/meta_spec.rb +41 -0
  53. data/spec/unit/tag_spec.rb +31 -0
  54. data/spec/unit/tags_spec.rb +132 -0
  55. metadata +185 -0
@@ -0,0 +1,67 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+
7
+ # Full error reports are disabled and caching is turned on
8
+ config.consider_all_requests_local = false
9
+ config.action_controller.perform_caching = true
10
+
11
+ # Disable Rails's static asset server (Apache or nginx will already do this)
12
+ config.serve_static_assets = false
13
+
14
+ # Compress JavaScripts and CSS
15
+ config.assets.compress = true
16
+
17
+ # Don't fallback to assets pipeline if a precompiled asset is missed
18
+ config.assets.compile = false
19
+
20
+ # Generate digests for assets URLs
21
+ config.assets.digest = true
22
+
23
+ # Defaults to nil and saved in location specified by config.assets.prefix
24
+ # config.assets.manifest = YOUR_PATH
25
+
26
+ # Specifies the header that your server uses for sending files
27
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
28
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
29
+
30
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
31
+ # config.force_ssl = true
32
+
33
+ # See everything in the log (default is :info)
34
+ # config.log_level = :debug
35
+
36
+ # Prepend all log lines with the following tags
37
+ # config.log_tags = [ :subdomain, :uuid ]
38
+
39
+ # Use a different logger for distributed setups
40
+ # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
41
+
42
+ # Use a different cache store in production
43
+ # config.cache_store = :mem_cache_store
44
+
45
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server
46
+ # config.action_controller.asset_host = "http://assets.example.com"
47
+
48
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
49
+ # config.assets.precompile += %w( search.js )
50
+
51
+ # Disable delivery errors, bad email addresses will be ignored
52
+ # config.action_mailer.raise_delivery_errors = false
53
+
54
+ # Enable threaded mode
55
+ # config.threadsafe!
56
+
57
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
58
+ # the I18n.default_locale when a translation can not be found)
59
+ config.i18n.fallbacks = true
60
+
61
+ # Send deprecation notices to registered listeners
62
+ config.active_support.deprecation = :notify
63
+
64
+ # Log the query plan for queries taking more than this (works
65
+ # with SQLite, MySQL, and PostgreSQL)
66
+ # config.active_record.auto_explain_threshold_in_seconds = 0.5
67
+ end
@@ -0,0 +1,37 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Configure static asset server for tests with Cache-Control for performance
11
+ config.serve_static_assets = true
12
+ config.static_cache_control = "public, max-age=3600"
13
+
14
+ # Log error messages when you accidentally call methods on nil
15
+ config.whiny_nils = true
16
+
17
+ # Show full error reports and disable caching
18
+ config.consider_all_requests_local = true
19
+ config.action_controller.perform_caching = false
20
+
21
+ # Raise exceptions instead of rendering exception templates
22
+ config.action_dispatch.show_exceptions = false
23
+
24
+ # Disable request forgery protection in test environment
25
+ config.action_controller.allow_forgery_protection = false
26
+
27
+ # Tell Action Mailer not to deliver emails to the real world.
28
+ # The :test delivery method accumulates sent emails in the
29
+ # ActionMailer::Base.deliveries array.
30
+ # config.action_mailer.delivery_method = :test
31
+
32
+ # Raise exception on mass assignment protection for Active Record models
33
+ # config.active_record.mass_assignment_sanitizer = :strict
34
+
35
+ # Print deprecation notices to the stderr
36
+ config.active_support.deprecation = :stderr
37
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
11
+ #
12
+ # These inflection rules are supported but not enabled by default:
13
+ # ActiveSupport::Inflector.inflections do |inflect|
14
+ # inflect.acronym 'RESTful'
15
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = 'c1ef9f5b5366707897d05e24da0986300e3d5eae8332a25e09a97b9f748455722528aaa6748755015e907d5abd42945f4d74d37f0364413ed5d41c5b5c5fc77e'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+ #
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json]
9
+ end
10
+
11
+ # Disable root element in JSON by default.
12
+ ActiveSupport.on_load(:active_record) do
13
+ self.include_root_in_json = false
14
+ end
@@ -0,0 +1,18 @@
1
+ en:
2
+ meta:
3
+ _defaults:
4
+ og:site_name: IMDb
5
+ og:title: IMDb - Movies, TV and Celebrities
6
+ og:description: 'IMDb, the world`s most popular and authoritative source for movie, TV and celebrity content.'
7
+ og:image: imdb.jpg
8
+ og:type: article
9
+ fb:app_id: 115109575169727
10
+
11
+ movies:
12
+ _defaults:
13
+ og:type: video.movie
14
+ head: Movies on IMDb
15
+
16
+ show:
17
+ head: '%{og:title} - %{og:site_name}'
18
+ og:image: rock.jpg
@@ -0,0 +1,3 @@
1
+ Dummy::Application.routes.draw do
2
+ resources :movies, :only => [:index, :show]
3
+ end
File without changes
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'meta tags' do
4
+ def meta_tags(pattern = //)
5
+ all('meta').select{|meta| pattern === meta[:property]}
6
+ end
7
+
8
+ def value_for(pattern = //)
9
+ meta_tags(pattern).first[:value]
10
+ end
11
+
12
+ def title
13
+ page.html.match(%r{<title>(.*?)</title>})[1]
14
+ end
15
+
16
+ context 'on movie page' do
17
+ before do
18
+ visit movie_path('rock')
19
+ end
20
+
21
+ it 'include og:title from controller' do
22
+ value_for('og:title').should == 'The Rock (1996)'
23
+ end
24
+
25
+ it 'include og:image from locale' do
26
+ value_for('og:image').should == '/images/rock.jpg'
27
+ end
28
+
29
+ it 'include og:type from controller defaults' do
30
+ value_for('og:type').should == 'video.movie'
31
+ end
32
+
33
+ it 'include title with interpolation' do
34
+ title.should == 'The Rock (1996) - IMDb'
35
+ end
36
+ end
37
+
38
+ context 'on movies page' do
39
+ before do
40
+ visit movies_path
41
+ end
42
+
43
+ it 'include og:title & og:type from views' do
44
+ value_for('og:title').should == 'Movies'
45
+ value_for('og:type').should == 'article'
46
+ end
47
+
48
+ it 'include default title' do
49
+ title.should == 'Movies on IMDb'
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,14 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
4
+
5
+ require 'rspec/rails'
6
+ require 'capybara/rspec'
7
+
8
+ Rails.backtrace_cleaner.remove_silencers!
9
+
10
+ ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
11
+
12
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
13
+
14
+ require_relative '../lib/crystalmeta'
@@ -0,0 +1,3 @@
1
+ def tag(*args)
2
+ Crystal::Tag.new(*args)
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Crystal::HashWithStringifyKeys do
4
+ def new(*args)
5
+ Crystal::HashWithStringifyKeys.new(*args)
6
+ end
7
+
8
+ context '.new' do
9
+ it 'stringifies keys when hash is passed' do
10
+ new(:key => {:key2 => 1}).should == {'key' => {'key2' => 1}}
11
+ end
12
+
13
+ it 'keeps a default value if one argument has been passed' do
14
+ new(1)['unknown'].should == 1
15
+ end
16
+
17
+ it 'returns an empty hash when no args have been passed' do
18
+ new.should be_empty
19
+ new['unknown'].should be_nil
20
+ end
21
+ end
22
+
23
+ context '#deep_merge with the same tag' do
24
+ it 'returns a hash with stringified keys' do
25
+ hash = new(:key => {:key2 => 1})
26
+ merge = hash.deep_merge!(new(:key => {:key2 => 2}))
27
+ merge.should == {'key' => {'key2' => 2}}
28
+ merge.should be_kind_of(Crystal::HashWithStringifyKeys)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Crystal::Meta do
4
+ def options
5
+ subject.send(:options)
6
+ end
7
+
8
+ context '#store' do
9
+ it 'stringifies keys' do
10
+ subject.store :title => 'something'
11
+ subject.store :head => 'something else'
12
+ options.should == {'title' => 'something', 'head' => 'something else'}
13
+ end
14
+
15
+ it 'merges options deeply' do
16
+ subject.store({
17
+ :og => {
18
+ :title => 'og title',
19
+ :site_name => 'site name'
20
+ }
21
+ })
22
+
23
+ subject.store({
24
+ 'og' => {
25
+ 'site_name' => 'site name 2',
26
+ 'url' => 'something',
27
+ },
28
+ :"fb:admins" => '322132'
29
+ })
30
+
31
+ options.should == {
32
+ 'og' => {
33
+ 'title' => 'og title',
34
+ 'site_name' => 'site name 2',
35
+ 'url' => 'something',
36
+ },
37
+ 'fb:admins' => '322132'
38
+ }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Crystal::Tag do
4
+ subject { Crystal::Tag.new('og:title', 'Caesar must die') }
5
+ its(:name) {should == 'og:title'}
6
+ its(:value) {should == 'Caesar must die'}
7
+
8
+ context '#value_for_context' do
9
+ let(:context) { double }
10
+
11
+ it 'returns asset path if asset' do
12
+ image = '1.png'
13
+ asset_path = "/assets/#{image}"
14
+ context.should_receive('image_path').with(image).and_return(asset_path)
15
+ tag('og:image', image).value_for_context(context).should == asset_path
16
+ end
17
+
18
+ it 'returns iso8601 for date' do
19
+ tag('date', Date.parse('1979-12-09')).value_for_context(context).should == '1979-12-09'
20
+ end
21
+
22
+ it 'returns iso8601 for time' do
23
+ tag('date', DateTime.parse('1979-12-09 13:09')).value_for_context(context).should == '1979-12-09T13:09:00+00:00'
24
+ end
25
+
26
+ it 'returns original value otherwise' do
27
+ tag('og:image:width', 100).value_for_context(context).should == 100
28
+ tag('published', true).value_for_context(context).should be_true
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,132 @@
1
+ require 'spec_helper'
2
+
3
+ describe Crystal::Tags do
4
+ def new(options)
5
+ Crystal::Tags.new(options)
6
+ end
7
+
8
+ context '#tags' do
9
+ it 'folds hashes' do
10
+ new({
11
+ 'og' => {
12
+ 'title' => 'Caesar must die'
13
+ }
14
+ }).tags.should == [
15
+ tag('og:title', 'Caesar must die')
16
+ ]
17
+ end
18
+
19
+ it 'sorts keys in hashes alphabetically' do
20
+ new({
21
+ 'og' => {
22
+ 'type' => 'book',
23
+ 'title' => 'One day of Ivan Denisovich'
24
+ }
25
+ }).tags.should == [
26
+ tag('og:title', 'One day of Ivan Denisovich'),
27
+ tag('og:type', 'book')
28
+ ]
29
+ end
30
+
31
+ it 'moves url key to the top' do
32
+ new({
33
+ 'og' => {
34
+ 'image' => [
35
+ {
36
+ 'url' => '1.png',
37
+ 'width' => 200,
38
+ 'height' => 300,
39
+ },
40
+ {
41
+ 'url' => '2.png',
42
+ 'width' => 400,
43
+ 'height' => 500,
44
+ },
45
+ ]
46
+ }
47
+ }).tags.should == [
48
+ tag('og:image:url', '1.png'),
49
+ tag('og:image:height', 300),
50
+ tag('og:image:width', 200),
51
+ tag('og:image:url', '2.png'),
52
+ tag('og:image:height', 500),
53
+ tag('og:image:width', 400),
54
+ ]
55
+ end
56
+
57
+ it 'duplicates tags for arrays' do
58
+ new('og:image' => ['1.png', '2.png']).tags.should == [
59
+ tag('og:image', '1.png'),
60
+ tag('og:image', '2.png')
61
+ ]
62
+ end
63
+
64
+ it 'interpolates values' do
65
+ new({
66
+ :og => {
67
+ :title => 'The Rock (%{year})',
68
+ :site_name => 'IMDb'
69
+ },
70
+ :year => 1996,
71
+ :head => '%{og:title} - %{og:site_name}'
72
+ }).tags.should == [
73
+ tag('head', 'The Rock (1996) - IMDb'),
74
+ tag('og:site_name', 'IMDb'),
75
+ tag('og:title', 'The Rock (1996)'),
76
+ tag('year', 1996)
77
+ ]
78
+ end
79
+ end
80
+
81
+ context '#find_by_name' do
82
+ it 'returns first tag' do
83
+ new('og' => {'image' => %w{1.png 2.png}}).find_by_name(:"og:image").value.should == '1.png'
84
+ end
85
+
86
+ it 'raises an error if tag was not found' do
87
+ expect { new({}).find_by_name('og:image') }.to raise_error(Crystal::TagNotFound)
88
+ end
89
+ end
90
+
91
+ context '#filter' do
92
+ subject {
93
+ new({
94
+ 'og' => {
95
+ 'image' => %w{1.png 2.png},
96
+ 'title' => 'Title'
97
+ },
98
+ 'fb:admins' => '322132'
99
+ })
100
+ }
101
+
102
+ context 'without pattern' do
103
+ it 'returns all tags' do
104
+ subject.filter.should == [
105
+ tag('fb:admins', '322132'),
106
+ tag('og:image', '1.png'),
107
+ tag('og:image', '2.png'),
108
+ tag('og:title', 'Title')
109
+ ]
110
+ end
111
+ end
112
+
113
+ context 'with string' do
114
+ it 'returns tags with this name' do
115
+ subject.filter('og:image').should == [
116
+ tag('og:image', '1.png'),
117
+ tag('og:image', '2.png')
118
+ ]
119
+ end
120
+ end
121
+
122
+ context 'with regexp' do
123
+ it 'returns tags with names matched regexp' do
124
+ subject.filter(/og/).should == [
125
+ tag('og:image', '1.png'),
126
+ tag('og:image', '2.png'),
127
+ tag('og:title', 'Title')
128
+ ]
129
+ end
130
+ end
131
+ end
132
+ end