Class: Cortex::Snippets::Client
- Inherits:
-
Object
- Object
- Cortex::Snippets::Client
- Defined in:
- lib/cortex/snippets/client.rb
Instance Method Summary collapse
- #current_webpage ⇒ Object
-
#initialize(cortex_client) ⇒ Client
constructor
A new instance of Client.
- #snippet(options = {}, &block) ⇒ Object
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_webpage ⇒ Object
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( = {}, &block) snippets = current_webpage.snippets || [] snippet = snippets.find { |snippet| snippet[:document][:name] == [:id] } if snippet.nil? || snippet[:document][:body].nil? || snippet[:document][:body].empty? content_tag(:snippet, capture(&block), ) else content_tag(:snippet, snippet[:document][:body].html_safe, ) end end |