Module: Cortex::Snippets::Client

Defined in:
lib/cortex/snippets/client.rb,
lib/cortex/snippets/client/helper.rb,
lib/cortex/snippets/client/railtie.rb,
lib/cortex/snippets/client/version.rb

Defined Under Namespace

Modules: Helper Classes: Railtie

Constant Summary collapse

VERSION =
'0.8.0'

Class Method Summary collapse

Class Method Details

.cortex_clientObject



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

def cortex_client
  if ENV['CORTEX_SNIPPET_ACCESS_TOKEN'].nil? || ENV['CORTEX_SNIPPET_ACCESS_TOKEN'].empty?
    @cortex_client ||= ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Cortex::Client.new(key: ENV['CORTEX_SNIPPET_KEY'], secret: ENV['CORTEX_SNIPPET_SECRET'], base_url: ENV['CORTEX_SNIPPET_BASE_URL'], scopes: ENV['CORTEX_SNIPPET_SCOPES']) }
  else
    @cortex_client ||= ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Cortex::Client.new(access_token: ENV['CORTEX_SNIPPET_ACCESS_TOKEN']) }
  end
end

.current_webpage(request) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/cortex/snippets/client.rb', line 19

def current_webpage(request)
  if defined?(Rails)
    sanitized_url = sanitized_webpage_url(request.original_url)
    Rails.cache.fetch("webpages/#{sanitized_url}", race_condition_ttl: 10) do
      cortex_client.webpages.get_feed(sanitized_url).contents
    end
  else
    raise 'Your Web framework is not supported. Supported frameworks: Rails'
  end
end

.sanitized_webpage_url(url) ⇒ Object



30
31
32
33
34
# File 'lib/cortex/snippets/client.rb', line 30

def sanitized_webpage_url(url)
  uri = Addressable::URI.parse(url)
  path = uri.path == '/' ? uri.path : uri.path.chomp('/')
  "#{uri.scheme}://#{uri.authority}#{path}"
end