Class: Cortex::Snippets::Client

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/cortex/snippets/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(cortex_client) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/cortex/snippets/client.rb', line 9

def initialize(cortex_client)
  @cortex_client = cortex_client
end

Instance Method Details

#current_webpage(request) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# 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?
    (:snippet, block, options)
  else
    (:snippet, snippet[:document][:body].html_safe, options)
  end
end