Class: Cortex::Snippets::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cortex/snippets/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(cortex_client) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/cortex/snippets/client.rb', line 7

def initialize(cortex_client)
  @cortex_client = cortex_client
end

Instance Method Details

#current_webpageObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/cortex/snippets/client.rb', line 22

def current_webpage
  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(options = {}, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/cortex/snippets/client.rb', line 11

def snippet(options = {}, &block)
  snippets = current_webpage.snippets || []
  snippet = snippets.find { |snippet| snippet[:document][:name] == options[:id] }

  if snippet.nil? || snippet[:document][:body].nil? || snippet[:document][:body].empty?
    (:snippet, capture(&block), options)
  else
    (:snippet, snippet[:document][:body].html_safe, options)
  end
end