Sha256: 7ae0ac42a3388cdc16b3fb385929f7470416660db36b1485699f16018c34b0b8
Contents?: true
Size: 1.3 KB
Versions: 17
Compression:
Stored size: 1.3 KB
Contents
require 'cortex/snippets/webpage' require 'addressable/uri' module Cortex module Snippets class Client include ActionView::Helpers::TagHelper def initialize(cortex_client) @cortex_client = cortex_client end def snippet(request, options = {}, block) snippets = current_webpage(request).snippets || [] snippet = snippets.find { |snippet| snippet[:document][:name] == options[:id] } if snippet.nil? || snippet[:document][:body].nil? content_tag(:snippet, block, options) else content_tag(:snippet, snippet[:document][:body].html_safe, options) end end def current_webpage(request) if defined?(Rails) url = sanitized_webpage_url(request.original_url) Rails.cache.fetch("webpages/#{@cortex_client.access_token.client.id}/#{url}", race_condition_ttl: 10) do Cortex::Snippets::Webpage.new(@cortex_client, url) end else raise 'Your Web framework is not supported. Supported frameworks: Rails' end end private def sanitized_webpage_url(url) uri = Addressable::URI.parse(url) path = uri.path == '/' ? uri.path : uri.path.chomp('/') "#{uri.scheme}://#{uri.authority}#{path}" end end end end
Version data entries
17 entries across 17 versions & 1 rubygems