class Cortex::Snippets::Client
Public Class Methods
new(cortex_client)
click to toggle source
# File lib/cortex/snippets/client.rb, line 9 def initialize(cortex_client) @cortex_client = cortex_client end
Public Instance Methods
current_webpage(request)
click to toggle source
# File lib/cortex/snippets/client.rb, line 24 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
snippet(request, options = {}, block)
click to toggle source
# File lib/cortex/snippets/client.rb, line 13 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
Private Instance Methods
sanitized_webpage_url(url)
click to toggle source
# File lib/cortex/snippets/client.rb, line 37 def sanitized_webpage_url(url) uri = Addressable::URI.parse(url) path = uri.path == '/' ? uri.path : uri.path.chomp('/') "#{uri.scheme}://#{uri.authority}#{path}" end