diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..c12a832 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +Version History +==== + * All Version bumps are required to update this file as well!! +---- + +* 0.5.0 - Auto-inject Client Helper into Views/Controllers. Pass through to Snippet tag entire set of HTML attributes. Implement new namespacing and support Bundler.require. diff --git a/cortex-snippets-client-ruby.gemspec b/cortex-snippets-client-ruby.gemspec index 07f0a6a..d6df060 100644 --- a/cortex-snippets-client-ruby.gemspec +++ b/cortex-snippets-client-ruby.gemspec @@ -1,11 +1,11 @@ # coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'cortex/snippets/version' +require 'cortex/snippets/client/version' Gem::Specification.new do |spec| spec.name = 'cortex-snippets-client-ruby' - spec.version = Cortex::Snippets::VERSION + spec.version = Cortex::Snippets::Client::VERSION spec.authors = ['CB Content Enablement'] spec.email = ['ContentEnablementProductTeam@careerbuilder.com'] spec.license = 'Apache-2.0' diff --git a/lib/cortex-snippets-client-ruby.rb b/lib/cortex-snippets-client-ruby.rb new file mode 100644 index 0000000..9b24e14 --- /dev/null +++ b/lib/cortex-snippets-client-ruby.rb @@ -0,0 +1,2 @@ +require 'cortex/snippets/client' +require 'cortex/snippets/client/version' diff --git a/lib/cortex/snippets-client.rb b/lib/cortex/snippets-client.rb deleted file mode 100644 index f452967..0000000 --- a/lib/cortex/snippets-client.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'cortex/snippets/version' -require 'cortex/snippets/client' -require 'cortex/snippets/view_helpers' -require 'cortex/snippets/railtie' if defined?(Rails) diff --git a/lib/cortex/snippets/client.rb b/lib/cortex/snippets/client.rb index 660292b..e186fc6 100644 --- a/lib/cortex/snippets/client.rb +++ b/lib/cortex/snippets/client.rb @@ -1,3 +1,5 @@ +require 'cortex/snippets/client/helper' +require 'cortex/snippets/client/railtie' if defined?(Rails) require 'cortex-client' require 'connection_pool' require 'addressable/template' @@ -16,7 +18,7 @@ def cortex_client def current_webpage(request) if defined?(Rails) - Rails.cache.fetch("webpages/#{request_url(request)}", expires_in: 30.minutes) do + Rails.cache.fetch("webpages/#{request_url(request)}", expires_in: 30.minutes, race_condition_ttl: 10) do cortex_client.webpages.get_feed(request_url(request)).contents end else @@ -24,8 +26,6 @@ def current_webpage(request) end end - private - def request_url(request) # TODO: Should be grabbing request URL in a framework-agnostic manner, but this is fine for now uri = Addressable::URI.parse(request.original_url) diff --git a/lib/cortex/snippets/client/helper.rb b/lib/cortex/snippets/client/helper.rb new file mode 100644 index 0000000..ea2093c --- /dev/null +++ b/lib/cortex/snippets/client/helper.rb @@ -0,0 +1,60 @@ +module Cortex + module Snippets + module Client + module Helper + def snippet(options = {}, &block) + snippets = webpage[:snippets] || [] + snippet = snippets.find { |snippet| snippet[:document][:name] == options[:id] } + + if snippet.nil? || snippet[:document][:body].nil? || snippet[:document][:body].empty? + content_tag(:snippet, capture(&block), options) + else + content_tag(:snippet, snippet[:document][:body].html_safe, options) + end + end + + def seo_title + webpage[:seo_title] + end + + def seo_description + webpage[:seo_description] + end + + def seo_keywords + webpage[:seo_keywords] + end + + def noindex + webpage[:noindex] + end + + def nofollow + webpage[:nofollow] + end + + def noodp + webpage[:noodp] + end + + def nosnippet + webpage[:nosnippet] + end + + def noarchive + webpage[:noarchive] + end + + def noimageindex + webpage[:noimageindex] + end + + private + + def webpage + Cortex::Snippets::Client::current_webpage(request) + end + end + end + end +end diff --git a/lib/cortex/snippets/client/railtie.rb b/lib/cortex/snippets/client/railtie.rb new file mode 100644 index 0000000..b172670 --- /dev/null +++ b/lib/cortex/snippets/client/railtie.rb @@ -0,0 +1,17 @@ +module Cortex + module Snippets + module Client + class Railtie < Rails::Railtie + initializer 'cortex-snippets-client.view_controller_helpers' do |app| + ActiveSupport.on_load :action_view do + include Helper + end + + ActiveSupport.on_load :action_controller do + include Helper + end + end + end + end + end +end diff --git a/lib/cortex/snippets/client/version.rb b/lib/cortex/snippets/client/version.rb new file mode 100644 index 0000000..909f48b --- /dev/null +++ b/lib/cortex/snippets/client/version.rb @@ -0,0 +1,7 @@ +module Cortex + module Snippets + module Client + VERSION = '0.5.0' + end + end +end diff --git a/lib/cortex/snippets/railtie.rb b/lib/cortex/snippets/railtie.rb deleted file mode 100644 index 95441ea..0000000 --- a/lib/cortex/snippets/railtie.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Cortex - module Snippets - class Railtie < Rails::Railtie - initializer 'cortex.snippets.view_helpers' do - ActiveSupport.on_load( :action_view ){ include ViewHelpers } - end - end - end -end diff --git a/lib/cortex/snippets/version.rb b/lib/cortex/snippets/version.rb deleted file mode 100644 index 3b2f211..0000000 --- a/lib/cortex/snippets/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Cortex - module Snippets - VERSION = '0.4.4' - end -end diff --git a/lib/cortex/snippets/view_helpers.rb b/lib/cortex/snippets/view_helpers.rb deleted file mode 100644 index 58051fc..0000000 --- a/lib/cortex/snippets/view_helpers.rb +++ /dev/null @@ -1,53 +0,0 @@ -module Cortex - module Snippets - module ViewHelpers - def snippet(options = {}, &block) - snippet = webpage[:snippets].find { |snippet| snippet.name == options[:id] } - - if snippet.nil? || snippet.empty? - content_tag(:snippet, capture(&block), id: options[:id]) - else - content_tag(:snippet, snippet, id: options[:id]) - end - end - - def seo_title - webpage[:seo_title] - end - - def seo_description - webpage[:seo_description] - end - - def noindex - webpage[:noindex] - end - - def nofollow - webpage[:nofollow] - end - - def noodp - webpage[:noodp] - end - - def nosnippet - webpage[:nosnippet] - end - - def noarchive - webpage[:noarchive] - end - - def noimageindex - webpage[:noimageindex] - end - - private - - def webpage - Client::current_webpage(request) - end - end - end -end diff --git a/spec/cortex/snippets_client_spec.rb b/spec/cortex/snippets_client_spec.rb index b0a4576..c77ea89 100644 --- a/spec/cortex/snippets_client_spec.rb +++ b/spec/cortex/snippets_client_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' -describe Cortex::Snippets do +describe Cortex::Snippets::Client do it 'has a version number' do - expect(Cortex::Snippets::VERSION).not_to be nil + expect(Cortex::Snippets::Client::VERSION).not_to be nil end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 56215b7..920f49d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,2 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) -require 'cortex/snippets-client' +require 'cortex-snippets-client-ruby'